add optional template; use it in linear solver; add const in gurobi interface
This commit is contained in:
53
ortools/base/optional.h
Normal file
53
ortools/base/optional.h
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2010-2017 Google
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef OR_TOOLS_BASE_OPTIONAL_H_
|
||||
#define OR_TOOLS_BASE_OPTIONAL_H_
|
||||
|
||||
namespace absl {
|
||||
|
||||
template <class T>
|
||||
class optional {
|
||||
public:
|
||||
optional() : has_(false) {}
|
||||
optional& operator=(const T& t) {
|
||||
has_ = true;
|
||||
data_ = t;
|
||||
}
|
||||
const T& data() const { return data_; }
|
||||
constexpr const T& operator*() const & { return reference(); }
|
||||
T& operator*() & {
|
||||
assert(this->has_);
|
||||
return reference();
|
||||
}
|
||||
constexpr explicit operator bool() const noexcept { return has_; }
|
||||
const T* operator->() const {
|
||||
assert(this->has_);
|
||||
return std::addressof(this->data_);
|
||||
}
|
||||
T* operator->() {
|
||||
assert(this->has_);
|
||||
return std::addressof(this->data_);
|
||||
}
|
||||
|
||||
private:
|
||||
// Private accessors for internal storage viewed as reference to T.
|
||||
constexpr const T& reference() const { return this->data_; }
|
||||
T& reference() { return this->data_; }
|
||||
bool has_;
|
||||
T data_;
|
||||
};
|
||||
|
||||
} // namespace absl
|
||||
|
||||
#endif // OR_TOOLS_BASE_OPTIONAL_H_
|
||||
@@ -185,7 +185,7 @@ GurobiInterface::GurobiInterface(MPSolver* const solver, bool mip)
|
||||
env_(nullptr),
|
||||
mip_(mip),
|
||||
current_solution_index_(0) {
|
||||
int ret = GRBloadenv(&env_, nullptr);
|
||||
const int ret = GRBloadenv(&env_, nullptr);
|
||||
if (ret != 0 || env_ == nullptr) {
|
||||
std::string err_msg = GRBgeterrormsg(env_);
|
||||
LOG(DFATAL) << "Error: could not create environment: " << err_msg;
|
||||
|
||||
@@ -412,8 +412,6 @@ MPSolver::MPSolver(const std::string& name,
|
||||
problem_type_(DetourProblemType(problem_type)),
|
||||
time_limit_(0.0) {
|
||||
timer_.Restart();
|
||||
constraint_name_to_index_.reset(nullptr);
|
||||
variable_name_to_index_.reset(nullptr);
|
||||
interface_.reset(BuildSolverInterface(this));
|
||||
if (FLAGS_linear_solver_enable_verbose_output) {
|
||||
EnableOutput();
|
||||
@@ -1263,7 +1261,7 @@ bool MPSolver::ExportModelAsMpsFormat(bool fixed_format, bool obfuscate,
|
||||
|
||||
void MPSolver::GenerateVariableNameIndex() const {
|
||||
if (variable_name_to_index_) return;
|
||||
variable_name_to_index_.reset(new std::unordered_map<std::string, int>(1));
|
||||
variable_name_to_index_ = std::unordered_map<std::string, int>();
|
||||
for (const MPVariable* const var : variables_) {
|
||||
gtl::InsertOrDie(&*variable_name_to_index_, var->name(), var->index());
|
||||
}
|
||||
@@ -1271,7 +1269,7 @@ void MPSolver::GenerateVariableNameIndex() const {
|
||||
|
||||
void MPSolver::GenerateConstraintNameIndex() const {
|
||||
if (constraint_name_to_index_) return;
|
||||
constraint_name_to_index_.reset(new std::unordered_map<std::string, int>(1));
|
||||
constraint_name_to_index_ = std::unordered_map<std::string, int>();
|
||||
for (const MPConstraint* const cst : constraints_) {
|
||||
gtl::InsertOrDie(&*constraint_name_to_index_, cst->name(), cst->index());
|
||||
}
|
||||
|
||||
@@ -150,6 +150,7 @@
|
||||
#include "ortools/base/integral_types.h"
|
||||
#include "ortools/base/logging.h"
|
||||
#include "ortools/base/macros.h"
|
||||
#include "ortools/base/optional.h"
|
||||
#include "ortools/base/port.h"
|
||||
#include "ortools/base/status.h"
|
||||
#include "ortools/base/stringprintf.h"
|
||||
@@ -637,7 +638,7 @@ class MPSolver {
|
||||
// The vector of variables in the problem.
|
||||
std::vector<MPVariable*> variables_;
|
||||
// A map from a variable's name to its index in variables_.
|
||||
mutable std::unique_ptr<std::unordered_map<std::string, int> >
|
||||
mutable absl::optional<std::unordered_map<std::string, int> >
|
||||
variable_name_to_index_;
|
||||
// Whether variables have been extracted to the underlying interface.
|
||||
std::vector<bool> variable_is_extracted_;
|
||||
@@ -645,7 +646,7 @@ class MPSolver {
|
||||
// The vector of constraints in the problem.
|
||||
std::vector<MPConstraint*> constraints_;
|
||||
// A map from a constraint's name to its index in constraints_.
|
||||
mutable std::unique_ptr<std::unordered_map<std::string, int> >
|
||||
mutable absl::optional<std::unordered_map<std::string, int> >
|
||||
constraint_name_to_index_;
|
||||
// Whether constraints have been extracted to the underlying interface.
|
||||
std::vector<bool> constraint_is_extracted_;
|
||||
|
||||
Reference in New Issue
Block a user