Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
visitor.cc
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#include <cstddef>
15#include <cstdint>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "absl/container/flat_hash_map.h"
21#include "absl/log/check.h"
26
27namespace operations_research {
28// ---------- ArgumentHolder ----------
29
30const std::string& ArgumentHolder::TypeName() const { return type_name_; }
31
32void ArgumentHolder::SetTypeName(const std::string& type_name) {
33 type_name_ = type_name;
34}
35
36void ArgumentHolder::SetIntegerArgument(const std::string& arg_name,
37 int64_t value) {
38 integer_argument_[arg_name] = value;
39}
40
42 const std::string& arg_name, const std::vector<int64_t>& values) {
43 integer_array_argument_[arg_name] = values;
44}
45
46void ArgumentHolder::SetIntegerMatrixArgument(const std::string& arg_name,
47 const IntTupleSet& values) {
48 std::pair<std::string, IntTupleSet> to_insert =
49 std::make_pair(arg_name, values);
50 matrix_argument_.insert(to_insert);
51}
52
53void ArgumentHolder::SetIntegerExpressionArgument(const std::string& arg_name,
54 IntExpr* const expr) {
55 integer_expression_argument_[arg_name] = expr;
56}
57
59 const std::string& arg_name, const std::vector<IntVar*>& vars) {
60 integer_variable_array_argument_[arg_name] = vars;
61}
62
63void ArgumentHolder::SetIntervalArgument(const std::string& arg_name,
64 IntervalVar* const var) {
65 interval_argument_[arg_name] = var;
66}
67
69 const std::string& arg_name, const std::vector<IntervalVar*>& vars) {
70 interval_array_argument_[arg_name] = vars;
71}
72
73void ArgumentHolder::SetSequenceArgument(const std::string& arg_name,
74 SequenceVar* const var) {
75 sequence_argument_[arg_name] = var;
76}
77
79 const std::string& arg_name, const std::vector<SequenceVar*>& vars) {
80 sequence_array_argument_[arg_name] = vars;
81}
82
84 const std::string& arg_name) const {
85 return integer_expression_argument_.contains(arg_name);
86}
87
89 const std::string& arg_name) const {
90 return integer_variable_array_argument_.contains(arg_name);
91}
92
94 const std::string& arg_name, int64_t def) const {
95 return gtl::FindWithDefault(integer_argument_, arg_name, def);
96}
97
99 const std::string& arg_name) const {
100 return gtl::FindOrDie(integer_argument_, arg_name);
101}
102
104 const std::string& arg_name) const {
105 return gtl::FindOrDie(integer_array_argument_, arg_name);
106}
107
109 const std::string& arg_name) const {
110 return gtl::FindOrDie(integer_expression_argument_, arg_name);
111}
112
113const std::vector<IntVar*>&
115 const std::string& arg_name) const {
116 return gtl::FindOrDie(integer_variable_array_argument_, arg_name);
117}
118
120 const std::string& arg_name) const {
121 return gtl::FindOrDie(matrix_argument_, arg_name);
122}
123
124// ---------- ModelParser ---------
125
127
128ModelParser::~ModelParser() { CHECK(holders_.empty()); }
129
130void ModelParser::BeginVisitModel(const std::string& solver_name) {
132}
133
134void ModelParser::EndVisitModel(const std::string& solver_name) {
136}
137
138void ModelParser::BeginVisitConstraint(const std::string& type_name,
139 const Constraint* const constraint) {
141}
142
143void ModelParser::EndVisitConstraint(const std::string& type_name,
144 const Constraint* const constraint) {
145 // Constraint parsing is usually done here.
147}
148
149void ModelParser::BeginVisitIntegerExpression(const std::string& type_name,
150 const IntExpr* const expr) {
152}
153
154void ModelParser::EndVisitIntegerExpression(const std::string& type_name,
155 const IntExpr* const expr) {
156 // Expression parsing is usually done here.
158}
159
160void ModelParser::VisitIntegerVariable(const IntVar* const variable,
161 IntExpr* const delegate) {
162 // Usual place for parsing.
163}
164
165void ModelParser::VisitIntegerVariable(const IntVar* const variable,
166 const std::string& operation,
167 int64_t value, IntVar* const delegate) {
168 delegate->Accept(this);
169 // Usual place for parsing.
170}
171
173 const std::string& operation,
174 int64_t value,
175 IntervalVar* const delegate) {
176 if (delegate != nullptr) {
177 delegate->Accept(this);
178 }
179 // Usual place for parsing.
180}
181
183 // Usual place for parsing.
184}
185
186// Integer arguments
187void ModelParser::VisitIntegerArgument(const std::string& arg_name,
188 int64_t value) {
189 Top()->SetIntegerArgument(arg_name, value);
190}
191
193 const std::string& arg_name, const std::vector<int64_t>& values) {
194 Top()->SetIntegerArrayArgument(arg_name, values);
195}
196
197void ModelParser::VisitIntegerMatrixArgument(const std::string& arg_name,
198 const IntTupleSet& values) {
199 Top()->SetIntegerMatrixArgument(arg_name, values);
200}
201
202// Variables.
203void ModelParser::VisitIntegerExpressionArgument(const std::string& arg_name,
204 IntExpr* const argument) {
205 Top()->SetIntegerExpressionArgument(arg_name, argument);
206 argument->Accept(this);
207}
208
210 const std::string& arg_name, const std::vector<IntVar*>& arguments) {
211 Top()->SetIntegerVariableArrayArgument(arg_name, arguments);
212 for (int i = 0; i < arguments.size(); ++i) {
213 arguments[i]->Accept(this);
214 }
215}
216
217// Visit interval argument.
218void ModelParser::VisitIntervalArgument(const std::string& arg_name,
219 IntervalVar* const argument) {
220 Top()->SetIntervalArgument(arg_name, argument);
221 argument->Accept(this);
222}
223
225 const std::string& arg_name, const std::vector<IntervalVar*>& arguments) {
226 Top()->SetIntervalArrayArgument(arg_name, arguments);
227 for (int i = 0; i < arguments.size(); ++i) {
228 arguments[i]->Accept(this);
229 }
230}
231
232// Visit sequence argument.
233void ModelParser::VisitSequenceArgument(const std::string& arg_name,
234 SequenceVar* const argument) {
235 Top()->SetSequenceArgument(arg_name, argument);
236 argument->Accept(this);
237}
238
240 const std::string& arg_name, const std::vector<SequenceVar*>& arguments) {
241 Top()->SetSequenceArrayArgument(arg_name, arguments);
242 for (int i = 0; i < arguments.size(); ++i) {
243 arguments[i]->Accept(this);
244 }
245}
246
248 holders_.push_back(new ArgumentHolder);
249}
250
252 CHECK(!holders_.empty());
253 delete holders_.back();
254 holders_.pop_back();
255}
256
258 CHECK(!holders_.empty());
259 return holders_.back();
260}
261} // namespace operations_research
Argument Holder: useful when visiting a model.
const IntTupleSet & FindIntegerMatrixArgumentOrDie(const std::string &arg_name) const
Definition visitor.cc:119
void SetIntegerVariableArrayArgument(const std::string &arg_name, const std::vector< IntVar * > &vars)
Definition visitor.cc:58
void SetIntervalArgument(const std::string &arg_name, IntervalVar *var)
Definition visitor.cc:63
void SetIntervalArrayArgument(const std::string &arg_name, const std::vector< IntervalVar * > &vars)
Definition visitor.cc:68
IntExpr * FindIntegerExpressionArgumentOrDie(const std::string &arg_name) const
Definition visitor.cc:108
void SetTypeName(const std::string &type_name)
Definition visitor.cc:32
bool HasIntegerExpressionArgument(const std::string &arg_name) const
Checks if arguments exist.
Definition visitor.cc:83
void SetIntegerArrayArgument(const std::string &arg_name, const std::vector< int64_t > &values)
Definition visitor.cc:41
const std::vector< IntVar * > & FindIntegerVariableArrayArgumentOrDie(const std::string &arg_name) const
Definition visitor.cc:114
void SetSequenceArgument(const std::string &arg_name, SequenceVar *var)
Definition visitor.cc:73
const std::string & TypeName() const
Type of the argument.
Definition visitor.cc:30
void SetIntegerMatrixArgument(const std::string &arg_name, const IntTupleSet &values)
Definition visitor.cc:46
bool HasIntegerVariableArrayArgument(const std::string &arg_name) const
Definition visitor.cc:88
const std::vector< int64_t > & FindIntegerArrayArgumentOrDie(const std::string &arg_name) const
Definition visitor.cc:103
int64_t FindIntegerArgumentOrDie(const std::string &arg_name) const
Definition visitor.cc:98
void SetIntegerArgument(const std::string &arg_name, int64_t value)
Setters.
Definition visitor.cc:36
void SetIntegerExpressionArgument(const std::string &arg_name, IntExpr *expr)
Definition visitor.cc:53
void SetSequenceArrayArgument(const std::string &arg_name, const std::vector< SequenceVar * > &vars)
Definition visitor.cc:78
int64_t FindIntegerArgumentWithDefault(const std::string &arg_name, int64_t def) const
Getters.
Definition visitor.cc:93
virtual void Accept(ModelVisitor *visitor) const
Accepts the given visitor.
--— Main IntTupleSet class --—
Definition tuple_set.h:47
void Accept(ModelVisitor *visitor) const override
Accepts the given visitor.
virtual void Accept(ModelVisitor *visitor) const =0
Accepts the given visitor.
void VisitIntervalArgument(const std::string &arg_name, IntervalVar *argument) override
Visit interval argument.
Definition visitor.cc:218
void VisitIntervalArrayArgument(const std::string &arg_name, const std::vector< IntervalVar * > &arguments) override
Definition visitor.cc:224
void VisitIntervalVariable(const IntervalVar *variable, const std::string &operation, int64_t value, IntervalVar *delegate) override
Definition visitor.cc:172
void VisitIntegerArgument(const std::string &arg_name, int64_t value) override
Integer arguments.
Definition visitor.cc:187
void VisitIntegerArrayArgument(const std::string &arg_name, const std::vector< int64_t > &values) override
Definition visitor.cc:192
void VisitSequenceVariable(const SequenceVar *variable) override
Definition visitor.cc:182
void BeginVisitIntegerExpression(const std::string &type_name, const IntExpr *expr) override
Definition visitor.cc:149
void VisitSequenceArgument(const std::string &arg_name, SequenceVar *argument) override
Visit sequence argument.
Definition visitor.cc:233
void VisitSequenceArrayArgument(const std::string &arg_name, const std::vector< SequenceVar * > &arguments) override
Definition visitor.cc:239
void VisitIntegerVariable(const IntVar *variable, IntExpr *delegate) override
Definition visitor.cc:160
void VisitIntegerVariableArrayArgument(const std::string &arg_name, const std::vector< IntVar * > &arguments) override
Definition visitor.cc:209
ModelParser()
-------— ModelParser ------—
Definition visitor.cc:126
void BeginVisitModel(const std::string &solver_name) override
Header/footers.
Definition visitor.cc:130
void BeginVisitConstraint(const std::string &type_name, const Constraint *constraint) override
Definition visitor.cc:138
void EndVisitConstraint(const std::string &type_name, const Constraint *constraint) override
Definition visitor.cc:143
ArgumentHolder * Top() const
Definition visitor.cc:257
void VisitIntegerMatrixArgument(const std::string &arg_name, const IntTupleSet &values) override
Definition visitor.cc:197
void EndVisitIntegerExpression(const std::string &type_name, const IntExpr *expr) override
Definition visitor.cc:154
void EndVisitModel(const std::string &solver_name) override
Definition visitor.cc:134
void VisitIntegerExpressionArgument(const std::string &arg_name, IntExpr *argument) override
Variables.
Definition visitor.cc:203
virtual void Accept(ModelVisitor *visitor) const
Accepts the given visitor.
int64_t value
IntVar * var
const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:211
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
Definition map_util.h:36
In SWIG mode, we don't want anything besides these top-level includes.