aGrUM  0.16.0
defaultPartialOrderedEliminationSequenceStrategy.cpp
Go to the documentation of this file.
1 
29 #include <agrum/agrum.h>
30 #include <agrum/core/math/math.h>
31 
33 #include <agrum/graphs/undiGraph.h>
34 
35 namespace gum {
36 
40  double theThreshold) :
41  __simplicial_ratio(theRatio),
42  __simplicial_threshold(theThreshold) {
43  // for debugging purposes
45  }
46 
51  const NodeProperty< Size >* dom_sizes,
52  const List< NodeSet >* subsets,
53  double ratio,
54  double threshold) :
55  __simplicial_ratio(ratio),
56  __simplicial_threshold(threshold) {
57  setGraph(graph, dom_sizes);
58  setPartialOrder(subsets);
59 
60  // for debugging purposes
62  }
63 
69  // no need to set __log_weights because the copy of the simplicial set
70  // will set it properly
72  _graph,
75  false)),
79  // for debugging purposes
81  }
82 
88  __log_weights(std::move(from.__log_weights)),
93  __simplicial_set->replaceLogWeights(&from.__log_weights, &__log_weights);
94  from.__simplicial_set = nullptr;
95 
96  // for debugging purposes
98  }
99 
103  // for debugging purposes
105 
107  }
108 
111  // remove the old simplicial set, if any
112  if (__simplicial_set != nullptr) {
113  delete __simplicial_set;
114  __simplicial_set = nullptr;
115  }
116 
117  if (_graph != nullptr) {
118  // create a simplicial set suited for the graph
121  &__log_weights,
124 
126  }
127  }
128 
131  UndiGraph* graph, const NodeProperty< Size >* domain_sizes) {
132  if (PartialOrderedEliminationSequenceStrategy::setGraph(graph, domain_sizes)) {
134  return true;
135  }
136 
137  return false;
138  }
139 
143  __log_weights.clear();
144 
145  if (__simplicial_set != nullptr) {
146  delete __simplicial_set;
147  __simplicial_set = nullptr;
148  }
149  }
150 
153  const PriorityQueue< NodeId, double >& possibleNodes) {
154  bool found = false;
155  double min_score = 0;
156  NodeId best_node = 0;
157 
158  for (const auto node : _nodeset) {
159  try {
160  double score = possibleNodes.priority(node);
161 
162  if (!found || (score < min_score)) {
163  found = true;
164  min_score = score;
165  best_node = node;
166  }
167  } catch (NotFound&) {}
168  }
169 
170  if (!found) { GUM_ERROR(NotFound, "no possible node to eliminate"); }
171 
172  return best_node;
173  }
174 
177  // if there is no simplicial set, send an exception
178  if (_graph == nullptr) GUM_ERROR(NotFound, "the graph is empty");
179 
182  "the partial order does not cover all the nodes "
183  "of the graph");
184 
185  if (_nodeset.empty()) { GUM_ERROR(NotFound, "no node is admissible"); }
186 
187  // select a node to be eliminated: try simplicial nodes, then almost
188  // simplicial nodes, then quasi-simplicial nodes
189  // note that if _graph != nullptr, __simplicial_set has been allocated
190  try {
192  } catch (NotFound&) {}
193 
194  try {
196  } catch (NotFound&) {}
197 
198  try {
200  } catch (NotFound&) {}
201 
202  // here: select the node through Kjaerulff's heuristic
203  auto iter = _nodeset.cbegin();
204  double min_score = __log_weights[*iter];
205  NodeId best_node = *iter;
206 
207  for (++iter; iter != _nodeset.cend(); ++iter) {
208  double score = __log_weights[*iter];
209 
210  if (score < min_score) {
211  min_score = score;
212  best_node = *iter;
213  }
214  }
215 
216  return best_node;
217  }
218 
222  __provide_fill_ins = do_it;
223 
225  }
226 
231  return __provide_fill_ins;
232  }
233 
237  const {
238  return true;
239  }
240 
243  const NodeId id) {
244  // check whether we can do something
245  if (__simplicial_set != nullptr) {
248 
249  if (!_partial_order_needed) {
250  // remove the node from _nodeset
251  _nodeset.erase(id);
252 
253  if (_nodeset.empty()) {
254  // go to the next non-empty subset
255  for (++_subset_iter; _subset_iter != _subsets->cend(); ++_subset_iter) {
256  for (const auto node : *_subset_iter) {
257  if (_graph->existsNode(node)) { _nodeset.insert(node); }
258  }
259  if (!_nodeset.empty()) break;
260  }
261  }
262  }
263  }
264  }
265 
269  if (!__provide_fill_ins || (__simplicial_set == nullptr))
271  else
272  return __simplicial_set->fillIns();
273  }
274 
281  }
282 
287  }
288 
289 } /* namespace gum */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void clear()
clears the sequence (to prepare, for instance, a new elimination sequence)
void makeClique(const NodeId id)
adds the necessary edges so that node &#39;id&#39; and its neighbors form a clique
virtual const EdgeSet & fillIns()
in case fill-ins are provided, this function returns the fill-ins due to all the nodes eliminated so ...
virtual void clear() final
clears the sequence (to prepare, for instance, a new elimination sequence)
void __createSimplicialSet()
create a new simplicial set suited for the current graph
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:707
UndiGraph * _graph
the graph to be triangulated
virtual void eliminationUpdate(const NodeId node) final
performs all the graph/fill-ins updates provided (if any)
virtual DefaultPartialOrderedEliminationSequenceStrategy * newFactory() const final
creates a new elimination sequence of the same type as the current object, but this sequence contains...
virtual bool providesFillIns() const final
indicates whether the fill-ins generated by the eliminated nodes, if needed, will be computed by the ...
virtual const EdgeSet & fillIns() final
in case fill-ins are provided, this function returns the fill-ins due to all the nodes eliminated so ...
void replaceLogWeights(NodeProperty< double > *old_weigths, NodeProperty< double > *new_weights)
reassigns a new set of cliques&#39; log weights (with the same content)
const Priority & priority(const Val &elt) const
Returns the priority of an instance of the value passed in argument.
virtual void askFillIns(bool do_it) final
if the elimination sequence is able to compute fill-ins, we indicate whether we want this feature to ...
NodeProperty< double > _log_domain_sizes
the log of the domain sizes of the variables/nodes
void eraseClique(const NodeId id)
removes a node and its adjacent edges from the underlying graph
virtual bool setPartialOrder(const List< NodeSet > *subsets)
sets a new partial ordering constraint on the elimination sequence
STL namespace.
NodeProperty< double > __log_weights
for each node, the weight of the clique created by the node&#39;s elimination
double __simplicial_ratio
the ratio used by __simplicial_set for its quasi-simplicial nodes
const_iterator cbegin() const
The usual unsafe begin iterator to parse the set.
Definition: set_tpl.h:524
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual NodeId nextNodeToEliminate() final
returns the new node to be eliminated within the triangulation algorithm
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
double __simplicial_threshold
the threshold used by __simplicial_set to determine small cliques
An Elimination sequence algorithm that imposes a given partial ordering on the nodes elimination sequ...
NodeId __nodeToEliminate(const PriorityQueue< NodeId, double > &possibleNodes)
returns the best possible node to be eliminated
The class for generic Hash Tables.
Definition: hashTable.h:679
UndiGraph * graph() const noexcept
returns the current graph
SimplicialSet * __simplicial_set
the simplicial set used for determining the best nodes to eliminate
Base class for all elimination sequence algorithm that impose a given partial ordering on the nodes e...
const PriorityQueue< NodeId, double > & allSimplicialNodes()
returns all the simplicial nodes
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes)
sets a new graph to be triangulated
Class enabling fast retrieval of simplicial, quasi and almost simplicial nodes.
Definition: simplicialSet.h:60
const const_iterator & cend() const noexcept
The usual unsafe end iterator to parse the set.
Definition: set_tpl.h:539
virtual bool providesGraphUpdate() const final
indicates whether the elimination sequence updates by itself the graph after a node has been eliminat...
NodeSet _nodeset
the nodes which can be currently eliminated
const List< NodeSet > * _subsets
the subsets constituting the partial ordering
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
virtual DefaultPartialOrderedEliminationSequenceStrategy * copyFactory() const final
virtual copy constructor
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes) final
sets a new graph to be triangulated
Base class for undirected graphs.
Definition: undiGraph.h:109
const PriorityQueue< NodeId, double > & allAlmostSimplicialNodes()
returns all the almost simplicial nodes
List< NodeSet >::const_iterator _subset_iter
the iterator indicating which is the current subset on which we work
bool _partial_order_needed
indicate whether a new partial ordering is necessary for the elimination
DefaultPartialOrderedEliminationSequenceStrategy(double theRatio=GUM_QUASI_RATIO, double theThreshold=GUM_WEIGHT_THRESHOLD)
default constructor (uses an empty graph)
const PriorityQueue< NodeId, double > & allQuasiSimplicialNodes()
returns all the quasi simplicial nodes
void setFillIns(bool on_off)
sets/unset the fill-ins storage in the standard triangulation procedure
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
const EdgeSet & fillIns() const
returns the set of all the fill-ins added to the graph so far