-
Notifications
You must be signed in to change notification settings - Fork 0
/
hillclimber.py
35 lines (27 loc) · 901 Bytes
/
hillclimber.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
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(0, c.numberOfGenerations):
self.Evolve_For_OneGeneration()
def Evolve_For_OneGeneration(self):
self.Spawn()
self.Mutate()
self.child.Evaluate("DIRECT")
self.Select()
self.Print()
def Print(self):
print("Parent fitness: " + str(self.parent.fitness) + " Child fitness: " + str(self.child.fitness))
def Spawn(self):
self.child = copy.deepcopy(self.parent)
def Mutate(self):
self.child.Mutate()
def Select(self):
if self.parent.fitness > self.child.fitness:
self.parent = self.child
def Show_Best(self):
self.parent.Evaluate("GUI")