|
1 | 1 | # Getting started |
2 | 2 |
|
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 | + ``` |
61 | 81 |
|
62 | 82 | 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 | | - |
84 | 83 | For more details, see the [Julia documentation](./interfaces/julia). |
0 commit comments