Creating simple problems
Below are a few examples on how to create simple LP, MIP, MIQP, and similar problems. Note that they make use of API functions that resemble the C API functions for creating problems, and are used similarly here.
Generating a small Linear Programming problem
In this example, we create a problem and load a matrix of coefficients, a rhs, and an objective coefficient list with the loadproblem function. We also assign names to both rows and columns (both are optional). These data correspond to the following problem with three variables and four constraints:
minimize: | 3 x1 + 4 x2 + 5 x3 | ||
subject to: | x1 + x3 | ≥ | -2.4 |
2x1 + 3x3 | ≥ | -3 | |
2x2 + 3x3 | = | 4 | |
x2 + x3 | ≤ | 5 | |
-1 ≤ x1 ≤ 3 | |||
-1 ≤ x1 ≤ 5 | |||
-1 ≤ x1 ≤ 8 |
p = xp.problem() p.loadproblem("", # probname ['G','G','E', 'L'], # qrtypes [-2.4, -3, 4, 5], # rhs None, # range [3,4,5], # obj [0,2,4,8], # mstart None, # mnel [0,1,2,3,0,1,2,3], # mrwind [1,2,2,1,1,3,3,1], # dmatval [-1,-1,-1], # lb [3,5,8], # ub colnames = ['X1','X2','X3'], # column names rownames = ['row1','row2','row3','constr_04']) # row names p.write("loadlp", "lp") p.solve()
We then create another variable and add it to the problem, then modify the objective function. Note that the objective function is replaced by, not amended with, the new expression. After solving the problem, it saves it into a file called update.lp.
x = xp.var() p.addVariable(x) p.setObjective(x**2 + 2*x + 444) p.solve() p.write("updated", "lp")
A Mixed Integer Linear Programming problem
This example uses loadproblem to create a Mixed Integer Quadratically Constrained Quadratic Programming problem with two Special Ordered Sets. Note that data that is not needed is simply set as None.
The Examples directory provides similar examples for different types of problems.
p = xp.problem() p.loadproblem("", # probname ['G','G','L', 'L'], # qrtypes [-2.4, -3, 4, 5], # rhs None, # range [3,4,5], # obj [0,2,4,8], # mstart None, # mnel [0,1,2,3,0,1,2,3], # mrwind [1,1,1,1,1,1,1,1], # dmatval [-1,-1,-1], # lb [3,5,8], # ub [0,0,0,1,1,2], # mqobj1 [0,1,2,1,2,2], # mqobj1 [2,1,1,2,1,2], # dqe [2,3], # qcrows [2,3], # qcnquads [1,2,0,0,2], # qcmqcol1 [1,2,0,2,2], # qcmqcol2 [3,4,1,1,1], # qcdqval ['I','S'], # qgtype [0,1], # mgcols [0,2], # dlim ['1','1'], # qstype [0,2,4], # msstart [0,1,0,2], # mscols [1.1,1.2,1.3,1.4]) # dref p.solve()