pyomeca / bioptim

An optimization framework that links CasADi, Ipopt, ACADOS and biorbd for Optimal Control Problem
MIT License
95 stars 47 forks source link

Custom parameter bound issue when using multiple phases #783

Open tiagocoelhoma opened 1 year ago

tiagocoelhoma commented 1 year ago

Hello!

I had some problems trying to implement a custom parameter while using multiphase. In order to get help from you, I replicate the problem by modifying the custom_parameter example for multiphase.

customParam.zip

The error i get is: TypeError: BoundsList['gravity_xyz'] is ambiguous because there are multiple phases. To access the boundaries in this case, you should write BoundsList[phase]['gravity_xyz'] instead of BoundsList['gravity_xyz']. If you didn't mean to use multiple phases, you may have specified BoundsList['gravity_xyz'] or BoundsList.add('gravity_xyz', ...) twice.

Thank you.

pariterre commented 1 year ago

Dear @tiagocoelhoma

Based on what you sent, you are trying to redefine gravity at each phase. However, they is currently no such thing in bioptim as "phase parameter" (i.e. a parameter applied to the duration of a phase). All the parameters are "ocp parameters" (i.e. a parameter applied to the full scope of the program). There is therefore no such thing as setting bounds to a single phase as such:

parameter_bounds.add(
    "gravity_xyz",
    ...
    phase=i  # This cannot be defined
)

That said, phase parameters is precisely something I was planning to implement this week as we need it for the phase time.

Therefore, if you need phase parameters, I suggest to stay tune as it should be available soon :) If you only need paramaters that are defined ocp wide, it is already the case, just remove you for loop around the definition of the gravity_xyz parameter

Hope this helps

tiagocoelhoma commented 1 year ago

Dear @pariterre

thank you for the answer. The example I implemented with gravity was only intended to replicate the errors I am getting in my other problem, which has to have parameters defined for each phase. I understood that I could implement it because, in the documentation, it says:

The bounds are the maximal and minimal values of the parameter. The size is the number of elements of this parameter. If an objective function is provided, the return of the objective function should match the size. The phase that the parameter applies to. Even though a parameter is time-independent, one biorbd_model is loaded per phase. Since parameters are associated to a specific bio_model, one must define a parameter per phase.

Ps: the part "The phase that the parameter applies to." I dont really get it.

So, as I understood, I should be tuned. :)

Thank you once again.

pariterre commented 1 year ago

Since parameters are associated to a specific bio_model, one must define a parameter per phase.`

This sentence refers to the dynamic model (and not the parameter itself). That is, if your parameter is modifying your dynamic model (as gravity does), you must manually apply this parameter to all the dynamic models (i.e. the bioMod of each phase). Maybe we should have written one must apply the parameter at each phase.

The phase that the parameter applies to.

This is probably a legacy sentence haha