OpenSim Moco  0.4.0
Public Member Functions | Protected Member Functions | List of all members
OpenSim::MocoStudy Class Reference

Description

The top-level class for solving a custom optimal control problem.

This class consists of a MocoProblem, which describes the optimal control problem, and a MocoSolver, which describes the numerical method for solving the problem.

Workflow

When building a MocoStudy programmatically (e.g., in C++), the workflow is as follows:

  1. Build the MocoProblem (set the model, constraints, etc.).
  2. Call MocoStudy::initSolver(), which returns a reference to the MocoSolver. After this, you cannot edit the MocoProblem.
  3. Edit the settings of the MocoSolver (returned by initSolver()).
  4. Call MocoStudy::solve(). This returns the MocoSolution.
  5. (Optional) Postprocess the solution, perhaps using MocoStudy::visualize().

After calling solve(), you can edit the MocoProblem and/or the MocoSolver. You can then call solve() again, if you wish.

Saving the study setup to a file

You can save the MocoStudy to a file by calling MocoStudy::print(), and you can load the setup using MocoStudy(const std::string& omocoFile). MocoStudy setup files have a .omoco extension.

Solver

The default solver uses the tropter direct collocation library. We also provide the CasADi solver, which depends on the CasADi automatic differentiation and optimization library. If you want to use CasADi programmatically, call initCasADiSolver() before solve(). We would like to support users plugging in their own solvers, but there is no timeline for this. If you require additional features or enhancements to the solver, please consider contributing to tropter.

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

#include <Moco/Moco/MocoStudy.h>

Public Member Functions

 OpenSim_DECLARE_PROPERTY (write_solution, std::string, "Provide the folder path (relative to working directory) to which " "the " "solution files should be written. Set to 'false' to not write the " "solution to disk.")
 
 MocoStudy (const std::string &omocoFile)
 Load a MocoStudy setup file.
 
const MocoProblemgetProblem () const
 
MocoProblemupdProblem ()
 If using this method in C++, make sure to include the "&" in the return type; otherwise, you'll make a copy of the problem, and the copy will have no effect on this MocoStudy. More...
 
MocoCasADiSolverinitCasADiSolver ()
 Call this method once you have finished setting up your MocoProblem. More...
 
MocoTropterSolverinitTropterSolver ()
 Call this method once you have finished setting up your MocoProblem. More...
 
MocoSolverupdSolver ()
 Access the solver. More...
 
MocoSolution solve () const
 Solve the provided MocoProblem using the provided MocoSolver, and obtain the solution to the problem. More...
 
void visualize (const MocoTrajectory &it) const
 Interactively visualize a trajectory using the simbody-visualizer. More...
 
TimeSeriesTable analyze (const MocoTrajectory &it, std::vector< std::string > outputPaths) const
 Calculate the requested outputs using the model in the problem and the states and controls in the MocoTrajectory. More...
 
template<>
OSIMMOCO_API MocoTropterSolverinitSolver ()
 
template<>
OSIMMOCO_API MocoCasADiSolverinitSolver ()
 
Using other solvers
template<typename SolverType >
void setCustomSolver ()
 
template<typename SolverType >
SolverType & initSolver ()
 
template<typename SolverType >
SolverType & updSolver ()
 

Protected Member Functions

 OpenSim_DECLARE_PROPERTY (problem, MocoProblem, "The optimal control problem to solve.")
 
 OpenSim_DECLARE_PROPERTY (solver, MocoSolver, "The optimal control algorithm for solving the problem.")
 

Member Function Documentation

◆ analyze()

TimeSeriesTable OpenSim::MocoStudy::analyze ( const MocoTrajectory it,
std::vector< std::string >  outputPaths 
) const

Calculate the requested outputs using the model in the problem and the states and controls in the MocoTrajectory.

The output paths can be regular expressions. For example, ".*activation" gives the activation of all muscles. Constraints are not enforced but prescribed motion (e.g., PositionMotion) is.

See also
OpenSim::analyze()
Note
Parameters in the MocoTrajectory are not applied to the model.

◆ initCasADiSolver()

MocoCasADiSolver& OpenSim::MocoStudy::initCasADiSolver ( )

Call this method once you have finished setting up your MocoProblem.

This returns a reference to the MocoSolver, which you can then edit. If using this method in C++, make sure to include the "&" in the return type; otherwise, you'll make a copy of the solver, and the copy will have no effect on this MocoStudy. This deletes the previous solver if one exists.

Examples:
example2DWalking.cpp, exampleMarkerTracking.cpp, exampleMocoCustomEffortGoal.cpp, exampleSlidingMass.cpp, exampleSlidingMassAdvanced.cpp, and exampleTracking.cpp.

◆ initSolver()

template<typename SolverType >
SolverType& OpenSim::MocoStudy::initSolver ( )
inline
Precondition(s):
If not using MocoTropterSolver or MocoCasADiSolver, you must invoke setCustomSolver() first.

◆ initTropterSolver()

MocoTropterSolver& OpenSim::MocoStudy::initTropterSolver ( )

Call this method once you have finished setting up your MocoProblem.

This returns a reference to the MocoSolver, which you can then edit. If using this method in C++, make sure to include the "&" in the return type; otherwise, you'll make a copy of the solver, and the copy will have no effect on this MocoStudy. This deletes the previous solver if one exists.

◆ solve()

MocoSolution OpenSim::MocoStudy::solve ( ) const

Solve the provided MocoProblem using the provided MocoSolver, and obtain the solution to the problem.

If the write_solution property contains a file path (that is, it's not "false"), then the solution is also written to disk.

Precondition(s):
You must have finished setting up both the problem and solver.
This reinitializes the solver so that any changes you have made will hold.
Examples:
example2DWalking.cpp, exampleCustomImplicitAuxiliaryDynamics.cpp, exampleMarkerTracking.cpp, exampleMocoCustomEffortGoal.cpp, exampleMocoInverse.cpp, exampleMocoTrack.cpp, exampleSlidingMass.cpp, exampleSlidingMassAdvanced.cpp, and exampleTracking.cpp.

◆ updProblem()

MocoProblem& OpenSim::MocoStudy::updProblem ( )

If using this method in C++, make sure to include the "&" in the return type; otherwise, you'll make a copy of the problem, and the copy will have no effect on this MocoStudy.

Examples:
example2DWalking.cpp, exampleCustomImplicitAuxiliaryDynamics.cpp, exampleMarkerTracking.cpp, exampleMocoCustomEffortGoal.cpp, exampleMocoInverse.cpp, exampleMocoTrack.cpp, exampleSlidingMass.cpp, exampleSlidingMassAdvanced.cpp, and exampleTracking.cpp.

◆ updSolver()

MocoSolver& OpenSim::MocoStudy::updSolver ( )

Access the solver.

Make sure to call initSolver() beforehand. If using this method in C++, make sure to include the "&" in the return type; otherwise, you'll make a copy of the solver, and the copy will have no effect on this MocoStudy.

Examples:
example2DWalking.cpp.

◆ visualize()

void OpenSim::MocoStudy::visualize ( const MocoTrajectory it) const

Interactively visualize a trajectory using the simbody-visualizer.

The trajectory could be an initial guess, a solution, etc.

Precondition(s):
The MocoProblem must contain the model corresponding to
the provided trajectory.  
Examples:
example2DWalking.cpp, exampleMarkerTracking.cpp, exampleMocoCustomEffortGoal.cpp, exampleMocoTrack.cpp, exampleSlidingMass.cpp, exampleSlidingMassAdvanced.cpp, and exampleTracking.cpp.

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