Ignore state in guess file

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
Pagnon David
Posts: 82
Joined: Mon Jan 06, 2014 3:13 am

Ignore state in guess file

Post by Pagnon David » Thu Nov 23, 2023 5:00 am

I am running a few gait predictions, and I use a previous tracking solution as an initial guess (study.setGuessFile()).

Now I would like to set very different bounds to some joint angles to simulate pathological gait, let's say the lumbar flexion so that the model bends over as they walk. If I do so, the solver does not converge because the lumbar flexion is too far from what the guess file wants it to be.

Is there a way to ignore some states in the guess file?

User avatar
Pagnon David
Posts: 82
Joined: Mon Jan 06, 2014 3:13 am

Re: Ignore state in guess file

Post by Pagnon David » Mon Nov 27, 2023 7:25 am

From what I seem to understand, each state and each control need to have a guess trajectory, or Moco will not accept it.

What I am looking for is a way to "free" some states or controls, and let the solver guess by itself.

Is there a way to do it?
Or maybe a way to adjust the weight of some of the tracked columns?

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

Re: Ignore state in guess file

Post by Nicholas Bianco » Mon Nov 27, 2023 10:39 am

Hi David,

Yes, all variables need an initial guess. You could always create a default guess and then insert the states and controls that you want to selectively:

Code: Select all

guess = solver.createGuess('bounds');
% Here, 'statesTable' and 'controlsTable' are TimeSeriesTables with columns for the 
% variables for which you want to set specific initial guess values.
guess.insertStatesTrajectory(statesTable);
guess.insertControlsTrajectory(controlsTable);
-Nick

User avatar
Pagnon David
Posts: 82
Joined: Mon Jan 06, 2014 3:13 am

Re: Ignore state in guess file

Post by Pagnon David » Mon Nov 27, 2023 2:33 pm

Oh, interesting!

This is actually quite close to what I ended up doing, inspired from this answer.

If anyone is interested, here is a Python function that lets you remove states and/or controls from a guess, and take instead the midpoint between their bounds.

Code: Select all

def exclude_from_guess(solver, tracked_coord_path, excluded_names):
	'''
	Exclude states or controls from a guess.
	If for example you want to release the lumbar angle value and the ankle torque:
	excluded_names = ['/jointset/lumbar/lumbar/value', '/forceset/tau_lumbar']
	
        Returns guess trajectories taken from 'tracked_coord_path', but 
        excluding some states/controls which are set to the midpoint between their bounds.
	'''
	
	guess = solver.createGuess('bounds')

	traj = osim.MocoTrajectory(tracked_coord_path)

	states = traj.exportToStatesTable()
	excluded_states_names = [name for name in states.getColumnLabels() if name in excluded_names]
	[states.removeColumn(s) for s in excluded_states_names]
	guess.insertStatesTrajectory(states, True)

	controls = traj.exportToControlsTable()
	excluded_controls_names = [name for name in controls.getColumnLabels() if name in excluded_names]
	[controls.removeColumn(s) for s in excluded_controls_names]
	guess.insertControlsTrajectory(controls, True)
		
	return guess
Used this way:

Code: Select all

    guess = exclude_from_guess(solver, tracked_coord_path, excluded_names)
    solver.setGuess(guess)

POST REPLY