Skip to content

Instantly share code, notes, and snippets.

@jangirrishabh
Created June 14, 2018 11:41
Show Gist options
  • Save jangirrishabh/9faee18596467ee33ac5d91fd0cb675f to your computer and use it in GitHub Desktop.
Save jangirrishabh/9faee18596467ee33ac5d91fd0cb675f to your computer and use it in GitHub Desktop.
Snippet for toyCarIRL, blog usage, not executable
def optimization(self): # implement the convex optimization, posed as an SVM problem
m = len(self.expertPolicy)
P = matrix(2.0*np.eye(m), tc='d') # min ||w||
q = matrix(np.zeros(m), tc='d')
policyList = [self.expertPolicy]
h_list = [1]
for i in self.policiesFE.keys():
policyList.append(self.policiesFE[i])
h_list.append(1)
policyMat = np.matrix(policyList)
policyMat[0] = -1*policyMat[0]
G = matrix(policyMat, tc='d')
h = matrix(-np.array(h_list), tc='d')
sol = solvers.qp(P,q,G,h)
weights = np.squeeze(np.asarray(sol['x']))
norm = np.linalg.norm(weights)
weights = weights/norm
return weights # return the normalized weights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment