Performance Issues Out of Nowhere

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
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Performance Issues Out of Nowhere

Post by cedric attias » Mon Sep 23, 2024 9:03 am

Hello,

I am running an instance of MocoTrack on MATLAB, and suddenly, the simulation has been taking forever to run, even initialize, despite me not having made significant changes to the code.

For example, my last simulation took over 55 hours over the weekend, and did not even converge.

I tried looking to see if it was a CPU issue, and it seems like the simulation is suddenly hogging my CPU space.

Any fixes for this?

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

Re: Performance Issues Out of Nowhere

Post by Nicholas Bianco » Tue Sep 24, 2024 9:56 am

Hi Cedric,

Moco simulations are very CPU intensive, so it makes sense that the simulation is taking up a lot of your CPU resources.
despite me not having made significant changes to the code
What changes did you make? Sometimes small changes can have an unexpected affect on convergence. Can you roll back your code to a previous working version and work from there?

Best,
Nick

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Performance Issues Out of Nowhere

Post by cedric attias » Tue Sep 24, 2024 10:25 am

Thanks for the response. I actually managed to fix the issue, but now I have other problems.

In my Tracking simulation (where I provide kinematic data as reference), I am really struggling to get it to converge, and as time and iterations go on, the tracking gets worse...

I have been working on this for a lot of time now, and was hoping I could get some help. I even scaled back and instead of trying to track the entire motion of the full-body, I am trying to get just a single limb to track, which fails. I have tried heavily constraining the problem (which made things worse), and even updated the muscle model to better reflect the dynamic nature of the motion I am trying to model (throwing). Updating the muscle model and loosening the constraints have helped, but it still will not converge (and tracking gets worse as iterations increase). Can someone please give me any advice of how to proceed? Here is my code:

Code: Select all

clear;

% Load the Moco libraries
import org.opensim.modeling.*;

% Define the optimal control problem
% ==================================
track = MocoTrack();
track.setName('throwTracking');

% Set the weights for the terms in the objective function. The values below were
% obtained by trial and error.

controlEffortWeight = 10;
stateTrackingWeight = 1;

% Reference data for tracking problem
tableProcessor = TableProcessor('trackingCoordinatesTHISONE.sto');
tableProcessor.append(TabOpLowPassFilter(6));

modelProcessor = ModelProcessor('MAIN_MODEL_Arm_UPDMUSCMODEL.osim');
track.setModel(modelProcessor);
track.setName(throwTracking');
track.setStatesReference(tableProcessor);
track.set_states_global_tracking_weight(stateTrackingWeight);
track.set_allow_unused_references(true);
track.set_track_reference_position_derivatives(true);
track.set_apply_tracked_states_to_guess(true);
track.set_initial_time(0.0);

study = track.initialize();
problem = study.updProblem();

effort = MocoControlGoal.safeDownCast(problem.updGoal('control_effort'));
effort.setWeight(controlEffortWeight);
effort.setExponent(2);

%Shoulder
problem.setStateInfo('/jointset/shoulder0/elv_angle/speed',MocoBounds.unconstrained());
problem.setStateInfo('/jointset/shoulder1/shoulder_elv/speed',MocoBounds.unconstrained());
problem.setStateInfo('/jointset/shoulder2/shoulder_rot/speed',MocoBounds.unconstrained());

%Elbow
problem.setStateInfo('/jointset/elbow/elbow_flexion/speed',MocoBounds.unconstrained());

%Wrist
problem.setStateInfo('/jointset/radioulnar/pro_sup/speed',MocoBounds.unconstrained());
problem.setStateInfo('/jointset/radiocarpal/deviation/speed',MocoBounds.unconstrained());
problem.setStateInfo('/jointset/radiocarpal/flexion/speed',MocoBounds.unconstrained());

%Shoulder
problem.setStateInfo('/jointset/shoulder0/elv_angle/value', ...
    [-0.872664626, 2.26892803]); 
problem.setStateInfo('/jointset/shoulder1/shoulder_elv/value', ...
    [0, 3.14159265]);
problem.setStateInfo('/jointset/shoulder2/shoulder_rot/value', ...
    [-4.27605667, 0.55850536]);

%Elbow
problem.setStateInfo('/jointset/elbow/elbow_flexion/value', ...
    [0, 2.2689280276]);

%Wrist
problem.setStateInfo('/jointset/radioulnar/pro_sup/value', ...
    [-1.5707963268, 2.7925268]);
problem.setStateInfo('/jointset/radiocarpal/deviation/value', ...
    [-4, 0.436]);
problem.setStateInfo('/jointset/radiocarpal/flexion/value', ...
    [-1.222, 1.222]);
    
    % Solve the problem
% =================
% Configure the solver
pitchTrackingSolution = study.solve();

solver = study.initCasADiSolver();
% solver.setGuess(pitchTracking_tracked_states_REFERENCE);
solver.set_num_mesh_intervals(5); %Change to 10
solver.set_optim_convergence_tolerance(1e-3);
solver.set_optim_constraint_tolerance(1e-4);
solver.set_parameters_require_initsystem(false);
solver.set_optim_max_iterations(1000);


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

Re: Performance Issues Out of Nowhere

Post by Nicholas Bianco » Tue Sep 24, 2024 10:50 am

Hi Cedric,

The first thing I noticed that that you are using very few mesh intervals. Few mesh intervals can make the problem run faster, but too few mesh intervals can greatly hinder convergence. I'm not sure what the time range of your simulation, since you've not set a final time in MocoTrack. I would also provide bounds for all coordinates; unbounded variables can also hinder convergence.

I would try to give the problem the best chance to converge: set looser bounds, add strong residual/reserve actuators to the model, increase the number of mesh intervals, etc.

Best,
Nick

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Performance Issues Out of Nowhere

Post by cedric attias » Tue Sep 24, 2024 11:17 am

Thanks so much for the suggestions.

I currently do not have any residual actuators.

How necessary are they to getting a suitable convergence and are there exampled I can draw from to implement these successfully?

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

Re: Performance Issues Out of Nowhere

Post by Nicholas Bianco » Tue Sep 24, 2024 11:29 am

exampleMocoTrack uses ModOpAddReserves, which will also add residual actuators to the pelvis (if not actuators exist at those coordinates already).

You might find it useful to useful to add strong residuals to the model to get a solution that achieves the problem constraints, including the model dynamics defect constraints. Once you have a converged solution, you can then use that as an initial guess for the problem you actually want to solve.

POST REPLY