-
Notifications
You must be signed in to change notification settings - Fork 1
/
hm3_1.py
29 lines (28 loc) · 1019 Bytes
/
hm3_1.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
#solving problem of hw3_1 (extra question)
import cvxpy as cp
import cvxopt
A= cvxopt.matrix([1,2,0,1,0,0,3,1,0,3,1,1,2,1,2,5,1,0,3,2],(4,5)).T
c_max = cvxopt.matrix([100,100,100,100,100],(5,1))
p= cvxopt.matrix([3,2,7,6])
p_disc=cvxopt.matrix([2,1,4,2])
q=cvxopt.matrix([4,10,5,10])
u=cp.Variable(4,1)
x=cp.Variable(4,1)
objective = cp.Maximize(cp.sum(u))
constraints1 =[cp.min(p[i]*x[i],p[i]*q[i]+p_disc[i]*(x[i]-q[i]))>=u[i] for i in range(4)]
constraints =[x>=0, A*x<=c_max]
constraints1.extend(constraints)
prob = cp.Problem(objective,constraints1)
prob.solve()
print x.value
lamb = constraints1[2].dual_value
#############################################################
#original fomulation
#############################################################
print "equavlent form"
xx = cp.Variable(4,1)
objective1 = cp.Maximize(sum([cp.min(p[i]*xx[i],p[i]*q[i]+p_disc[i]*(xx[i]-q[i])) for i in range(4)]))
constraints2 = [xx>=0, A*xx<=c_max]
prob1 = cp.Problem(objective1, constraints2)
prob1.solve()
print xx.value