diff options
| author | zhang <zch921005@126.com> | 2022-04-09 18:33:47 +0800 |
|---|---|---|
| committer | zhang <zch921005@126.com> | 2022-04-09 18:33:47 +0800 |
| commit | 9f45c1775e69b8c0ebc38433777e9e71e81f557b (patch) | |
| tree | 2b23347db6c46d08177b286508ef6624b2ab8844 /classic_or | |
| parent | 3ed1c5d0e4018fc70012a6209a859a059f7127b5 (diff) | |
更新脚本
Diffstat (limited to 'classic_or')
| -rw-r--r-- | classic_or/__init__.py | 0 | ||||
| -rw-r--r-- | classic_or/kp/__init__.py | 0 | ||||
| -rw-r--r-- | classic_or/kp/simple_knapsack.py | 18 |
3 files changed, 18 insertions, 0 deletions
diff --git a/classic_or/__init__.py b/classic_or/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/classic_or/__init__.py diff --git a/classic_or/kp/__init__.py b/classic_or/kp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/classic_or/kp/__init__.py 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 |
