-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhillclimber.py
More file actions
40 lines (27 loc) · 901 Bytes
/
hillclimber.py
File metadata and controls
40 lines (27 loc) · 901 Bytes
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
from solution import SOLUTION
import constants as c
import copy
class HILL_CLIMBER:
def __init__(self):
self.parent = SOLUTION()
def Evolve(self):
self.parent.Evaluate('GUI')
for currentGeneration in range(c.numberOfGenerations):
self.Evolve_For_One_Generation()
def Evolve_For_One_Generation(self):
self.Spawn()
self.Mutate()
self.child.Evaluate('DIRECT')
self.Print()
self.Select()
def Spawn(self):
self.child = copy.deepcopy(self.parent)
def Mutate(self):
self.child.Mutate()
def Select(self):
if self.child.fitness < self.parent.fitness:
self.parent = self.child
def Print(self):
print("child fitness: {}, parent fitness: {}".format(self.child.fitness, self.parent.fitness))
def Show_Best(self):
self.parent.Evaluate('GUI')