Opensim moco-matlab

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
mersad saberi
Posts: 5
Joined: Sat Jul 11, 2020 10:57 am

Opensim moco-matlab

Post by mersad saberi » Tue May 24, 2022 2:29 pm

Hi everyone
I use opesim moco for my master thesis and I have several questions and if posibble,please help me.

Our main task is to calibrate the parameters of the contacts while walking so that it can reproduce the experimental forces well.
To do this, we have used states and contacts tracking and so on.
Our question are:
Can the contact tracking tool,calibrate the contact parameters?If not, what does the contact tracking tool change so that the contacts can reproduce the experimental forces?yes, how can these parameters be reported or stored on the model after solving the moco track problem? Unfortunately, after solving the moco track problem, our model has the same basic parameters as the previous problem-solving contacts, and the parameters are not updated in the model.
Thanks

User avatar
Pasha van Bijlert
Posts: 229
Joined: Sun May 10, 2020 3:15 am

Re: Opensim moco-matlab

Post by Pasha van Bijlert » Wed May 25, 2022 12:16 am

Hi Mersad,

Contact tracking tool adds a cost to the objective function that represents the difference in simulated GRF and the desired (measured) GRF. This means the optimizer will look for solutions that minimize this difference, by changing the trajectory (the movements & the muscle forces). You're basically looking for ways for a model to more closely follow the measured data, without changing anything about the model.

What you are describing is a parameter optimization problem. I think Moco allows for this, using MocoParameter, but I'm not sure if this includes parameters of the contact model.

Cheers,
Pasha

User avatar
mersad saberi
Posts: 5
Joined: Sat Jul 11, 2020 10:57 am

Re: Opensim moco-matlab

Post by mersad saberi » Wed May 25, 2022 10:54 am

Hello dear Pasha
Thank you for your prompt reply.
Your guidance helped me and cleared up some of my ambiguities.
I added several parameters to test the problem and got this error:
"Error using opt (line 187)
Java exception occurred:
java.lang.RuntimeException: D:\a\opensim-moco\opensim-moco\dependencies\casadi\casadi\core\function_internal.hpp:1188:
Input 0 (x0) has mismatching shape. Got 1157-by-1. Allowed dimensions, in general, are:
- The input dimension N-by-M (here 1159-by-1)
- A scalar, i.e. 1-by-1
- M-by-N if N=1 or M=1 (i.e. a transposed vector)
- N-by-M1 if K*M1=M for some K (argument repeated horizontally)
- N-by-P*M, indicating evaluation with multiple arguments (P must be a multiple of 1 for consistency with previous
inputs)

at org.opensim.modeling.opensimMocoJNI.MocoStudy_solve(Native Method)

at org.opensim.modeling.MocoStudy.solve(MocoStudy.java:125)"


I tried hard to solve this problem but I did not get any result.
Do you have any ideas to solve this problem?
I attached my code. If possible, please check my code and if there is a particular problem, let me know so that I can correct it.
Thank you very much for your help.
Attachments
matlab code.txt
(9.45 KiB) Downloaded 77 times

User avatar
Nicholas Bianco
Posts: 1051
Joined: Thu Oct 04, 2012 8:09 pm

Re: Opensim moco-matlab

Post by Nicholas Bianco » Wed May 25, 2022 3:17 pm

HI Mersad,

Hmm, it seems like the problem is losing track of the two parameters that you added to the problem, based on 1157 versus 1159 error message. Can you try calling "solver.resetProblem(problem)" before updating any of the other solver settings?

-Nick

User avatar
mersad saberi
Posts: 5
Joined: Sat Jul 11, 2020 10:57 am

Re: Opensim moco-matlab

Post by mersad saberi » Tue May 31, 2022 1:44 am

Hello again Nicholas
I use the command solver.clearGuess() and I was able to solve the problem and define my parameters in the problem, but I encountered a new problem.
When the problem starts to be solved, If I use solver.set_parameters_require_initsystem (true), the problem will not be solved and it will be freezed in the command window and it will not enter iterations.
But if I use false in this command, the problem enters the iterations, and after solving, when I check the parameters in sto file, their value has not changed and has the same values in the middle of the intervals that are used as default at the beginning of solving the problem.
Where does the problem come from?
Do I have to use false in this command or true.
If I am going to use false, how can I extract the parameters, or if I am going to use true, how can I solve the problem of freezing the solution?
by the way, I also used another method to optimize the parameters (using the moco inverse approach)
But when I make the above command true (solver.set_parameters_require_initsystem (true)), I get this error (i attach code and error in below).
How can I solve this problem?
Attachments
2022-05-31 12_49_34-Window.png
2022-05-31 12_49_34-Window.png (37.12 KiB) Viewed 1123 times
mocoinverse.txt
(4.37 KiB) Downloaded 31 times

User avatar
Nicholas Bianco
Posts: 1051
Joined: Thu Oct 04, 2012 8:09 pm

Re: Opensim moco-matlab

Post by Nicholas Bianco » Tue May 31, 2022 11:09 am

Hi Mersad,

You definitely want "set_parameters_require_initsystem" to be true. The problem freezing is likely a different issue.

How are you setting up the MocoInverse problem? The error shows a SimTK::Stage realization issue, which is a little odd. It seems to be related to the MocoInitialActivationGoal that is added to every problem in MocoInverse. It's possible that this is a bug, but we've successfully run parameter optimization problems with MocoInverse before.

-Nick

User avatar
mersad saberi
Posts: 5
Joined: Sat Jul 11, 2020 10:57 am

Re: Opensim moco-matlab

Post by mersad saberi » Wed Jun 15, 2022 11:37 pm

Hello again Nick
I was able to define the contact parameters as design variables in the problem and I was also able to run the problem, but unfortunately my problem did not converge.
I feel that the contact tracking function is causing the problem.
I want to compare the amount of force of the contacts with the amount of experimental force and define them in the Moco control tracking goal function and not use the contact tracking function.
Is this possible?
If so, how can I do this?
There are very few examples of this tracking control function and I am very confused.

Can you give me an overview to use this function ???
I have an experimental file for ground reaction forces, but I do not know how to compare the contact forces with this file, unfortunately function does not accept xml file as input and I have to add all the contact forces together and Compare with each of the components of the experimental file, which does not seem to be the true.
My problem here is how to sum the forces of contcts and compare them with experimental data. I do not know what functions I should use to do this.
If possible, please describe the general process of using this function.
Thank a lot for your help.

User avatar
Nicholas Bianco
Posts: 1051
Joined: Thu Oct 04, 2012 8:09 pm

Re: Opensim moco-matlab

Post by Nicholas Bianco » Tue Jun 21, 2022 9:30 am

Hi Mersad,

Are you still using MocoInverse? If you are trying to track ground reaction forces, you should build a problem using MocoStudy directly and add a MocoContactTrackingGoal to it. We have a Matlab example that includes the contact tracking goal here: https://github.com/opensim-org/opensim- ... DWalking.m.

If you want to compare the model-generated ground reaction forces to experimental data, you can use the 'opensimMoco.createExternalLoadsTableForGait()' (located in the example here: https://github.com/opensim-org/opensim- ... ing.m#L216).

I would recommend simplifying the problem as much as needed for it to converge as a starting point before solving the full tracking problem. For example, you might start by only tracking ground reaction forces (and maybe control effort) with a small weight with no path constraints or endpoint constraints in the problem. Once that converges, you can add in other cost terms you need and use the previous solution as a initial guess to the next problem.

Best,
Nick

User avatar
Aravind Sundararajan
Posts: 17
Joined: Mon Aug 29, 2016 2:59 pm

Re: Opensim moco-matlab

Post by Aravind Sundararajan » Thu Dec 15, 2022 11:00 am

nbianco wrote:
Tue May 31, 2022 11:09 am
Hi Mersad,

You definitely want "set_parameters_require_initsystem" to be true. The problem freezing is likely a different issue.

How are you setting up the MocoInverse problem? The error shows a SimTK::Stage realization issue, which is a little odd. It seems to be related to the MocoInitialActivationGoal that is added to every problem in MocoInverse. It's possible that this is a bug, but we've successfully run parameter optimization problems with MocoInverse before.

-Nick
Hi Nick,
Sorry to revive an old thread.

Regarding set_parameters_require_initsystem, it makes things pretty slow... Are there rules of thumb for when parameters can be changed without requiring it? I assume this call is there because there are situations that require rerunning buildSystem() like maybe if there are properties of a component that get computed downstream from the property you change (muscle_mass downstream of specific_tension in Bhargava2004SmoothedMuscleMetabolics), but is it really necessary if I'm using MocoParameters to optimize location or orientation of a frame or pathpoint?

-Sunny

POST REPLY