Skip to content

Commit

Permalink
Restore check that GRB_LICENSE_FILE and MOSEKLM_LICENSE_FILE are set
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Apr 20, 2018
1 parent b69410d commit aa3511f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
18 changes: 18 additions & 0 deletions solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,15 @@ drake_cc_googletest(
],
)

drake_cc_googletest(
name = "gurobi_solver_grb_license_file_test",
tags = gurobi_test_tags(),
deps = [
":gurobi_solver",
":mathematical_program",
],
)

drake_cc_googletest(
name = "integer_optimization_util_test",
deps = [
Expand Down Expand Up @@ -980,6 +989,15 @@ drake_cc_googletest(
],
)

drake_cc_googletest(
name = "mosek_solver_moseklm_license_file_test",
tags = mosek_test_tags(),
deps = [
":mathematical_program",
":mosek_solver",
],
)

drake_cc_googletest(
name = "nlopt_solver_test",
srcs = [
Expand Down
8 changes: 8 additions & 0 deletions solvers/gurobi_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <stdexcept>
#include <vector>
Expand Down Expand Up @@ -608,6 +609,13 @@ SolutionResult GurobiSolver::Solve(MathematicalProgram& prog) const {
// We only process quadratic costs and linear / bounding box
// constraints.

const char* grb_license_file = std::getenv("GRB_LICENSE_FILE");
if (grb_license_file == nullptr) {
throw std::runtime_error(
"Could not locate Gurobi license key file because GRB_LICENSE_FILE "
"environment variable was not set.");
}

GRBenv* env = nullptr;
GRBloadenv(&env, nullptr);

Expand Down
8 changes: 8 additions & 0 deletions solvers/mosek_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <list>
#include <stdexcept>
Expand Down Expand Up @@ -588,6 +589,13 @@ MSKrescodee SpecifyVariableType(const MathematicalProgram& prog,
class MosekSolver::License {
public:
License() {
const char* moseklm_license_file = std::getenv("MOSEKLM_LICENSE_FILE");
if (moseklm_license_file == nullptr) {
throw std::runtime_error(
"Could not locate MOSEK license file because MOSEKLM_LICENSE_FILE "
"environment variable was not set.");
}

MSKrescodee rescode = MSK_makeenv(&mosek_env_, nullptr);
if (rescode != MSK_RES_OK) {
throw std::runtime_error("Could not create MOSEK environment.");
Expand Down
58 changes: 58 additions & 0 deletions solvers/test/gurobi_solver_grb_license_file_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <cstdlib>
#include <stdexcept>

#include <gtest/gtest.h>

#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/mathematical_program.h"

namespace drake {
namespace solvers {
namespace {

// These tests are deliberately not in gurobi_solver_test.cc to avoid causing
// license issues during tests in that file.

GTEST_TEST(GrbLicenseFileTest, GrbLicenseFileSet) {
const char* grb_license_file = std::getenv("GRB_LICENSE_FILE");
ASSERT_STRNE(nullptr, grb_license_file);

MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external Gurobi solver.
const auto x = program.NewContinuousVariables<1>();
GurobiSolver solver;

EXPECT_NO_THROW(solver.Solve(program));
}

GTEST_TEST(GrbLicenseFileTest, GrbLicenseFileUnset) {
const char* grb_license_file = std::getenv("GRB_LICENSE_FILE");
ASSERT_STRNE(nullptr, grb_license_file);

const int unsetenv_result = ::unsetenv("GRB_LICENSE_FILE");
ASSERT_EQ(0, unsetenv_result);

MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external Gurobi solver.
const auto x = program.NewContinuousVariables<1>();
GurobiSolver solver;

try {
solver.Solve(program);
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
} catch (const std::runtime_error& err) {
EXPECT_EQ(err.what(), std::string("Could not locate Gurobi license key "
"file because GRB_LICENSE_FILE environment variable was not set."));
} catch (...) {
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
}

const int setenv_result = ::setenv("GRB_LICENSE_FILE", grb_license_file, 1);
ASSERT_EQ(0, setenv_result);
}

} // namespace
} // namespace solvers
} // namespace drake
59 changes: 59 additions & 0 deletions solvers/test/mosek_solver_moseklm_license_file_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <cstdlib>
#include <stdexcept>

#include <gtest/gtest.h>

#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/mosek_solver.h"

namespace drake {
namespace solvers {
namespace {

// These tests is deliberately not in mosek_solver_test.cc to avoid causing
// license issues during tests in that file.

GTEST_TEST(MoseklmLicenseFileTest, MoseklmLicenseFileSet) {
const char* moseklm_license_file = std::getenv("MOSEKLM_LICENSE_FILE");
ASSERT_STRNE(nullptr, moseklm_license_file);

MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external MOSEK solver.
const auto x = program.NewContinuousVariables<1>();
MosekSolver solver;

EXPECT_NO_THROW(solver.Solve(program));
}

GTEST_TEST(MoseklmLicenseFileTest, MoseklmLicenseFileUnset) {
const char* moseklm_license_file = std::getenv("MOSEKLM_LICENSE_FILE");
ASSERT_STRNE(nullptr, moseklm_license_file);

const int unsetenv_result = ::unsetenv("MOSEKLM_LICENSE_FILE");
ASSERT_EQ(0, unsetenv_result);

MathematicalProgram program;
// Add a variable to avoid the "Solve" function terminating without calling
// the external MOSEK solver.
const auto x = program.NewContinuousVariables<1>();
MosekSolver solver;

try {
solver.Solve(program);
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
} catch (const std::runtime_error& err) {
EXPECT_EQ(err.what(), std::string("Could not locate MOSEK license file "
"because MOSEKLM_LICENSE_FILE environment variable was not set."));
} catch (...) {
ADD_FAILURE() << "Expected exception of type std::runtime_error.";
}

const int setenv_result =
::setenv("MOSEKLM_LICENSE_FILE", moseklm_license_file, 1);
ASSERT_EQ(0, setenv_result);
}

} // namespace
} // namespace solvers
} // namespace drake

0 comments on commit aa3511f

Please sign in to comment.