Skip to content

Commit

Permalink
[solvers] Ipopt supports kPrintFileName (RobotLocomotion#17034)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri authored Apr 26, 2022
1 parent 3c937b7 commit 22bd902
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,8 @@ drake_cc_googletest(
":optimization_examples",
":quadratic_program_examples",
":second_order_cone_program_examples",
"//common:filesystem",
"//common:temp_directory",
"//common/test_utilities:eigen_matrix_compare",
] + select({
"//conditions:default": [
Expand Down
12 changes: 11 additions & 1 deletion solvers/ipopt_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,20 @@ void SetIpoptOptions(const MathematicalProgram& prog,
// useful for debugging. Otherwise, we default to printing nothing. The user
// can always select an arbitrary print level, by setting the ipopt value
// directly in the solver options.
int common_print_level = merged_solver_options.get_print_to_console() ? 4 : 0;
const int verbose_level = 4;
const int common_print_level =
merged_solver_options.get_print_to_console() ? verbose_level : 0;
SetIpoptOptionsHelper<int>("print_level", common_print_level,
&merged_solver_options);

std::string print_file_name = merged_solver_options.get_print_file_name();
if (!print_file_name.empty()) {
SetIpoptOptionsHelper<std::string>("output_file", print_file_name,
&merged_solver_options);
SetIpoptOptionsHelper<int>("file_print_level", verbose_level,
&merged_solver_options);
}

const auto& ipopt_options_double =
merged_solver_options.GetOptionsDouble(IpoptSolver::id());
const auto& ipopt_options_str =
Expand Down
23 changes: 23 additions & 0 deletions solvers/test/ipopt_solver_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <gtest/gtest.h>

#include "drake/common/filesystem.h"
#include "drake/common/temp_directory.h"
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/test/linear_program_examples.h"
#include "drake/solvers/test/mathematical_program_test_util.h"
Expand Down Expand Up @@ -257,6 +259,27 @@ GTEST_TEST(IpoptSolverTest, SolverOptionsVerbosity) {
}
}

// This is to verify we can set the print out file through CommonSolverOption.
GTEST_TEST(IpoptSolverTest, PrintToFile) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(1);
prog.AddLinearConstraint(x(0) <= 3);
prog.AddLinearConstraint(x(0) >= -3);
prog.AddLinearCost(x(0));

const std::string filename = temp_directory() + "/ipopt.log";
EXPECT_FALSE(filesystem::exists({filename}));
SolverOptions solver_options;
solver_options.SetOption(CommonSolverOption::kPrintFileName, filename);

IpoptSolver solver;
if (solver.is_available()) {
const auto result = solver.Solve(prog, {}, solver_options);
EXPECT_TRUE(result.is_success());
EXPECT_TRUE(filesystem::exists({filename}));
}
}

TEST_P(TestEllipsoidsSeparation, TestSOCP) {
IpoptSolver ipopt_solver;
if (ipopt_solver.available()) {
Expand Down

0 comments on commit 22bd902

Please sign in to comment.