-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimize.py
More file actions
68 lines (55 loc) · 1.18 KB
/
Copy pathoptimize.py
File metadata and controls
68 lines (55 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from scipy.optimize import minimize
import math
import random
import matplotlib.pyplot as plt
f = open('windows.txt','r')
n = 10
a = []
for line in f:
a.append(line.split())
x0 = [0]*n
for i in range(n):
x0[i] = random.uniform(float(a[i][1]),float(a[i][2]))
def length(x):
s = 0
for i in range(len(a)-1):
s += math.sqrt((float(a[i][0])-float(a[i+1][0]))**2+(x[i]-x[i+1])**2)
return s
def length2(x):
s = 0
for i in range(len(a)-1):
s += abs(x[i]-x[i+1])
return s
b = []
for i in range(n):
b.append((float(a[i][1]),float(a[i][2])))
res = minimize(length,x0,bounds=b)
res2 = minimize(length2,x0,bounds=b)
x1 = res.x
x2 = res2.x
print(x1)
print(x2)
points_x = []
points_y = []
p_x = []
p_y = []
p_x2 = []
p_y2 = []
for i in range(n):
p_x.append(a[i][0])
p_x2.append(a[i][0])
points_x.append(a[i][0])
points_x.append(a[i][0])
p_y.append(x1[i])
p_y2.append(x2[i])
points_y.append(a[i][1])
points_y.append(a[i][2])
p = []
for i in range(n):
p.append((a[i][0],x1[i]))
plt.plot(points_x,points_y, 'ro')
plt.plot(p_x,p_y, linewidth=2.0)
plt.plot(p_x,p_y,'g^')
plt.plot(p_x2,p_y2,'b^')
plt.plot(p_x2,p_y2, color='r', linewidth=1.0)
plt.show()