PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
lo_wines.py
Go to the documentation of this file.
1 
3 """
4 It solves the same instance as lo_wines_simple.py:
5 
6 maximize 15x + 18y + 30z
7 subject to 2x + y + z <= 60
8  x + 2y + z <= 60
9  z <= 30
10  x,y,z >= 0
11 Variables correspond to the production of three types of wine blends,
12 made from pure-grape wines.
13 Constraints correspond to the inventory of pure-grape wines.
14 
15 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
16 """
17 from pyscipopt import Model, quicksum, SCIP_PARAMSETTING
18 
19 # Initialize model
20 model = Model("Wine blending")
21 model.setPresolve(SCIP_PARAMSETTING.OFF)
22 
23 Inventory = {"Alfrocheiro": 60, "Baga": 60, "Castelao": 30}
24 Grapes = Inventory.keys()
25 
26 Profit = {"Dry": 15, "Medium": 18, "Sweet": 30}
27 Blends = Profit.keys()
28 
29 Use = {
30  ("Alfrocheiro", "Dry"): 2,
31  ("Alfrocheiro", "Medium"): 1,
32  ("Alfrocheiro", "Sweet"): 1,
33  ("Baga", "Dry"): 1,
34  ("Baga", "Medium"): 2,
35  ("Baga", "Sweet"): 1,
36  ("Castelao", "Dry"): 0,
37  ("Castelao", "Medium"): 0,
38  ("Castelao", "Sweet"): 1
39 }
40 
41 # Create variables
42 x = {}
43 for j in Blends:
44  x[j] = model.addVar(vtype="C", name="x(%s)" % j)
45 
46 # Create constraints
47 c = {}
48 for i in Grapes:
49  c[i] = model.addCons(quicksum(Use[i, j] * x[j] for j in Blends) <= Inventory[i], name="Use(%s)" % i)
50 
51 # Objective
52 model.setObjective(quicksum(Profit[j] * x[j] for j in Blends), "maximize")
53 
54 model.optimize()
55 
56 if model.getStatus() == "optimal":
57  print("Optimal value:", model.getObjVal())
58 
59  for j in x:
60  print(x[j].name, "=", model.getVal(x[j]), " (red. cost: ", model.getVarRedcost(x[j]), ")")
61  for i in c:
62  try:
63  dual = model.getDualsolLinear(c[i])
64  except:
65  dual = None
66  print("dual of", c[i].name, ":", dual)
67 else:
68  print("Problem could not be solved to optimality")
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357