Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

iyse6999_swelch34_hw5

February 21, 2021

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 )

f ij = Bij (θi − θ j ) ∀(i, j) ∈ E


− Fij ≤ f ij ≤ Fij ∀(i, j) ∈ E
pim ≤ pi ≤ piM ∀i ∈ G
dim ≤ di ≤ diM ∀i ∈ D
where p1min = 20, p1max = 70, p2min = 20, p2max = 150, p3min = 10, p3max = 150
and d1 = 10, d2 = 120, d3 = 90
max = 100, f max = 120, f max = 50, f max = 90, f max = 60, f max = 50
and f 12 23 34 45 56 61
and B12 = 11.6, B23 = 5.9, B34 = 13.7, B45 = 9.8, B56 = 5.6, B61 = 10.5
and c1 = 10, c2 = 6, c3 = 5.

2
3.2 B)
In [47]: import cvxpy as cp
import numpy as np

p_1_min, p_1_max = 20, 70


p_2_min, p_2_max = 20, 150
p_3_min, p_3_max = 10, 150

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_fn = cp.sum(loads) - C @ generators

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)}')

Optimal value -50.00000002277086


Optimal assignment
Flows:
[[-17.77 98.84 -17.77 -17.77 -17.77 -17.77]
[-22.77 -22.77 118.84 -22.77 -22.77 -22.77]
[ -7.77 -7.77 -7.77 48.84 -7.77 -7.77]
[ -5.77 -5.77 -5.77 -5.77 88.84 -5.77]
[-10.77 -10.77 -10.77 -10.77 -10.77 58.84]
[ 48.84 -0.77 -0.77 -0.77 -0.77 -0.77]]

You might also like