Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use allow_free option in Opti::to_function in Python #3655

Open
S-Dafarra opened this issue Apr 12, 2024 · 3 comments
Open

Unable to use allow_free option in Opti::to_function in Python #3655

S-Dafarra opened this issue Apr 12, 2024 · 3 comments

Comments

@S-Dafarra
Copy link

I have the following snippet of code

import casadi as cs

opti = cs.Opti()
v = opti.variable(1)
p = opti.parameter(1)
opti.minimize(v)
opti.solver("ipopt")
opti.to_function("test",[v,p], [v,p], {"allow_free": True})

but it fails with the error

Traceback (most recent call last):
  File "C:\Software\mambaforge\envs\hippopt_dev\Lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-19-fbf0b281ebea>", line 1, in <module>
    opti.to_function("test",[v,p], [v,p], {"allow_free": True})
  File "C:\Software\mambaforge\envs\hippopt_dev\Lib\site-packages\casadi\casadi.py", line 48679, in to_function
    return _casadi.Opti_to_function(self, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Error in Opti::to_function [OptiNode] at D:\bld\casadi_1709758764158\work\casadi\core\optistack.cpp:331:
Error in Opti::to_function [OptiNode] at D:\bld\casadi_1709758764158\work\casadi\core\optistack.cpp:331:
Error in Function::Function for 'helper' [MXFunction] at D:\bld\casadi_1709758764158\work\casadi\core\function.cpp:280:
D:\bld\casadi_1709758764158\work\casadi\core\function_internal.cpp:147: Error calling MXFunction::init for 'helper':
D:\bld\casadi_1709758764158\work\casadi\core\mx_function.cpp:409: helper::init: Initialization failed since variables [opti0_p_1] are free. These symbols occur in the output expressions but you forgot to declare these as inputs. Set option 'allow_free' to allow free variables.

I am using version 3.6.5.

@ernestds
Copy link

You are not using p in the problem so it is sort of not registered in Opti.

Ex:

opti.minimize(v + p)
opti.solver("ipopt")
opti.to_function("test",[v,p], [v,p],)

or

opti.minimize(v)
opti.solver("ipopt")
opti.to_function("test",[v,p], [v],)

both work.

@S-Dafarra
Copy link
Author

Thanks @ernestds, but I thought that was what the allow_free option is meant for.

@ernestds
Copy link

Tbh I think that isn't possible with Opti to_function, but I'm not sure.

import casadi as cs

opti = cs.Opti()
v = opti.variable(1)
p = opti.parameter(1)
opti.minimize(v**2+p)
opti.solver("ipopt")
f = opti.to_function("test",[v], [v,p], {"allow_free": True,})
# f = cs.Function("test",[v], [v+p,p], {"allow_free": True})
cs.Function('bla',[v,p],[*f(v)])(1,2)

This raises no error but still doesn't work for the optimization. You might want to play around with this or try the nlpsol function instead (I have no idea whether that will be any better).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants