Last active
February 6, 2016 14:58
-
-
Save jgillis/de15f427174858f15414 to your computer and use it in GitHub Desktop.
casadistructs in matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [out] = casadi_struct2vec(s) | |
flat = {}; | |
if isstruct(s) | |
for f=fieldnames(s)' | |
flat = {flat{:} casadi_struct2vec(s.(f{1}))}; | |
end | |
out = vertcat(flat{:}); | |
else if iscell(s) | |
for i=1:length(s) | |
flat = {flat{:} casadi_struct2vec(s{i})}; | |
end | |
out = vertcat(flat{:}); | |
else | |
try | |
out = vec(s); | |
catch | |
import casadi.* | |
out = vec(DM(s)); | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [out] = casadi_vec2struct(s,vec) | |
import casadi.* | |
assert(isvector(vec)) | |
try | |
vec.sparsity(); | |
catch | |
vec = DM(vec); | |
end | |
flat = {}; | |
if isstruct(s) | |
out = struct; | |
sizes = {0}; | |
for f=fieldnames(s)' | |
n = size(casadi_struct2vec(s.(f{1})),1); | |
sizes = {sizes{:} sizes{end}+n}; | |
end | |
comps = vertsplit(vec,sizes); | |
i = 1; | |
for f=fieldnames(s)' | |
out.(f{1}) = casadi_vec2struct(s.(f{1}),comps{i}); | |
i = i+1; | |
end | |
else if iscell(s) | |
out = cell(size(s)); | |
sizes = {0}; | |
for i=1:length(s) | |
n = size(casadi_struct2vec(s{i}),1); | |
sizes = {sizes{:} sizes{end}+n}; | |
end | |
comps = vertsplit(vec,sizes); | |
for i=1:length(s) | |
out{i} = casadi_vec2struct(s{i},comps{i}); | |
end | |
else | |
out = reshape(vec,size(s)); | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import casadi.* | |
% Write all decision variables as a structure | |
V = struct; | |
V.x = MX.sym('x'); | |
V.y = MX.sym('y'); | |
V.z = MX.sym('z',4,2); | |
% Write all constraints as a structure | |
constr = struct; | |
constr.initial = V.x+V.y; | |
constr.z = V.z'*diag([2,4,7,6])*V.z; | |
lbg = struct; | |
lbg.initial = 2; | |
lbg.z = 1*ones(2,2); | |
ubg = struct; | |
ubg.initial = 2; | |
ubg.z = 3*ones(2,2); | |
% Solve the nlp | |
e = (2+V.z)*[1+V.x;2-V.y]; | |
f = e'*e + V.z(1).^2; | |
nlp = struct('x', casadi_struct2vec(V), 'f', f, 'g' , casadi_struct2vec(constr)); | |
solver = nlpsol('solver', 'ipopt', nlp); | |
args = struct; | |
args.lbg = casadi_struct2vec(lbg); | |
args.ubg = casadi_struct2vec(ubg); | |
res = solver(args); | |
% Read solution as flat vectors | |
x_opt = full(res.x); | |
g_opt = full(res.g); | |
lambda_opt = full(res.lam_g); | |
% Create the original structures from these | |
Vsol = casadi_vec2struct(V,x_opt) | |
lam = casadi_vec2struct(constr,lambda_opt) | |
g = casadi_vec2struct(constr,g_opt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment