aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
orderedEliminationSequenceStrategy.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#include <agrum/agrum.h>
51
53
54#ifdef GUM_NO_INLINE
56#endif // GU%_NO_INLINE
57
58namespace gum {
59
64
68 const NodeProperty< Size >* dom_sizes,
69 const std::vector< NodeId >* order) : EliminationSequenceStrategy(graph, dom_sizes) {
70 // check that the user passed appropriate graphs and orders
71 if (((graph == nullptr) && (order != nullptr)) || ((graph != nullptr) && (order == nullptr))) {
73 "OrderedEliminationSequenceStrategy needs either both nullptrs "
74 "or both non-nullptrs on graph and elimination ordering");
75 }
76
78
80 }
81
89
97
102
108
113
116 const NodeProperty< Size >* domain_sizes) {
117 if (EliminationSequenceStrategy::setGraph(graph, domain_sizes)) {
119 return true;
120 }
121
122 return false;
123 }
124
127 const std::vector< NodeId >* order) const {
128 if ((graph_ == nullptr) || (order == nullptr)) return true;
129
130 // determine the set of nodes in the order that belong to the graph
131 NodeSet nodes_found(graph_->size() / 2);
132 for (const auto node: *order) {
133 if (graph_->existsNode(node)) { nodes_found.insert(node); }
134 }
135
136 // check that the size of nodes_found is equal to that of the graph
137 return nodes_found.size() != graph_->size();
138 }
139
141 bool OrderedEliminationSequenceStrategy::setOrder(const std::vector< NodeId >* order) {
142 // check that the order contains all the nodes of the graph
144
145 if (!_order_needed_) {
146 _order_ = order;
147
148 // determine the first element in order that belong to the graph
149 // here, note that we have the guarantee that _order_index_ will be
150 // lower than the size of _order_ since all the nodes in the graph
151 // belong to vector _order_
152 _order_index_ = 0;
153 while (!graph_->existsNode((*_order_)[_order_index_]))
155
156 return true;
157 }
158
159 return false;
160 }
161
168
171 // check that we can return a nodeId
172 if (_order_needed_ || (_order_->size() <= _order_index_)) {
173 GUM_ERROR(NotFound, "no node id can be returned")
174 }
175
176 return (*_order_)[_order_index_];
177 }
178
182 // do nothing: we are not able to compute fill-ins
183 }
184
189
193
196 // check whether there is something to update
197 if (!_order_needed_) {
198 // check that node corresponds to the current index
199 if ((_order_index_ >= _order_->size()) || ((*_order_)[_order_index_] != node)) {
201 "update impossible because node "
202 << node << " does not correspond to the current elimination index");
203 }
204
205 // now perform the update: goto the next node that belongs to graph_
207 std::size_t size = _order_->size();
208 while ((_order_index_ < size) && !graph_->existsNode((*_order_)[_order_index_]))
210 }
211 }
212
218
219} /* namespace gum */
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes)
sets a new graph to be triangulated
UndiGraph * graph_
the graph to be triangulated
virtual void clear()
clears the sequence (to prepare, for instance, a new elimination sequence)
UndiGraph * graph() const noexcept
returns the current graph
virtual const EdgeSet & fillIns()
in case fill-ins are provided, this function returns the fill-ins due to all the nodes eliminated so ...
Exception base for graph error.
Exception : the element we looked for cannot be found.
virtual OrderedEliminationSequenceStrategy * copyFactory() const final
virtual copy constructor
virtual OrderedEliminationSequenceStrategy * newFactory() const final
creates a new elimination sequence of the same type as the current object, but this sequence contains...
virtual NodeId nextNodeToEliminate() final
returns the new node to be eliminated within the triangulation algorithm
bool _isOrderNeeded_(const std::vector< NodeId > *order) const
indicates whether an order is compatible with the current graph
const std::vector< NodeId > * order() const noexcept
returns the current complete ordering
virtual const EdgeSet & fillIns() final
in case fill-ins are provided, this function returns the fill-ins due to all the nodes eliminated so ...
virtual bool providesFillIns() const final
indicates whether the fill-ins generated by the eliminated nodes, if needed, will be computed by the ...
const std::vector< NodeId > * _order_
the vector indicating in which order we should eliminate the nodes
bool _order_needed_
indicate whether a new complete ordering is necessary for the elimination
virtual bool setGraph(UndiGraph *graph, const NodeProperty< Size > *dom_sizes) final
sets a new graph to be triangulated
std::size_t _order_index_
the index in the order indicating the new node to eliminate
virtual void eliminationUpdate(const NodeId node) final
performs all the graph/fill-ins updates provided (if any)
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 void clear() final
clears the order (to prepare, for instance, a new elimination sequence)
virtual bool providesGraphUpdate() const final
indicates whether the elimination sequence updates by itself the graph after a node has been eliminat...
virtual bool setOrder(const std::vector< NodeId > *order) final
sets the sequence of elimination
OrderedEliminationSequenceStrategy()
default constructor (uses an empty graph)
Exception : out of bound.
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:636
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
Base class for undirected graphs.
Definition undiGraph.h:128
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Set< Edge > EdgeSet
Some typdefs and define for shortcuts ...
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
An Elimination sequence algorithm that imposes a given complete ordering on the nodes elimination seq...
An Elimination sequence algorithm that imposes a given complete ordering on the nodes elimination seq...