From 9f45c1775e69b8c0ebc38433777e9e71e81f557b Mon Sep 17 00:00:00 2001 From: zhang Date: Sat, 9 Apr 2022 18:33:47 +0800 Subject: =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classic_or/__init__.py | 0 classic_or/kp/__init__.py | 0 classic_or/kp/simple_knapsack.py | 18 ++++++++++++++++++ 3 files changed, 18 insertions(+) create mode 100644 classic_or/__init__.py create mode 100644 classic_or/kp/__init__.py create mode 100644 classic_or/kp/simple_knapsack.py (limited to 'classic_or') diff --git a/classic_or/__init__.py b/classic_or/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/classic_or/kp/__init__.py b/classic_or/kp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/classic_or/kp/simple_knapsack.py b/classic_or/kp/simple_knapsack.py new file mode 100644 index 0000000..7244862 --- /dev/null +++ b/classic_or/kp/simple_knapsack.py @@ -0,0 +1,18 @@ +from pyomo.environ import * + +A = ['hammer', 'wrench', 'screwdriver', 'towel'] +b = {'hammer': 8, 'wrench': 3, 'screwdriver': 6, 'towel': 11} +w = {'hammer': 5, 'wrench': 7, 'screwdriver': 4, 'towel': 3} + +W_max = 14 +model = ConcreteModel() +model.x = Var(A, within=Binary) + +model.value = Objective( + expr=sum(b[i] * model.x[i] for i in A), sense=maximize) +model.weight = Constraint( + expr=sum(w[i] * model.x[i] * model.x[i] for i in A) <= W_max) +opt = SolverFactory('ipopt') +result_obj = opt.solve(model, tee=True) + +model.pprint() \ No newline at end of file -- cgit v1.2.3