Skip to content

Commit 05bb56b

Browse files
committed
Trying content tabs
1 parent 3d2738f commit 05bb56b

2 files changed

Lines changed: 88 additions & 79 deletions

File tree

docs/getting_started.md

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,83 @@
11
# Getting started
22

3-
## In Python
4-
5-
```python
6-
import unopy
7-
Inf = float("inf")
8-
9-
# hs015.mod
10-
def objective(x):
11-
return 100.*(x[1] - x[0]**2)**2 + (1. - x[0])**2
12-
13-
def constraints(x, constraint_values):
14-
constraint_values[:] = [x[0]*x[1], x[0] + x[1]**2]
15-
16-
def objective_gradient(x, gradient):
17-
gradient[:] = [400.*x[0]**3 - 400.*x[0]*x[1] + 2.*x[0] - 2.,
18-
200.*(x[1] - x[0]**2)]
19-
20-
def jacobian(x, jacobian_values):
21-
jacobian_values[:] = [x[1], 1., x[0], 2.*x[1]]
22-
23-
def lagrangian_hessian(x, objective_multiplier, multipliers, hessian_values):
24-
hessian_values[:] = [objective_multiplier*(1200*x[0]**2 - 400.*x[1] + 2.),
25-
-400.*objective_multiplier*x[0] - multipliers[0],
26-
200.*objective_multiplier - 2.*multipliers[1]]
27-
28-
def lagrangian_hessian_operator(x, evaluate_at_x, objective_multiplier,
29-
multipliers, vector, result):
30-
hessian00 = objective_multiplier*(1200*x[0]**2. - 400.*x[1] + 2.)
31-
hessian10 = -400.*objective_multiplier*x[0] - multipliers[0]
32-
hessian11 = 200.*objective_multiplier - 2.*multipliers[1]
33-
result[:] = [hessian00*vector[0] + hessian10*vector[1],
34-
hessian10*vector[0] + hessian11*vector[1]]
35-
36-
if __name__ == '__main__':
37-
number_variables = 2
38-
number_constraints = 2
39-
40-
model = unopy.Model(unopy.PROBLEM_NONLINEAR, number_variables,
41-
unopy.ZERO_BASED_INDEXING)
42-
model.set_variables_lower_bounds([-Inf, -Inf])
43-
model.set_variables_upper_bounds([0.5, Inf])
44-
model.set_objective(unopy.MINIMIZE, objective, objective_gradient)
45-
model.set_constraints(number_constraints, constraints, [1., 0.], [Inf, Inf],
46-
4, [0, 1, 0, 1], [0, 0, 1, 1], jacobian)
47-
model.set_lagrangian_hessian(3, unopy.LOWER_TRIANGLE, [0, 1, 1], [0, 0, 1],
48-
lagrangian_hessian)
49-
model.set_lagrangian_sign_convention(unopy.MULTIPLIER_NEGATIVE)
50-
model.set_initial_primal_iterate([-2., 1.])
51-
52-
# solver creation
53-
uno_solver = unopy.UnoSolver()
54-
uno_solver.set_preset("filtersqp")
55-
56-
# solve with the filtersqp preset
57-
print("Solving with Uno", unopy.current_uno_version())
58-
result = uno_solver.optimize(model)
59-
print("Objective at solution:", result.solution_objective)
60-
```
3+
=== "Python"
4+
5+
```py
6+
import unopy
7+
Inf = float("inf")
8+
9+
# hs015.mod
10+
def objective(x):
11+
return 100.*(x[1] - x[0]**2)**2 + (1. - x[0])**2
12+
13+
def constraints(x, constraint_values):
14+
constraint_values[:] = [x[0]*x[1], x[0] + x[1]**2]
15+
16+
def objective_gradient(x, gradient):
17+
gradient[:] = [400.*x[0]**3 - 400.*x[0]*x[1] + 2.*x[0] - 2.,
18+
200.*(x[1] - x[0]**2)]
19+
20+
def jacobian(x, jacobian_values):
21+
jacobian_values[:] = [x[1], 1., x[0], 2.*x[1]]
22+
23+
def lagrangian_hessian(x, objective_multiplier, multipliers, hessian_values):
24+
hessian_values[:] = [objective_multiplier*(1200*x[0]**2 - 400.*x[1] + 2.),
25+
-400.*objective_multiplier*x[0] - multipliers[0],
26+
200.*objective_multiplier - 2.*multipliers[1]]
27+
28+
def lagrangian_hessian_operator(x, evaluate_at_x, objective_multiplier,
29+
multipliers, vector, result):
30+
hessian00 = objective_multiplier*(1200*x[0]**2. - 400.*x[1] + 2.)
31+
hessian10 = -400.*objective_multiplier*x[0] - multipliers[0]
32+
hessian11 = 200.*objective_multiplier - 2.*multipliers[1]
33+
result[:] = [hessian00*vector[0] + hessian10*vector[1],
34+
hessian10*vector[0] + hessian11*vector[1]]
35+
36+
if __name__ == '__main__':
37+
number_variables = 2
38+
number_constraints = 2
39+
40+
model = unopy.Model(unopy.PROBLEM_NONLINEAR, number_variables,
41+
unopy.ZERO_BASED_INDEXING)
42+
model.set_variables_lower_bounds([-Inf, -Inf])
43+
model.set_variables_upper_bounds([0.5, Inf])
44+
model.set_objective(unopy.MINIMIZE, objective, objective_gradient)
45+
model.set_constraints(number_constraints, constraints, [1., 0.], [Inf, Inf],
46+
4, [0, 1, 0, 1], [0, 0, 1, 1], jacobian)
47+
model.set_lagrangian_hessian(3, unopy.LOWER_TRIANGLE, [0, 1, 1], [0, 0, 1],
48+
lagrangian_hessian)
49+
model.set_lagrangian_sign_convention(unopy.MULTIPLIER_NEGATIVE)
50+
model.set_initial_primal_iterate([-2., 1.])
51+
52+
# solver creation
53+
uno_solver = unopy.UnoSolver()
54+
uno_solver.set_preset("filtersqp")
55+
56+
# solve with the filtersqp preset
57+
print("Solving with Uno", unopy.current_uno_version())
58+
result = uno_solver.optimize(model)
59+
print("Objective at solution:", result.solution_objective)
60+
```
61+
62+
=== "Julia"
63+
64+
```julia
65+
using UnoSolver, JuMP
66+
67+
jump_model = Model(() -> UnoSolver.Optimizer(preset="filtersqp"))
68+
x0 = [-2, 1]
69+
uvar = [0.5, Inf]
70+
@variable(jump_model, x[i = 1:2] ≤ uvar[i], start = x0[i])
71+
@objective(jump_model, Min, 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2)
72+
@constraint(jump_model, x[1] * x[2] - 1 ≥ 0)
73+
@constraint(jump_model, x[1] + x[2]^2 ≥ 0)
74+
75+
optimize!(jump_model)
76+
77+
termination_status(jump_model) # solver termination status
78+
objective_value(jump_model) # objective value
79+
value.(x) # primal solution
80+
```
6181

6282
For more details, see the [Python documentation](./interfaces/python).
63-
64-
## In Julia
65-
66-
```julia
67-
using UnoSolver, JuMP
68-
69-
jump_model = Model(() -> UnoSolver.Optimizer(preset="filtersqp"))
70-
x0 = [-2, 1]
71-
uvar = [0.5, Inf]
72-
@variable(jump_model, x[i = 1:2] uvar[i], start = x0[i])
73-
@objective(jump_model, Min, 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2)
74-
@constraint(jump_model, x[1] * x[2] - 1 0)
75-
@constraint(jump_model, x[1] + x[2]^2 0)
76-
77-
optimize!(jump_model)
78-
79-
termination_status(jump_model) # solver termination status
80-
objective_value(jump_model) # objective value
81-
value.(x) # primal solution
82-
```
83-
8483
For more details, see the [Julia documentation](./interfaces/julia).

mkdocs.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ theme:
44
features:
55
- navigation.footer
66
- navigation.top
7+
- content.code.copy
78

89
nav:
910
- Introduction: 'index.md'
@@ -35,6 +36,15 @@ markdown_extensions:
3536
generic: true
3637
- footnotes
3738
- admonition
39+
- pymdownx.highlight:
40+
anchor_linenums: true
41+
line_spans: __span
42+
pygments_lang_class: true
43+
- pymdownx.inlinehilite
44+
- pymdownx.snippets
45+
- pymdownx.superfences
46+
- pymdownx.tabbed:
47+
alternate_style: true
3848

3949
plugins:
4050
- gh-admonitions

0 commit comments

Comments
 (0)