Skip to content

Commit

Permalink
enable stream logger for mosek. (RobotLocomotion#9097)
Browse files Browse the repository at this point in the history
* enable stream logger for mosek.

* Address Soonho's comments.
  • Loading branch information
hongkai-dai authored Jul 9, 2018
1 parent 53d9c58 commit 8af9e7e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,8 @@ drake_cc_googletest(
":quadratic_program_examples",
":second_order_cone_program_examples",
":semidefinite_program_examples",
"//common:temp_directory",
"@spruce",
],
)

Expand Down
17 changes: 17 additions & 0 deletions solvers/mosek_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ namespace drake {
namespace solvers {
namespace {

// This function is used to print information for each iteration to the console,
// it will show PRSTATUS, PFEAS, DFEAS, etc. For more information, check out
// https://docs.mosek.com/8.1/capi/solver-io.html. This printstr is copied
// directly from https://docs.mosek.com/8.1/capi/solver-io.html#stream-logging.
void MSKAPI printstr(void* , const char str[]) {
printf("%s", str);
}

// Add LinearConstraints and LinearEqualityConstraints to the Mosek task.
template <typename C>
MSKrescodee AddLinearConstraintsFromBindings(
Expand Down Expand Up @@ -704,6 +712,15 @@ SolutionResult MosekSolver::Solve(MathematicalProgram& prog) const {
if (rescode == MSK_RES_OK) {
rescode = AddLinearMatrixInequalityConstraint(prog, &task);
}
if (rescode == MSK_RES_OK && stream_logging_) {
if (log_file_.empty()) {
rescode =
MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, nullptr, printstr);
} else {
rescode =
MSK_linkfiletotaskstream(task, MSK_STREAM_LOG, log_file_.c_str(), 0);
}
}

SolutionResult result = SolutionResult::kUnknownError;
// Run optimizer.
Expand Down
21 changes: 21 additions & 0 deletions solvers/mosek_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ class MosekSolver : public MathematicalProgramSolverInterface {
/// @return same as MathematicalProgramSolverInterface::solver_id()
static SolverId id();

/**
* Control stream logging. Refer to
* https://docs.mosek.com/8.1/capi/solver-io.html for more details.
* @param flag Set to true if the user want to turn on stream logging.
* @param log_file If the user wants to output the logging to a file, then
* set @p log_file to the name of that file. If the user wants to output the
* logging to the console, then set log_file to empty string.
*/
void set_stream_logging(bool flag, const std::string& log_file) {
stream_logging_ = flag;
log_file_ = log_file;
}

/**
* This type contains a valid MOSEK license environment, and is only to be
* used from AcquireLicense().
Expand All @@ -56,6 +69,14 @@ class MosekSolver : public MathematicalProgramSolverInterface {
// during the first call of Solve() (which avoids grabbing a Mosek license
// before we know that we actually want one).
mutable std::shared_ptr<License> license_;
// Set to true if the user wants the solver to produce output to the console
// or a log file. Default to false, such that the solver runs silently.
// Check out https://docs.mosek.com/8.1/capi/solver-io.html for more info.
bool stream_logging_{false};
// set @p log_file to the name of that file. If the user wants to output the
// logging to the console, then set log_file to empty string. Default to an
// empty string.
std::string log_file_{};
};

} // namespace solvers
Expand Down
24 changes: 24 additions & 0 deletions solvers/test/mosek_solver_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "drake/solvers/mosek_solver.h"

#include <gtest/gtest.h>
#include <spruce.hh>

#include "drake/common/temp_directory.h"
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/test/linear_program_examples.h"
#include "drake/solvers/test/quadratic_program_examples.h"
Expand Down Expand Up @@ -116,6 +118,28 @@ GTEST_TEST(TestSemidefiniteProgram, EigenvalueProblem) {
SolveEigenvalueProblem(mosek_solver, 1E-7);
}
}

GTEST_TEST(MosekTest, TestLogFile) {
// Test if we can print the logging info to a log file.
MathematicalProgram prog;
const auto x = prog.NewContinuousVariables<2>();
prog.AddLinearConstraint(x(0) + x(1) == 1);

const std::string log_file = temp_directory() + "mosek.log";
EXPECT_FALSE(spruce::path(log_file).exists());
MosekSolver solver;
solver.Solve(prog);
// By default, no logging file.
EXPECT_FALSE(spruce::path(log_file).exists());
// Output the logging to the console
solver.set_stream_logging(true, "");
solver.Solve(prog);
EXPECT_FALSE(spruce::path(log_file).exists());
// Output the logging to the file.
solver.set_stream_logging(true, log_file);
solver.Solve(prog);
EXPECT_TRUE(spruce::path(log_file).exists());
}
} // namespace test
} // namespace solvers
} // namespace drake
Expand Down

0 comments on commit 8af9e7e

Please sign in to comment.