You are on page 1of 6

Introduction to Operations Research · 2022 - 2023

Python Practice Questions

Question 1
In Python, let nodes = [1, 2, 3, . . . , n] denote the list of nodes, and arcs = [(1, 2), (1, 3), (1, 4), . . . , (n, n − 1)]
denote the list of arcs in a directed complete network. Suppose you computed all non-empty proper node
subsets with at least two nodes, and recorded these in a list of tuples:

subsets = [(1, 2), (1, 3), (1, 4), . . . , (1, 2, . . . , n − 1), (2, 3, . . . , n)]

Write Python code that would add all subtour elimination constraints (SECs) of option 1 into a Gurobi
model of the Traveling Salesman Problem (TSP), named tsp. You can assume that the decision variables
are defined with the following Python code:

x = tsp.addVars(arcs, name = "x", vtype = GRB.BINARY)

Question 2
As in the previous question, suppose you defined a set of nodes and arcs in Python, named nodes
and arcs, respectively. Furthermore, suppose you defined a Gurobi model named minC ostF low and
continuous non-negative flow variables named f low as follows:

from gurobipy import *


minCostFlow = Model("Minimum Cost Flow Model")
flow = minCostFlow.addVars(arcs, name = "f")

Given that the unit flow costs are stored in a dictionary named costs, where the keys are the arc tuples
and the values are the unit flow costs, write down Python code that sets a cost minimizing objective for
this flow model.

Question 3
Consider the flow model in the previous question. Now, suppose you defined the net supply quantities
of each node in dictionary named supplies, where the keys are the nodes and the values are the net
supply quantities. Write Python code that adds flow balance constraints to the model minC ostF low.

Question 4
See the Python code below, which creates a Gurobi model for a well-known optimization problem:

from gurobipy import *


mysteryModel = Model("Mystery")
x = mysteryModel.addVars(mysterySet1, mysterySet2, name = "x")

mysteryModel.addConstrs( (x.sum(i, "*") == mysteryParameter1[i] for i in mysterySet1), "MysteryConstraint1")


mysteryModel.addConstrs( (x.sum("*", j) == mysteryParameter2[j] for j in mysterySet2), "MysteryConstraint2")

mysteryModel.setObjective(sum(cost[i, j] * x[i, j] for i in mysterySet1 for j in mysterySet2), GRB.MINIMIZE)

Which optimization problem(s) can this Gurobi model represent?

Question 5
Consider the following Gurobi model and decision variables defined in Python:

1
Introduction to Operations Research · 2022 - 2023

from gurobipy import *


I = {0, 1, 2, 3, 4, 5, 6}
J = {1, 2, 3, 4}

model = Model("Practice Model")


x = model.addVars(I, name = "x", vtype = GRB.BINARY)
y = model.addVars(J, name = "y", vtype = GRB.BINARY)
z = model.addVars(I, J, name = "z")
For each of the following lines of code, write down mathematically which constraints they represent.

(a) model.addConstrs( (sum(z[i, j] for i in range(k) for j in J) <= Q[k] for k in


range(1, 6)), "Constraint1")

(b) model.addConstrs( (y[j] >= z.sum("*", j) + M * (1 - x.sum("*")) for j in J),


"Constraint2" )

(c) model.addConstr( (sum(z[i, j] for i in I for j in J if i > j + 1) <= sum(x[i]


for i in I if i <= 2)), "Constraint3")

Question 6
Consider the following gurobipy optimization console output.
Gurobi Optimizer version 10.0.0 build v10.0.0rc2 (win64)

CPU model: Intel(R) Core(TM) i3-4130T CPU @ 2.90GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads

Optimize a model with 44 rows, 54 columns and 128 nonzeros


Model fingerprint: 0xb84d3971
Variable types: 0 continuous, 54 integer (54 binary)
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [1e+02, 4e+02]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 1e+00]
Found heuristic solution: objective 740.0000000
Variable types: 0 continuous, 54 integer (54 binary)

Root relaxation: objective 6.500000e+02, 15 iterations, 0.00 seconds (0.00 work units)

Nodes | Current Node | Objective Bounds | Work


Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time

* 0 0 0 650.0000000 650.00000 0.00% - 0s

Explored 1 nodes (15 simplex iterations) in 0.03 seconds (0.00 work units)
Thread count was 4 (of 4 available processors)

2
Introduction to Operations Research · 2022 - 2023

Solution count 2: 650 740

Optimal solution found (tolerance 1.00e-04)


Best objective 6.500000000000e+02, best bound 6.500000000000e+02, gap 0.0000%

(a) How many decision variables are in the optimization problem? Of which type are these variables?

(b) How many feasible solutions did Gurobi find while searching for an optimal solution? What is the
optimal solution?

(c) How many simplex iterations where performed?

Question 7
Assume a model is defined and feasible solutions exist. What is the effect of the following code?

model.setParam(‘Outputflag’, False)
model.optimize()

Question 8
Consider the following runnable gurobipy program:

from gurobipy import GRB, Model

grid = [[ 1 , ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, 6 ],


[‘.’, ‘.’, 6 , ‘.’, 2 , ‘.’, 7 , ‘.’, ‘.’],
[ 7 , 8 , 0 , 4 , 5 , ‘.’, 1 , ‘.’, 3 ],
[‘.’, ‘.’, ‘.’, 8 , ‘.’, 7 , ‘.’, ‘.’, 4 ],
[‘.’, ‘.’, ‘.’, ‘.’, 3 , ‘.’, ‘.’, ‘.’, ‘.’],
[‘.’, 0 , ‘.’, ‘.’, ‘.’, 4 , 2 , ‘.’, 1 ],
[ 3 , 1 , 2 , 0 , 7 , ‘.’, ‘.’, 4 , ‘.’],
[‘.’, 4 , ‘.’, ‘.’, 1 , 2 , ‘.’, 7 , 8 ],
[ 0 , ‘.’, 8 , ‘.’, ‘.’, ‘.’, ‘.’, ‘.’, ‘.’]]

n = len(grid[0])
s = int(n ** 0.5)

model = Model("?")
vars = model.addVars(n, n, n, vtype=GRB.BINARY, name=‘X’)

model.addConstrs( (vars[i, j, grid[i][j]] == 1


for i in range(n)
for j in range(n) if grid[i][j] != ‘.’), name =‘C1’)

model.addConstrs((vars.sum(i, j, "*") == 1
for i in range(n)
for j in range(n)), name=‘C2’)

model.addConstrs((vars.sum(i, "*", v) == 1

3
Introduction to Operations Research · 2022 - 2023

for i in range(n)
for v in range(n)), name=‘C3’)

model.addConstrs((vars.sum("*", j, v) == 1
for j in range(n)
for v in range(n)), name=‘C4’)

model.addConstrs((
sum(vars[i, j, v] for i in range(i0 * s, (i0 + 1) * s)
for j in range(j0 * s, (j0 + 1) * s)) == 1
for v in range(n)
for i0 in range(s)
for j0 in range(s)), name=‘C5’)

model.setObjective(0, GRB.MINIMIZE)
model.optimize()

(a) What does this program do?

(b) What are the decision variables? What do they represent?

(c) What is the objective function? What does this mean?

(d) In this formulation, there are 5 sets of constraints, labeled as C1, C2, C3, C4 and C5. Provide an
explanation per constraint.

(e) (Bonus question, obviously not suitable for the exam) What is the optimal solution of the problem?

Question 9
The following program gives an error. What goes wrong?

from gurobipy import Model, GRB

nodes = [i for i in range(12)]


customers = [j for j in range(15)]

m = Model(name = "A Model")

x = m.addVars(customers, nodes, vtype = GRB.BINARY)

m.addConstrs( (x[i,j] >= 1 for i in nodes for j in customers), "Customer-Node constraint")

Question 10
Match the code lines 1-3 to lines 4-6 such that both lines together produce runnable code. You may
assume the data structures cost, buy, and f oods are defined.

1) import gurobipy as gp
2) from gurobipy import *
3) from gurobipy import Model as mod

4
Introduction to Operations Research · 2022 - 2023

4) m = mod("diet")
5) m.setObjective(quicksum(cost[f] * buy[f] for f in foods ), GRB.MINIMIZE)
6) m = gp.Model("diet2")

Question 11
nodes = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
workers = {"Amy": 4, "Dan": 2, "Willfred": 5}
names = list(workers.keys())
x = m.addVars(nodes, nodes, vtype=GRB.BINARY, name=’x’)
d = m.addVars(names, nodes, name = "d")

How many decision variables does the model m have? Of which type are these variables?

Question 12
Assume a model m is solved to optimality. Write code that prints the optimal objective value and the
optimal value of decision variable x onto the console.

Question 13
Write down the mathematical formulation of the objective function of the following Gurobi model:

locations = (1, 2, 3)

distances = { (1, 1): 0.00, (1, 2): 5.26, (1, 3): 5.00,
(2, 1): 5.26, (2, 2): 0.00, (2, 3): 1.26,
(3, 1): 5.00, (3, 2): 1.26, (3, 3): 0.00}

for i in locations:
model.setObjective((sum(distances[i, j] * x[i, j] for j in locations)), GRB.MINIMIZE)

Question 14
for j in nodes[1:]:
for i in nodes[1:]:
m.addConstr( (u[i] - u[j] + len(nodes) * x[i, j] <= len(nodes) - 1), "SEC3")

Above is one way of adding the Option-3 subtour elimination constraints (SECs). Give an alternative
code snippet for adding these SECs.

Question 15
Consider the following Gurobi program in Python:
from gurobipy import GRB, Model

m = Model("linearProgram")

x = m.addVar(name="x")
y = m.addVar(name="y")
z = m.addVar(name="z")

5
Introduction to Operations Research · 2022 - 2023

m.setObjective(x + 2.0 * y - 4.0 * z)

m.addConstr(x + y + z <= 10, "c0")


m.addConstr(x - 2.0 * y + 2.0 * z <= 5, "c1")
m.addConstr(4.0 * x + y + z >= 2, "c2")

(a) Give the linear program formulation for the given code.

(b) Give a line of code that will ensure that the given model has a maximization objective.

You might also like