Created
August 20, 2020 15:18
-
-
Save mvernacc/3f7bd8c777c47f447c594a3e2d4fed6d to your computer and use it in GitHub Desktop.
This file contains 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
"""Minimum working example to generate glp_add_cols issue with an integer linear program in CVXPY. | |
""" | |
import cvxpy as cp | |
import numpy as np | |
n = 4 | |
x = cp.Variable(n, integer=True) | |
A = np.array( | |
[[ 52., 102., 0., 0.], | |
[ 0., 0., 52., 102.]]) | |
a_max = np.array([300., 40.]) | |
B = np.array( | |
[[150., 150., 150., 150.], | |
[ 0., 150., 0., 150.]]) | |
b = np.array([300., 150.]) | |
c = np.ones(n) | |
constraint_positive = x >= 0 | |
constraint_A = A @ x <= a_max | |
constraint_B_low = B @ x >= 0.98 * b | |
constraint_B_high = B @ x <= 1.02 * b | |
# Add a trivial equality constraint. | |
# A potential source of this error is that | |
# "the CVXOPT GLPK_MI interface can't handle problems with no equality constraints." | |
constraint_equality_trivial = np.zeros(n) @ x == 0 | |
objective = cp.Minimize(c @ x) | |
problem = cp.Problem( | |
objective, | |
[constraint_positive, | |
constraint_A, | |
constraint_B_low, | |
constraint_B_high, | |
constraint_equality_trivial | |
]) | |
# With SCIP, the problem is solved with the correct result. | |
# problem.solve(solver='SCIP') | |
# With GLPK_MI, the problem fails with: | |
# glp_add_cols: ncs = 0; invalid number of columns | |
# Error detected in file api/prob1.c at line 362 | |
# Aborted (core dumped) | |
problem.solve(solver='GLPK_MI') | |
print(problem.status) | |
print('\nproblem.value, should be 2') | |
print(problem.value) | |
print('\nx.value, should be [1, 1, 0, 0]') | |
print(x.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment