-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelHillClimber.py
More file actions
72 lines (52 loc) · 1.9 KB
/
parallelHillClimber.py
File metadata and controls
72 lines (52 loc) · 1.9 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
from solution import SOLUTION
import constants as c
import copy
import os
class PARALLEL_HILL_CLIMBER:
def __init__(self):
self.parents = {}
self.nextAvailableID = 0
for n in range(c.populationSize):
self.parents[n] = SOLUTION(self.nextAvailableID)
self.nextAvailableID += 1
os.system('rm brain*.nndf')
os.system('rm fitness*.txt')
def Evolve(self):
self.Evaluate(self.parents)
for currentGeneration in range(c.numberOfGenerations):
self.Evolve_For_One_Generation()
def Evaluate(self, solutions):
for parent in solutions.values():
parent.Start_Simulation('DIRECT')
for parent in solutions.values():
parent.Wait_For_Simulation_To_End()
def Evolve_For_One_Generation(self):
self.Spawn()
self.Mutate()
self.Evaluate(self.children)
self.Print()
self.Select()
def Spawn(self):
self.children = {}
for i, parent in self.parents.items():
child = copy.deepcopy(parent)
child.Set_ID(self.nextAvailableID)
self.children[i] = child
self.nextAvailableID += 1
def Mutate(self):
for child in self.children.values():
child.Mutate()
def Select(self):
for n in self.parents.keys():
if self.children[n].fitness < self.parents[n].fitness:
self.parents[n] = self.children[n]
def Print(self):
print('\n')
for n in self.parents.keys():
print("key: {}, child fitness: {}, parent fitness: {}".format(n, self.children[n].fitness, self.parents[n].fitness))
def Show_Best(self):
best_parent = self.parents[0]
for parent in self.parents.values():
if best_parent.fitness > parent.fitness:
best_parent = parent
best_parent.Start_Simulation('GUI')