Skip to content

Commit cb58465

Browse files
authored
Merge pull request #2834 from ERGO-Code/modelling-doc
Introduced `model-py.md` and `chip0.py`
2 parents a3a1f2f + 87d7f70 commit cb58465

File tree

4 files changed

+120
-7
lines changed

4 files changed

+120
-7
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Documenter.makedocs(
8888
"Python" => Any[
8989
"interfaces/python/index.md",
9090
"interfaces/python/example-py.md",
91+
"interfaces/python/model-py.md",
9192
],
9293
"CSharp" => "interfaces/csharp.md",
9394
"Julia" => "interfaces/julia/index.md",

docs/src/interfaces/python/index.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ To use `highspy` within a Python program, it must be imported
1818
import highspy
1919
```
2020

21-
When using `highspy`, it is likely that `numpy` structures will be needed,
22-
so must also be imported
23-
24-
```python
25-
import numpy as np
26-
```
27-
2821
## Initialize
2922

3023
HiGHS must be initialized before making calls to the HiGHS Python library:
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+

examples/chip0.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# model and solve the LP
2+
#
3+
# maximize 10 x1 + 25x2
4+
# s. t. x1 + 2x2 <= 80
5+
# x1 + 4x2 <= 120
6+
# x1 >= 0; x2 >= 0
7+
import highspy
8+
9+
h = highspy.Highs()
10+
11+
x1 = h.addVar(obj = 10)
12+
x2 = h.addVar(obj = 25)
13+
14+
h.addConstr(x1 + 2*x2 <= 80)
15+
h.addConstr(x1 + 4*x2 <= 120)
16+
17+
h.writeModel("")
18+
print("x1 = ", h.val(x1))
19+
print("x2 = ", h.val(x2))
20+
21+
h.maximize()
22+
23+
print("x1 = ", h.val(x1))
24+
print("x2 = ", h.val(x2))
25+
26+
print("x = ", h.vals([x1, x2]))
27+
28+
29+

0 commit comments

Comments
 (0)