alpaqa 0.0.1
Nonconvex constrained optimization
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
main.py
Go to the documentation of this file.
1import alpaqa as pa
2import numpy as np
3import casadi as cs
4
5# Problem specification
6# minimize ½ xᵀHx
7# s.t. Ax ≤ b
8H = np.array([[3, -1],
9 [-1, 3]])
10A = np.array([[2, 1]])
11b = np.array([-1])
12
13x = cs.SX.sym("x", 2)
14f = cs.Function("f", [x], [0.5 * cs.dot(x, H @ x)])
15g = cs.Function("g", [x], [A @ x])
16problem = pa.generate_and_compile_casadi_problem(f, g, "example_name")
17
18# Specify the bounds
19problem.C.lowerbound = np.full((problem.n,), -np.inf)
20problem.C.upperbound = np.full((problem.n,), np.inf)
21problem.D.lowerbound = np.full((problem.m,), -np.inf)
22problem.D.upperbound = b
23
24# Settings for the outer augmented Lagrangian method
25almparam = pa.ALMParams(
26 ε = 1e-8, # tolerance
27 δ = 1e-8,
28 Δ = 10, # penalty update factor
29 max_iter = 20,
30 print_interval = 1,
31)
32# Settings for the inner PANOC solver
33panocparam = pa.PANOCParams(
34 max_iter = 500,
35 print_interval = 10,
36)
37# Settings for the L-BFGS algorithm used by PANOC
38lbfgsparam = pa.LBFGSParams(
39 memory = 2,
40)
41
42# Create an ALM solver using PANOC as inner solver
43solver = pa.ALMSolver(
44 almparam, # Params for outer solver
45 pa.PANOCSolver(panocparam, lbfgsparam), # Inner solver
46)
47
48# Initial guess
49x = np.array([2, 2]) # decision variables
50y = np.array([1]) # Lagrange multipliers
51
52# Solve the problem
53x_sol, y_sol, stats = solver(problem, x, y)
54
55# Print the results
56print(f"""
57status: {stats['status']}
58inner iterations: {stats['inner']['iterations']}
59outer iterations: {stats['outer_iterations']}
60elapsed time: {stats['elapsed_time']}
61x = {x_sol}
62y = {y_sol}
63f = {problem.f(x_sol)}
64""")
solver
Definition: main.py:43