aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
scheduleProjection_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
49
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53# include <agrum/agrum.h>
54
56
57namespace gum {
58
60 template < typename TABLE >
62 const gum::VariableSet& del_vars,
63 TABLE (*project)(const TABLE&,
64 const gum::VariableSet&),
65 const bool is_result_persistent) :
66 ScheduleOperator(ScheduleOperatorType::PROJECT_MULTIDIM, false, is_result_persistent),
67 _arg_(&table), _del_vars_(del_vars), _project_(project) {
68 // compute the variables that shall belong to the result of the projection
69 Sequence< const DiscreteVariable* > vars = table.variablesSequence();
70 for (const auto var: del_vars) {
71 if (vars.exists(var)) vars.erase(var);
72 }
73
74 // create the scheduleMultiDim that should result from the projection
75 // if table is a constant, just copy it
76 if (table.variablesSequence().empty() && !table.isAbstract())
77 _result_ = new ScheduleMultiDim< TABLE >(table.multiDim(), true, Idx(0));
78 else _result_ = new ScheduleMultiDim< TABLE >(vars, Idx(0));
79
80 // save the args and result into _args_ and _results_
81 _args_ << _arg_;
82 _results_ << _result_;
83
84 // for debugging purposes
85 GUM_CONSTRUCTOR(ScheduleProjection);
86 }
87
89 template < typename TABLE >
90 ScheduleProjection< TABLE >::ScheduleProjection(const ScheduleProjection< TABLE >& from) :
91 ScheduleOperator(from), _arg_(from._arg_), _del_vars_(from._del_vars_),
92 _project_(from._project_) {
93 // copy the result of the from operator
94 _result_ = new ScheduleMultiDim< TABLE >(*(from._result_));
95
96 // save the args and result into _args_ and _results_
97 _args_ << _arg_;
98 _results_ << _result_;
99
100 // for debugging purposes
101 GUM_CONS_CPY(ScheduleProjection);
102 }
103
105 template < typename TABLE >
106 ScheduleProjection< TABLE >::ScheduleProjection(ScheduleProjection< TABLE >&& from) :
107 ScheduleOperator(std::move(from)), _arg_(from._arg_), _result_(from._result_),
108 _del_vars_(std::move(from._del_vars_)), _project_(from._project_) {
109 // indicate that from does not contain anything anymore
110 from.makeResultsPersistent(true); // prevent deleting nullptr
111 from._result_ = nullptr;
112
113 // save the args and result into _args_ and _results_
114 _args_ << _arg_;
115 _results_ << _result_;
116
117 // for debugging purposes
118 GUM_CONS_MOV(ScheduleProjection);
119 }
120
122 template < typename TABLE >
123 ScheduleProjection< TABLE >* ScheduleProjection< TABLE >::clone() const {
124 return new ScheduleProjection< TABLE >(*this);
125 }
126
128 template < typename TABLE >
129 ScheduleProjection< TABLE >::~ScheduleProjection() {
130 if (!this->hasPersistentResults()) { delete _result_; }
131
132 // for debugging purposes
133 GUM_DESTRUCTOR(ScheduleProjection);
134 }
135
137 template < typename TABLE >
138 ScheduleProjection< TABLE >&
139 ScheduleProjection< TABLE >::operator=(const ScheduleProjection< TABLE >& from) {
140 // avoid self assignment
141 if (this != &from) {
142 // copy the set of variables to delete in a temporary variable, just
143 // in case something goes wrong below
144 const gum::VariableSet new_del_vars = from._del_vars_;
145
146 // try to copy result (no need to update _results_)
147 *_result_ = *(from._result_);
148 ScheduleOperator::operator=(from);
149
150 _del_vars_ = std::move(new_del_vars);
151 _arg_ = from._arg_;
152 _args_.clear();
153 _args_ << _arg_;
154 _project_ = from._project_;
155 }
156 return *this;
157 }
158
160 template < typename TABLE >
161 ScheduleProjection< TABLE >&
162 ScheduleProjection< TABLE >::operator=(ScheduleProjection< TABLE >&& from) {
163 // avoid self assignment
164 if (this != &from) {
165 if (!this->hasPersistentResults()) delete _result_;
166 _result_ = from._result_;
167 ScheduleOperator::operator=(std::move(from));
168
169 _del_vars_ = std::move(from._del_vars_);
170 _arg_ = from._arg_;
171 _args_.clear();
172 _args_ << _arg_;
173 _project_ = from._project_;
174
175 from.makeResultsPersistent(true); // prevent deleting nullptr
176 from._result_ = nullptr;
177 }
178 return *this;
179 }
180
182 template < typename TABLE >
183 bool ScheduleProjection< TABLE >::operator==(const ScheduleProjection< TABLE >& op) const {
184 return (_project_ == op._project_) && (*_arg_ == *op._arg_) && (_del_vars_ == op._del_vars_);
185 }
186
188 template < typename TABLE >
189 bool ScheduleProjection< TABLE >::operator==(const ScheduleOperator& op) const {
190 if (ScheduleOperator::operator!=(op)) return false;
191
192 try {
193 const ScheduleProjection< TABLE >& real_op
194 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
195 return ScheduleProjection< TABLE >::operator==(real_op);
196 } catch (std::bad_cast&) { return false; }
197 }
198
200 template < typename TABLE >
201 bool ScheduleProjection< TABLE >::operator!=(const ScheduleOperator& op) const {
202 return !ScheduleProjection< TABLE >::operator==(op);
203 }
204
206 template < typename TABLE >
207 bool ScheduleProjection< TABLE >::operator!=(const ScheduleProjection< TABLE >& op) const {
208 return !ScheduleProjection< TABLE >::operator==(op);
209 }
210
212 template < typename TABLE >
213 bool ScheduleProjection< TABLE >::hasSimilarArguments(
214 const ScheduleProjection< TABLE >& op) const {
215 return (_arg_->hasSameVariables(*op._arg_) && (_del_vars_ == op._del_vars_));
216 }
217
219 template < typename TABLE >
220 bool ScheduleProjection< TABLE >::hasSimilarArguments(const ScheduleOperator& op) const {
221 try {
222 const ScheduleProjection< TABLE >& real_op
223 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
224 return ScheduleProjection< TABLE >::hasSimilarArguments(real_op);
225 } catch (std::bad_cast&) { return false; }
226 }
227
229 template < typename TABLE >
230 bool ScheduleProjection< TABLE >::hasSameArguments(const ScheduleProjection< TABLE >& op) const {
231 return (_arg_->hasSameVariables(*op._arg_) && _arg_->hasSameContent(*op._arg_)
232 && (_del_vars_ == op._del_vars_));
233 }
234
236 template < typename TABLE >
237 bool ScheduleProjection< TABLE >::hasSameArguments(const ScheduleOperator& op) const {
238 try {
239 const ScheduleProjection< TABLE >& real_op
240 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
241 return ScheduleProjection< TABLE >::hasSameArguments(real_op);
242 } catch (std::bad_cast&) { return false; }
243 }
244
246 template < typename TABLE >
247 bool ScheduleProjection< TABLE >::isSameOperator(const ScheduleProjection< TABLE >& op) const {
248 return _project_ == op._project_;
249 }
250
252 template < typename TABLE >
253 bool ScheduleProjection< TABLE >::isSameOperator(const ScheduleOperator& op) const {
254 try {
255 const ScheduleProjection< TABLE >& real_op
256 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
257 return ScheduleProjection< TABLE >::isSameOperator(real_op);
258 } catch (std::bad_cast&) { return false; }
259 }
260
262 template < typename TABLE >
263 const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::arg() const {
264 return *_arg_;
265 }
266
268 template < typename TABLE >
269 const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::args() const {
270 return _args_;
271 }
272
274 template < typename TABLE >
275 const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::result() const {
276 return *_result_;
277 }
278
280 template < typename TABLE >
281 const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::results() const {
282 return _results_;
283 }
284
286 template < typename TABLE >
287 void ScheduleProjection< TABLE >::updateArgs(
288 const Sequence< const IScheduleMultiDim* >& new_args) {
289 // check that there is exactly one argument in new_args and that its type
290 // is compatible with TABLE
291 if (new_args.size() != Size(1)) {
293 "Method ScheduleProjection::updateArgs expects 1 new "
294 << "argument, but " << new_args.size() << " were passed.");
295 }
296 const ScheduleMultiDim< TABLE >* new_table;
297 try {
298 new_table = dynamic_cast< const ScheduleMultiDim< TABLE >* >(new_args[0]);
299 } catch (std::bad_cast&) {
301 "The type of the argument passed to "
302 << "ScheduleProjection::updateArgs does not match what "
303 << "the ScheduleOperator expects");
304 }
305
306 // if the new table is a constant, just copy it
307 if (new_table->variablesSequence().empty() && !new_table->isAbstract()) {
308 *_result_ = std::move(ScheduleMultiDim< TABLE >(new_table->multiDim(), true, _result_->id()));
309 } else {
310 // get the variables remaining after the projection
311 Sequence< const DiscreteVariable* > vars = new_table->variablesSequence();
312 for (const auto var: _del_vars_) {
313 if (vars.exists(var)) vars.erase(var);
314 }
315
316 *_result_ = std::move(ScheduleMultiDim< TABLE >(vars, _result_->id()));
317 }
318
319 _arg_ = new_table;
320 _args_.clear();
321 _args_ << _arg_;
322 }
323
325 template < typename TABLE >
326 bool ScheduleProjection< TABLE >::isExecuted() const {
327 return !_result_->isAbstract();
328 }
329
331 template < typename TABLE >
332 void ScheduleProjection< TABLE >::execute() {
333 if (_result_->isAbstract()) {
334 const TABLE& tab = _arg_->multiDim();
335 if (_arg_->domainSize() > 1) {
336 TABLE res = _project_(tab, _del_vars_);
337 _result_->setMultiDim(std::move(res));
338 } else {
339 _result_->setMultiDim(tab, true);
340 }
341 }
342 }
343
345 template < typename TABLE >
346 void ScheduleProjection< TABLE >::undo() {
347 _result_->makeAbstract();
348 }
349
352 template < typename TABLE >
353 double ScheduleProjection< TABLE >::nbOperations() const {
354 return double(_arg_->domainSize());
355 }
356
358 template < typename TABLE >
359 std::pair< double, double > ScheduleProjection< TABLE >::memoryUsage() const {
360 const double domsize
361 = double(_result_->domainSize()) * _result_->sizeOfContent() + sizeof(TABLE);
362 return {domsize, domsize};
363 }
364
366 template < typename TABLE >
367 std::string ScheduleProjection< TABLE >::toString() const {
368 return _result_->toString() + " = project ( " + _arg_->toString() + " , "
369 + _del_vars_.toString() + " )";
370 }
371
373 template < typename TABLE >
374 void ScheduleProjection< TABLE >::setProjectionFunction(
375 TABLE (*project)(const TABLE&, const gum::VariableSet&)) {
376 _project_ = project;
377 _result_->makeAbstract();
378 }
379
380
381} /* namespace gum */
382
383#endif /* DOXYGEN_SHOULD_SKIP_THIS */
a Wrapper for multi-dimensional tables used for scheduling inferences
the base class for "low-level" operators used to schedule inferences
ScheduleProjection(const ScheduleMultiDim< TABLE > &table, const gum::VariableSet &del_vars, TABLE(*project)(const TABLE &, const gum::VariableSet &), const bool is_result_persistent=false)
default constructor
Exception : problem with size.
Exception : wrong type for this operation.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
ScheduleOperatorType
the currently supported types of "low-level" operators
@ PROJECT_MULTIDIM
project a ScheduleMultiDim over a subset of its variables
STL namespace.
a Projection operator class used for scheduling inferences