aGrUM  0.14.2
defaultEliminationSequenceStrategy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #include <agrum/agrum.h>
27 #include <agrum/core/math/math.h>
29 
30 namespace gum {
31 
34  double theRatio, double theThreshold) :
35  __simplicial_ratio(theRatio),
36  __simplicial_threshold(theThreshold) {
37  // for debugging purposes
38  GUM_CONSTRUCTOR(DefaultEliminationSequenceStrategy);
39  }
40 
44  const NodeProperty< Size >* domain_sizes,
45  double ratio,
46  double threshold) :
47  __simplicial_ratio(ratio),
48  __simplicial_threshold(threshold) {
49  setGraph(graph, domain_sizes);
50 
51  // for debugging purposes
52  GUM_CONSTRUCTOR(DefaultEliminationSequenceStrategy);
53  }
54 
59  // no need to set __log_weights because the copy of the simplicial set
60  // will set it properly
62  _graph,
65  false)),
69  // for debugging purposes
71  }
72 
77  __log_weights(std::move(from.__log_weights)),
82  __simplicial_set->replaceLogWeights(&from.__log_weights, &__log_weights);
83  from.__simplicial_set = nullptr;
84 
85  // for debugging purposes
87  }
88 
91  // for debugging purposes
92  GUM_DESTRUCTOR(DefaultEliminationSequenceStrategy);
93 
94  if (__simplicial_set != nullptr) delete __simplicial_set;
95  }
96 
103  }
104 
108  return new DefaultEliminationSequenceStrategy(*this);
109  }
110 
113  // remove the old simplicial set, if any
114  if (__simplicial_set != nullptr) {
115  delete __simplicial_set;
116  __simplicial_set = nullptr;
117  }
118 
119  if (_graph != nullptr) {
120  // create a simplicial set suited for the graph
123  &__log_weights,
126 
128  }
129  }
130 
133  UndiGraph* graph, const NodeProperty< Size >* domain_sizes) {
134  if (UnconstrainedEliminationSequenceStrategy::setGraph(graph, domain_sizes)) {
136  return true;
137  }
138 
139  return false;
140  }
141 
145 
146  __log_weights.clear();
147  if (__simplicial_set != nullptr) {
148  delete __simplicial_set;
149  __simplicial_set = nullptr;
150  }
151  }
152 
155  // if there is no simplicial set, send an exception
156  if (_graph == nullptr) { GUM_ERROR(NotFound, "the graph is empty"); }
157 
158  // select a node to be eliminated: try simplicial nodes, then almost
159  // simplicial nodes, then quasi-simplicial nodes
160  // note that if __graph != 0, __simplicial_set has been allocated
167  else {
168  // here: select the node through Kjaerulff's heuristic
169  auto iter_heuristic = __log_weights.cbegin();
170 
171  if (iter_heuristic == __log_weights.cend())
172  GUM_ERROR(NotFound, "there exists no more node to eliminate");
173 
174  double min_weight = iter_heuristic.val();
175  NodeId removable_node = iter_heuristic.key();
176  for (++iter_heuristic; iter_heuristic != __log_weights.cend();
177  ++iter_heuristic) {
178  if (iter_heuristic.val() < min_weight) {
179  removable_node = iter_heuristic.key();
180  min_weight = iter_heuristic.val();
181  }
182  }
183 
184  return removable_node;
185  }
186  }
187 
192  __provide_fill_ins = do_it;
193 
194  if (__simplicial_set != nullptr)
196  }
197 
202  return __provide_fill_ins;
203  }
204 
208  return true;
209  }
210 
213  if (__simplicial_set != nullptr) {
216  }
217  }
218 
222  if (!__provide_fill_ins || (__simplicial_set == nullptr))
224  else
225  return __simplicial_set->fillIns();
226  }
227 
228 } /* namespace gum */
Useful macros for maths.
virtual void clear()
clears the sequence (to prepare, for instance, a new elimination sequence)
void __createSimplicialSet()
create a new simplicial set suited for the current graph
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 ...
NodeId bestQuasiSimplicialNode()
gets a quasi simplicial node with the lowest clique weight
NodeProperty< double > __log_weights
for each node, the weight of the clique created by the node&#39;s elimination
UndiGraph * _graph
the graph to be triangulated
bool hasQuasiSimplicialNode()
indicates whether there exists a quasi simplicial node
void replaceLogWeights(NodeProperty< double > *old_weigths, NodeProperty< double > *new_weights)
reassigns a new set of cliques&#39; log weights (with the same content)
virtual void eliminationUpdate(const NodeId node) final
performs all the graph/fill-ins updates provided (if any)
bool hasAlmostSimplicialNode()
indicates whether there exists an almost simplicial node
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
STL namespace.
DefaultEliminationSequenceStrategy(double theRatio=GUM_QUASI_RATIO, double theThreshold=GUM_WEIGHT_THRESHOLD)
default constructor (uses an empty graph)
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 ...
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
SimplicialSet * __simplicial_set
the simplicial set used for determining the best nodes to eliminate
The class for generic Hash Tables.
Definition: hashTable.h:676
double __simplicial_ratio
the ratio used by __simplicial_set for its quasi-simplicial nodes
UndiGraph * graph() const noexcept
returns the current graph
An efficient unconstrained elimination sequence algorithm.
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 ...
virtual bool providesGraphUpdate() const final
indicates whether the elimination sequence updates by itself the graph after a node has been eliminat...
Class enabling fast retrieval of simplicial, quasi and almost simplicial nodes.
Definition: simplicialSet.h:57
bool hasSimplicialNode()
indicates whether there exists a simplicial node
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes) final
sets a new graph to be triangulated
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes)
sets a new graph to be triangulated
NodeId bestAlmostSimplicialNode()
gets the almost simplicial node with the lowest clique weight
virtual void clear() final
clears the sequence (to prepare, for instance, a new elimination sequence)
Base class for undirected graphs.
Definition: undiGraph.h:106
NodeId bestSimplicialNode()
returns the simplicial node with the lowest clique weight
An efficient unconstrained elimination sequence algorithm.
bool __provide_fill_ins
indicates whether we compute new fill-ins
virtual NodeId nextNodeToEliminate() final
returns the new node to be eliminated within the triangulation algorithm
void setFillIns(bool on_off)
sets/unset the fill-ins storage in the standard triangulation procedure
The base class for all elimination sequence algorithms that require only the graph to be triangulated...
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
virtual DefaultEliminationSequenceStrategy * copyFactory() const final
virtual copy constructor
const EdgeSet & fillIns() const
returns the set of all the fill-ins added to the graph so far
double __simplicial_threshold
the threshold used by __simplicial_set to determine small cliques
virtual DefaultEliminationSequenceStrategy * newFactory() const final
creates a new elimination sequence of the same type as the current object, but this sequence contains...