aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scheduleCombine_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief a Combination operator class used for scheduling inferences
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/agrum.h>
31 # include <agrum/tools/graphicalModels/inference/scheduler/scheduleCombine.h>
32 # include <limits>
33 
34 namespace gum {
35 
36  /// default constructor
37  template < typename GUM_SCALAR >
38  ScheduleCombine< GUM_SCALAR >::ScheduleCombine(const ScheduleMultiDim< GUM_SCALAR >& table1,
39  const ScheduleMultiDim< GUM_SCALAR >& table2,
40  MultiDimImplementation< GUM_SCALAR >* (*combine)(
41  const MultiDimImplementation< GUM_SCALAR >&,
42  const MultiDimImplementation< GUM_SCALAR >&)) :
43  ScheduleOperation< GUM_SCALAR >(ScheduleOperation< GUM_SCALAR >::Type::COMBINE_MULTIDIM),
44  _table1_(table1), _table2_(table2), _args_(0), _results_(0), _combine_(combine) {
45  // for debugging purposes
46  GUM_CONSTRUCTOR(ScheduleCombine);
47 
48  // compute the variables of the resulting table
49  Sequence< const DiscreteVariable* > vars = _table1_.variablesSequence();
50  const Sequence< const DiscreteVariable* >& vars2 = _table2_.variablesSequence();
51 
52  for (typename Sequence< const DiscreteVariable* >::const_iterator_safe iter = vars2.beginSafe();
53  iter != vars2.endSafe();
54  ++iter) {
55  if (!vars.exists(*iter)) { vars.insert(*iter); }
56  }
57 
58  // create the scheduleMultiDim that should result from the combination of
59  // table1 and table2
60  _result_ = new ScheduleMultiDim< GUM_SCALAR >(vars);
61  }
62 
63  /// copy constructor
64  template < typename GUM_SCALAR >
69  // for debugging purposes
71  }
72 
73  /// virtual copy constructor: creates a clone of the operation
74  template < typename GUM_SCALAR >
76  return new ScheduleCombine< GUM_SCALAR >(*this);
77  }
78 
79  /// destructor
80  template < typename GUM_SCALAR >
82  // for debugging purposes
84  delete _result_;
85 
86  if (_args_) delete _args_;
87 
88  if (_results_) delete _results_;
89  }
90 
91  /// copy operator
92  template < typename GUM_SCALAR >
95  // avoid self assignment
96  if (this != &from) {
100  *_result_ = *(from._result_);
102 
103  // update _args_ and _results_ if they were already created
104  if (_args_) {
105  _args_->clear();
108  }
109 
110  if (_results_) {
111  _results_->clear();
113  }
114  }
115 
116  return *this;
117  }
118 
119  /// operator ==
120  template < typename GUM_SCALAR >
121  INLINE bool
123  if (this->type() != op.type()) return false;
124 
126  = static_cast< const ScheduleCombine< GUM_SCALAR >& >(op);
127  return ((((_table1_ == real_op._table1_) && (_table2_ == real_op._table2_))
128  || ((_table1_ == real_op._table2_) && (_table2_ == real_op._table1_)))
129  && (_combine_ == real_op._combine_));
130  }
131 
132  /// operator !=
133  template < typename GUM_SCALAR >
134  INLINE bool
136  return !operator==(op);
137  }
138 
139  /// executes the operation
140  template < typename GUM_SCALAR >
141  void ScheduleCombine< GUM_SCALAR >::execute() {
142  if (_result_->isAbstract()) {
143  // first, get the multidims to combine
146 
147  // perform the combination and store the result
150  }
151  }
152 
153  /** @brief returns an estimation of the number of elementary operations
154  * needed to perform the ScheduleOperation */
155  template < typename GUM_SCALAR >
156  float ScheduleCombine< GUM_SCALAR >::nbOperations() const {
159 
160  if (seq1.empty() && seq2.empty()) return 0.0f;
161 
162  float size = 1;
163 
164  for (const auto var: seq1)
165  size *= var->domainSize();
166 
167  for (const auto var: seq2)
168  if (!seq1.exists(var)) size *= var->domainSize();
169 
170  return size;
171  }
172 
173  /// returns the memory consumption used during the operation
174  template < typename GUM_SCALAR >
175  std::pair< long, long > ScheduleCombine< GUM_SCALAR >::memoryUsage() const {
178 
179  if (seq1.empty() && seq2.empty()) return std::pair< long, long >(0, 0);
180 
181  long size = 1;
182 
183  for (const auto var: seq1) {
184  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
185  GUM_ERROR(OutOfBounds, "memory usage out of long int range")
186  }
187 
188  size *= long(var->domainSize());
189  }
190 
191  for (const auto var: seq2)
192  if (!seq1.exists(var)) {
193  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
194  GUM_ERROR(OutOfBounds, "memory usage out of long int range")
195  }
196 
197  size *= long(var->domainSize());
198  }
199 
200  return std::pair< long, long >(size, size);
201  }
202 
203  /// returns the set of multidims passed in argument to the operation
204  template < typename GUM_SCALAR >
205  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
207  if (!_args_) {
208  _args_ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
211  }
212 
213  return *_args_;
214  }
215 
216  /// returns the set of multidims that should be the result of the operation
217  template < typename GUM_SCALAR >
218  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
220  if (!_results_) {
221  _results_ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
223  }
224 
225  return *_results_;
226  }
227 
228  /// displays the content of the operation
229  template < typename GUM_SCALAR >
231  return _result_->toString() + " = combine ( " + _table1_.toString() + " , "
232  + _table2_.toString() + " )";
233  }
234 
235  /// returns the scheduleMultidim resulting from the execution of the operation
236  template < typename GUM_SCALAR >
238  return *_result_;
239  }
240 
241 } // namespace gum
242 
243 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643