I get a build error for this line:
P = matrix(np.diag([1,0]), tc=’d’) --> SyntaxError: invalid syntax
Any idea what I'm doing wrong?
# From "Quadratic Programming with Python and CVXOPT," https://courses.csail.mit.edu/6.867/wiki/images/a/a7/Qp-cvxopt.pdf
import numpy as np
from cvxopt import matrix, solvers
def initialize(context):
context.spy = sid(8554) # SPY
def handle_data(context, data):
# Define QP parameters (directly)
# P = matrix([[1.0,0.0],[0.0,0.0]])
# q = matrix([3.0,4.0])
# G = matrix([[-1.0,0.0,-1.0,2.0,3.0],[0.0,-1.0,-3.0,5.0,4.0]])
# h = matrix([0.0,0.0,-15.0,100.0,80.0])
# Define QP parameters (with NumPy)
P = matrix(np.diag([1,0]), tc=’d’)
q = matrix(np.array([3,4]), tc=’d’)
G = matrix(np.array([[-1,0],[0,-1],[-1,-3],[2,5],[3,4]]), tc=’d’)
h = matrix(np.array([0,0,-15,100,80]), tc=’d’)
# Construct the QP, invoke solver
sol = solvers.qp(P,q,G,h)
# Extract optimal value and solution
x = sol[’x’] # [7.13e-07, 5.00e+00]
po = sol[’primal objective’] # 20.0000061731
print 'x = ' + str(x)
print 'po = ' + str(po)