|
| 1 | +import numpy as np |
| 2 | +from random import Random |
| 3 | +from inspyred import ec |
| 4 | +from inspyred.swarm import PSO |
| 5 | +from problem import BakeryProblem |
| 6 | + |
| 7 | +available_ingredients = np.array([1750, 55, 30, 1000]) |
| 8 | + |
| 9 | +products_price = {} |
| 10 | +products_price['Cookie'] = 3.5 |
| 11 | +products_price['Waffle'] = 5.2 |
| 12 | + |
| 13 | +products_consumption = {} |
| 14 | +products_consumption['Cookie'] = np.array([10, 0.3, 0.2, 1.2]) |
| 15 | +products_consumption['Waffle'] = np.array([12, 0.5, 0.2, 1.7]) |
| 16 | + |
| 17 | +problem = BakeryProblem(available_ingredients = available_ingredients, products_price = products_price, products_consumption = products_consumption) |
| 18 | + |
| 19 | +pso = PSO(Random()) |
| 20 | + |
| 21 | +pso.terminator = [ec.terminators.evaluation_termination, ec.terminators.diversity_termination] |
| 22 | + |
| 23 | +final_pop = pso.evolve(generator=problem.generator, |
| 24 | + evaluator=problem.evaluator,pop_size=25, |
| 25 | + bounder=problem.bounder,maximize=problem.maximize, |
| 26 | + max_evaluations=10000,mp_num_cpus=4,social_rate=3) |
| 27 | + |
| 28 | +best = np.array(max(final_pop).candidate).astype(int) |
| 29 | +print('Best Solution: \n{0}'.format(str(best)), best[0]*3.5 + best[1]*5.2) |
0 commit comments