API 4.4.1-2022-10-19-2c4045e59
For MATLAB, Python, Java, and C++ users
OpenSim::MocoSolution Class Reference

Return type for MocoStudy::solve(). More...

+ Inheritance diagram for OpenSim::MocoSolution:

Public Member Functions

virtual MocoSolutionclone () const override
 Returns a dynamically-allocated copy of this solution. More...
 
bool success () const
 Was the problem solved successfully? If not, then you cannot access the solution until you call unlock(). More...
 
double getObjective () const
 
 operator bool () const
 Same as success(). More...
 
const std::string & getStatus () const
 Obtain a solver-dependent string describing the return status of the optimization. More...
 
int getNumIterations () const
 Number of solver iterations at which this solution was obtained (-1 if not set). More...
 
double getSolverDuration () const
 Get the amount of time (clock time, not CPU time) spent within solve(). More...
 
Breakdown of objective

Some solvers provide a breakdown of the terms in the objective.

Use these functions to access this breakdown. Some terms may come from MocoGoals in the problem, while other terms may be added by the solver.

int getNumObjectiveTerms () const
 Returns the number of terms in the objective. More...
 
std::vector< std::string > getObjectiveTermNames () const
 Get the names of all terms in the objective (either from MocoGoals in the problem or added by the solver). More...
 
double getObjectiveTerm (const std::string &name) const
 Get the value of a term in the objective by name. More...
 
double getObjectiveTermByIndex (int index) const
 Get the value of a term in the objective, using an index. More...
 
void printObjectiveBreakdown () const
 Print to the console the terms in the objective and their values. More...
 
Access control
MocoSolutionunseal ()
 If the solver did not succeed, call this to enable read and write access to the (failed) solution. More...
 
MocoSolutionseal ()
 
bool isSealed () const
 
- Public Member Functions inherited from OpenSim::MocoTrajectory
 MocoTrajectory ()=default
 
 MocoTrajectory (std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > parameter_names)
 Create a trajectory with no data. More...
 
 MocoTrajectory (std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names)
 Create a trajectory (including columns for derivatives) with no data. More...
 
 MocoTrajectory (const SimTK::Vector &time, std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > parameter_names, const SimTK::Matrix &statesTrajectory, const SimTK::Matrix &controlsTrajectory, const SimTK::Matrix &multipliersTrajectory, const SimTK::RowVector &parameters)
 
 MocoTrajectory (const SimTK::Vector &time, std::vector< std::string > state_names, std::vector< std::string > control_names, std::vector< std::string > multiplier_names, std::vector< std::string > derivative_names, std::vector< std::string > parameter_names, const SimTK::Matrix &statesTrajectory, const SimTK::Matrix &controlsTrajectory, const SimTK::Matrix &multipliersTrajectory, const SimTK::Matrix &derivativesTrajectory, const SimTK::RowVector &parameters)
 This constructor is for use with the implicit dynamics mode, and allows specifying a derivativesTrajectory. More...
 
 MocoTrajectory (const SimTK::Vector &time, const std::map< std::string, NamesAndData< SimTK::Matrix > > &continuousVars, const NamesAndData< SimTK::RowVector > &parameters={})
 
 MocoTrajectory (const std::string &filepath)
 Read a MocoTrajectory from an STO file (see STOFileAdapter). More...
 
virtual ~MocoTrajectory ()=default
 
bool empty () const
 
bool hasCoordinateStates () const
 
void setNumTimes (int numTimes)
 Resize the time vector and the time dimension of the states, controls, multipliers, and derivatives trajectories, and set all times, states, controls, multipliers, and derivatives to NaN. More...
 
double resampleWithNumTimes (int numTimes)
 Uniformly resample (interpolate) the trajectory so that it retains the same initial and final times but now has the provided number of time points. More...
 
double resampleWithInterval (double desiredTimeInterval)
 Uniformly resample (interpolate) the trajectory to try to achieve the provided time interval between mesh points, while preserving the initial and final times. More...
 
double resampleWithFrequency (double desiredNumTimePointsPerSecond)
 Uniformly resample (interpolate) the trajectory to try to achieve the provided frequency of time points per second of the trajectory, while preserving the initial and final times. More...
 
void resample (SimTK::Vector newTime)
 Resample (interpolate) the data in this trajectory at the provided times. More...
 
void setTime (const SimTK::Vector &time)
 Set the time vector. More...
 
void setState (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single state variable across time. More...
 
void setControl (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single control variable across time. More...
 
void setMultiplier (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single Lagrange multiplier variable across time. More...
 
void setDerivative (const std::string &name, const SimTK::Vector &trajectory)
 Set the value of a single state derivative variable across time. More...
 
void setParameter (const std::string &name, const SimTK::Real &value)
 Set the value of a single parameter variable. More...
 
void setTime (std::initializer_list< double > time)
 Set the time vector. More...
 
void setState (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single state variable across time. More...
 
void setControl (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single control variable across time. More...
 
void setMultiplier (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single Lagrange multiplier variable across time. More...
 
void setDerivative (const std::string &name, std::initializer_list< double > trajectory)
 Set the value of a single state derivative variable across time. More...
 
void setStatesTrajectory (const TimeSeriesTable &states, bool allowMissingColumns=false, bool allowExtraColumns=false)
 Set the states trajectory. More...
 
void insertStatesTrajectory (const TimeSeriesTable &subsetOfStates, bool overwrite=false)
 Add additional state columns. More...
 
void insertControlsTrajectory (const TimeSeriesTable &subsetOfControls, bool overwrite=false)
 Add additional control columns. More...
 
void generateSpeedsFromValues ()
 Compute coordinate speeds based on coordinate position values and append to the trajectory. More...
 
void generateAccelerationsFromValues ()
 Compute coordinate accelerations based on coordinate position values and append to the trajectory. More...
 
void generateAccelerationsFromSpeeds ()
 Compute coordinate accelerations based on coordinate speeds and append to the trajectory. More...
 
int getNumTimes () const
 
const SimTK::Vector & getTime () const
 
double getInitialTime () const
 The first time in the time vector. More...
 
double getFinalTime () const
 The last time in the time vector. More...
 
int getNumStates () const
 
int getNumControls () const
 
int getNumMultipliers () const
 
int getNumDerivatives () const
 
int getNumValues () const
 
int getNumSpeeds () const
 
int getNumAccelerations () const
 
int getNumDerivativesWithoutAccelerations () const
 
int getNumParameters () const
 
const std::vector< std::string > & getStateNames () const
 
const std::vector< std::string > & getControlNames () const
 
const std::vector< std::string > & getMultiplierNames () const
 
const std::vector< std::string > & getDerivativeNames () const
 
std::vector< std::string > getValueNames () const
 
std::vector< std::string > getSpeedNames () const
 
std::vector< std::string > getAccelerationNames () const
 
std::vector< std::string > getDerivativeNamesWithoutAccelerations () const
 
const std::vector< std::string > & getParameterNames () const
 
SimTK::VectorView_< double > getState (const std::string &name) const
 
SimTK::VectorView_< double > getControl (const std::string &name) const
 
SimTK::VectorView_< double > getMultiplier (const std::string &name) const
 
SimTK::VectorView_< double > getDerivative (const std::string &name) const
 
const SimTK::Real & getParameter (const std::string &name) const
 
const SimTK::Matrix & getStatesTrajectory () const
 
const SimTK::Matrix & getControlsTrajectory () const
 
const SimTK::Matrix & getMultipliersTrajectory () const
 
const SimTK::Matrix & getDerivativesTrajectory () const
 
SimTK::Matrix getValuesTrajectory () const
 
SimTK::Matrix getSpeedsTrajectory () const
 
SimTK::Matrix getAccelerationsTrajectory () const
 
SimTK::Matrix getDerivativesWithoutAccelerationsTrajectory () const
 
const SimTK::RowVector & getParameters () const
 
bool isCompatible (const MocoProblemRep &, bool requireAccelerations=false, bool throwOnError=false) const
 Do the state, control, multiplier, derivative, and parameter names in this trajectory match those in the problem? This may not catch all possible incompatibilities. More...
 
bool isNumericallyEqual (const MocoTrajectory &other, double tol=SimTK::NTraits< SimTK::Real >::getDefaultTolerance()) const
 Check if this trajectory is numerically equal to another trajectory. More...
 
double compareContinuousVariablesRMS (const MocoTrajectory &other, std::map< std::string, std::vector< std::string > > columnsToUse={}) const
 Compute the root-mean-square error between the continuous variables of this trajectory and another. More...
 
double compareContinuousVariablesRMSPattern (const MocoTrajectory &other, std::string columnType, std::string pattern) const
 This is an alternative interface for compareContinuousVariablesRMS() that uses regular expression patterns to select columns. More...
 
double compareParametersRMS (const MocoTrajectory &other, std::vector< std::string > parameterNames={}) const
 Compute the root-mean-square error between the parameters in this trajectory and another. More...
 
void write (const std::string &filepath) const
 Save the trajectory to a STO file. Use the ."sto" file extension. More...
 
TimeSeriesTable exportToStatesTable () const
 This table can be saved as a Storage file that can be used in the OpenSim GUI to visualize a motion, or as input to OpenSim's conventional tools (e.g., AnalyzeTool). More...
 
TimeSeriesTable exportToControlsTable () const
 Export the controls trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToMultipliersTable () const
 Export the multipliers trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToDerivativesTable () const
 Export the derivatives trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToValuesTable () const
 Export the coordinate values from the states trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToSpeedsTable () const
 Export the coordinate speeds from the states trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToAccelerationsTable () const
 Export the coordinate accelerations from the derivatives trajectory to a TimeSeriesTable. More...
 
TimeSeriesTable exportToDerivativesWithoutAccelerationsTable () const
 Export the derivatives trajectory without coordinate accelerations to a TimeSeriesTable. More...
 
StatesTrajectory exportToStatesTrajectory (const MocoProblem &) const
 Controls are not carried over to the StatesTrajectory. More...
 
StatesTrajectory exportToStatesTrajectory (const Model &) const
 This is similar to the above function but requires only a model, not a MocoProblem. More...
 
void randomizeReplace (const SimTK::Random &randGen=SimTK::Random::Uniform(-0.1, 0.1))
 Randomize all data except time using the provided random number generator. More...
 
void randomizeAdd (const SimTK::Random &randGen=SimTK::Random::Uniform(-0.1, 0.1))
 Randomize all data except time using the provided random number generator. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from OpenSim::MocoTrajectory
static MocoTrajectory createFromStatesControlsTables (const MocoProblemRep &, const TimeSeriesTable &statesTrajectory, const TimeSeriesTable &controlsTrajectory)
 (Experimental) Create a trajectory from a states trajectory and controls trajectory (i.e, from Manager::getStatesTable() and Model::getControlsTable()). More...
 

Detailed Description

Return type for MocoStudy::solve().

Use success() to check if the solver succeeded. You can also use this object as a boolean in an if-statement:

auto solution = study.solve();
if (solution) {
std::cout << solution.getStatus() << std::endl;
}

You can use getStatus() to get more details about the return status of the optimizer. If the solver was not successful, then this object is "sealed", which means you cannot do anything with it until calling unseal(). This prevents you from silently proceeding with a failed solution. In the file written by write(), the header contains solver success, the objective, the individual terms in the objective (including the weight), the breakdown of the objective, and other quantities.

Member Function Documentation

◆ clone()

virtual MocoSolution * OpenSim::MocoSolution::clone ( ) const
inlineoverridevirtual

Returns a dynamically-allocated copy of this solution.

You must manage the memory for return value.

Note
This works even if the trajectory is sealed.

Reimplemented from OpenSim::MocoTrajectory.

◆ getNumIterations()

int OpenSim::MocoSolution::getNumIterations ( ) const
inline

Number of solver iterations at which this solution was obtained (-1 if not set).

◆ getNumObjectiveTerms()

int OpenSim::MocoSolution::getNumObjectiveTerms ( ) const
inline

Returns the number of terms in the objective.

If the solver did not provide this breakdown, then this returns 0.

◆ getObjective()

double OpenSim::MocoSolution::getObjective ( ) const
inline

◆ getObjectiveTerm()

double OpenSim::MocoSolution::getObjectiveTerm ( const std::string &  name) const

Get the value of a term in the objective by name.

See getObjectiveTermNames(). The value includes the weight on the term.

◆ getObjectiveTermByIndex()

double OpenSim::MocoSolution::getObjectiveTermByIndex ( int  index) const

Get the value of a term in the objective, using an index.

The order of terms is the same as in getObjectiveTermNames(). The value includes the weight on the term.

◆ getObjectiveTermNames()

std::vector< std::string > OpenSim::MocoSolution::getObjectiveTermNames ( ) const

Get the names of all terms in the objective (either from MocoGoals in the problem or added by the solver).

Terms from MocoGoals are named with the name of the associated MocoGoal. If the solver did not provide this breakdown, then this returns an empty vector.

◆ getSolverDuration()

double OpenSim::MocoSolution::getSolverDuration ( ) const
inline

Get the amount of time (clock time, not CPU time) spent within solve().

Units: seconds.

◆ getStatus()

const std::string & OpenSim::MocoSolution::getStatus ( ) const
inline

Obtain a solver-dependent string describing the return status of the optimization.

◆ isSealed()

bool OpenSim::MocoSolution::isSealed ( ) const
inline

◆ operator bool()

OpenSim::MocoSolution::operator bool ( ) const
inlineexplicit

Same as success().

◆ printObjectiveBreakdown()

void OpenSim::MocoSolution::printObjectiveBreakdown ( ) const

Print to the console the terms in the objective and their values.

◆ seal()

MocoSolution & OpenSim::MocoSolution::seal ( )
inline

◆ success()

bool OpenSim::MocoSolution::success ( ) const
inline

Was the problem solved successfully? If not, then you cannot access the solution until you call unlock().

◆ unseal()

MocoSolution & OpenSim::MocoSolution::unseal ( )
inline

If the solver did not succeed, call this to enable read and write access to the (failed) solution.

If the solver succeeded, then the solution is already unsealed.

Note
In Python, you must invoke this function on a separate line:
solution = moco.solve()
solution.unseal()
Otherwise, Moco will cause a crash.

References OpenSim::MocoTrajectory::setSealed().


The documentation for this class was generated from the following file: