|
| 1 | +# [Modelling](@id model-py) |
| 2 | + |
| 3 | +HiGHS has a rudimentry modelling language that allows models to be built and run using `highspy`. |
| 4 | + |
| 5 | +Below is an example of building a mathematical LP. The functions used are documented in detail below |
| 6 | +``` |
| 7 | +# model and solve the LP |
| 8 | +# |
| 9 | +# maximize 10 x1 + 25x2 |
| 10 | +# s. t. x1 + 2x2 <= 80 |
| 11 | +# x1 + 4x2 <= 120 |
| 12 | +# x1 >= 0; x2 >= 0 |
| 13 | +import highspy |
| 14 | +
|
| 15 | +h = highspy.Highs() |
| 16 | +
|
| 17 | +x1 = h.addVar() |
| 18 | +x2 = h.addVar() |
| 19 | +
|
| 20 | +h.addConstr(x1 + 2*x2 <= 80) |
| 21 | +h.addConstr(x1 + 4*x2 <= 120) |
| 22 | +
|
| 23 | +h.maximize(10*x1 + 25*x2) |
| 24 | +
|
| 25 | +print("x1 = ", h.val(x1)) |
| 26 | +print("x2 = ", h.val(x2)) |
| 27 | +``` |
| 28 | + |
| 29 | +## addVar |
| 30 | + |
| 31 | +Adds a variable to the model. By default it is continuous, |
| 32 | +non-negative, with zero objective coefficient, and has no name |
| 33 | +associated with it. |
| 34 | + |
| 35 | +``` |
| 36 | +addVar(lb = 0, ub = kHighsInf, obj = 0, type=HighsVarType.kContinuous, name = None) |
| 37 | +``` |
| 38 | + |
| 39 | +## addConstr |
| 40 | + |
| 41 | +Adds a constraint to the model. It must be defined in terms of a |
| 42 | +linear function, with `*` used when there are non-unit |
| 43 | +coefficients. By default it has a lower bound of -infinity, an upper |
| 44 | +bound of +infinity, and no name associated with it. |
| 45 | + |
| 46 | +``` |
| 47 | +addConstr(cons, name = None) |
| 48 | +``` |
| 49 | + |
| 50 | +## maximize |
| 51 | + |
| 52 | +Calls HiGHS to maximize the objective. By default it uses the |
| 53 | +objective coefficients defined when the variables were added to the |
| 54 | +model. However, a linear function can be passed as an argument. |
| 55 | + |
| 56 | +``` |
| 57 | +maximize(obj=None) |
| 58 | +``` |
| 59 | + |
| 60 | +## minimize |
| 61 | + |
| 62 | +Calls HiGHS to minimize the objective. By default it uses the |
| 63 | +objective coefficients defined when the variables were added to the |
| 64 | +model. However, a linear function can be passed as an argument. |
| 65 | + |
| 66 | +``` |
| 67 | +minimize(obj=None) |
| 68 | +``` |
| 69 | + |
| 70 | +## val |
| 71 | + |
| 72 | +Extracts the current value of a particular variable |
| 73 | + |
| 74 | +``` |
| 75 | +val(var) |
| 76 | +``` |
| 77 | + |
| 78 | +## vals |
| 79 | + |
| 80 | +Extracts the current values of a particular set of variables |
| 81 | + |
| 82 | +``` |
| 83 | +vals(vars) |
| 84 | +``` |
| 85 | + |
| 86 | +## MIP Example |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments