OR-Tools  9.3
gurobi_init_arguments.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_INIT_ARGUMENTS_H_
15#define OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_INIT_ARGUMENTS_H_
16
17#include <memory>
18#include <optional>
19
20#include "absl/status/statusor.h"
22#include "ortools/math_opt/parameters.pb.h"
23#include "ortools/math_opt/solvers/gurobi.pb.h"
25
26namespace operations_research {
27namespace math_opt {
28
29// Returns a new master environment.
30//
31// The typical use of this function is to share the same environment between
32// multiple solver instances. This is necessary when a single-use license is
33// used since only one master environment can exists in that case.
34//
35// A single master environment is not thread-safe and thus it should only be
36// used in a single thread. Even if the user has a license that authorizes
37// multiple master environments, Gurobi still recommends to use only one and to
38// share it as it is more efficient (see GRBloadenv() documentation).
39//
40// Of course, if the user wants to run multiple solves in parallel and has a
41// license that authorizes that, one environment should be used per thread.
42//
43// The master environment can be passed to MathOpt via the
44// NonStreamableGurobiInitArguments structure and its master_env field.
45//
46// The optional ISV key can be used to build the environment from an ISV key
47// instead of using the default license file. See
48// http://www.gurobi.com/products/licensing-pricing/isv-program for details.
49//
50// Example with default license file:
51//
52// // Solving two models on the same thread, sharing the same master
53// // environment.
54// Model model_1;
55// Model model_2;
56//
57// ...
58//
59// ASSIGN_OR_RETURN(const GRBenvUniquePtr master_env,
60// NewMasterEnvironment());
61//
62// NonStreamableGurobiInitArguments gurobi_args;
63// gurobi_args.master_env = master_env.get();
64//
65// ASSIGN_OR_RETURN(
66// const std::unique_ptr<IncrementalSolver> incremental_solve_1,
67// IncrementalSolver::New(model, SOLVER_TYPE_GUROBI,
68// SolverInitArguments(gurobi_args)));
69// ASSIGN_OR_RETURN(
70// const std::unique_ptr<IncrementalSolver> incremental_solve_2,
71// IncrementalSolver::New(model, SOLVER_TYPE_GUROBI,
72// SolverInitArguments(gurobi_args)));
73//
74// ASSIGN_OR_RETURN(const SolveResult result_1, incremental_solve_1->Solve());
75// ASSIGN_OR_RETURN(const SolveResult result_2, incremental_solve_2->Solve());
76//
77//
78// With ISV key:
79//
80// ASSIGN_OR_RETURN(const GRBenvUniquePtr master_env,
81// NewMasterEnvironment(GurobiISVKey{
82// .name = "the name",
83// .application_name = "the application",
84// .expiration = 0,
85// .key = "...",
86// }.Proto()));
87//
88absl::StatusOr<GRBenvUniquePtr> NewMasterEnvironment(
89 std::optional<GurobiInitializerProto::ISVKey> proto_isv_key = {});
90
91// Non-streamable Gurobi specific parameters for solver instantiation.
92//
93// See NonStreamableSolverInitArguments for details.
96 NonStreamableGurobiInitArguments, SOLVER_TYPE_GUROBI> {
97 // Master environment to use. This is only useful to pass when either the
98 // default master environment created by the solver implementation is not
99 // enough or when multiple Gurobi solvers are used with a single-use
100 // license. In the latter case, only one master environment can be created so
101 // it must be shared.
102 //
103 // The solver does not take ownership of the environment, it is the
104 // responsibility of the caller to properly dispose of it after all solvers
105 // that used it have been destroyed.
106 GRBenv* master_env = nullptr;
107
109 const override {
110 return this;
111 }
112};
113
114} // namespace math_opt
115} // namespace operations_research
116
117#endif // OR_TOOLS_MATH_OPT_SOLVERS_GUROBI_INIT_ARGUMENTS_H_
struct _GRBenv GRBenv
Definition: environment.h:25
absl::StatusOr< GRBenvUniquePtr > NewMasterEnvironment(std::optional< GurobiInitializerProto::ISVKey > proto_isv_key)
Collection of objects used to extend the Constraint Solver library.
const NonStreamableGurobiInitArguments * ToNonStreamableGurobiInitArguments() const override