Created
January 9, 2021 13:59
-
-
Save tdavchev/37e1816ca15fc2424320507b92ad937a to your computer and use it in GitHub Desktop.
QP Optimiser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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