Stiffness optimization in the exmpleSquatToStand of 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
yimeng li
Posts: 3
Joined: Sun May 02, 2021 5:11 am

Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by yimeng li » Tue Dec 19, 2023 10:14 am

Dear All,

I am trying to test the MocoParameter in Matlab and find the optimal stiffness of the SpringGeneralizedForce. The example of squat to stand which was originally provided with the Opensim Moco focused on a specific stiffness. I wonder how to predict the optimal stiffness in such a moco study (just like the Mocopaper) in matlab. I added a MocoParameter in part 5, however, it did not work. Here is my code (modified part 5 of exmpleSquatToStand )

Code: Select all

%% Part 5: Muscle-driven Inverse Problem with Passive Assistance
% Part 5a: Create a new muscle-driven model, now adding a SpringGeneralizedForce 
% about the knee coordinate.
device = SpringGeneralizedForce('knee_angle_r');
device.setName('spring')
initialstiffness=0;
device.setStiffness(initialstiffness);
device.setRestLength(0);
device.setViscosity(0);
muscleDrivenModel.addForce(device);

newModelFile = 'model_with_knee_device.osim';
muscleDrivenModel.print(newModelFile);

% Add MocoParameter for the knee assistance device parameters 
mypara = MocoParameter('stiffness','/forceset/spring','stiffness',MocoBounds(1,99));
problem.addParameter(mypara);

% Part 5b: Create a ModelProcessor similar to the previous one, using the same
% reserve actuator strength so we can compare muscle activity accurately.
modelProcessor = ModelProcessor(muscleDrivenModel);
modelProcessor.append(ModOpAddReserves(2));
inverse.setModel(modelProcessor);

% Part 5c: Solve! Write solution.
inverseDeviceSolution = inverse.solve();
inverseDeviceSolution.getMocoSolution().write('inverseDeviceSolution.sto');
My questions are:
  1. Is there anything wrong with my codes? If it is possible for you to provide the right version
  • As a beginner of Moco, are there any ways to learn the Moco fast?
I would appreciate it if you can help me.

Sincerely
yours,
Yimeng

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

Re: Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by Nicholas Bianco » Tue Dec 19, 2023 12:57 pm

Hi Yimeng,

That code snippet adds a spring force to the model (with its parameters), but it does not tell Moco which parameter to optimize. Therefore, no parameter is added to the problem.

You can use a MocoParameter to add the spring force stiffness to the MocoProblem.

Best,
Nick

User avatar
yimeng li
Posts: 3
Joined: Sun May 02, 2021 5:11 am

Re: Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by yimeng li » Mon Jan 08, 2024 4:33 am

Hi Nick,

Thank you for your reply!

I successfully added the Mocoparameter in part 1(MocoStudy), I suppose I should not call addParameter in MocoInverse (part 5). However, the 'SpringStiffness' column of the solution.sto file is a column of 'nan' all the time, and I could not find the final optimal value anywhere.

I wonder if there is anything wrong with my code, and what function I should call to get the final optimal value of the stiffness.

Waiting for your reply and thanks in advance.

Cheers,
Yimeng

Code: Select all

%% Part 1: Torque-driven Predictive Problem
% Part 1a: Create a new MocoStudy.
study = MocoStudy();

% Part 1b: Initialize the problem and set the model.
problem = study.updProblem();

% Part 1c: Set bounds on the problem.

% Time bounds
problem.setTimeBounds(0, 1);

% Position bounds: the model should start in a squat and finish 
% standing up.
problem.setStateInfo('/jointset/hip_r/hip_flexion_r/value', ...
    [-2, 0.5], -2, 0);
problem.setStateInfo('/jointset/knee_r/knee_angle_r/value', ...
    [-2, 0], -2, 0);
problem.setStateInfo('/jointset/ankle_r/ankle_angle_r/value', ...
    [-0.5, 0.7], -0.5, 0);

% Velocity bounds: all model coordinates should start and end at rest.
problem.setStateInfoPattern('/jointset/.*/speed', [], 0, 0);

% Part 1d: Add a MocoControlGoal to the problem.
problem.addGoal(MocoControlGoal('myeffort'));


% Add Spring
device = SpringGeneralizedForce('knee_angle_r');
device.setName('spring')
initialstiffness=60;
device.setStiffness(initialstiffness);
device.setRestLength(0);
device.setViscosity(0);
torqueDrivenModel.addForce(device);

%Set Model
problem.setModel(torqueDrivenModel);


% Add MocoParameter for the knee assistance device parameters 
  mypara = MocoParameter('springstiffness','/forceset/spring','stiffness',MocoBounds(49,70));
  problem.addParameter(mypara);

% Part 1e: Configure the solver.
solver = study.initCasADiSolver();
solver.set_num_mesh_intervals(25);
solver.set_optim_convergence_tolerance(1e-4);
solver.set_optim_constraint_tolerance(1e-4);

if ~exist('predictSolution.sto', 'file')
% Part 1f: Solve! Write the solution to file, and visualize.
predictSolution = study.solve();
predictSolution.write('predictSolution.sto');
study.visualize(predictSolution);
end


User avatar
Ross Miller
Posts: 375
Joined: Tue Sep 22, 2009 2:02 pm

Re: Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by Ross Miller » Mon Jan 08, 2024 7:19 am

Hi Yimeng,

In the solution file, are you 100% sure there is no value indicated for the stiffness? In my solution files with parameters optimized, these values are easy to miss, they will have the single parameter value in the first entry (first "timestep"), then the value at all other timesteps will be NaN.

Ross

User avatar
yimeng li
Posts: 3
Joined: Sun May 02, 2021 5:11 am

Re: Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by yimeng li » Mon Jan 08, 2024 7:38 am

Hi Ross,

Thank you for your reply. I apologize for not noticing the parameter optimized earlier. I found the value in the first entry, that was really easy to miss :D. It was an oversight on my part. Thank you for your information and understanding.

Cheers,
Yimeng

User avatar
Ross Miller
Posts: 375
Joined: Tue Sep 22, 2009 2:02 pm

Re: Stiffness optimization in the exmpleSquatToStand of MOCO matlab

Post by Ross Miller » Mon Jan 08, 2024 11:00 am

Nice, glad you found it!

POST REPLY