-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13624e0
commit 5994c80
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
10 269 | ||
55 95 | ||
10 4 | ||
47 60 | ||
5 32 | ||
4 23 | ||
50 72 | ||
8 80 | ||
61 62 | ||
85 65 | ||
87 46 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import math | ||
import sys | ||
from pathlib import Path | ||
|
||
from gamspy import ( | ||
Container, | ||
Equation, | ||
Model, | ||
Parameter, | ||
Problem, | ||
Sense, | ||
Set, | ||
Sum, | ||
Variable, | ||
VariableType, | ||
) | ||
|
||
# Declaration | ||
m = Container() | ||
i = Set(m, "i", description="items") | ||
p = Parameter(m, "p", description="profits", domain=i) | ||
w = Parameter(m, "p", description="weights", domain=i) | ||
c = Parameter(m, "c", description="capacity") | ||
x = Variable(m, "x", domain=i, description="chosen", type=VariableType.BINARY) | ||
|
||
cap_restr = Equation(m, name="capacity_restriction") | ||
cap_restr[...] = Sum(i, w[i] * x[i]) <= c | ||
|
||
utility = Sum(i, p[i] * x[i]) | ||
|
||
knapsack = Model( | ||
m, | ||
name="knapsack", | ||
equations=m.getEquations(), | ||
problem=Problem.MIP, | ||
sense=Sense.MAX, | ||
objective=utility, | ||
) | ||
|
||
|
||
# Instance data | ||
def load_instance_from_file(filename): | ||
def ints(elems): | ||
return [int(elem) for elem in elems] | ||
|
||
global items, capacity, profits, weights | ||
items, capacity, profits, weights = [], [], [], [] | ||
num_items = None | ||
with open(filename) as fp: | ||
for line in fp.readlines(): | ||
if not line.strip(): | ||
continue | ||
parts = line.split() | ||
assert len(parts) == 2 | ||
if not num_items: | ||
num_items, capacity = ints(parts) | ||
continue | ||
profits.append(parts[0]) | ||
weights.append(parts[1]) | ||
assert num_items | ||
items = [f"i{i + 1}" for i in range(num_items)] | ||
|
||
|
||
# Example instance taken from | ||
# http://artemisa.unicauca.edu.co/~johnyortega/instances_01_KP/ | ||
# and also available at https://github.com/JordiHOFC/knapsackproblemboolean | ||
load_instance_from_file( | ||
str(Path(__file__).parent.absolute()) + "/f1_l-d_kp_10_269" | ||
) | ||
i.setRecords(items) | ||
p.setRecords(zip(items, profits)) | ||
w.setRecords(zip(items, weights)) | ||
c.setRecords(capacity) | ||
|
||
# Run solve and display results | ||
knapsack.solve(output=sys.stdout) | ||
print(f"Objective function value = {knapsack.objective_value}") | ||
assert math.isclose(knapsack.objective_value, 269.0, rel_tol=0.001) | ||
levels = list(x.records["level"]) | ||
print( | ||
f'Chosen items = {", ".join([j for ix, j in enumerate(items) if levels[ix] == 1.0])}' | ||
) |