-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvolutionController.py
124 lines (109 loc) · 4.47 KB
/
EvolutionController.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
#Python code for de evolution model
#Author Joaquín Silveira
import vrep
import time
from RobotController import robotController
import functools
import numpy as np
import math
from multiprocessing.pool import ThreadPool
import secuenceGenerator as sg
import argparse
import SecuenceRecorder as sr
from EvolutionModel import EvolutionModel
import yaml as y
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def recoverPorts():
with open("evolutionModelPorts.yml", 'r') as ymlFile:
portConfig = y.load(ymlFile)
return portConfig['ports']
def main(sec_file, best_file, numb_threads, exploration_factor, num_gen):
#Maing sure no connection is active
vrep.simxFinish(-1)
#Recover port numbers
ports = recoverPorts()
pool = ThreadPool(numb_threads)
for gen in range(num_gen):
instructions = sr.readInstructions(sec_file)
instructionChunks = list(chunks(instructions, math.floor(len(instructions)/numb_threads)))
runInfo = []
for i in range(numb_threads):
runInfo.append(pool.apply_async(runModel, args=(int(ports[i]), instructionChunks[i])))
pool.close()
pool.join()
runInfo = [r.get() for r in runInfo]
runInfo2 = []
for i in runInfo:
runInfo2 += i
#Sort by score
sortedByScore = sorted(runInfo2, key=lambda x:x['Score'],reverse=True)
bestSecuences = [r['instructions'] for r in sortedByScore[:10]]
newSecuences = [sg.generateNewSec(sec, exploration_factor) for sec in bestSecuences]
newSecuences = sum(newSecuences,[])
sr.recordSecuences(bestSecuences + newSecuences, sec_file)
sr.recordSecuences(bestSecuences, best_file)
def runModel(portNumber, secuenceList):
#Establish connection
clientID = vrep.simxStart('127.0.0.1', portNumber, True, True, 5000, 5)
#Verify connection
if clientID == -1 :
print("Connection to the simluator api could not be established")
print("Make sure the simulator is up and running on port {}".format(portNumber))
else:
evolutionModel = EvolutionModel()
robotcontroller = robotController(clientID)
#Set simulation to be Synchronous instead of Asynchronous
vrep.simxSynchronous(clientID, True)
#Setting Time Step to 50 ms (miliseconds)
dt = 0.05
#Start simulation
vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
runInfo = []
for secuence in secuenceList:
runtime = 0
observationTrace = []
for instruction in secuence:
robotcontroller.moveRobot(instruction[0])
#This is what makes the simulation Synchronous
initialTime = 0.0
actualTime = initialTime + dt
runtime += dt
#Condition to stop simulation
done = False
vrep.simxSynchronousTrigger(clientID)
n = 1
while (n <= instruction[1] and not done):
n+=1
#Make de simulation run one step (dt determines how many seconds pass by between step and step)
vrep.simxSynchronousTrigger(clientID)
#Advance time in my internal counter
actualTime = actualTime + dt
runtime += dt
observations = robotcontroller.observablePositions()
observationTrace.append({'Observation': observations, 'runtime' : runtime})
done = evolutionModel.isDone(observations)
runInfo.append({'instructions' : secuence, 'observations' : observationTrace, 'Score' : evolutionModel.getScore(observationTrace)})
robotcontroller.resetRobot()
#Stop_Start_Simulation
vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking)
#This sleep is necesary for the simulation to finish stopping before starting again
time.sleep(2)
robotcontroller.resetRobot()
vrep.simxStartSimulation(clientID, vrep.simx_opmode_blocking)
robotcontroller.resetRobot()
#Should always end by finishing connetions
vrep.simxStopSimulation(clientID, vrep.simx_opmode_blocking)
vrep.simxFinish(clientID)
return runInfo
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Evolution Model for robots')
parser.add_argument( '-sf','--sec_file', type=str, help='File Name where secuences are stored', default='secuenceFile.txt')
parser.add_argument('-bf', '--best_file', type=str, help='File Name where bes secuences are stored', default='bestSecuences.txt')
parser.add_argument('-th', '--numb_threads', type=int, help='Number of threads to be run', default=4)
parser.add_argument('-ef', '--exploration_factor', type=int, help='How many new secuences for every old secuence', default=10)
parser.add_argument('-g', '--num_gen', type=int, help='Number of generations to be run', default=50)
args = parser.parse_args()
main(**vars(args))