-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
284 lines (226 loc) · 8.19 KB
/
main.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import socket
import json
import time
from matplotlib import pyplot as plt
from robot import Robot
from path import Path
from pid import PIDAngle
from datetime import datetime
def restartRobot():
data = conn.recv(1024)
recData = json.loads(data.decode('utf-8'))
robot.setCoord(recData['robot0']['x'],recData['robot0']['y'],recData['robot0']['z'])
x, y, theta = robot.getCoord()
robot.setVel(0,0)
d = robot.getVel()
conn.send(d.encode())
return x,y,theta
def updateRobot(i):
data = conn.recv(1024)
recData = json.loads(data.decode('utf-8'))
robot.setCoord(recData['robot0']['x'],recData['robot0']['y'],recData['robot0']['z'])
x, y, theta = robot.getCoord()
#robotPositions[countInt].append((x, y))
erro = pid.calcSpeeds(x, 130-y, 360-theta, i[0], 130-i[1])
# print ("t: {0:.2f} e: {1:.2f} x:{2:.2f} y:{3:.2f} rx:{4:.2f} ry:{5:.2f}".format(theta, erro, i[0], i[1], x, y))
robot.setVel(pid.getLeftSpeed(),pid.getRightSpeed())
d = robot.getVel()
conn.send(d.encode())
return x,y,theta
######################################################################################
import numpy as np
import random
from tqdm import tqdm
HOST = '127.0.0.1' # Local host
PORT = 50007 # Arbitrary port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
print ('Waiting for connection...')
conn, addr = s.accept()
robot = Robot()
pid = PIDAngle(0.1, 0, 3, 20)
path = Path(40)
print ('Connected by client', addr)
robotPositions = []
startPoints = []
endPoints = []
startPoints.append((125, 65))
restartFlag = False
path.circleDiscretization(50)
#while 1: #DEA a todo gene
def fitnessFunction(kp, ki, kd):
path.circleDiscretization(50)
# print("Trying: Kp: {:0.2f} Ki: {:0.2f} Kd: {:0.2f}".format(kp, ki, kd))
fitness_error = 0.0
pid.setK(kp, ki, kd)
robotPositions = []
startPoints = (125, 65)
x, y, theta = restartRobot()
while (path.pointDistance(125, 65, x, y) != 0.0):
x,y,theta = restartRobot()
x, y, theta = restartRobot()
restartFlag = False
countInt = 0
pid.setK(kp, ki, kd)
#print ("chegggou")
warningRestart = True
for i in path.getPath():
robotPositions = []
start_time = time.time()
endPoints = (i[0], i[1])
#print(countInt)
while path.pointDistance(i[0], i[1], x, y) > 1 and time.time() - start_time < 0.06:
x,y,theta = updateRobot(i)
if(path.pointDistance(125, 65, x, y) <= 0.05 and countInt > 20 and warningRestart):
#print("Warring: Possible Restart")
warningRestart = False
robotPositions.append((x, y))
if restartFlag == True:
break
fitness_error += path.getPerimeterError(startPoints, endPoints, robotPositions)
startPoints = (i[0], i[1])
countInt += 1
#print("Path complete!, with Fitness = ", fitness_error)
if countInt != len(path.getPath()):
print("ERROR: Count Interation Error {}".format(countInt))
if(fitness_error < 250):
print("Warring: Fitness error too low! {}".format(fitness_error))
print("Warring: Set Fitness to Inf")
fitness_error = 99999999
restartRobot()
time.sleep(0.2)
return fitness_error
class DEA:
def __init__(self, NP=15, D=3, MaxGen=500, CR=0.90, F=0.7):
"""
Attributes:
NP = Number of Population
D = Dimentions
MaxGen = Max generations
CR = Crossover and Mutation rate
F = Scaling Factor
"""
self.NP = NP
self.D = D
self.MaxGen = MaxGen
self.CR = CR
self.F = F
self.kpMax, self.kiMax, self.kdMax = 1, 1, 1
self.timeStamp = (datetime.now())
self.logName = '_'.join(str(x) for x in (self.timeStamp.year,self.timeStamp.month, self.timeStamp.day, self.timeStamp.hour, self.timeStamp.minute))
self.__init_csv()
self.__init__population()
self.__init__fitness()
def __init__population(self):
kp = np.random.rand(self.NP, 1) * self.kpMax
ki = np.random.rand(self.NP, 1) * self.kiMax
kd = np.random.rand(self.NP, 1) * self.kdMax
population = (np.hstack((kp, ki, kd)))
self.population = np.zeros((self.NP, self.D), dtype=np.float)
for i in range(0, self.NP ):
for j in range(self.D):
minD = np.min(population[:, j])
maxD = np.max(population[:, j])
self.population[i][j] = minD + np.random.rand(1) * (maxD - minD)
def __init__fitness(self):
self.fitness = np.zeros((self.NP, 1), dtype=np.float)
for i in range(self.NP ):
self.fitness[i] = fitnessFunction(self.population[i][0], self.population[i][1], self.population[i][2])
print("PID: {}, Fitness: {}".format(self.population[i], self.fitness[i]) )
def __get_fitness(self, genotype):
return fitnessFunction(genotype[0],genotype[1],genotype[2]);
def __init_csv(self):
with open ('log/log_'+self.logName+'_epoch.csv', 'a') as log:
log.write("Epoch\t Min\t Mean\t Best\t\n")
with open ('log/log_'+self.logName+'_population.csv', 'a') as log:
log.write("Kp\tKi\tKd\tFitness\t\n")
def forward(self):
# Mutation and Cross Over
t = tqdm(range(self.MaxGen))
epoch = 0
for G in t:
with open ('log/log_'+self.logName+'_population.csv', 'a') as log:
log.write("Epoch: {}\n".format(epoch))
#logWrite = np.array2string(np.hstack((self.population, self.fitness)), formatter={'float_kind':lambda x: "%.4f" % x})
PIDList = np.hstack((self.population, self.fitness))
for PID in PIDList:
log.write("{:0.4f}\t{:0.4f}\t{:0.4f}\t{:0.4f}\t\n".format(PID[0], PID[1], PID[2], PID[3]))
with open ('log/log_'+self.logName+'_epoch.csv', 'a') as log:
indexMin = np.argmin(self.fitness)
minFit = np.min(self.fitness)
meanFit = np.mean(self.fitness)
logWrite = np.array2string( self.population[indexMin] , formatter={'float_kind':lambda x: "%.4f" % x})
log.write("{}\t{:.4f}\t{:.4f}\t{}\t\n".format(epoch, minFit, meanFit, logWrite))
epoch += 1
self.popG = np.zeros((self.NP, self.D), dtype=np.float)
for i in range(self.NP):
r1, r2, r3 = random.sample([x for x in range(self.NP) if x != i], 3)
jrand = np.random.randint(self.D)
for j in range(self.D):
if(np.random.random(1) < self.CR) or (j == jrand):
geneR1 = self.population[r1, j]
geneR2 = self.population[r2, j]
geneR3 = self.population[r3, j]
gene = geneR1 + self.F * (geneR2 - geneR3)
if gene < 0.0:
gene = np.random.random(1) * 0.01
self.popG[i,j] = abs(gene)
else:
self.popG[i,j] = self.population[i, j]
# Selection
popGFit = self.__get_fitness( self.popG[i])
t.set_description("PID: {}, Fitness: {}".format(self.popG[i],popGFit))
if popGFit <= self.fitness[i]:
self.population[i] = self.popG[i]
self.fitness[i] = popGFit
#######################################################################################
class GridSearch:
def __init__(self, KpS=0.0, KpE=1.0, KdS=0.0, KdE=3.0, step=0.1):
self.KpS = KpS
self.KpE = KpE
self.KdS = KdS
self.KdE = KdE
self.step = step
self.timeStamp = (datetime.now())
self.logName = '_'.join(str(x) for x in (self.timeStamp.year,self.timeStamp.month, self.timeStamp.day, self.timeStamp.hour, self.timeStamp.minute))
self.__init_csv()
def __get_fitness(self, genotype):
return fitnessFunction(genotype[0], 0.0,genotype[1]);
def __init_csv(self):
with open ('log/log_grid_'+self.logName+'_population.csv', 'a') as log:
log.write("Kp\tKd\tFitness\t\n")
def forward(self):
# Mutation and Cross Over
KpAxis = []
KdAxis = []
fitAxis = []
t = tqdm(np.arange(self.KpS, self.KpE, self.step))
for kp in t:
for kd in np.arange(self.KdS, self.KdE, self.step):
fitness = self.__get_fitness(np.array([kp, kd]))
KpAxis.append(kp)
KdAxis.append(kd)
fitAxis.append(fitness)
with open ('log/log_grid_'+self.logName+'_population.csv', 'a') as log:
log.write("{:0.4f}\t{:0.4f}\t{:0.4f}\t\n".format(kp, kd, fitness))
t.set_description("PID: {:0.4f} {:0.4f}, Fitness: {:0.4f}".format(kp, kd, fitness))
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(KpAxis, KdAxis, fitAxis, cmap="jet")
plt.show()
if __name__ == '__main__':
try:
#dea = DEA(NP=15, MaxGen=200)
#dea.forward()
#print (np.hstack((dea.population, dea.fitness)))
# gs = GridSearch()
# gs.forward()
fitnessFunction(0.1, 0, 2)
fitnessFunction(0.1, 0, 2)
fitnessFunction(0.0509, 0.0009, 0.0084)
except:
conn.close()
print ('Server closed.')
conn.close()
print ('Server closed.')