-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelHillClimber.py
90 lines (75 loc) · 3.37 KB
/
parallelHillClimber.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
import pybullet as p
from solution import SOLUTION
import constants as c
import copy
import os
import pickle
class PARALLEL_HILL_CLIMBER:
def __init__(self):
os.system("rm brain*.nndf")
os.system("rm fitness*.txt")
os.system("rm body*.urdf")
self.parents = {}
self.nextAvailableID = 0
for i in range(0, c.populationSize):
self.parents[i] = SOLUTION(self.nextAvailableID)
self.nextAvailableID += 1
# self.parent = SOLUTION()
# print("Out HILL_CLIMBER")
self.bestFitnessValues = []
def Evolve(self, folderPath):
self.Evaluate(self.parents)
# self.parent.Evaluate("GUI")
for currentGeneration in range(0, c.numberOfGenerations):
self.Evolve_For_One_Generation(currentGeneration)
if currentGeneration % c.gensPerPickle == 0:
lowestFitnessParent = self.Get_Best_Robot()
pickle.dump( lowestFitnessParent.linkPlan, open( folderPath + "Gen" + str(currentGeneration) + "linkPlan.p", "wb" ) )
pickle.dump( lowestFitnessParent.jointPlan, open( folderPath + "Gen" + str(currentGeneration) + "jointPlan.p", "wb" ) )
pickle.dump( lowestFitnessParent.sensorWeights, open( folderPath + "Gen" + str(currentGeneration) + "sensorWeights.p", "wb" ) )
pickle.dump( lowestFitnessParent.motorWeights, open( folderPath + "Gen" + str(currentGeneration) + "motorWeights.p", "wb" ) )
pickle.dump( lowestFitnessParent.numHiddenNeurons, open( folderPath + "Gen" + str(currentGeneration) + "hiddenNeurons.p", "wb" ) )
def Evolve_For_One_Generation(self, generation):
self.Spawn()
self.Mutate(generation)
self.Evaluate(self.children)
# self.Print()
self.Select()
def Spawn(self):
self.children = {}
for id in self.parents:
self.children[id] = copy.deepcopy(self.parents[id])
self.children[id].Set_ID(self.nextAvailableID)
self.nextAvailableID += 1
def Mutate(self, generation):
for id in self.children:
self.children[id].Mutate(generation)
def Select(self):
minFitness = 10000
for id in self.parents:
if self.children[id].fitness < self.parents[id].fitness:
self.parents[id] = self.children[id]
if self.parents[id].fitness < minFitness:
minFitness = self.parents[id].fitness
self.bestFitnessValues.append(minFitness)
def Print(self):
for id in self.parents:
print("")
print('Parent fitness: ' + str(self.parents[id].fitness))
print('Child fitness: ' + str(self.children[id].fitness))
print("")
def Get_Best_Robot(self):
lowestFitnessParent = self.parents[0]
for id in self.parents:
if self.parents[id].fitness < lowestFitnessParent.fitness:
lowestFitnessParent = self.parents[id]
return lowestFitnessParent
def Show_Best(self):
lowestFitnessParent = self.Get_Best_Robot()
lowestFitnessParent.Start_Simulation("GUI")
lowestFitnessParent.Wait_For_Simulation_To_End()
def Evaluate(self, solutions):
for id in solutions:
solutions[id].Start_Simulation("DIRECT")
for id in solutions:
solutions[id].Wait_For_Simulation_To_End()