OpenSim Moco  0.2.0
Public Member Functions | Friends | List of all members
OpenSim::MocoSolution Class Reference

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. Solver success can also be found in the header of a solution (.sto) file written out by write.

Examples
example2DWalking.cpp, exampleCustomImplicitAuxiliaryDynamics.cpp, exampleMarkerTracking.cpp, exampleMocoCustomEffortGoal.cpp, exampleMocoTrack.cpp, exampleSlidingMass.cpp, exampleSlidingMassAdvanced.cpp, and exampleTracking.cpp.
Inheritance diagram for OpenSim::MocoSolution:
OpenSim::MocoTrajectory

#include <Moco/Moco/MocoTrajectory.h>

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().
 
double getObjective () const
 
 operator bool () const
 Same as success().
 
const std::string & getStatus () const
 Obtain a solver-dependent string describing the return status of the optimization.
 
int getNumIterations () const
 Number of solver iterations at which this solution was obtained (-1 if not set).
 
double getSolverDuration () const
 Get the amount of time (clock time, not CPU time) spent within solve(). 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 (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.
 
 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 a data file (e.g., STO, CSV). More...
 
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 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
 
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
 
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 file(s). Use a ".sto" file extension.
 
Storage exportToStatesStorage () const
 The Storage can be used in the OpenSim GUI to visualize a motion, or as input to OpenSim's conventional tools (e.g., AnalyzeTool). More...
 
TimeSeriesTable exportToStatesTable () const
 Same as exportToStatesStorage() except using TimeSeriesTable.
 
TimeSeriesTable exportToControlsTable () const
 Export the controls trajectory to a TimeSeriesTable.
 
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.
 
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...
 

Friends

class MocoSolver
 

Additional Inherited Members

- Public Types inherited from OpenSim::MocoTrajectory
template<typename T >
using NamesAndData = std::pair< std::vector< std::string >, T >
 This constructor allows you to control which data you provide for the trajectory. More...
 
- 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...
 
- Protected Member Functions inherited from OpenSim::MocoTrajectory
void setSealed (bool sealed)
 
bool isSealed () const
 
void ensureUnsealed () const
 

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.

◆ getSolverDuration()

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

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

Units: seconds.

◆ 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.

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