Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
non_streamable_solver_init_arguments.h
Go to the documentation of this file.
1// Copyright 2010-2024 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_CORE_NON_STREAMABLE_SOLVER_INIT_ARGUMENTS_H_
15#define OR_TOOLS_MATH_OPT_CORE_NON_STREAMABLE_SOLVER_INIT_ARGUMENTS_H_
16
17#include <memory>
18
19#include "ortools/math_opt/parameters.pb.h"
20
21namespace operations_research {
22namespace math_opt {
23
24struct NonStreamableCpSatInitArguments;
25struct NonStreamableGScipInitArguments;
26struct NonStreamableGlopInitArguments;
27struct NonStreamableGlpkInitArguments;
28struct NonStreamableGurobiInitArguments;
29struct NonStreamablePdlpInitArguments;
30
31// Interface for solver specific parameters used at the solver instantiation
32// that can't be streamed (for example instances of C/C++ types that only exist
33// in the process memory).
34//
35// Since implementations of this interface usually depend on solver specific
36// C/C++ types, they are in a dedicated header in the solver library.
37//
38// This class is the interface shared by the parameters of each solver, users
39// should instantiate the solver specific class below.
40//
41// To enable safe cast of a pointer to this interface, there is an
42// ToNonStreamableXxxInitArguments() function for each solver. Only one of these
43// function will return a non-null value, depending on the type of the
44// implementation class.
45//
46// Implementation should use NonStreamableSolverInitArgumentsHelper to
47// automatically implements some methods.
50
51 // Returns the type of solver that the implementation is for.
52 virtual SolverTypeProto solver_type() const = 0;
53
54 // Returns this for the NonStreamableCpSatInitArguments class, nullptr for
55 // other classes.
56 virtual const NonStreamableCpSatInitArguments*
58 return nullptr;
59 }
60
61 // Returns this for the NonStreamableGScipInitArguments class, nullptr for
62 // other classes.
63 virtual const NonStreamableGScipInitArguments*
65 return nullptr;
66 }
67
68 // Returns this for the NonStreamableGlopInitArguments class, nullptr for
69 // other classes.
70 virtual const NonStreamableGlopInitArguments*
72 return nullptr;
73 }
74
75 // Returns this for the NonStreamableGlpkInitArguments class, nullptr for
76 // other classes.
77 virtual const NonStreamableGlpkInitArguments*
79 return nullptr;
80 }
81
82 // Returns this for the NonStreamableGurobiInitArguments class, nullptr for
83 // other classes.
86 return nullptr;
87 }
88
89 // Returns this for the NonStreamablePdlpInitArguments class, nullptr for
90 // other classes.
91 virtual const NonStreamablePdlpInitArguments*
93 return nullptr;
94 }
95
96 // Return a copy of this.
97 //
98 // The NonStreamableSolverInitArgumentsHelper implements this automatically
99 // using the copy constructor (this base class is copyable intentionally).
100 virtual std::unique_ptr<const NonStreamableSolverInitArguments> Clone()
101 const = 0;
102};
103
104// Base struct for implementations that automatically implements solver_type()
105// and Clone() virtual methods.
106//
107// The Clone() method is implemented with the copy constructor of the struct.
108//
109// All that is left to the implementation is to provide are the solver specific
110// field and the implementation of the ToNonStreamableXxxInitArguments()
111// corresponding to the solver type.
112//
113// Usage:
114//
115// struct NonStreamableXxxInitArguments
116// : public NonStreamableSolverInitArgumentsHelper<
117// NonStreamableXxxInitArguments, SOLVER_TYPE_XXX> {
118//
119// ... some data member here ...
120//
121// const NonStreamableXxxInitArguments*
122// ToNonStreamableXxxInitArguments() const { return this; }
123// };
124template <typename Implementation, SolverTypeProto impl_solver_type>
127 SolverTypeProto solver_type() const final { return impl_solver_type; }
129 std::unique_ptr<const NonStreamableSolverInitArguments> Clone() const final {
130 return std::make_unique<Implementation>(
131 *static_cast<const Implementation*>(this));
132 }
133};
134
135// Value-like class holding an optional NonStreamableSolverInitArguments. On
136// copy it clones it.
137//
138// NonStreamableSolverInitArgumentsValue::get() gives access the pointed
139// arguments. The implicit constructor from NonStreamableSolverInitArguments is
140// used to build new instance.
141class NonStreamableSolverInitArgumentsValue {
142 public:
143 // Initializes with no value.
146 // Initializes with the provided value, cloning it.
148 const NonStreamableSolverInitArguments& non_streamable);
149
150 // Clones other.get() if not nullptr.
153
154 // Clones other.get() if not nullptr.
157
158 // Steals other.get() and resets it to nullptr.
161
162 // Steals other.get() and resets it to nullptr.
165
166 // Return a pointer on the value; nullptr if unset (default value).
168 return non_streamable_.get();
169 }
170
171 private:
172 // The pointed value.
173 std::unique_ptr<const NonStreamableSolverInitArguments> non_streamable_;
174};
175
176} // namespace math_opt
177} // namespace operations_research
178
179#endif // OR_TOOLS_MATH_OPT_CORE_NON_STREAMABLE_SOLVER_INIT_ARGUMENTS_H_
NonStreamableSolverInitArgumentsValue & operator=(const NonStreamableSolverInitArgumentsValue &other)
Clones other.get() if not nullptr.
NonStreamableSolverInitArgumentsValue()=default
Initializes with no value.
const NonStreamableSolverInitArguments * get() const
Return a pointer on the value; nullptr if unset (default value).
In SWIG mode, we don't want anything besides these top-level includes.
std::unique_ptr< const NonStreamableSolverInitArguments > Clone() const final
SolverTypeProto solver_type() const final
Returns the type of solver that the implementation is for.
virtual const NonStreamableGlpkInitArguments * ToNonStreamableGlpkInitArguments() const
virtual const NonStreamableGurobiInitArguments * ToNonStreamableGurobiInitArguments() const
virtual const NonStreamableGlopInitArguments * ToNonStreamableGlopInitArguments() const
virtual const NonStreamablePdlpInitArguments * ToNonStreamablePdlpInitArguments() const
virtual std::unique_ptr< const NonStreamableSolverInitArguments > Clone() const =0
virtual const NonStreamableCpSatInitArguments * ToNonStreamableCpSatInitArguments() const
virtual SolverTypeProto solver_type() const =0
Returns the type of solver that the implementation is for.
virtual const NonStreamableGScipInitArguments * ToNonStreamableGScipInitArguments() const