polish gurobi environment code
This commit is contained in:
@@ -9,11 +9,12 @@ cc_library(
|
||||
"environment.h",
|
||||
],
|
||||
deps = [
|
||||
"//ortools/base",
|
||||
"//ortools/base:dynamic_library",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/statusor",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/synchronization",
|
||||
"@com_google_absl//absl/types:optional",
|
||||
"//ortools/base:dynamic_library",
|
||||
"//ortools/base",
|
||||
]
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
@@ -25,14 +25,15 @@ namespace operations_research {
|
||||
|
||||
bool GurobiIsCorrectlyInstalled(
|
||||
const std::string& gurobi_shared_library_full_path) {
|
||||
GRBenv* env;
|
||||
if (!LoadGurobiEnvironment(&env, gurobi_shared_library_full_path).ok()) {
|
||||
absl::StatusOr<GRBenv*> status =
|
||||
LoadGurobiEnvironment(gurobi_shared_library_full_path);
|
||||
if (!status.ok()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (env == nullptr) return false;
|
||||
if (status.value() == nullptr) return false;
|
||||
|
||||
GRBfreeenv(env);
|
||||
GRBfreeenv(status.value());
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -280,9 +281,10 @@ bool LoadGurobiSharedLibrary(
|
||||
|
||||
} // namespace
|
||||
|
||||
absl::Status LoadGurobiEnvironment(
|
||||
GRBenv** env, const std::string& gurobi_shared_library_full_path) {
|
||||
absl::StatusOr<GRBenv*> LoadGurobiEnvironment(
|
||||
const std::string& gurobi_shared_library_full_path) {
|
||||
constexpr int GRB_OK = 0;
|
||||
GRBenv* env = nullptr;
|
||||
const char kGurobiEnvErrorMsg[] =
|
||||
"Could not load Gurobi environment. Is gurobi correctly installed and"
|
||||
" licensed on this machine ? ";
|
||||
@@ -294,10 +296,10 @@ absl::Status LoadGurobiEnvironment(
|
||||
return absl::FailedPreconditionError(kGurobiEnvNoSharedLibraryMsg);
|
||||
}
|
||||
|
||||
if (GRBloadenv(env, nullptr) != 0 || *env == nullptr) {
|
||||
if (GRBloadenv(&env, nullptr) != 0 || env == nullptr) {
|
||||
return absl::FailedPreconditionError(
|
||||
absl::StrFormat("%s %s", kGurobiEnvErrorMsg, GRBgeterrormsg(*env)));
|
||||
absl::StrFormat("%s %s", kGurobiEnvErrorMsg, GRBgeterrormsg(env)));
|
||||
}
|
||||
return absl::OkStatus();
|
||||
return env;
|
||||
}
|
||||
} // namespace operations_research
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#define OR_TOOLS_GUROBI_ENVIRONMENT_H_
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "ortools/base/commandlineflags.h"
|
||||
#include "ortools/base/dynamic_library.h"
|
||||
#include "ortools/base/logging.h"
|
||||
@@ -36,11 +37,11 @@ namespace operations_research {
|
||||
// successfully created and assigned to 'env'.
|
||||
// You can add the full path for the shared library by passing the
|
||||
// 'gurobi_shared_library_full_path' argument.
|
||||
absl::Status LoadGurobiEnvironment(
|
||||
GRBenv** env, const std::string& gurobi_shared_library_full_path);
|
||||
absl::StatusOr<GRBenv*> LoadGurobiEnvironment(
|
||||
const std::string& gurobi_shared_library_full_path);
|
||||
|
||||
// Checks the that Gurobi is correctly installed, and that licenses can be
|
||||
// obtained. This method will attempt grab a license, and then release it.
|
||||
// obtained. This method will try to grab a license, and then release it.
|
||||
bool GurobiIsCorrectlyInstalled(
|
||||
const std::string& gurobi_shared_library_full_path);
|
||||
|
||||
|
||||
@@ -612,7 +612,10 @@ GurobiInterface::GurobiInterface(MPSolver* const solver, bool mip)
|
||||
env_(nullptr),
|
||||
mip_(mip),
|
||||
current_solution_index_(0) {
|
||||
CHECK_OK(LoadGurobiEnvironment(&env_, GurobiSharedLibraryFullPath()));
|
||||
const absl::StatusOr<GRBenv*> status =
|
||||
LoadGurobiEnvironment(GurobiSharedLibraryFullPath());
|
||||
CHECK_OK(status.status());
|
||||
env_ = status.value();
|
||||
CheckedGurobiCall(GRBnewmodel(env_, &model_, solver_->name_.c_str(),
|
||||
0, // numvars
|
||||
nullptr, // obj
|
||||
|
||||
@@ -283,8 +283,8 @@ absl::StatusOr<MPSolutionResponse> GurobiSolveProto(
|
||||
// `LoadGurobiEnvironment()` since this function still returns a non null
|
||||
// value even when it fails.
|
||||
gurobi_env_was_created = true;
|
||||
RETURN_IF_ERROR(
|
||||
LoadGurobiEnvironment(&gurobi_env, GurobiSharedLibraryFullPath()));
|
||||
ASSIGN_OR_RETURN(gurobi_env,
|
||||
LoadGurobiEnvironment(GurobiSharedLibraryFullPath()));
|
||||
}
|
||||
|
||||
GRBmodel* gurobi_model = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user