Skip to content

Instantly share code, notes, and snippets.

@adbuerger
Last active April 11, 2016 06:05
Show Gist options
  • Save adbuerger/bed9fefe277877941fa0f2049b5545bb to your computer and use it in GitHub Desktop.
Save adbuerger/bed9fefe277877941fa0f2049b5545bb to your computer and use it in GitHub Desktop.
for CasADi 3.0
import casadi as ca
import pylab as pl
N = 50
WORKING_EXAMPLE = False
if WORKING_EXAMPLE:
x = ca.MX.sym("x", 2)
x0 = x[0]
x1 = x[1]
else:
x0 = ca.MX.sym("x0", 1)
x1 = ca.MX.sym("x1", 1)
x = ca.veccat(x0, x1)
y = x0
for k in range(N):
y = pl.sin(y)
z = x1
for i in range(N):
z = pl.cos(z)
# Serial NLP solving
f_s = y + z
nlp_s = {"x" :x, "f": f_s}
nlpsolver_s = ca.nlpsol("nlpsolver", "ipopt", nlp_s)
sol_s = nlpsolver_s(x0 = [4, 8])
# Parallel NLP solving
F0 = ca.Function("F0",[x],[y])
F1 = ca.Function("F1",[x],[z])
s = ca.Function.conditional('s',[F0,F1],F0)
mapres = s.map([ca.DM(range(2)).T,ca.repmat(x,1,2)],"openmp")
fun_p = ca.Function("fun2",[x],[ca.sum2(mapres[0])])
f_p = fun_p(x)
nlp_p = {"x" : x, "f" : f_p}
nlpsolver_p = ca.nlpsol("nlpsolver", "ipopt", nlp_p)
sol_p = nlpsolver_p(x0 = [4, 8])
# Print results
print "x_opt_s = " + str(sol_s["x"])
print "f_opt_s = " + str(sol_s["f"])
print "x_opt_p = " + str(sol_p["x"])
print "f_opt_p = " + str(sol_p["f"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment