Skip to content

Instantly share code, notes, and snippets.

@jgillis
Last active September 24, 2020 21:29
Show Gist options
  • Save jgillis/9d12df1994b6fea08eddd0a3f0b0737f to your computer and use it in GitHub Desktop.
Save jgillis/9d12df1994b6fea08eddd0a3f0b0737f to your computer and use it in GitHub Desktop.
retrieving nlpsol stats with to_function
import casadi.*
opti = Opti();
x = opti.variable(2);
fx = sin(x)-[2;0.3];
opti.minimize(sumsqr(fx));
opti.solver('ipopt');
f = opti.to_function('f',{x},{fx});
f(3);
get_stats(f)
# Note: this is very hacky.
from casadi import *
opti = Opti()
x = opti.variable(2)
fx = sin(x)-vertcat(2,0.3)
opti.minimize(sumsqr(fx))
opti.solver('ipopt')
f = opti.to_function('f',[x],[fx])
f(3)
def get_stats(f):
dep = None
# Loop over the algorithm
for k in range(f.n_instructions()):
if f.instruction_id(k)==OP_CALL:
d = f.instruction_MX(k).which_function()
if d.name()=="solver":
dep = d
break
if dep is None:
return {}
else:
return dep.stats(1)
print(get_stats(f))
function [stats] = get_stats(f)
dep = 0;
% Loop over the algorithm
for k=0:f.n_instructions()-1
if f.instruction_id(k)==casadi.OP_CALL
d = f.instruction_MX(k).which_function();
if d.name()=='solver'
dep = d;
break
end
end
end
if dep==0
stats = struct;
else
stats = dep.stats(1);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment