Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'negate' property to MocoOutputGoal. #637

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Change Log

0.5.0 (in development)
----------------------
- 2020-05-25: MocoOutputGoal can now negate the value of an output.

- 2020-05-16: Moved ActivationCoordinateActuator from opensim-moco to
opensim-core.

Expand Down
4 changes: 4 additions & 0 deletions Moco/Moco/MocoGoal/MocoOutputGoal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void MocoOutputGoal::constructProperties() {
constructProperty_output_path("");
constructProperty_divide_by_displacement(false);
constructProperty_divide_by_mass(false);
constructProperty_negate(false);
}

void MocoOutputGoal::initializeOnModelImpl(const Model& output) const {
Expand All @@ -45,6 +46,9 @@ void MocoOutputGoal::calcIntegrandImpl(
const SimTK::State& state, double& integrand) const {
getModel().getSystem().realize(state, m_output->getDependsOnStage());
integrand = m_output->getValue(state);
if (get_negate()) {
integrand *= -1.0;
}
}

void MocoOutputGoal::calcGoalImpl(
Expand Down
9 changes: 9 additions & 0 deletions Moco/Moco/MocoGoal/MocoOutputGoal.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class OSIMMOCO_API MocoOutputGoal : public MocoGoal {
return get_divide_by_mass();
}

/// Set if the output value should be negated (i.e., to maximize rather
/// than minimize its value).
void setNegate(bool tf) { set_negate(tf); }
bool getNegate() const { return get_negate(); }

protected:
void initializeOnModelImpl(const Model&) const override;
void calcIntegrandImpl(
Expand All @@ -72,6 +77,10 @@ class OSIMMOCO_API MocoOutputGoal : public MocoGoal {
"false)");
OpenSim_DECLARE_PROPERTY(divide_by_mass, bool,
"Divide by the model's total mass (default: false)");
OpenSim_DECLARE_PROPERTY(negate, bool,
"Negate the output value (i.e., maximize the output) "
"(default: false).");

void constructProperties();

mutable SimTK::ReferencePtr<const Output<double>> m_output;
Expand Down