Professional Documents
Culture Documents
Iyse6999 - Swelch34 - hw5: 1 Problem 1
Iyse6999 - Swelch34 - hw5: 1 Problem 1
1 Problem 1
The LP problem is formulated as follows:
min C T X
where ci,j ∈ C is the cost for assigning employee i to task j and xi,j ∈ X takes on the value of 1 if
employee i is assigned to task j.
The cost matrix C is:
3 7 11 8
0 4 4 6
C= 0 4
10 9
0 0 6 5
In [18]: import cvxpy as cp
import numpy as np
C = np.array([[3,7,11,8],
[0,4,4,6],
[0,4,10,9],
[0,0,6,5]])
X = cp.Variable((4,4), boolean=True)
objective = cp.Minimize(cp.sum(C @ X))
constraints = [cp.sum(X, axis=1) == np.ones(4)]
prob = cp.Problem(objective, constraints)
result = prob.solve(solver='ECOS_BB')
print("Optimal value", result)
print("Optimal assignment")
print(np.array(X.value > 0.9).astype(int)) # A numpy ndarray.
Optimal value 77.00000000002758
Optimal assignment
[[0 1 0 0]
[1 0 0 0]
[1 0 0 0]
[1 0 0 0]]
1
2 Problem 2
The shortest path indicated by red numbers below was found using the Bellman equation.
3 Problem 3
3.1 A)
The problem is formulated as follows:
[ |D| |G| ]
max ∑ vi di − ∑ ci pi
i i
subject to
∑ f ij − ∑ f ij = pi ∀i ∈ G
j∈O(i ) j ∈ I (i )
∑ f ij − ∑ f ij = −di ∀i ∈ D
j∈O(i ) j ∈ I (i )
∑ f ij − ∑ f ij = 0 ∀i ∈ N \( G ∪ D )
j∈O(i ) j ∈ I (i )
2
3.2 B)
In [47]: import cvxpy as cp
import numpy as np
d_1 = 10
d_2 = 120
d_3 = 90
f_12_max = 100
f_23_max = 120
f_34_max = 50
f_45_max = 90
f_56_max = 60
f_61_max = 50
B_12 = 11.6
B_23 = 5.9
B_34 = 13.7
B_45 = 9.8
B_56 = 5.6
B_61 = 10.5
F = cp.Variable((6,6))
generators = cp.sum(F, axis=1)
loads = cp.sum(F, axis=1)
E = generators + loads
C = np.array([10, 0, 6, 0, 5, 0])
constraints = [
(p_1_min <= E[0]),
(E[0] <= p_1_max),
(p_2_min <= E[2]),
(E[2] <= p_2_max),
(p_3_min <= E[4]),
(E[4] <= p_3_max),
(d_1 == E[1]),
(d_2 == E[3]),
(d_3 == E[5]),
(F[0,1] <= f_12_max),
(F[1,2] <= f_23_max),
(F[2,3] <= f_34_max),
(F[3,4] <= f_45_max),
3
(F[4,5] <= f_56_max),
(F[5,0] <= f_61_max),
]
objective = cp.Maximize(objective_fn)
prob = cp.Problem(objective, constraints)
result = prob.solve()
print("Optimal value", result)
print("Optimal assignment")
print(f'Flows: \n{np.round(F.value, 2)}')