forked from scipopt/PySCIPOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.py
32 lines (24 loc) · 967 Bytes
/
puzzle.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
##@file tutorial/puzzle.py
#@brief solve a simple puzzle using SCIP
"""
On a beach there are octopuses, turtles and cranes.
The total number of legs of all animals is 80, while the number of heads is 32.
What are the minimum numbers of turtles and octopuses, respectively?
Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
"""
from pyscipopt import Model
model = Model("puzzle")
x = model.addVar(vtype="I", name="octopusses")
y = model.addVar(vtype="I", name="turtles")
z = model.addVar(vtype="I", name="cranes")
# Set up constraint for number of heads
model.addCons(x + y + z == 32, name="Heads")
# Set up constraint for number of legs
model.addCons(8*x + 4*y + 2*z == 80, name="Legs")
# Set objective function
model.setObjective(x + y, "minimize")
model.hideOutput()
model.optimize()
#solution = model.getBestSol()
print("Optimal value:", model.getObjVal())
print((x.name, y.name, z.name), " = ", (model.getVal(x), model.getVal(y), model.getVal(z)))