-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (129 loc) · 4.47 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import random
from genetic_algorithm import GeneticAlgorithm
from genetic_function import GeneticFunction
ALL_PRODUCTS = [
"salt",
"sweet Italian sausage",
"lean ground beef",
"minced onion",
"garlic",
"tomatoes",
"sugar",
"water",
"rice",
"butter",
"eggs",
"lemon",
"salmon",
"apple",
"bread",
"olives",
"cheese",
"milk",
"paper",
"chocolate",
"flour",
"vanilla",
"blueberries",
"nuts",
"crackers",
"margarine",
"prunes",
"soda",
"corn",
"celery",
"orange",
"bacon",
# operations
"bake",
"fry",
"chill",
"wash",
"mix",
"boil",
"steam",
"melt",
"spray",
"chop",
]
class GuessRecipe(GeneticFunction):
def __init__(self, target,
limit=150, size=350, prob_selection=0.4,
prob_crossover=0.7, prob_mutation=0.3):
self.target = self.products_to_chromo(target)
self.counter = 0
self.prob_selection = prob_selection
self.limit = limit
self.size = size
self.prob_crossover = prob_crossover
self.prob_mutation = prob_mutation
# GeneticFunctions interface impls
def probability_crossover(self):
return self.prob_crossover
def probability_mutation(self):
return self.prob_mutation
def initial(self):
return [self.random_chromo() for j in range(self.size)]
def fitness(self, chromo):
# larger is better, matched == 0
return -sum(abs(c - t) for c, t in zip(chromo, self.target))
def get_generation_count(self):
return self.counter
def check_stop(self, fits_population):
self.counter += 1
fits_population.sort()
fits_population.reverse()
best_end = int(len(fits_population) * self.prob_selection)
fits_population = fits_population[0:best_end]
best_match = fits_population[0][1]
fits = [f for f, ch in fits_population]
best = max(fits)
worst = min(fits)
ave = sum(fits) / len(fits)
print(
"[G %3d] score=(%4d, %4d, %4d): %r" %
(self.counter, best, ave, worst,
self.chromo_to_products(best_match)))
return best == 0 or self.counter >= self.limit
def parents(self, fits_populations):
while True:
father = self.tournament(fits_populations)
mother = self.tournament(fits_populations)
yield (father, mother)
pass
pass
def crossover(self, parents):
father, mother = parents
index1 = random.randint(1, len(self.target) - 2)
index2 = random.randint(1, len(self.target) - 2)
if index1 > index2: index1, index2 = index2, index1
child1 = father[:index1] + mother[index1:index2] + father[index2:]
child2 = mother[:index1] + father[index1:index2] + mother[index2:]
return (child1, child2)
def mutation(self, chromosome):
index = random.randint(0, len(self.target) - 1)
vary = random.randint(0, len(ALL_PRODUCTS) - 1)
mutated = list(chromosome)
mutated[index] = vary
return mutated
# internals
def tournament(self, fits_populations):
alicef, alice = self.select_random(fits_populations)
bobf, bob = self.select_random(fits_populations)
return alice if alicef > bobf else bob
def select_random(self, fits_populations):
return fits_populations[random.randint(0, len(fits_populations)-1)]
def products_to_chromo(self, products):
indices = [ALL_PRODUCTS.index(product) for product in products]
# id = "".join(map(str, indices))
# return [ord(ch) for ch in id]
return indices
def chromo_to_products(self, chromo):
# return "".join(chr(max(1, min(ch, 255))) for ch in chromo)
return [ALL_PRODUCTS[index] for index in chromo]
def random_chromo(self):
return [random.randint(0, len(ALL_PRODUCTS) - 1) for i in range(len(self.target))]
pass
if __name__ == "__main__":
lasagna = ["tomatoes", "steam", "cheese", "melt", "sweet Italian sausage", "mix", "garlic"]
GeneticAlgorithm(GuessRecipe(lasagna)).run()