Skip to content

Commit a7801df

Browse files
committed
initial commit - add knapsack version 1
Very basic implementation using pyomo
0 parents commit a7801df

File tree

8 files changed

+57
-0
lines changed

8 files changed

+57
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
.idea

Readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Pyomo: https://pyomo.readthedocs.io/en/stable/index.html
2+
3+
Refer
4+
https://jckantor.github.io/ND-Pyomo-Cookbook/
5+
6+
Github page: https://github.com/Pyomo/pyomo
7+
8+
9+
Knapsack

knapsack/__init__.py

Whitespace-only changes.

knapsack/basic_v1.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pyomo.core import ConcreteModel, Constraint, Objective, Var, maximize
2+
3+
# Problem
4+
# V = [1, 1, 1, 10, 10, 13, 3]
5+
# W = [2, 2, 2, 5, 5, 8, 3]
6+
from pyomo.repn.plugins.baron_writer import Binary
7+
8+
from pyomo_solver.solver_factory import Solver, get_solver
9+
10+
model = ConcreteModel()
11+
model.x_1 = Var(within=Binary)
12+
model.x_2 = Var(within=Binary)
13+
model.x_3 = Var(within=Binary)
14+
model.x_4 = Var(within=Binary)
15+
model.x_5 = Var(within=Binary)
16+
model.x_6 = Var(within=Binary)
17+
model.x_7 = Var(within=Binary)
18+
19+
model.obj = Objective(
20+
expr=model.x_1 + model.x_2 + model.x_3 + 10 * model.x_4 + 10 * model.x_5 + 13 * model.x_6 + 3 * model.x_7,
21+
sense=maximize
22+
)
23+
24+
model.con_0 = Constraint(
25+
expr=2 * model.x_1 + 2 * model.x_2 + 2 * model.x_3 + 5 * model.x_4 +
26+
5 * model.x_5 + 8 * model.x_6 + 3 * model.x_7 <= 10
27+
)
28+
29+
solver = get_solver(Solver.BONMIN)
30+
results = solver.results(model)
31+
32+
for v in model.component_data_objects(Var):
33+
print(str(v), v.value)

pyomo_solver/__init__.py

Whitespace-only changes.

pyomo_solver/solver_factory.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pyomo.environ as pyomo_env
2+
from enum import Enum
3+
4+
5+
class Solver(Enum):
6+
CBC = 1
7+
BONMIN = 2
8+
9+
10+
def get_solver(solver: Solver):
11+
return pyomo_env.SolverFactory('./bonmin')

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy==1.19.0
2+
pyomo==5.7

solvers/bonmin

9.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)