Created
March 4, 2024 16:01
-
-
Save WPettersson/414dbd0655382ed8e0f84040d2739368 to your computer and use it in GitHub Desktop.
Testing HiGHS in pulp
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
import pulp | |
import highspy | |
import numpy as np | |
prob = pulp.LpProblem("hi") | |
x = pulp.LpVariable("x", 0, 3) | |
y = pulp.LpVariable("y", 0, 3) | |
prob += x + y | |
prob += x <= y | |
print("Start pulp.HiGHS") | |
s = pulp.HiGHS(msg=False) | |
prob.solve(s) | |
print("End pulp.HiGHS") | |
print("Start pulp.PULP_CBC_CMD") | |
s = pulp.PULP_CBC_CMD(msg=False) | |
prob.solve(s) | |
print("End pulp.PULP_CBC_CMD") | |
print("Start highspy") | |
h = highspy.Highs() | |
h.setOptionValue("output_flag", False) | |
inf = highspy.kHighsInf | |
# Define two variables, first using identifiers for the bound values, | |
# and then using constants | |
lower = 0 | |
upper = 4 | |
h.addVar(lower, upper) | |
h.addVar(1, inf) | |
# Define the objective coefficients (costs) of the two variables, | |
# identifying the variable by index, and changing its cost from the | |
# default value of zero | |
cost = 1 | |
h.changeColCost(0, cost) | |
h.changeColCost(1, 1) | |
# Define constraints for the model | |
# | |
# The first constraint (x1<=7) has only one nonzero coefficient, | |
# identified by variable index 1 and value 1 | |
num_nz = 1 | |
index = 1 | |
value = 1 | |
h.addRow(-inf, 7, num_nz, index, value) | |
# The second constraint (5 <= x0 + 2x1 <= 15) has two nonzero | |
# coefficients, so arrays of indices and values are required | |
num_nz = 2 | |
index = np.array([0, 1]) | |
value = np.array([1, 2]) | |
h.addRow(5, 15, num_nz, index, value) | |
# The final constraint (6 <= 3x0 + 2x1) has the same indices but | |
# different values | |
num_nz = 2 | |
value = np.array([3, 2]) | |
h.addRow(6, inf, num_nz, index, value) | |
h.run() | |
print("End highspy") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment