-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (137 loc) · 5.85 KB
/
Copy pathmain.py
File metadata and controls
177 lines (137 loc) · 5.85 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import time
import random
import math
import numpy as np
import csv
import os
import _thread
import matplotlib.pyplot as plt
import matplotlib
print(matplotlib.__version__)
print(str(0)+','+str(0), file=open('target.csv', 'w'))
def start_drag():
os.system('python Draggable.py')
_thread.start_new_thread(start_drag, ())
initial = [5, 5]
bounds = [(-800, 800), (-800, 800)]
colors = np.array([
(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229),
(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229),
(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229),
(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
(44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
(148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
(227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
(188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)
]) / 255.
class Particle:
def __init__(self, initial):
self.pos = []
self.vel = []
self.best_pos = []
self.best_error = -1
self.error = -1
for i in range(0, num_dimensions):
self.vel.append(random.uniform(-1, 1))
self.pos.append(initial[i])
def update_velocity(self, global_best_position):
w = 0.5
c1 = 1
c2 = 2
for i in range(0, num_dimensions):
r1 = random.random()
r2 = random.random()
cog_vel = c1*r1*(self.best_pos[i]-self.pos[i])
social_vel = c2*r2*(global_best_position[i]-self.pos[i])
self.vel[i] = w*self.vel[i]+cog_vel+social_vel
def update_position(self, bounds):
for i in range(0, num_dimensions):
self.pos[i] = self.pos[i]+self.vel[i]
if self.pos[i] > bounds[i][1]:
self.pos[i] = bounds[i][1]
if self.pos[i] < bounds[i][0]:
self.pos[i] = bounds[i][0]
def evaluate_fitness(self, fitness_function):
self.error = fitness_function(self.pos)
print("ERROR------->", self.error)
if self.error < self.best_error or self.best_error == -1:
self.best_pos = self.pos
self.best_error = self.error
def fitness_function(x):
x0, y0 = getXY('target.csv')
x0 = float(x0)
y0 = float(y0)
total = 0
total += (x0-x[0])**2 + (y0-x[1])**2
return total
def getXY(filename):
lat = 0
long = 0
with open(filename) as csvDataFile:
csvReader = csv.reader(csvDataFile)
for row in csvReader:
lat = row[0]
long = row[1]
return lat, long
class Interactive_PSO():
def __init__(self, fitness_function, initial, bounds, num_particles):
global num_dimensions
num_dimensions = len(initial)
global_best_error = -1
global_best_position = []
self.gamma = 0.0001
swarm = []
for i in range(0, num_particles):
swarm.append(Particle(initial))
i = 0
while True:
#print('x'+','+'y',file = open('pos.csv','w'))
for j in range(0, num_particles):
swarm[j].evaluate_fitness(fitness_function)
print('global_best_position', swarm[j].error, global_best_error)
if swarm[j].error < global_best_error or global_best_error == -1:
global_best_position = list(swarm[j].pos)
global_best_error = float(swarm[j].error)
plt.title("PyShine Interactive PSO, Particles:{}, Error:{}".format(
num_particles, round(global_best_error, 1)))
if i % 2 == 0:
global_best_error = -1
global_best_position = list(
[swarm[j].pos[0] + self.gamma * (swarm[j].error) * random.random(),
swarm[j].pos[1] + self.gamma * (swarm[j].error) * random.random()])
pos_0 = {}
pos_1 = {}
for j in range(0, num_particles):
pos_0[j] = []
pos_1[j] = []
for j in range(0, num_particles):
swarm[j].update_velocity(global_best_position)
swarm[j].update_position(bounds)
pos_0[j].append(swarm[j].pos[0])
pos_1[j].append(swarm[j].pos[1])
#print(str(swarm[j].pos[0])+','+str(swarm[j].pos[1]),file = open('pos.csv','a'))
plt.xlim([-500, 500])
plt.ylim([-500, 500])
for j in range(0, num_particles):
plt.plot(pos_0[j], pos_1[j], color=colors[j], marker='o')
x, y = getXY('target.csv')
plt.plot(float(x), float(y), color='k', marker='o')
plt.pause(0.01)
plt.clf()
i += 1
print('Results')
print('Best Position:', global_best_position)
print('Best Error:', global_best_error)
Interactive_PSO(fitness_function, initial, bounds, num_particles=16) # let say 2 particles and 50 iterations