Skip to content

Instantly share code, notes, and snippets.

@tdavchev
Created January 9, 2021 13:59
Show Gist options
  • Save tdavchev/37e1816ca15fc2424320507b92ad937a to your computer and use it in GitHub Desktop.
Save tdavchev/37e1816ca15fc2424320507b92ad937a to your computer and use it in GitHub Desktop.
QP Optimiser
class QPoptimizer(object):
def __call__(self, feature_num, learner, expert):
w = cp.Variable(feature_num)
obj_func = cp.Minimize(cp.norm(w))
constraints = [(expert-learner) @ w >= 2]
prob = cp.Problem(obj_func, constraints)
prob.solve()
if prob.status == "optimal":
log.debug("status: {0}".format(prob.status))
log.debug("optimal value {0}".format(prob.value))
weights = np.squeeze(np.asarray(w.value))
norm = np.linalg.norm(weights)
weights = weights/norm
return weights, prob.value
else:
log.debug("status: {0}".format(prob.status))
log.debug("returning Zeros..")
weights = np.zeros(feature_num)
return weights, prob.status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment