Last active
September 24, 2020 21:29
-
-
Save jgillis/9d12df1994b6fea08eddd0a3f0b0737f to your computer and use it in GitHub Desktop.
retrieving nlpsol stats with to_function
This file contains hidden or 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.* | |
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) |
This file contains hidden or 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
# 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)) |
This file contains hidden or 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 [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