API  4.5
For C++ developers
OpenSim::MocoProblemRep Class Reference

The primary intent of this class is for use by MocoSolvers, but users can also use this class to apply parameter values to the model and evaluate cost terms. More...

Public Member Functions

 MocoProblemRep ()=default
 
 MocoProblemRep (const MocoProblemRep &)=delete
 
MocoProblemRepoperator= (const MocoProblemRep &)=delete
 
 MocoProblemRep (MocoProblemRep &&source)
 
MocoProblemRepoperator= (MocoProblemRep &&source)
 
const std::string & getName () const
 
const ModelgetModelBase () const
 Get a reference to the copy of the model being used by this MocoProblemRep. More...
 
SimTK::State & updStateBase () const
 This is a state object that solvers can use along with ModelBase. More...
 
const DiscreteControllergetDiscreteControllerBase () const
 This is a component inside ModelBase that you can use to set the value of control signals. More...
 
const ModelgetModelDisabledConstraints () const
 Get a reference to a copy of the model being used by this MocoProblemRep, but with all constraints disabled and an additional DiscreteForces component. More...
 
SimTK::State & updStateDisabledConstraints (int index=0) const
 This is a state object that solvers can use with ModelDisabledConstraints. More...
 
const DiscreteControllergetDiscreteControllerDisabledConstraints () const
 This is a component inside ModelDisabledConstraints that you can use to set the value of control signals. More...
 
const DiscreteForcesgetConstraintForces () const
 This is a component inside ModelDisabledConstraints that you can use to set the value of discrete forces, intended to hold the constraint forces obtained from ModelBase. More...
 
const AccelerationMotiongetAccelerationMotion () const
 This is a component inside ModelDisabledConstraints that you can use to set the value of generalized accelerations UDot, for use in implicit dynamics formulations. More...
 
int getNumStates () const
 
int getNumControls () const
 
int getNumParameters () const
 
int getNumCosts () const
 Get the number of goals in cost mode. More...
 
int getNumEndpointConstraints () const
 Get the number of goals in endpoint constraint mode. More...
 
int getNumKinematicConstraints () const
 
bool isPrescribedKinematics () const
 Does the model contain a PositionMotion to prescribe all generalized coordinates, speeds, and accelerations? More...
 
int getNumImplicitAuxiliaryResiduals () const
 
std::vector< std::string > createStateVariableNamesInSystemOrder (std::unordered_map< int, int > &yIndexMap) const
 This excludes generalized coordinate and speed states if isPrescribedKinematics() is true. More...
 
std::vector< std::string > createStateInfoNames () const
 Get the state names of all the state infos. More...
 
std::vector< std::string > createControlInfoNames () const
 Get the control names of all the control infos. More...
 
std::vector< std::string > createParameterNames () const
 Get the names of all the parameters. More...
 
std::vector< std::string > createCostNames () const
 Get the names of all the goals in cost mode. More...
 
std::vector< std::string > createEndpointConstraintNames () const
 Get the names of all the goals in endpoint constraint mode. More...
 
std::vector< std::string > createPathConstraintNames () const
 Get the names of all the MocoPathConstraints. More...
 
std::vector< std::string > createMultiplierInfoNames () const
 Get the names of all the Lagrange multiplier infos. More...
 
std::vector< std::string > createKinematicConstraintNames () const
 Get the constraint names of all the kinematic constraints. More...
 
std::vector< std::string > getKinematicConstraintEquationNames (bool includeDerivatives) const
 Get a vector of names for all kinematic constraint equations. More...
 
MocoInitialBounds getTimeInitialBounds () const
 
MocoFinalBounds getTimeFinalBounds () const
 
const MocoVariableInfogetStateInfo (const std::string &name) const
 Get information for state variables. See MocoPhase::setStateInfo(). More...
 
const MocoVariableInfogetControlInfo (const std::string &name) const
 Get information for actuator controls. More...
 
const MocoParametergetParameter (const std::string &name) const
 
const MocoGoalgetCost (const std::string &name) const
 Get a cost by name. This returns a MocoGoal in cost mode. More...
 
const MocoGoalgetCostByIndex (int index) const
 Get a cost by index. More...
 
const MocoGoalgetEndpointConstraint (const std::string &name) const
 Get an endpoint constraint by name. More...
 
const MocoGoalgetEndpointConstraintByIndex (int index) const
 Get an endpoint constraint by index. More...
 
const MocoPathConstraintgetPathConstraint (const std::string &name) const
 Get a MocoPathConstraint. More...
 
const MocoPathConstraintgetPathConstraintByIndex (int index) const
 Get a path constraint by index. More...
 
int getNumPathConstraintEquations () const
 Get the number of scalar path constraints in the MocoProblem. More...
 
const std::vector< MocoVariableInfo > & getMultiplierInfos (const std::string &kinematicConstraintInfoName) const
 Given a kinematic constraint name, get a vector of MocoVariableInfos corresponding to the Lagrange multipliers for that kinematic constraint. More...
 
const MocoKinematicConstraintgetKinematicConstraint (const std::string &name) const
 Get a MocoKinematicConstraint from this MocoPhase. More...
 
int getNumKinematicConstraintEquations () const
 Get the number of scalar kinematic constraints in the MocoProblem. More...
 
void printDescription () const
 Print a description of this problem, including costs and variable bounds. More...
 
Interface for solvers

These functions are for use by MocoSolvers, but can also be called by users for debugging.

Calculate the errors in all the scalar path constraint equations in this phase.

void calcPathConstraintErrors (const SimTK::State &state, SimTK::Vector &errors) const
 
SimTK::Vector calcKinematicConstraintErrors (const SimTK::State &state) const
 Calculate the errors in all the scalar kinematic constraint equations in this phase. More...
 
void applyParametersToModelProperties (const SimTK::Vector &parameterValues, bool initSystemAndDisableConstraints=false) const
 Apply paramater values to the models created from the model passed to initialize() within the current MocoProblem. More...
 
const std::vector< SimTK::ReferencePtr< const Output< double > > > & getImplicitResidualReferencePtrs () const
 Get a vector of reference pointers to model outputs that return residual values for any components with dynamics in implicit forms. More...
 
const std::vector< std::pair< std::string, SimTK::ReferencePtr< const Component > > > & getImplicitComponentReferencePtrs () const
 Get reference pointers to components that enforce dynamics in implicit form. More...
 

Detailed Description

The primary intent of this class is for use by MocoSolvers, but users can also use this class to apply parameter values to the model and evaluate cost terms.

This class also checks the MocoProblem for various errors. To get an instance of this class, use MocoProblem::createRep(). This interface currently supports only single-phase problems. This class stores a reference (not a copy) to the original MocoProblem from which it was created.

ModelBase and ModelDisabledConstraints
This class provides access to two models: ModelBase is obtained by processing the ModelProcessor that the user gives to MocoProblem. ModelDisabledConstraints is a copy of ModelBase in which all kinematic constraints are disabled. ModelDisabledConstraints contains a DiscreteForces component, which is used to apply constraint forces computed using ModelBase. If kinematics are not prescribed (with PositionMotion), ModelDisabledConstraints also contains an AccelerationMotion component, which is used by solvers that rely on implicit multibody dynamics. The initialize() function adds a DiscreteController to both models; this controller is used by a solver to set the control signals for actuators to use. To learn the need for and use of these two models, see Supporting diverse formulations using OpenSim and Simbody.

Constructor & Destructor Documentation

◆ MocoProblemRep() [1/3]

OpenSim::MocoProblemRep::MocoProblemRep ( )
default

◆ MocoProblemRep() [2/3]

OpenSim::MocoProblemRep::MocoProblemRep ( const MocoProblemRep )
delete

◆ MocoProblemRep() [3/3]

OpenSim::MocoProblemRep::MocoProblemRep ( MocoProblemRep &&  source)
inline

Member Function Documentation

◆ applyParametersToModelProperties()

void OpenSim::MocoProblemRep::applyParametersToModelProperties ( const SimTK::Vector &  parameterValues,
bool  initSystemAndDisableConstraints = false 
) const

Apply paramater values to the models created from the model passed to initialize() within the current MocoProblem.

Values must be consistent with the order of parameters returned from createParameterNames().

Note: initSystem() must be called on each model after calls to this method in order for provided parameter values to be applied to the model. You can pass true to have initSystem() called for you, and to also re-disable any constraints re-enabled by the initSystem() call (see getModelDisabledConstraints()).

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::applyParametersToModelProperties().

◆ calcKinematicConstraintErrors()

SimTK::Vector OpenSim::MocoProblemRep::calcKinematicConstraintErrors ( const SimTK::State &  state) const
inline

Calculate the errors in all the scalar kinematic constraint equations in this phase.

This may not be the most efficient solution for solvers, but is rather intended as a convenience method for a quick implementation or for debugging model constraints causing issues in an optimal control problem.

◆ calcPathConstraintErrors()

void OpenSim::MocoProblemRep::calcPathConstraintErrors ( const SimTK::State &  state,
SimTK::Vector &  errors 
) const
inline

◆ createControlInfoNames()

std::vector<std::string> OpenSim::MocoProblemRep::createControlInfoNames ( ) const

Get the control names of all the control infos.

◆ createCostNames()

std::vector<std::string> OpenSim::MocoProblemRep::createCostNames ( ) const

Get the names of all the goals in cost mode.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addCosts().

◆ createEndpointConstraintNames()

std::vector<std::string> OpenSim::MocoProblemRep::createEndpointConstraintNames ( ) const

Get the names of all the goals in endpoint constraint mode.

◆ createKinematicConstraintNames()

std::vector<std::string> OpenSim::MocoProblemRep::createKinematicConstraintNames ( ) const

Get the constraint names of all the kinematic constraints.

Note: this should only be called after initialize().

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addKinematicConstraints().

◆ createMultiplierInfoNames()

std::vector<std::string> OpenSim::MocoProblemRep::createMultiplierInfoNames ( ) const

Get the names of all the Lagrange multiplier infos.

◆ createParameterNames()

std::vector<std::string> OpenSim::MocoProblemRep::createParameterNames ( ) const

Get the names of all the parameters.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addParameters().

◆ createPathConstraintNames()

std::vector<std::string> OpenSim::MocoProblemRep::createPathConstraintNames ( ) const

◆ createStateInfoNames()

std::vector<std::string> OpenSim::MocoProblemRep::createStateInfoNames ( ) const

Get the state names of all the state infos.

◆ createStateVariableNamesInSystemOrder()

std::vector<std::string> OpenSim::MocoProblemRep::createStateVariableNamesInSystemOrder ( std::unordered_map< int, int > &  yIndexMap) const

This excludes generalized coordinate and speed states if isPrescribedKinematics() is true.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::TropterProblemBase().

◆ getAccelerationMotion()

const AccelerationMotion& OpenSim::MocoProblemRep::getAccelerationMotion ( ) const
inline

This is a component inside ModelDisabledConstraints that you can use to set the value of generalized accelerations UDot, for use in implicit dynamics formulations.

The motion is not necessarily enabled.

Referenced by OpenSim::MocoTropterSolver::ImplicitTropterProblem< T >::ImplicitTropterProblem(), and OpenSim::MocoTropterSolver::TropterProblemBase< T >::setSimTKState().

◆ getConstraintForces()

const DiscreteForces& OpenSim::MocoProblemRep::getConstraintForces ( ) const
inline

This is a component inside ModelDisabledConstraints that you can use to set the value of discrete forces, intended to hold the constraint forces obtained from ModelBase.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::calcAndApplyKinematicConstraintForces().

◆ getControlInfo()

const MocoVariableInfo& OpenSim::MocoProblemRep::getControlInfo ( const std::string &  name) const

Get information for actuator controls.

If the control is associated with a non-scalar actuator (i.e. uses multiple control variables), then the control name will be the actuator path appended by the control index (e.g. "/actuator_0"); See MocoPhase::setControlInfo().

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addControlVariables().

◆ getCost()

const MocoGoal& OpenSim::MocoProblemRep::getCost ( const std::string &  name) const

Get a cost by name. This returns a MocoGoal in cost mode.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addCosts().

◆ getCostByIndex()

const MocoGoal& OpenSim::MocoProblemRep::getCostByIndex ( int  index) const

Get a cost by index.

The order is the same as in getCostNames(). Note: this does not perform a bounds check.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::calc_cost(), and OpenSim::MocoTropterSolver::TropterProblemBase< T >::calc_cost_integrand().

◆ getDiscreteControllerBase()

const DiscreteController& OpenSim::MocoProblemRep::getDiscreteControllerBase ( ) const
inline

This is a component inside ModelBase that you can use to set the value of control signals.

◆ getDiscreteControllerDisabledConstraints()

const DiscreteController& OpenSim::MocoProblemRep::getDiscreteControllerDisabledConstraints ( ) const
inline

◆ getEndpointConstraint()

const MocoGoal& OpenSim::MocoProblemRep::getEndpointConstraint ( const std::string &  name) const

Get an endpoint constraint by name.

This returns a MocoGoal in endpoint constraint mode.

◆ getEndpointConstraintByIndex()

const MocoGoal& OpenSim::MocoProblemRep::getEndpointConstraintByIndex ( int  index) const

Get an endpoint constraint by index.

The order is the same as in getEndpointConstraintNames(). Note: this does not perform a bounds check.

◆ getImplicitComponentReferencePtrs()

const std::vector<std::pair<std::string, SimTK::ReferencePtr<const Component> > >& OpenSim::MocoProblemRep::getImplicitComponentReferencePtrs ( ) const
inline

Get reference pointers to components that enforce dynamics in implicit form.

This returns a vector of pairs including the name of the discrete derivative variable and the component reference pointer.

◆ getImplicitResidualReferencePtrs()

const std::vector<SimTK::ReferencePtr<const Output<double> > >& OpenSim::MocoProblemRep::getImplicitResidualReferencePtrs ( ) const
inline

Get a vector of reference pointers to model outputs that return residual values for any components with dynamics in implicit forms.

The references returned are from the model returned by getModelDisabledConstraints().

◆ getKinematicConstraint()

const MocoKinematicConstraint& OpenSim::MocoProblemRep::getKinematicConstraint ( const std::string &  name) const

Get a MocoKinematicConstraint from this MocoPhase.

Note: this does not include MocoPathConstraints, use getPathConstraint() instead.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addKinematicConstraints().

◆ getKinematicConstraintEquationNames()

std::vector<std::string> OpenSim::MocoProblemRep::getKinematicConstraintEquationNames ( bool  includeDerivatives) const

Get a vector of names for all kinematic constraint equations.

Kinematic constraint equations are ordered as so:

  • position-level constraints
  • velocity-level constraints
  • acceleration-level constraints If includeDerivatives is true, the ordering is:
  • position-level constraints
  • first derivative of position-level constraints (denoted by suffix "d")
  • velocity-level constraints
  • second derivative of position-level constraints (suffix "dd")
  • first derivative of velocity-level constraints (suffix "d")
  • acceleration-level constraints

◆ getModelBase()

const Model& OpenSim::MocoProblemRep::getModelBase ( ) const
inline

Get a reference to the copy of the model being used by this MocoProblemRep.

This model is obtained by processing the ModelProcessor the user gives to MocoProblem. This model is not the model given to MocoGoal or MocoPathConstraint, but can be used within solvers to compute constraint forces and constraint errors (see getModelDisabledConstraints() for more details). Any parameter updates via a MocoParameter added to the problem will be applied to this model.

◆ getModelDisabledConstraints()

const Model& OpenSim::MocoProblemRep::getModelDisabledConstraints ( ) const
inline

Get a reference to a copy of the model being used by this MocoProblemRep, but with all constraints disabled and an additional DiscreteForces component.

This new component can be used to apply constraint forces computed from the base model to this model, which updates the discrete variables in the state associated with these forces. You should use this model to compute accelerations via getModelDisabledConstraints().realizeAccleration(state), making sure to add any constraint forces to the model preceding the realization. This model is the same instance as that given to MocoGoal and MocoPathConstraint, ensuring that realizing to Stage::Acceleration in these classes produces the same accelerations computed by the solver. Any parameter updates via a MocoParameter added to the problem will be applied to this model.

◆ getMultiplierInfos()

const std::vector<MocoVariableInfo>& OpenSim::MocoProblemRep::getMultiplierInfos ( const std::string &  kinematicConstraintInfoName) const

Given a kinematic constraint name, get a vector of MocoVariableInfos corresponding to the Lagrange multipliers for that kinematic constraint.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addKinematicConstraints().

◆ getName()

const std::string& OpenSim::MocoProblemRep::getName ( ) const

◆ getNumControls()

int OpenSim::MocoProblemRep::getNumControls ( ) const
inline

◆ getNumCosts()

int OpenSim::MocoProblemRep::getNumCosts ( ) const
inline

Get the number of goals in cost mode.

◆ getNumEndpointConstraints()

int OpenSim::MocoProblemRep::getNumEndpointConstraints ( ) const
inline

Get the number of goals in endpoint constraint mode.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addCosts().

◆ getNumImplicitAuxiliaryResiduals()

int OpenSim::MocoProblemRep::getNumImplicitAuxiliaryResiduals ( ) const
inline

◆ getNumKinematicConstraintEquations()

int OpenSim::MocoProblemRep::getNumKinematicConstraintEquations ( ) const
inline

Get the number of scalar kinematic constraints in the MocoProblem.

This does not include path constraints equations.

◆ getNumKinematicConstraints()

int OpenSim::MocoProblemRep::getNumKinematicConstraints ( ) const
inline

◆ getNumParameters()

int OpenSim::MocoProblemRep::getNumParameters ( ) const
inline

◆ getNumPathConstraintEquations()

int OpenSim::MocoProblemRep::getNumPathConstraintEquations ( ) const
inline

Get the number of scalar path constraints in the MocoProblem.

This does not include kinematic constraints equations.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addGenericPathConstraints().

◆ getNumStates()

int OpenSim::MocoProblemRep::getNumStates ( ) const
inline

◆ getParameter()

const MocoParameter& OpenSim::MocoProblemRep::getParameter ( const std::string &  name) const

◆ getPathConstraint()

const MocoPathConstraint& OpenSim::MocoProblemRep::getPathConstraint ( const std::string &  name) const

Get a MocoPathConstraint.

Note: this does not include MocoKinematicConstraints, use getKinematicConstraint() instead.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addGenericPathConstraints().

◆ getPathConstraintByIndex()

const MocoPathConstraint& OpenSim::MocoProblemRep::getPathConstraintByIndex ( int  index) const

Get a path constraint by index.

The order is the same as in getPathConstraintNames(). Note: this does not perform a bounds check.

◆ getStateInfo()

const MocoVariableInfo& OpenSim::MocoProblemRep::getStateInfo ( const std::string &  name) const

◆ getTimeFinalBounds()

MocoFinalBounds OpenSim::MocoProblemRep::getTimeFinalBounds ( ) const

Note: the return value is constructed fresh on every call from the internal property. Avoid repeated calls to this function.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addStateVariables().

◆ getTimeInitialBounds()

MocoInitialBounds OpenSim::MocoProblemRep::getTimeInitialBounds ( ) const

Note: the return value is constructed fresh on every call from the internal property. Avoid repeated calls to this function.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::addStateVariables().

◆ isPrescribedKinematics()

bool OpenSim::MocoProblemRep::isPrescribedKinematics ( ) const
inline

Does the model contain a PositionMotion to prescribe all generalized coordinates, speeds, and accelerations?

Referenced by OpenSim::MocoTropterSolver::ImplicitTropterProblem< T >::ImplicitTropterProblem(), and OpenSim::MocoTropterSolver::TropterProblemBase< T >::setSimTKState().

◆ operator=() [1/2]

MocoProblemRep& OpenSim::MocoProblemRep::operator= ( const MocoProblemRep )
delete

◆ operator=() [2/2]

MocoProblemRep& OpenSim::MocoProblemRep::operator= ( MocoProblemRep &&  source)
inline

◆ printDescription()

void OpenSim::MocoProblemRep::printDescription ( ) const

Print a description of this problem, including costs and variable bounds.

Printing is done using OpenSim::log_cout().

◆ updStateBase()

SimTK::State& OpenSim::MocoProblemRep::updStateBase ( ) const
inline

This is a state object that solvers can use along with ModelBase.

◆ updStateDisabledConstraints()

SimTK::State& OpenSim::MocoProblemRep::updStateDisabledConstraints ( int  index = 0) const
inline

This is a state object that solvers can use with ModelDisabledConstraints.

Some solvers may need to use 2 state objects at once; you can supply an index of 1 to get a second state object.

Referenced by OpenSim::MocoTropterSolver::TropterProblemBase< T >::calc_cost(), and OpenSim::MocoTropterSolver::TropterProblemBase< T >::setSimTKState().


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