Skip to content

Instantly share code, notes, and snippets.

@amueller
Last active May 4, 2018 00:26
Show Gist options
  • Save amueller/8866e02e5132c999fa9f7b0c8b8977df to your computer and use it in GitHub Desktop.
Save amueller/8866e02e5132c999fa9f7b0c8b8977df to your computer and use it in GitHub Desktop.
import cvxpy as cvx
n_students = 130
n_projects = 30
assignment = cvx.Int(rows=n_students, cols=n_projects)
import numpy as np
rng = np.random.RandomState(0)
project_preferences = rng.rand(n_students, n_projects)
constraints = [cvx.sum_entries(assignment, axis=0) >= 4,
cvx.sum_entries(assignment, axis=0) <= 5,
cvx.sum_entries(assignment, axis=1) == 1,
assignment >=0, assignment <= 1]
# This works:
obj = cvx.Maximize(cvx.sum_entries(cvx.mul_elemwise(project_preferences, assignment)))
prob = cvx.Problem(obj, constraints)
prob.solve()
res = np.array(assignment.value)
# this is non-linear aka not allowed.
student_preferences = rng.rand(n_students, n_students)
obj = cvx.Maximize(cvx.sum_entries(cvx.mul_elemwise(project_preferences, assignment)) +
cvx.sum_entries(cvx.mul_elemwise(student_preferences, (assignment * assignment.T))))
prob = cvx.Problem(obj, constraints)
prob.solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment