Setting up guess/accelerations with implicit multibody dynamics

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
Ross Miller
Posts: 373
Joined: Tue Sep 22, 2009 2:02 pm

Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Thu Apr 02, 2020 4:37 am

Hi all,

I'm trying to run Moco with the implicit formulations of multibody dynamics and tendon compliance and am unable to get the setup right. The issue seems clear (the trajectory I'm supplying for the initial guess does not have generalized accelerations in it) but I can't figure out how to get them in there. Here is the code for generating the guess, solver, and then solving:

Code: Select all

% Set the normalized tendon forces in guess
solver = MocoCasADiSolver.safeDownCast(study.updSolver());
solver.set_multibody_dynamics_mode('implicit')
solver.set_minimize_implicit_multibody_accelerations(true)
solver.set_implicit_multibody_accelerations_weight(0.001)
solver.set_optim_max_iterations(2000);
solver.set_num_mesh_intervals(21);
solver.set_optim_constraint_tolerance(1e-3);
solver.set_optim_convergence_tolerance(1e-3);
solver.set_minimize_implicit_auxiliary_derivatives(true)
solver.set_implicit_auxiliary_derivatives_weight(0.01)
solver.resetProblem(problem);

guess = solver.getGuess();
numRows = guess.getNumTimes();
StateNames = model.getStateVariableNames();
for i = 1:model.getNumStateVariables()
   currentStateName = string(StateNames.getitem(i-1));
   if contains(currentStateName,'normalized_tendon_force')
      guess.setState(currentStateName, linspace(0.2,0.2,numRows));
   end
end
solver.setGuess(guess);

% Solve the problem
gaitTrackingSolution = study.solve();
The error returned is "The trajectory and provided problem are not compatible. The following derivative(s) are in the problem but not the trajectory:" followed by a list of all generalized accelerations in the model.

Any guesses?

Ross

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Thu Apr 02, 2020 6:44 am

Update: I figured it out! Sort of...

I set the accelerations with:

Code: Select all

guess.generateAccelerationsFromSpeeds;
But now Moco wants the derivatives of the muscle forces:

Code: Select all

The trajectory and provided problem are not compatible. The
following derivative(s) are in the problem but not the trajectory:
  /bifemsh_l/implicitderiv_normalized_tendon_force
  /bifemsh_r/implicitderiv_normalized_tendon_force
  ...
I tried setting these manually (couldn't figure out a way to grab the tendon force derivative names in a loop), e.g.:

Code: Select all

guess.setDerivative('/bifemsh_l/implicitderiv_normalized_tendon_force',linspace(0.0,0.0,numRows));
but this appears to be incorrect, it returns the "No method 'setDerivative' with matching signature found for class 'org.opensim.modeling.MocoTrajectory'." error.

Ross

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Nicholas Bianco » Thu Apr 02, 2020 9:36 am

Hi Ross,

It looks like we didn't add that signature of the setDerivative() method to the MATLAB bindings. You can pass an OpenSim Vector instead of the MATLAB array to the same argument and that will work. Or, you create a guess that's already compatible with the problem like this:

Code: Select all

guess = solver.createGuess()
You can then modify it to fit whatever initial guess you want to create.

Hope that's helpful!
-Nick

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Sun Apr 05, 2020 12:22 pm

Thanks Nick, that worked.

The model I'm working with has a multi-segment foot and the "mtp" joints unlocked (so, articulating "toes" segment with very small mass). With explicit dynamics Moco "works" (converges) but usually does not have good tracking. When I set it to Implicit dynamics it's typically slower but converges with good tracking more consistently. It will not usually converge if running from the initial guess on the final grid (e.g. 51 nodes for a step of walking), I will first have to get a decent solution on a coarse grid (e.g. 11 nodes) then use that solution as the initial guess interpolated onto a finer grid, repeating that process until it's converged on a grid I want. This was also my experience with my own DC codes and symbolic gradients of the implicit state equations (had to start on a coarse grid). I never tried explicit since the software I was using only spit out implicit equations of motion.

This is all for tracking problems, that's all I've tried so far.

Ross

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Nicholas Bianco » Wed Apr 15, 2020 11:34 am

Hi Ross,

Thanks for the details about debugging your problem. We've tried to make Moco as convenient as possible, but it seems like a lot of the tricks we use for direct collocation are still necessary to get good solutions. As Moco evolves, we aim to assemble all the "tips & tricks" we've learned into a single reference within the documentation so new users won't have to reinvent the wheel.

We've actually already started doing this in the docs (https://opensim-org.github.io/opensim-moco-site/docs/): "User Guide" > "MocoStudy" > "Tips for solving a custom problem" (at the bottom of the page). If you have any tips to add to the list based on your experience with DC, that would be great!

Best,
-Nick

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Wed Apr 15, 2020 2:04 pm

Thanks Nick. I'll refrain from posting any immediate recommendations since I'm now getting the opposite result from what I stated in that post: getting great results with explicit multibody dynamics and not so great with implicit (slow, and usually won't converge). So I'd like to do some more tinkering, but I'll definitely chime in when I feel like I can make a general recommendation with confidence.

"Grid refinement" is something I do a lot and can recommend now. Especially for complex models, I find it's rare that the problem will converge on a "fine" grid of something like 100 nodes/stride in gait and it's hard to tell when/if you should give up on it, but the same problem will usually converge pretty quickly on 10-20 nodes and gives you a nice guess to interpolate onto the 100-node grid. This is fairly easy to do in Moco already as Aaron Fox pointed out earlier so I'm not sure it needs to be a built-in feature. "How dense does the grid need to be?" is a good open question. I like to use at least 100 nodes but mostly just because the result looks nice. But would you get essentially the same story/conclusions in a study on a 20-node grid, just with uglier graphs and much less CPU time? Probably, at least in some cases. Coarse grids let you do cool things like nested optimizations (Brian has a recent paper on this). For gait 20 nodes/stride seems to be a good starting point.

With OpenSim it would probably work well to get an initial guess on a fine grid from the IK->RRA->CMC workflow, although that's not always easy either.

Anil Rao's software, GPOPS-II (http://www.gpops2.com) does automatic grid refinement, or least it did when I was trying it out several years ago. I'm not sure how it does that exactly.

I'm not sure I fully understand how the implicit multibody dynamics are used in Moco. The way I would previously formulate it (which is Ton's way, originally) is that the "errors" (violations of the system dynamics) have units of generalized force. This is nice for stiff problems, e.g. foot force and torso force are similar, foot acceleration and torso acceleration are not similar. But it looks like with the implicit multibody dynamics in Moco, Moco is still computing the acceleration errors (I think those are acc = F/m vs. acc from finite differencing of the states on the collocation grid?) and adding them to the cost function; is that correct? Is there a benefit for dealing with the accelerations in that way for implicit dynamics?

Ross

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Wed Apr 15, 2020 2:23 pm

I should mention that all of my statements here are specifically for MocoTrack problems. I've not tried any predictive simulations.

User avatar
Karthick Ganesan
Posts: 119
Joined: Thu Oct 10, 2013 12:11 am

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Karthick Ganesan » Sat May 16, 2020 11:12 am

Dear Nick and Ross,
I tried to implement implicit multibody dynamics and I encountered the same problem of setting appropriate guess. I would like to understand what is going wrong. When using solver.getGuess() with only implicit muscle dynamics, the guess has the derivatives of tendon forces. When implicit multibody dynamics is added, the guess has derivative of tendon forces but no accelerations. If I use generateAccelerationFromSpeeds, the guess has accelerations but no derivatives of tendon forces. Why is it so?
When I tried to use setDerivs(), I get an error that can not find /.../.../implicitderiv_normalized_tendon_force (different from the above post).
With solver.createGuess(), it seems like tracked states are not applied to the initial guess. I could probably edit the initial guess manually. Is there any other way around?
Thanks,
Karthick.

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

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Ross Miller » Sat May 16, 2020 12:34 pm

Hi Karthick,

I've so far only gotten good results when using implicit multibody dynamics if I start from a "good" initial guess that was generated using explicit dynamics. I use createGuess then replace the guess with the states and controls from the previous solution:

Code: Select all

guess = solver.createGuess();
prevSolution = MocoTrajectory('previous_soln.sto');
prevStatesTable = prevSolution.exportToStatesTable();
prevControlsTable = prevSolution.exportToControlsTable();
guess.insertStatesTrajectory(prevStatesTable, true);
guess.insertControlsTrajectory(prevControlsTable, true);
solver.setGuess(guess);
This will replace whatever is generated by createGuess (which should have all the necessary aux derivatives in it) with the states and controls of the previous solution.

I've not yet found any advantages to using the implicit multibody dynamics in Moco. It tends to converge in fewer iterations but the time per iteration is longer, and getting the weighting right on the accelerations term is tricky. When starting from "bad" initial guesses I've so far only gotten convergence consistently when using explicit multibody dynamics.

Hope this helps,
Ross

User avatar
Karthick Ganesan
Posts: 119
Joined: Thu Oct 10, 2013 12:11 am

Re: Setting up guess/accelerations with implicit multibody dynamics

Post by Karthick Ganesan » Fri May 22, 2020 10:10 pm

Thanks Ross. That was helpful.

POST REPLY