Skip to content

Ipopt options file #3025

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
% Load the Moco libraries
import org.opensim.modeling.*;

% Flag to enable or disable the custom ipopt options files.
USE_CUSTOM_OPTIONS = true;

% ---------------------------------------------------------------------------
% Set up a coordinate tracking problem where the goal is to minimize the
% difference between provided and simulated coordinate values and speeds (and
Expand Down Expand Up @@ -183,6 +186,16 @@
problem.setStateInfo('/jointset/ankle_r/ankle_angle_r/value', [-15*pi/180, 25*pi/180]);
problem.setStateInfo('/jointset/lumbar/lumbar/value', [0, 20*pi/180]);

solver = MocoDirectCollocationSolver.safeDownCast(study.updSolver());
solver.set_verbosity(2);
solver.set_optim_solver('ipopt');
solver.set_optim_convergence_tolerance(1e-4);
solver.set_optim_constraint_tolerance(1e-4);
solver.set_optim_max_iterations(1000);
if (USE_CUSTOM_OPTIONS)
% This is a file which contains more custom solver options for ipopt.
solver.set_optim_ipopt_opt_filename("track_opt.opt");
end

% Solve the problem
% =================
Expand Down Expand Up @@ -326,6 +339,10 @@
solver.set_optim_max_iterations(1000);
solver.setGuess(gaitTrackingSolution); % Use tracking solution as initial guess

if (USE_CUSTOM_OPTIONS)
% This is a file which contains more custom solver options for ipopt.
solver.set_optim_ipopt_opt_filename("predi_opt.opt");
end

% Solve problem
% =============
Expand Down
4 changes: 3 additions & 1 deletion OpenSim/Examples/Moco/example2DWalking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ set(EXAMPLE_2D_WALKING_FILES
2D_gait.osim
referenceCoordinates.sto
referenceGRF.sto
referenceGRF.xml)
referenceGRF.xml
predi_opt.opt
track_opt.opt)

OpenSimAddExampleCXX(NAME example2DWalking SUBDIR Moco
EXECUTABLES example2DWalking example2DWalkingMetabolics
Expand Down
12 changes: 12 additions & 0 deletions OpenSim/Examples/Moco/example2DWalking/example2DWalking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

using namespace OpenSim;

bool USE_CUSTOM_OPTIONS = false;

/// Set a coordinate tracking problem where the goal is to minimize the
/// difference between provided and simulated coordinate values and speeds
/// (and ground reaction forces) as well as to minimize an effort cost (squared
Expand Down Expand Up @@ -185,6 +187,11 @@ MocoSolution gaitTracking(double controlEffortWeight = 10,
solver.set_optim_constraint_tolerance(1e-4);
solver.set_optim_max_iterations(1000);

if (USE_CUSTOM_OPTIONS) {
// This is a file which contains more custom solver options for ipopt.
solver.set_optim_ipopt_opt_filename("track_opt.opt");
}

// Solve problem.
// ==============
MocoSolution solution = study.solve();
Expand Down Expand Up @@ -318,6 +325,11 @@ void gaitPrediction(const MocoSolution& gaitTrackingSolution) {
// Use the solution from the tracking simulation as initial guess.
solver.setGuess(gaitTrackingSolution);

if (USE_CUSTOM_OPTIONS) {
// This is a file which contains more custom solver options for ipopt.
solver.set_optim_ipopt_opt_filename("predi_opt.opt");
}

// Solve problem.
// ==============
MocoSolution solution = study.solve();
Expand Down
2 changes: 2 additions & 0 deletions OpenSim/Examples/Moco/example2DWalking/predi_opt.opt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Warm start since the problem is probably somewhat converged from the first problem.
warm_start_init_point yes
3 changes: 3 additions & 0 deletions OpenSim/Examples/Moco/example2DWalking/track_opt.opt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example of an option that can be specified when we use an external options file.

least_square_init_duals yes
2 changes: 2 additions & 0 deletions OpenSim/Moco/MocoCasADiSolver/MocoCasADiSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ std::unique_ptr<CasOC::Solver> MocoCasADiSolver::createCasOCSolver(
checkPropertyValueIsInSet(getProperty_verbosity(), {0, 1, 2});
if (get_optim_solver() == "ipopt") {
solverOptions["print_user_options"] = "yes";
// Set the optional options file name.
solverOptions["option_file_name"] = get_optim_ipopt_opt_filename();
if (get_verbosity() < 2) {
solverOptions["print_level"] = 0;
} else if (get_optim_ipopt_print_level() != -1) {
Expand Down
1 change: 1 addition & 0 deletions OpenSim/Moco/MocoDirectCollocationSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void MocoDirectCollocationSolver::constructProperties() {
constructProperty_implicit_auxiliary_derivative_bounds({-1000, 1000});
constructProperty_minimize_lagrange_multipliers(false);
constructProperty_lagrange_multiplier_weight(1.0);
constructProperty_optim_ipopt_opt_filename("ipopt.opt");
}

void MocoDirectCollocationSolver::setMesh(const std::vector<double>& mesh) {
Expand Down
4 changes: 4 additions & 0 deletions OpenSim/Moco/MocoDirectCollocationSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class OSIMMOCO_API MocoDirectCollocationSolver : public MocoSolver {
"Newton.");
OpenSim_DECLARE_PROPERTY(optim_ipopt_print_level, int,
"IPOPT's verbosity (see IPOPT documentation).");
OpenSim_DECLARE_PROPERTY(optim_ipopt_opt_filename, std::string,
"When using IPOPT, 'ipopt.opt' (default)."
"IPOPT looks for this file before it starts and uses it "
"to set optimizer options. See: https://coin-or.github.io/Ipopt/OPTIONS.html.");
OpenSim_DECLARE_OPTIONAL_PROPERTY(enforce_constraint_derivatives, bool,
"'true' (default) or 'false', whether or not derivatives of "
"kinematic constraints are enforced as path constraints in the "
Expand Down
3 changes: 3 additions & 0 deletions OpenSim/Moco/MocoTropterSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ MocoTropterSolver::createTropterSolver(
// Check that IPOPT print level is valid.
checkPropertyValueIsInRangeOrSet(
getProperty_optim_ipopt_print_level(), 0, 12, {-1});
// Set the optional options file name.
optsolver.set_advanced_option_string(
"option_file_name", get_optim_ipopt_opt_filename());
if (get_verbosity() < 2) {
optsolver.set_advanced_option_int("print_level", 0);
} else {
Expand Down