forked from TomasSQ/vrepSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteste_com_ga_2pontos.py
280 lines (219 loc) · 12.1 KB
/
teste_com_ga_2pontos.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
import vrep
import time
import random
import math
from manage_joints import *
from deap import base
from deap import creator
from deap import tools
def JointControl2(clientID,i,Body, commandAngles):
vrep.simxSetJointTargetPosition(clientID,Body[0][i],commandAngles[0],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[1][i],commandAngles[1],vrep.simx_opmode_streaming)
#Left Leg
vrep.simxSetJointTargetPosition(clientID,Body[2][i],commandAngles[2],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[3][i],commandAngles[3],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[4][i],commandAngles[4],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[5][i],commandAngles[5],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[6][i],commandAngles[6],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[7][i],commandAngles[7],vrep.simx_opmode_streaming)
#Right Leg
vrep.simxSetJointTargetPosition(clientID,Body[8][i],commandAngles[8],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[9][i],commandAngles[9],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[10][i],commandAngles[10],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[11][i],commandAngles[11],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[12][i],commandAngles[12],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[13][i],commandAngles[13],vrep.simx_opmode_streaming)
#Left Arm
vrep.simxSetJointTargetPosition(clientID,Body[14][i],commandAngles[14],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[15][i],commandAngles[15],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[16][i],commandAngles[16],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[17][i],commandAngles[17],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[18][i],commandAngles[18],vrep.simx_opmode_streaming)
#Right Arm
vrep.simxSetJointTargetPosition(clientID,Body[19][i],commandAngles[19],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[20][i],commandAngles[20],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[21][i],commandAngles[21],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[22][i],commandAngles[22],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[23][i],commandAngles[23],vrep.simx_opmode_streaming)
#Left Fingers
vrep.simxSetJointTargetPosition(clientID,Body[25][i][0],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][1],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][2],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][3],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][4],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][5],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][6],1.0-commandAngles[25],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[25][i][7],1.0-commandAngles[25],vrep.simx_opmode_streaming)
#Right Fingers
vrep.simxSetJointTargetPosition(clientID,Body[27][i][0],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][1],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][2],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][3],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][4],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][5],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][6],1.0-commandAngles[27],vrep.simx_opmode_streaming)
vrep.simxSetJointTargetPosition(clientID,Body[27][i][7],1.0-commandAngles[27],vrep.simx_opmode_streaming)
DEBUG = True
POPULATION_SIZE = 5
COEF_RANGE = 1.0
N_COEF = 0
JOINT_SIZE = 2 * N_COEF + 2
DELTA_TIME = 10
INTERVAL = 0.05 #50ms
ANGLE_THRESHOLD = 15.0 * (math.pi)/180
#Serie de Fourier Truncada
def truncated_Fourier(coeficients, time, stable):
value = stable and coeficients[0] / 2.0 or 0.0
for i in xrange(N_COEF):
if stable: value += coeficients[2 * i + 2] * math.cos((i + 1) * time)
if not stable: value += coeficients[2 * i + 3] * math.sin((i + 1) * time)
return value
#Conexao com o vrep
vrep.simxFinish(-1) # just in case, close all opened connections
clientID = vrep.simxStart('127.0.0.1',19997,True,True,5000,5) # Conecta com o VREP. Por padrao ele ja abre essa porta.
if clientID == -1:
exit (10)
#Juntas do NAO
Head_Yaw=[];Head_Pitch=[];
L_Hip_Yaw_Pitch=[];L_Hip_Roll=[];L_Hip_Pitch=[];L_Knee_Pitch=[];L_Ankle_Pitch=[];L_Ankle_Roll=[];
R_Hip_Yaw_Pitch=[];R_Hip_Roll=[];R_Hip_Pitch=[];R_Knee_Pitch=[];R_Ankle_Pitch=[];R_Ankle_Roll=[];
L_Shoulder_Pitch=[];L_Shoulder_Roll=[];L_Elbow_Yaw=[];L_Elbow_Roll=[];L_Wrist_Yaw=[]
R_Shoulder_Pitch=[];R_Shoulder_Roll=[];R_Elbow_Yaw=[];R_Elbow_Roll=[];R_Wrist_Yaw=[]
R_H=[];L_H=[];R_Hand=[];L_Hand=[];
Body = [Head_Yaw,Head_Pitch,L_Hip_Yaw_Pitch,L_Hip_Roll,L_Hip_Pitch,L_Knee_Pitch,L_Ankle_Pitch,L_Ankle_Roll,R_Hip_Yaw_Pitch,R_Hip_Roll,R_Hip_Pitch,R_Knee_Pitch,R_Ankle_Pitch,R_Ankle_Roll,L_Shoulder_Pitch,L_Shoulder_Roll,L_Elbow_Yaw,L_Elbow_Roll,L_Wrist_Yaw,R_Shoulder_Pitch,R_Shoulder_Roll,R_Elbow_Yaw,R_Elbow_Roll,R_Wrist_Yaw,L_H,L_Hand,R_H,R_Hand]
#Multiplicador das juntas do NAO
Head_Yaw=1.0;Head_Pitch=1.0;
L_Hip_Yaw_Pitch=1.0;L_Hip_Roll=1.0;L_Hip_Pitch=1.0;L_Knee_Pitch=1.0;L_Ankle_Pitch=1.0;L_Ankle_Roll=1.0;
R_Hip_Yaw_Pitch=1.0;R_Hip_Roll=1.0;R_Hip_Pitch=1.0;R_Knee_Pitch=1.0;R_Ankle_Pitch=1.0;R_Ankle_Roll=1.0;
L_Shoulder_Pitch=1.0;L_Shoulder_Roll=1.0;L_Elbow_Yaw=1.0;L_Elbow_Roll=1.0;L_Wrist_Yaw=1.0
R_Shoulder_Pitch=1.0;R_Shoulder_Roll=1.0;R_Elbow_Yaw=1.0;R_Elbow_Roll=1.0;R_Wrist_Yaw=1.0
R_H=1.0;L_H=1.0;R_Hand=1.0;L_Hand=1.0;
control_joints = [Head_Yaw,Head_Pitch,L_Hip_Yaw_Pitch,L_Hip_Roll,L_Hip_Pitch,L_Knee_Pitch,L_Ankle_Pitch,L_Ankle_Roll,R_Hip_Yaw_Pitch,R_Hip_Roll,R_Hip_Pitch,R_Knee_Pitch,R_Ankle_Pitch,R_Ankle_Roll,L_Shoulder_Pitch,L_Shoulder_Roll,L_Elbow_Yaw,L_Elbow_Roll,L_Wrist_Yaw,R_Shoulder_Pitch,R_Shoulder_Roll,R_Elbow_Yaw,R_Elbow_Roll,R_Wrist_Yaw,L_H,L_Hand,R_H,R_Hand]
[Head_Yaw,Head_Pitch,]
get_all_handles(3, clientID,Body)
f = file('start_moviment_ga.txt')
startMoves = []
for line in f:
split_line = line.split(' ')
startMoves += [[float(x) for x in split_line[1:]]]
#Reset da simulacao
def reset_simulation(clientID):
vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
time.sleep(1) # um pequeno sleep entre o stop e o start
vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
for move in startMoves:
JointControl(clientID, 0, Body, move)
time.sleep(0.1)
ret, NAO = vrep.simxGetObjectHandle(clientID, "NAO", vrep.simx_opmode_blocking)
ret, NAO_Head = vrep.simxGetObjectHandle(clientID, "HeadYaw", vrep.simx_opmode_blocking)
#Funcoes de GA
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness = creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_coef", lambda: (random.random() * COEF_RANGE - COEF_RANGE / 2.0))
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_coef, len(Body) * JOINT_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalRobot(individual):
reset_simulation(clientID)
dt = 0
fit = 0.0
ox = 0.0
negative_walk = 0
stable = True
scycles = 0
stcmax = 10
while dt < DELTA_TIME:
joint_movements = []
for i in xrange(len(Body)):
coefs = individual[i * JOINT_SIZE:(i + 1) * JOINT_SIZE]
joint_movements.append(truncated_Fourier(coefs, dt, stable)*control_joints[i])
JointControl2(clientID, 0, Body, joint_movements)
x = vrep.simxGetObjectPosition(clientID, NAO, -1, vrep.simx_opmode_blocking)[1][0]
dx = (x - ox)
ox = x
if dx < 0.0:
negative_walk += 1
else:
negative_walk = 0
ret, orientation = vrep.simxGetObjectOrientation(clientID, NAO, -1, vrep.simx_opmode_blocking)
orientation_alpha = min(ANGLE_THRESHOLD, max(-ANGLE_THRESHOLD, orientation[0]))
orientation_beta = min(ANGLE_THRESHOLD, max(-ANGLE_THRESHOLD, orientation[1]))
orientation_gama = min(ANGLE_THRESHOLD, max(-ANGLE_THRESHOLD, orientation[2]))
ha = hb = hg = 0
if orientation_alpha >= 0.0 :
ha = (- orientation_alpha) / ANGLE_THRESHOLD + 1.0
else:
ha = (orientation_alpha + ANGLE_THRESHOLD) / ANGLE_THRESHOLD
if orientation_beta >= 0.0 :
hb = (- orientation_beta) / ANGLE_THRESHOLD + 1.0
else:
hb = (orientation_beta + ANGLE_THRESHOLD) / ANGLE_THRESHOLD
if orientation_gama >= 0.0 :
hg = (- orientation_gama) / (ANGLE_THRESHOLD + 0.1) + 1.0
else:
hg = (orientation_gama + (ANGLE_THRESHOLD + 0.1)) / (ANGLE_THRESHOLD + 0.1)
if stable:
fit += (ha * hb * hg)
else:
fit += (ha * hb * dx)
if (ha * hb * hg < 0.00000001):
fit -= 2.0
if negative_walk >= 10:
fit -= 2.0
time.sleep(INTERVAL)
dt += INTERVAL
scycles += 1
if scycles >= stcmax:
scycles = 0
stable = not stable
if DEBUG: print(fit)
return fit,
def mutate(individual):
return [(random.random() * COEF_RANGE - COEF_RANGE / 2.0) for x in individual if random.random() < 0.05]
toolbox.register("evaluate", evalRobot)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", mutate)
toolbox.register("elite",tools.selBest, k = 2)
toolbox.register("select",tools.selTournament, tournsize = 3)
def main():
pop = toolbox.population(n = POPULATION_SIZE)
CXPB, MUTPB, NGEN = 0.6, 0.2, 100
if DEBUG: print("-- Life Span --")
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
for g in xrange(NGEN):
print("-- Generation %i --" % (g+1))
elite = toolbox.elite(pop)
elite = list(map(toolbox.clone, elite))
offspring = toolbox.select(pop, POPULATION_SIZE-2)
offspring = list(map(toolbox.clone, offspring))
for child1, child2 in zip(offspring[::2],offspring[1::2]):
if random.random() < CXPB:
toolbox.mate(child1, child2)
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
offspring = elite + offspring
if DEBUG: print("-- Calculating fitness for prole --")
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = map(toolbox.evaluate,invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
fits = [ind.fitness.values[0] for ind in offspring]
length = len(pop)
mean = sum(fits) / length
sum2 = sum(x*x for x in fits)
std = abs(sum2/length - mean**2)**0.5
print(" Min %s" % min(fits))
print(" Max %s" % max(fits))
print(" Avg %s" % mean)
print(" Std %s" % std)
if DEBUG: print(" Best Chromossome: %s"%tools.selBest(offspring, k = 1))
pop[:] = offspring
print(" The Walker: %s"% tools.selBest(pop, k = 1))
if __name__ == "__main__":
main()