aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
scheduleCombine_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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(
39  const ScheduleMultiDim< GUM_SCALAR >& table1,
40  const ScheduleMultiDim< GUM_SCALAR >& table2,
41  MultiDimImplementation< GUM_SCALAR >* (*combine)(
42  const MultiDimImplementation< GUM_SCALAR >&,
43  const MultiDimImplementation< GUM_SCALAR >&)) :
44  ScheduleOperation< GUM_SCALAR >(
45  ScheduleOperation< GUM_SCALAR >::Type::COMBINE_MULTIDIM),
46  table1__(table1), table2__(table2), args__(0), results__(0),
47  combine__(combine) {
48  // for debugging purposes
49  GUM_CONSTRUCTOR(ScheduleCombine);
50 
51  // compute the variables of the resulting table
52  Sequence< const DiscreteVariable* > vars = table1__.variablesSequence();
53  const Sequence< const DiscreteVariable* >& vars2
54  = table2__.variablesSequence();
55 
56  for (typename Sequence< const DiscreteVariable* >::const_iterator_safe iter
57  = vars2.beginSafe();
58  iter != vars2.endSafe();
59  ++iter) {
60  if (!vars.exists(*iter)) { vars.insert(*iter); }
61  }
62 
63  // create the scheduleMultiDim that should result from the combination of
64  // table1 and table2
65  result__ = new ScheduleMultiDim< GUM_SCALAR >(vars);
66  }
67 
68  /// copy constructor
69  template < typename GUM_SCALAR >
71  const ScheduleCombine< GUM_SCALAR >& from) :
76  // for debugging purposes
78  }
79 
80  /// virtual copy constructor: creates a clone of the operation
81  template < typename GUM_SCALAR >
84  return new ScheduleCombine< GUM_SCALAR >(*this);
85  }
86 
87  /// destructor
88  template < typename GUM_SCALAR >
90  // for debugging purposes
92  delete result__;
93 
94  if (args__) delete args__;
95 
96  if (results__) delete results__;
97  }
98 
99  /// copy operator
100  template < typename GUM_SCALAR >
102  const ScheduleCombine< GUM_SCALAR >& from) {
103  // avoid self assignment
104  if (this != &from) {
108  *result__ = *(from.result__);
110 
111  // update args__ and results__ if they were already created
112  if (args__) {
113  args__->clear();
116  }
117 
118  if (results__) {
119  results__->clear();
121  }
122  }
123 
124  return *this;
125  }
126 
127  /// operator ==
128  template < typename GUM_SCALAR >
130  const ScheduleOperation< GUM_SCALAR >& op) const {
131  if (this->type() != op.type()) return false;
132 
134  = static_cast< const ScheduleCombine< GUM_SCALAR >& >(op);
135  return ((((table1__ == real_op.table1__) && (table2__ == real_op.table2__))
136  || ((table1__ == real_op.table2__) && (table2__ == real_op.table1__)))
137  && (combine__ == real_op.combine__));
138  }
139 
140  /// operator !=
141  template < typename GUM_SCALAR >
143  const ScheduleOperation< GUM_SCALAR >& op) const {
144  return !operator==(op);
145  }
146 
147  /// executes the operation
148  template < typename GUM_SCALAR >
149  void ScheduleCombine< GUM_SCALAR >::execute() {
150  if (result__->isAbstract()) {
151  // first, get the multidims to combine
154 
155  // perform the combination and store the result
158  }
159  }
160 
161  /** @brief returns an estimation of the number of elementary operations
162  * needed to perform the ScheduleOperation */
163  template < typename GUM_SCALAR >
164  float ScheduleCombine< GUM_SCALAR >::nbOperations() const {
167 
168  if (seq1.empty() && seq2.empty()) return 0.0f;
169 
170  float size = 1;
171 
172  for (const auto var: seq1)
173  size *= var->domainSize();
174 
175  for (const auto var: seq2)
176  if (!seq1.exists(var)) size *= var->domainSize();
177 
178  return size;
179  }
180 
181  /// returns the memory consumption used during the operation
182  template < typename GUM_SCALAR >
183  std::pair< long, long > ScheduleCombine< GUM_SCALAR >::memoryUsage() const {
186 
187  if (seq1.empty() && seq2.empty()) return std::pair< long, long >(0, 0);
188 
189  long size = 1;
190 
191  for (const auto var: seq1) {
192  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
193  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
194  }
195 
196  size *= long(var->domainSize());
197  }
198 
199  for (const auto var: seq2)
200  if (!seq1.exists(var)) {
201  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
202  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
203  }
204 
205  size *= long(var->domainSize());
206  }
207 
208  return std::pair< long, long >(size, size);
209  }
210 
211  /// returns the set of multidims passed in argument to the operation
212  template < typename GUM_SCALAR >
213  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
215  if (!args__) {
216  args__ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
219  }
220 
221  return *args__;
222  }
223 
224  /// returns the set of multidims that should be the result of the operation
225  template < typename GUM_SCALAR >
226  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
228  if (!results__) {
229  results__ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
231  }
232 
233  return *results__;
234  }
235 
236  /// displays the content of the operation
237  template < typename GUM_SCALAR >
239  return result__->toString() + " = combine ( " + table1__.toString() + " , "
240  + table2__.toString() + " )";
241  }
242 
243  /// returns the scheduleMultidim resulting from the execution of the operation
244  template < typename GUM_SCALAR >
246  ScheduleCombine< GUM_SCALAR >::result() const {
247  return *result__;
248  }
249 
250 } // namespace gum
251 
252 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669