alpaqa
0.0.1
Nonconvex constrained optimization
examples
CasADi
Rosenbrock
codegen-rosenbrock.py
Go to the documentation of this file.
1
from
casadi
import
SX, Function, CodeGenerator, vertcat, jtimes, gradient
2
from
sys
import
argv
3
4
if
len(argv) < 2:
5
print(f
"Usage: {argv[0]} <name>"
)
6
exit(0)
7
8
x = SX.sym(
"x"
)
9
y = SX.sym(
"y"
)
10
z = SX.sym(
"z"
)
11
unknwns = vertcat(x, y, z)
12
13
w = SX.sym(
"w"
)
14
15
# Formulate the NLP
16
f = x**2 + 100*z**2
17
g = z + (1-x)**2 - y
18
19
cg = CodeGenerator(f
"{argv[1]}.c"
)
20
cg.add(Function(
"f"
, [unknwns],
21
[f],
22
[
"x"
], [
"f"
]))
23
cg.add(Function(
"grad_f"
, [unknwns],
24
[gradient(f, unknwns)],
25
[
"x"
], [
"grad_f"
]))
26
cg.add(Function(
"g"
, [unknwns],
27
[g],
28
[
"x"
], [
"g"
]))
29
cg.add(Function(
"grad_g"
, [unknwns, w],
30
[jtimes(g, unknwns, w,
True
)],
31
[
"x"
,
"w"
], [
"grad_g"
]))
32
cg.generate()
Generated by
1.9.2