aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51# include <agrum/agrum.h>
52
54
55namespace gum {
56
58 template < typename TABLE >
60 const gum::VariableSet& del_vars,
61 TABLE (*project)(const TABLE&,
62 const gum::VariableSet&),
63 const bool is_result_persistent) :
64 ScheduleOperator(ScheduleOperatorType::PROJECT_MULTIDIM, false, is_result_persistent),
65 _arg_(&table), _del_vars_(del_vars), _project_(project) {
66 // compute the variables that shall belong to the result of the projection
67 Sequence< const DiscreteVariable* > vars = table.variablesSequence();
68 for (const auto var: del_vars) {
69 if (vars.exists(var)) vars.erase(var);
70 }
71
72 // create the scheduleMultiDim that should result from the projection
73 // if table is a constant, just copy it
74 if (table.variablesSequence().empty() && !table.isAbstract())
75 _result_ = new ScheduleMultiDim< TABLE >(table.multiDim(), true, Idx(0));
76 else _result_ = new ScheduleMultiDim< TABLE >(vars, Idx(0));
77
78 // save the args and result into _args_ and _results_
79 _args_ << _arg_;
80 _results_ << _result_;
81
82 // for debugging purposes
83 GUM_CONSTRUCTOR(ScheduleProjection);
84 }
85
87 template < typename TABLE >
88 ScheduleProjection< TABLE >::ScheduleProjection(const ScheduleProjection< TABLE >& from) :
89 ScheduleOperator(from), _arg_(from._arg_), _del_vars_(from._del_vars_),
90 _project_(from._project_) {
91 // copy the result of the from operator
92 _result_ = new ScheduleMultiDim< TABLE >(*(from._result_));
93
94 // save the args and result into _args_ and _results_
95 _args_ << _arg_;
96 _results_ << _result_;
97
98 // for debugging purposes
99 GUM_CONS_CPY(ScheduleProjection);
100 }
101
103 template < typename TABLE >
104 ScheduleProjection< TABLE >::ScheduleProjection(ScheduleProjection< TABLE >&& from) :
105 ScheduleOperator(std::move(from)), _arg_(from._arg_), _result_(from._result_),
106 _del_vars_(std::move(from._del_vars_)), _project_(from._project_) {
107 // indicate that from does not contain anything anymore
108 from.makeResultsPersistent(true); // prevent deleting nullptr
109 from._result_ = nullptr;
110
111 // save the args and result into _args_ and _results_
112 _args_ << _arg_;
113 _results_ << _result_;
114
115 // for debugging purposes
116 GUM_CONS_MOV(ScheduleProjection);
117 }
118
120 template < typename TABLE >
121 INLINE ScheduleProjection< TABLE >* ScheduleProjection< TABLE >::clone() const {
122 return new ScheduleProjection< TABLE >(*this);
123 }
124
126 template < typename TABLE >
127 ScheduleProjection< TABLE >::~ScheduleProjection() {
128 if (!this->hasPersistentResults()) { delete _result_; }
129
130 // for debugging purposes
131 GUM_DESTRUCTOR(ScheduleProjection);
132 }
133
135 template < typename TABLE >
136 ScheduleProjection< TABLE >&
137 ScheduleProjection< TABLE >::operator=(const ScheduleProjection< TABLE >& from) {
138 // avoid self assignment
139 if (this != &from) {
140 // copy the set of variables to delete in a temporary variable, just
141 // in case something goes wrong below
142 const gum::VariableSet new_del_vars = from._del_vars_;
143
144 // try to copy result (no need to update _results_)
145 *_result_ = *(from._result_);
146 ScheduleOperator::operator=(from);
147
148 _del_vars_ = std::move(new_del_vars);
149 _arg_ = from._arg_;
150 _args_.clear();
151 _args_ << _arg_;
152 _project_ = from._project_;
153 }
154 return *this;
155 }
156
158 template < typename TABLE >
159 ScheduleProjection< TABLE >&
160 ScheduleProjection< TABLE >::operator=(ScheduleProjection< TABLE >&& from) {
161 // avoid self assignment
162 if (this != &from) {
163 if (!this->hasPersistentResults()) delete _result_;
164 _result_ = from._result_;
165 ScheduleOperator::operator=(std::move(from));
166
167 _del_vars_ = std::move(from._del_vars_);
168 _arg_ = from._arg_;
169 _args_.clear();
170 _args_ << _arg_;
171 _project_ = from._project_;
172
173 from.makeResultsPersistent(true); // prevent deleting nullptr
174 from._result_ = nullptr;
175 }
176 return *this;
177 }
178
180 template < typename TABLE >
181 INLINE bool ScheduleProjection< TABLE >::operator==(const ScheduleProjection< TABLE >& op) const {
182 return (_project_ == op._project_) && (*_arg_ == *op._arg_) && (_del_vars_ == op._del_vars_);
183 }
184
186 template < typename TABLE >
187 bool ScheduleProjection< TABLE >::operator==(const ScheduleOperator& op) const {
188 if (ScheduleOperator::operator!=(op)) return false;
189
190 try {
191 const ScheduleProjection< TABLE >& real_op
192 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
193 return ScheduleProjection< TABLE >::operator==(real_op);
194 } catch (std::bad_cast&) { return false; }
195 }
196
198 template < typename TABLE >
199 INLINE bool ScheduleProjection< TABLE >::operator!=(const ScheduleOperator& op) const {
200 return !ScheduleProjection< TABLE >::operator==(op);
201 }
202
204 template < typename TABLE >
205 INLINE bool ScheduleProjection< TABLE >::operator!=(const ScheduleProjection< TABLE >& op) const {
206 return !ScheduleProjection< TABLE >::operator==(op);
207 }
208
210 template < typename TABLE >
211 INLINE bool ScheduleProjection< TABLE >::hasSimilarArguments(
212 const ScheduleProjection< TABLE >& op) const {
213 return (_arg_->hasSameVariables(*op._arg_) && (_del_vars_ == op._del_vars_));
214 }
215
217 template < typename TABLE >
218 bool ScheduleProjection< TABLE >::hasSimilarArguments(const ScheduleOperator& op) const {
219 try {
220 const ScheduleProjection< TABLE >& real_op
221 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
222 return ScheduleProjection< TABLE >::hasSimilarArguments(real_op);
223 } catch (std::bad_cast&) { return false; }
224 }
225
227 template < typename TABLE >
228 INLINE bool
229 ScheduleProjection< TABLE >::hasSameArguments(const ScheduleProjection< TABLE >& op) const {
230 return (_arg_->hasSameVariables(*op._arg_) && _arg_->hasSameContent(*op._arg_)
231 && (_del_vars_ == op._del_vars_));
232 }
233
235 template < typename TABLE >
236 bool ScheduleProjection< TABLE >::hasSameArguments(const ScheduleOperator& op) const {
237 try {
238 const ScheduleProjection< TABLE >& real_op
239 = dynamic_cast< const ScheduleProjection< TABLE >& >(op);
240 return ScheduleProjection< TABLE >::hasSameArguments(real_op);
241 } catch (std::bad_cast&) { return false; }
242 }
243
245 template < typename TABLE >
246 INLINE bool
247 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 INLINE const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::arg() const {
264 return *_arg_;
265 }
266
268 template < typename TABLE >
269 INLINE const Sequence< const IScheduleMultiDim* >& ScheduleProjection< TABLE >::args() const {
270 return _args_;
271 }
272
274 template < typename TABLE >
275 INLINE const ScheduleMultiDim< TABLE >& ScheduleProjection< TABLE >::result() const {
276 return *_result_;
277 }
278
280 template < typename TABLE >
281 INLINE 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 INLINE 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:72
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