1 from typing
import Tuple, Union
4 from tempfile
import TemporaryDirectory
11 second_order: bool =
False,
12 name: str =
"PANOC_ALM_problem",
13 ) -> Tuple[cs.CodeGenerator, int, int, int]:
14 """Convert the objective and constraint functions into a CasADi code
17 :param f: Objective function.
18 :param g: Constraint function.
19 :param second_order: Whether to generate functions for evaluating Hessians.
20 :param name: Optional string description of the problem (used for filename).
22 :return: * Code generator that generates the functions and derivatives
24 * Dimensions of the decision variables (primal dimension).
25 * Number of nonlinear constraints (dual dimension).
26 * Number of parameters.
29 assert f.n_in()
in [1, 2]
30 assert f.n_in() == g.n_in()
31 assert f.size1_in(0) == g.size1_in(0)
33 assert f.size1_in(1) == g.size1_in(1)
38 p = f.size1_in(1)
if f.n_in() == 2
else 0
39 xp = (f.sx_in(0), f.sx_in(1))
if f.n_in() == 2
else (f.sx_in(0),)
40 xp_names = (f.name_in(0), f.name_in(1))
if f.n_in() == 2
else (f.name_in(0),)
45 L =
f(*xp) + cs.dot(y,
g(*xp))
if m > 0
else f(*xp)
48 cg = cs.CodeGenerator(cgname)
62 [cs.gradient(
f(*xp), x)],
80 [cs.jtimes(
g(*xp), x, y,
True)],
90 [cs.hessian(L, x)[0]],
99 [cs.gradient(cs.jtimes(L, x, v,
False), x)],
100 [*xp_names,
"y",
"v"],
110 second_order: bool =
False,
111 name: str =
"PANOC_ALM_problem",
112 ) -> Tuple[cs.CodeGenerator, int, int, int]:
113 """Convert the objective and constraint functions into a CasADi code
116 :param f: Objective function.
117 :param g1: ALM constraint function.
118 :param g2: Quadratic penalty constraint function.
119 :param second_order: Whether to generate functions for evaluating Hessians.
120 :param name: Optional string description of the problem (used for filename).
122 :return: * Code generator that generates the functions and derivatives
124 * Dimensions of the decision variables (primal dimension).
125 * Number of nonlinear constraints (dual dimension).
126 * Number of parameters.
129 assert f.n_in()
in [1, 2]
130 assert f.n_in() == g1.n_in() == g2.n_in()
131 assert f.size1_in(0) == g1.size1_in(0) == g2.size1_in(0)
133 assert f.size1_in(1) == g1.size1_in(1) == g2.size1_in(1)
134 assert f.n_out() == 1
135 assert g1.n_out() == 1
136 assert g2.n_out() == 1
140 p = f.size1_in(1)
if f.n_in() == 2
else 0
141 xp = (f.sx_in(0), f.sx_in(1))
if f.n_in() == 2
else (f.sx_in(0),)
142 xp_names = (f.name_in(0), f.name_in(1))
if f.n_in() == 2
else (f.name_in(0),)
144 y1 = cs.SX.sym(
"y1", m1)
145 y2 = cs.SX.sym(
"y2", m2)
146 v = cs.SX.sym(
"v", n)
148 L =
f(*xp) + cs.dot(y1, g1(*xp))
if m1 > 0
else f(*xp)
151 cg = cs.CodeGenerator(cgname)
165 [cs.gradient(
f(*xp), x)],
183 [cs.jtimes(g1(*xp), x, y1,
True)],
201 [cs.jtimes(g2(*xp), x, y2,
True)
if m2 > 0
else []],
211 [cs.hessian(L, x)[0]],
220 [cs.gradient(cs.jtimes(L, x, v,
False), x)],
221 [*xp_names,
"y1",
"v"],
225 return cg, n, m1, m2, p
228 cgen: cs.CodeGenerator,
232 name: str =
"PANOC_ALM_problem",
234 """Compile the C-code using the given code-generator and load it as a
237 :param cgen: Code generator to generate C-code for the costs and the
239 :param n: Dimensions of the decision variables (primal dimension).
240 :param m: Number of nonlinear constraints (dual dimension).
241 :param p: Number of parameters.
242 :param name: Optional string description of the problem (used for filename).
244 :return: * Problem specification that can be passed to the solvers.
247 with TemporaryDirectory(prefix=
"")
as tmpdir:
248 cfile = cgen.generate(os.path.join(tmpdir,
""))
249 sofile = os.path.join(tmpdir, f
"{name}.so")
250 os.system(f
"cc -fPIC -shared -O3 -march=native {cfile} -o {sofile}")
252 prob = pa.load_casadi_problem_with_param(sofile, n, m)
254 prob = pa.load_casadi_problem(sofile, n, m)
258 cgen: cs.CodeGenerator,
263 name: str =
"PANOC_full_problem",
265 """Compile the C-code using the given code-generator and load it as a
268 :param cgen: Code generator to generate C-code for the costs and the
270 :param n: Dimensions of the decision variables (primal dimension).
271 :param m1: Number of ALM constraints (dual dimension 1).
272 :param m2: Number of quadratic penalty constraints (dual dimension 2).
273 :param p: Number of parameters.
274 :param name: Optional string description of the problem (used for filename).
276 :return: * ProblemFull specification that can be passed to the solvers.
279 with TemporaryDirectory(prefix=
"")
as tmpdir:
280 cfile = cgen.generate(tmpdir)
281 sofile = os.path.join(tmpdir, f
"{name}.so")
282 os.system(f
"cc -fPIC -shared -O3 -march=native {cfile} -o {sofile}")
284 prob = pa.load_casadi_problem_full_with_param(sofile, n, m1, m2)
286 prob = pa.load_casadi_problem_full(sofile, n, m1, m2)
293 second_order: bool =
False,
294 name: str =
"PANOC_ALM_problem",
296 """Compile the objective and constraint functions into a panocpy Problem.
298 :param f: Objective function.
299 :param g: Constraint function.
300 :param second_order: Whether to generate functions for evaluating Hessians.
301 :param name: Optional string description of the problem (used for filename).
303 :return: * Problem specification that can be passed to the solvers.