Python Exam Practice - Exercises
Python Exam Practice - Exercises
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:
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:
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:
Question 5
Consider the following Gurobi model and decision variables defined in Python:
1
Introduction to Operations Research · 2022 - 2023
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
Root relaxation: objective 6.500000e+02, 15 iterations, 0.00 seconds (0.00 work units)
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
(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?
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:
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.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()
(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?
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
(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.