aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
scheduleProject_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 Projection 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 <limits>
32 
33 namespace gum {
34 
35  /// default constructor
36  template < typename GUM_SCALAR >
37  ScheduleProject< GUM_SCALAR >::ScheduleProject(
38  const ScheduleMultiDim< GUM_SCALAR >& table,
39  const Set< const DiscreteVariable* >& del_vars,
40  MultiDimImplementation< GUM_SCALAR >* (*project)(
41  const MultiDimImplementation< GUM_SCALAR >&,
42  const Set< const DiscreteVariable* >&)) :
43  ScheduleOperation< GUM_SCALAR >(
44  ScheduleOperation< GUM_SCALAR >::Type::PROJECT_MULTIDIM),
45  table__(table), del_vars__(del_vars), args__(0), results__(0),
46  project__(project) {
47  // for debugging purposes
48  GUM_CONSTRUCTOR(ScheduleProject);
49 
50  // compute the variables that shall belong to the result of the projection
51  Sequence< const DiscreteVariable* > vars = table__.variablesSequence();
52 
53  for (const auto var: del_vars)
54  vars.erase(var);
55 
56  // create the scheduleMultiDim that should result from the combination of
57  // table1 and table2
58  result__ = new ScheduleMultiDim< GUM_SCALAR >(vars);
59  }
60 
61  /// copy constructor
62  template < typename GUM_SCALAR >
64  const ScheduleProject< GUM_SCALAR >& f) :
69  // for debugging purposes
71  }
72 
73  /// virtual copy constructor: creates a clone of the operation
74  template < typename GUM_SCALAR >
77  return new ScheduleProject< GUM_SCALAR >(*this);
78  }
79 
80  /// destructor
81  template < typename GUM_SCALAR >
83  // for debugging purposes
85  delete result__;
86 
87  if (args__) delete args__;
88 
89  if (results__) delete results__;
90  }
91 
92  /// copy operator
93  template < typename GUM_SCALAR >
95  const ScheduleProject< GUM_SCALAR >& from) {
96  // avoid self assignment
97  if (this != &from) {
101  *result__ = *(from.result__);
103 
104  // update args__ and results__ if they were already created
105  if (args__) {
106  args__->clear();
107  args__->insert(&table__);
108  }
109 
110  if (results__) {
111  results__->clear();
113  }
114  }
115 
116  return *this;
117  }
118 
119  /// operator ==
120  template < typename GUM_SCALAR >
122  const ScheduleOperation< GUM_SCALAR >& op) const {
123  if (this->type() != op.type()) return false;
124 
126  = static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
127  return ((table__ == real_op.table__) && (del_vars__ == real_op.del_vars__)
128  && (project__ == real_op.project__));
129  }
130 
131  /// operator !=
132  template < typename GUM_SCALAR >
134  const ScheduleOperation< GUM_SCALAR >& op) const {
135  if (this->type() != op.type()) return true;
136 
138  = static_cast< const ScheduleProject< GUM_SCALAR >& >(op);
139  return ((table__ != real_op.table__) || (del_vars__ != real_op.del_vars__)
140  || (project__ != real_op.project__));
141  }
142 
143  /// executes the operation
144  template < typename GUM_SCALAR >
145  void ScheduleProject< GUM_SCALAR >::execute() {
146  if (result__->isAbstract()) {
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  INLINE float ScheduleProject< GUM_SCALAR >::nbOperations() const {
157  return float(table__.domainSize());
158  }
159 
160  /// returns the memory consumption used during the operation
161  template < typename GUM_SCALAR >
162  std::pair< long, long > ScheduleProject< GUM_SCALAR >::memoryUsage() const {
163  long size = 1;
165 
166  for (const auto var: seq)
167  if (!del_vars__.contains(var)) {
168  if (std::numeric_limits< long >::max() / (long)var->domainSize() < size) {
169  GUM_ERROR(OutOfBounds, "memory usage out of long int range");
170  }
171 
172  size *= long(var->domainSize());
173  }
174 
175  return std::pair< long, long >(size, size);
176  }
177 
178  /// returns the scheduleMultidim resulting from the execution of the operation
179  template < typename GUM_SCALAR >
181  ScheduleProject< GUM_SCALAR >::result() const {
182  return *result__;
183  }
184 
185  /// returns the set of multidims passed in argument to the operation
186  template < typename GUM_SCALAR >
187  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
189  if (!args__) {
190  args__ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
191  args__->insert(&table__);
192  }
193 
194  return *args__;
195  }
196 
197  /// returns the set of multidims that should be the result of the operation
198  template < typename GUM_SCALAR >
199  INLINE const Sequence< const ScheduleMultiDim< GUM_SCALAR >* >&
201  if (!results__) {
202  results__ = new Sequence< const ScheduleMultiDim< GUM_SCALAR >* >;
204  }
205 
206  return *results__;
207  }
208 
209  /// displays the content of the operation
210  template < typename GUM_SCALAR >
212  return result__->toString() + " = project ( " + table__.toString() + " , "
213  + del_vars__.toString() + " )";
214  }
215 
216 } /* namespace gum */
217 
218 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669