-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.py
51 lines (30 loc) · 1.68 KB
/
problem.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
from inspyred import ec
import numpy as np
class BakeryProblem:
def __init__(self, available_ingredients, products_price, products_consumption):
self.maximize = True
self.available_ingredients = available_ingredients
self.products_price = products_price
self.products_consumption = products_consumption
mins = []
maxs = []
for product in self.products_price.keys():
mins.append(0)
maxs.append(np.min(self.available_ingredients/self.products_consumption[product]))
self.bounder = ec.Bounder(lower_bound=mins, upper_bound=maxs)
def generator(self, random, args):
particle = []
for i in range(0,len(self.products_price)):
particle.append(random.uniform(self.bounder.lower_bound[i], self.bounder.upper_bound[i]))
return particle
def evaluator(self, candidates, args):
fitness = []
for c in candidates:
metric = sum(c*np.array(list(self.products_price.values()), dtype=float))
ingredient_use = np.zeros(shape=(len(self.available_ingredients)))
for index in range(0,len(c)):
ingredient_use += int(c[index])*self.products_consumption[list(self.products_consumption.keys())[index]]
ingredient_use = self.available_ingredients - ingredient_use
metric += sum(ingredient_use[ingredient_use < 0])*sum(c)
fitness.append(metric)
return fitness