-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
knapsack.py
27 lines (21 loc) · 883 Bytes
/
knapsack.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
from platypus import (Binary, Constraint, Direction, GeneticAlgorithm, Problem,
nondominated, unique)
# This simple example has an optimal value of 15 when picking items 1 and 4.
items = 7
capacity = 9
weights = [2, 3, 6, 7, 5, 9, 4]
profits = [6, 5, 8, 9, 6, 7, 3]
def knapsack(x):
selection = x[0]
total_weight = sum([weights[i] if selection[i] else 0 for i in range(items)])
total_profit = sum([profits[i] if selection[i] else 0 for i in range(items)])
return total_profit, total_weight
problem = Problem(1, 1, 1)
problem.types[0] = Binary(items)
problem.directions[0] = Direction.MAXIMIZE
problem.constraints[0] = Constraint("<=", capacity)
problem.function = knapsack
algorithm = GeneticAlgorithm(problem)
algorithm.run(10000)
for solution in unique(nondominated(algorithm.result)):
print(solution.variables, solution.objectives)