-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvilProducts.py
35 lines (28 loc) · 1.28 KB
/
EvilProducts.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
import numpy as np
import constants
class EvilProducts:
def __init__(self, DNA, initalFitness=None):
self.DNA = DNA
self.fitness = initalFitness
def setFitness(self, fitness):
self.fitness = fitness
def getFitness(self):
return self.fitness
def mutate(self, mutationRate, workstationsJson, workstationTypes):
if np.random.random() < mutationRate:
p = np.random.randint(0, len(self.DNA))
i = np.random.randint(0, constants.PRODUCTS_PATH_LENGTH)
workstationTypeIndex = np.random.randint(len(workstationsJson['workStations']))
first = self.DNA[p][2][:i]
change = workstationsJson['workStations'][workstationTypeIndex]['type']
last = self.DNA[p][2][i + 1:]
self.DNA[p] = (self.DNA[p][0], self.DNA[p][1], first + change + last)
return self
@staticmethod
def recombine(ancestor1, ancestor2):
new_Evil_Products = EvilProducts(list(ancestor1.DNA))
for i in range(len(new_Evil_Products.DNA)):
if np.random.random() < 0.5:
new_Evil_Products.setFitness(0)
new_Evil_Products.DNA[i] = (new_Evil_Products.DNA[i][0], new_Evil_Products.DNA[i][1], ancestor2.DNA[i][2])
return new_Evil_Products