Skip to content

Instantly share code, notes, and snippets.

@adbuerger
Last active March 15, 2016 12:43
Show Gist options
  • Save adbuerger/ee5c4de0f8eeec5d29b0 to your computer and use it in GitHub Desktop.
Save adbuerger/ee5c4de0f8eeec5d29b0 to your computer and use it in GitHub Desktop.
import numpy as np
# Tested with CasADi 2.4.4
import casadi as ca
# Optimization variables
x = ca.MX.sym("x", 1, 2)
# Objective
f = x[0]**2 + x[1]**2
# Constraints
a = ca.MX.sym("a", 1)
b = ca.MX.sym("b", 1)
g = 2*a -1 + b
g_fcn = ca.MXFunction("a", [a, b], [g])
g_fcn = g_fcn.expand()
# Set up optimization variable y
REPEATED = 1
# Both versions lead to the same result
if REPEATED:
# This one works with OpenMP
y = ca.MX.sym("y", 1, 1)
Y = ca.repmat(y, 1, 2)
else:
# This one does not, and causes:
#
# CasADi warning: "OpenMP not yet supported for reduced outputs.
# Falling back to serial mode." issued on line 73 of file
# "/home/ab/casadi-src/casadi/core/function/map_internal.cpp".
y = ca.MX.sym("y", 1, 1)
Y = y
[g] = g_fcn.map([x, Y], "openmp")
nlp = ca.MXFunction("nlp", \
ca.nlpIn(x = ca.horzcat([x, y])), \
ca.nlpOut(f = f, g = g))
nlpsolver = ca.NlpSolver("nlpsolver", "ipopt", nlp)
x0 = np.ones((1, x.shape[1] + y.shape[1]))
sol = nlpsolver(x0 = x0, lbg = 0, ubg = 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment