forked from ds4dm/PySCIPOpt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlo_wines.py
68 lines (56 loc) · 1.78 KB
/
lo_wines.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
##@file lo_wines.py
#@brief Simple SCIP example of linear programming.
"""
It solves the same instance as lo_wines_simple.py:
maximize 15x + 18y + 30z
subject to 2x + y + z <= 60
x + 2y + z <= 60
z <= 30
x,y,z >= 0
Variables correspond to the production of three types of wine blends,
made from pure-grape wines.
Constraints correspond to the inventory of pure-grape wines.
Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
"""
from pyscipopt import Model, quicksum, SCIP_PARAMSETTING
#Initialize model
model = Model("Wine blending")
model.setPresolve(SCIP_PARAMSETTING.OFF)
Inventory = {"Alfrocheiro":60, "Baga":60, "Castelao":30}
Grapes = Inventory.keys()
Profit = {"Dry":15, "Medium":18, "Sweet":30}
Blends = Profit.keys()
Use = {
("Alfrocheiro","Dry"):2,
("Alfrocheiro","Medium"):1,
("Alfrocheiro","Sweet"):1,
("Baga","Dry"):1,
("Baga","Medium"):2,
("Baga","Sweet"):1,
("Castelao","Dry"):0,
("Castelao","Medium"):0,
("Castelao","Sweet"):1
}
# Create variables
x = {}
for j in Blends:
x[j] = model.addVar(vtype="C", name="x(%s)"%j)
# Create constraints
c = {}
for i in Grapes:
c[i] = model.addCons(quicksum(Use[i,j]*x[j] for j in Blends) <= Inventory[i], name="Use(%s)"%i)
# Objective
model.setObjective(quicksum(Profit[j]*x[j] for j in Blends), "maximize")
model.optimize()
if model.getStatus() == "optimal":
print("Optimal value:", model.getObjVal())
for j in x:
print(x[j].name, "=", model.getVal(x[j]), " (red. cost: ", model.getVarRedcost(x[j]), ")")
for i in c:
try:
dual = model.getDualsolLinear(c[i])
except:
dual = None
print("dual of", c[i].name, ":", dual)
else:
print("Problem could not be solved to optimality")