MocoExtendProblem: Interface Between OpenSim and MATLAB for Rapidly Developing Direct Collocation Goals in Moco 1.1.0
add custom Moco goals to existing matlab scripts
MocoCustomOutputGoal Class Reference

Goal that minimizes a custom model output. More...

#include <MocoCustomOutputGoal.h>

Inheritance diagram for MocoCustomOutputGoal:

Public Member Functions

void setOutputPath (std::string path)
 Set the absolute path to the output to minimize.
 
const std::string & getOutputPath () const
 Get the output path.
 
void setExponent (int exponent)
 Set the exponent for the output value.
 
int getExponent () const
 Get the current exponent value.
 
void setOutputIndex (int index)
 Set the index for vector outputs.
 
int getOutputIndex () const
 Get the output index.
 
void setDivideByDisplacement (bool tf)
 Set whether to divide by displacement.
 
bool getDivideByDisplacement () const
 Get whether the goal is divided by displacement.
 
void setDivideByMass (bool mass)
 Set whether to divide by model mass.
 
bool getDivideByMass () const
 Get whether the goal is divided by mass.
 
Constructors
 MocoCustomOutputGoal ()
 Default constructor.
 
 MocoCustomOutputGoal (std::string name)
 Constructor with name.
 
 MocoCustomOutputGoal (std::string name, double weight)
 Constructor with name and weight.
 

Protected Member Functions

Required implementations of virtual methods
void initializeOnModelBase () const
 Initialize the goal on the base model.
 
double calcOutputValue (const SimTK::State &) const
 Calculate the output value for the current state.
 
double setValueToExponent (double value) const
 Apply the exponent to a value.
 
const SimTK::Stage & getDependsOnStage () const
 Get the stage that this goal depends on.
 
void initializeOnModelImpl (const Model &) const override
 Initialize the goal with the model.
 
void calcIntegrandImpl (const IntegrandInput &state, double &integrand) const override
 Calculate the integrand value for the cost function.
 
void calcGoalImpl (const GoalInput &input, SimTK::Vector &values) const override
 Calculate the goal value.
 
bool getSupportsEndpointConstraintImpl () const override
 Whether this goal supports endpoint constraint mode.
 
Mode getDefaultModeImpl () const override
 Get the default mode for this goal.
 

Private Types

Data type enumeration
enum  DataType { Type_double , Type_Vec3 , Type_SpatialVec }
 Enumeration of possible output data types. More...
 

Private Member Functions

 OpenSim_DECLARE_CONCRETE_OBJECT (MocoCustomOutputGoal, MocoGoal)
 
void constructProperties ()
 Initialize the goal's properties.
 
Properties
 OpenSim_DECLARE_PROPERTY (output_path, std::string, "The absolute path to the output in the model to use as the " "integrand for this goal.")
 
 OpenSim_DECLARE_PROPERTY (exponent, int, "The exponent applied to the output value in the integrand. " "The output can take on negative values in the integrand when the " "exponent is set to 1 (the default value). When the exponent is " "set to a value greater than 1, the absolute value function is " "applied to the output (before the exponent is applied), meaning " "that odd numbered exponents (greater than 1) do not take on " "negative values.")
 
 OpenSim_DECLARE_PROPERTY (output_index, int, "The index to the value to be minimized when a vector type " "Output is specified. For SpatialVec Outputs, indices 0, 1, " "and 2 refer to the rotational components and indices 3, 4, " "and 5 refer to the translational components. A value of -1 " "indicates to minimize the vector norm (default: -1).")
 
 OpenSim_DECLARE_PROPERTY (divide_by_displacement, bool, "Divide by the model's displacement over the phase (default: " "false)")
 
 OpenSim_DECLARE_PROPERTY (divide_by_mass, bool, "Divide by the model's total mass (default: false)")
 

Private Attributes

Internal working variables
DataType m_data_type
 
SimTK::ReferencePtr< const AbstractOutput > m_output
 Reference to the output being minimized.
 
std::function< double(const double &)> m_power_function
 Function to compute power of values.
 
int m_index1
 First index for vector components.
 
int m_index2
 Second index for vector components.
 
bool m_minimizeVectorNorm
 Whether to minimize vector norm.
 
SimTK::Stage m_dependsOnStage = SimTK::Stage::Acceleration
 Stage at which the goal depends on the model.
 

Detailed Description

Goal that minimizes a custom model output.

This goal minimizes any model output that can be specified by its absolute path, integrated over the phase. The output can be a scalar value, a component of a vector output, or the norm of a vector output.

For vector outputs (including SpatialVec), you can either:

  • Minimize a specific component by setting the output_index
  • Minimize the vector norm by setting output_index to -1 (default)

For SpatialVec outputs:

  • Indices 0-2 refer to rotational components
  • Indices 3-5 refer to translational components

This goal can be useful for:

  • Minimizing any custom quantity computed by the model
  • Creating complex objective functions
  • Targeting specific aspects of the motion

The goal value can optionally be:

  • Divided by the model's displacement over the phase
  • Divided by the model's total mass
  • Raised to a specified power using the exponent property

Member Enumeration Documentation

◆ DataType

enum DataType
private

Enumeration of possible output data types.

Enumerator
Type_double 

Scalar double value.

Type_Vec3 

3D vector

Type_SpatialVec 

6D spatial vector

Constructor & Destructor Documentation

◆ MocoCustomOutputGoal() [1/3]

Default constructor.

◆ MocoCustomOutputGoal() [2/3]

MocoCustomOutputGoal ( std::string name)
inline

Constructor with name.

Parameters
nameThe name of the goal

◆ MocoCustomOutputGoal() [3/3]

MocoCustomOutputGoal ( std::string name,
double weight )
inline

Constructor with name and weight.

Parameters
nameThe name of the goal
weightWeight for this goal term in the optimization

Member Function Documentation

◆ calcGoalImpl()

void calcGoalImpl ( const GoalInput & input,
SimTK::Vector & values ) const
overrideprotected

Calculate the goal value.

Parameters
inputInput data containing the integral
valuesVector to store the calculated values

◆ calcIntegrandImpl()

void calcIntegrandImpl ( const IntegrandInput & state,
double & integrand ) const
overrideprotected

Calculate the integrand value for the cost function.

Parameters
stateInput data for the current state
integrandReference to store the calculated integrand value
Here is the call graph for this function:

◆ calcOutputValue()

double calcOutputValue ( const SimTK::State & state) const
protected

Calculate the output value for the current state.

Parameters
stateThe current state
Returns
The calculated output value
Here is the caller graph for this function:

◆ constructProperties()

void constructProperties ( )
private

Initialize the goal's properties.

◆ getDefaultModeImpl()

Mode getDefaultModeImpl ( ) const
inlineoverrideprotected

Get the default mode for this goal.

◆ getDependsOnStage()

const SimTK::Stage & getDependsOnStage ( ) const
protected

Get the stage that this goal depends on.

Returns
The dependency stage
Here is the caller graph for this function:

◆ getDivideByDisplacement()

bool getDivideByDisplacement ( ) const
inline

Get whether the goal is divided by displacement.

Returns
True if divided by displacement, false otherwise

◆ getDivideByMass()

bool getDivideByMass ( ) const
inline

Get whether the goal is divided by mass.

Returns
True if divided by mass, false otherwise

◆ getExponent()

int getExponent ( ) const
inline

Get the current exponent value.

Returns
The exponent value

◆ getOutputIndex()

int getOutputIndex ( ) const
inline

Get the output index.

Returns
The output index

◆ getOutputPath()

const std::string & getOutputPath ( ) const
inline

Get the output path.

Returns
The output path

◆ getSupportsEndpointConstraintImpl()

bool getSupportsEndpointConstraintImpl ( ) const
inlineoverrideprotected

Whether this goal supports endpoint constraint mode.

◆ initializeOnModelBase()

void initializeOnModelBase ( ) const
protected

Initialize the goal on the base model.

Here is the caller graph for this function:

◆ initializeOnModelImpl()

void initializeOnModelImpl ( const Model & output) const
overrideprotected

Initialize the goal with the model.

Here is the call graph for this function:

◆ OpenSim_DECLARE_CONCRETE_OBJECT()

OpenSim_DECLARE_CONCRETE_OBJECT ( MocoCustomOutputGoal ,
MocoGoal  )
private

◆ OpenSim_DECLARE_PROPERTY() [1/5]

OpenSim_DECLARE_PROPERTY ( divide_by_displacement ,
bool ,
"Divide by the model's displacement over the phase (default: " "false)"  )
private

◆ OpenSim_DECLARE_PROPERTY() [2/5]

OpenSim_DECLARE_PROPERTY ( divide_by_mass ,
bool ,
"Divide by the model's total mass (default: false)"  )
private

◆ OpenSim_DECLARE_PROPERTY() [3/5]

OpenSim_DECLARE_PROPERTY ( exponent ,
int ,
"The exponent applied to the output value in the integrand. " "The output can take on negative values in the integrand when the " "exponent is set to 1 (the default value). When the exponent is " "set to a value greater than 1,
the absolute value function is " "applied to the outputbefore the exponent is applied,
meaning " "that odd numbered exponents(greater than 1) do not take on " "negative values."  )
private

◆ OpenSim_DECLARE_PROPERTY() [4/5]

OpenSim_DECLARE_PROPERTY ( output_index ,
int ,
"The index to the value to be minimized when a vector type " "Output is specified. For SpatialVec Outputs,
indices 0,
1 ,
" "and 2 refer to the rotational components and indices 3,
4 ,
" "and 5 refer to the translational components. A value of -1 " "indicates to minimize the vector norm(default:-1)."  )
private

◆ OpenSim_DECLARE_PROPERTY() [5/5]

OpenSim_DECLARE_PROPERTY ( output_path ,
std::string ,
"The absolute path to the output in the model to use as the " "integrand for this goal."  )
private

◆ setDivideByDisplacement()

void setDivideByDisplacement ( bool tf)
inline

Set whether to divide by displacement.

Parameters
tfTrue to divide by displacement, false otherwise

◆ setDivideByMass()

void setDivideByMass ( bool mass)
inline

Set whether to divide by model mass.

Parameters
massTrue to divide by mass, false otherwise

◆ setExponent()

void setExponent ( int exponent)
inline

Set the exponent for the output value.

Parameters
exponentThe exponent value

◆ setOutputIndex()

void setOutputIndex ( int index)
inline

Set the index for vector outputs.

Parameters
indexThe index to minimize (-1 for vector norm)

◆ setOutputPath()

void setOutputPath ( std::string path)
inline

Set the absolute path to the output to minimize.

Parameters
pathThe output path in the model

◆ setValueToExponent()

double setValueToExponent ( double value) const
protected

Apply the exponent to a value.

Parameters
valueThe value to modify
Returns
The value raised to the exponent
Here is the caller graph for this function:

Member Data Documentation

◆ m_data_type

DataType m_data_type
mutableprivate

Type of data being processed

◆ m_dependsOnStage

SimTK::Stage m_dependsOnStage = SimTK::Stage::Acceleration
mutableprivate

Stage at which the goal depends on the model.

◆ m_index1

int m_index1
mutableprivate

First index for vector components.

◆ m_index2

int m_index2
mutableprivate

Second index for vector components.

◆ m_minimizeVectorNorm

bool m_minimizeVectorNorm
mutableprivate

Whether to minimize vector norm.

◆ m_output

SimTK::ReferencePtr<const AbstractOutput> m_output
mutableprivate

Reference to the output being minimized.

◆ m_power_function

std::function<double(const double&)> m_power_function
mutableprivate

Function to compute power of values.


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