aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4DiGraph_tpl.h
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#pragma once
41
42
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51namespace gum {
52
53 namespace learning {
54
56 template < typename STRUCT_CONSTRAINT >
58 STRUCT_CONSTRAINT& constraint) : constraint_(&constraint) {
59 GUM_CONSTRUCTOR(GraphChangesGenerator4DiGraph);
60 }
61
63 template < typename STRUCT_CONSTRAINT >
64 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4DiGraph(
65 const GraphChangesGenerator4DiGraph& from) :
66 graph_(from.graph_), constraint_(from.constraint_), legal_changes_(from.legal_changes_),
67 _max_threads_number_(from._max_threads_number_) {
68 GUM_CONS_CPY(GraphChangesGenerator4DiGraph);
69 }
70
72 template < typename STRUCT_CONSTRAINT >
73 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4DiGraph(
74 GraphChangesGenerator4DiGraph&& from) :
75 graph_(std::move(from.graph_)), constraint_(from.constraint_),
76 legal_changes_(std::move(from.legal_changes_)),
77 _max_threads_number_(from._max_threads_number_) {
78 GUM_CONS_MOV(GraphChangesGenerator4DiGraph);
79 }
80
82 template < typename STRUCT_CONSTRAINT >
83 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::~GraphChangesGenerator4DiGraph() {
84 GUM_DESTRUCTOR(GraphChangesGenerator4DiGraph);
85 }
86
88 template < typename STRUCT_CONSTRAINT >
89 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
90 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::operator=(
91 const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >& from) {
92 if (this != &from) {
93 graph_ = from.graph_;
94 constraint_ = from.constraint_;
95 legal_changes_ = from.legal_changes_;
96 _max_threads_number_ = from._max_threads_number_;
97 }
98 return *this;
99 }
100
102 template < typename STRUCT_CONSTRAINT >
103 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
104 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::operator=(
105 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&& from) {
106 if (this != &from) {
107 graph_ = std::move(from.graph_);
108 constraint_ = std::move(from.constraint_);
109 legal_changes_ = std::move(from.legal_changes_);
110 _max_threads_number_ = from._max_threads_number_;
111 }
112 return *this;
113 }
114
116 template < typename STRUCT_CONSTRAINT >
117 void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::createChanges_() {
118 legal_changes_.clear();
119
120 // for all the pairs of nodes, consider adding, reverse and removing arcs
121 // do it for each thread
122 const Size nb_threads = _max_threads_number_;
123 std::vector< Set< GraphChange > > legal_changes(nb_threads);
124
125 // create the lambda that will be used to fill the legal changes
126 auto threadedLegalSet = [this, &legal_changes](const std::size_t this_thread,
127 const std::size_t nb_threads) -> void {
128 Idx i = 0;
129 for (const auto node1: this->graph_) {
130 if (i == this_thread) {
131 for (const auto node2: this->graph_) {
132 if (node1 != node2) {
133 // try arc additions
134 ArcAddition arc_add(node1, node2);
135 if (!this->constraint_->isAlwaysInvalid(arc_add)) {
136 legal_changes[this_thread].insert(std::move(arc_add));
137 }
138
139 // try arc deletion
140 ArcDeletion arc_del(node1, node2);
141 if (!this->constraint_->isAlwaysInvalid(arc_del)) {
142 legal_changes[this_thread].insert(std::move(arc_del));
143 }
144
145 // try arc reversal
146 ArcReversal arc_rev(node1, node2);
147 if (!this->constraint_->isAlwaysInvalid(arc_rev)) {
148 legal_changes[this_thread].insert(std::move(arc_rev));
149 }
150 }
151 }
152 }
153 ++i;
154 i %= nb_threads;
155 }
156 };
157
158 // launch the threads
159 ThreadExecutor::execute(nb_threads, threadedLegalSet);
160
161
162 // now store the changes into the protected vectors of the
163 // GraphChangesGenerator4DiGraph
164 for (const auto& changes: legal_changes) {
165 for (const auto& change: changes) {
166 legal_changes_.insert(std::move(change));
167 }
168 }
169 }
170
172 template < typename STRUCT_CONSTRAINT >
173 INLINE void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::setGraph(const DiGraph& graph) {
174 // sets the current graph
175 graph_ = graph;
176
177 // generate the set of all changes
178 createChanges_();
179 }
180
182 template < typename STRUCT_CONSTRAINT >
183 INLINE void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::clearChanges() noexcept {
184 legal_changes_.clear();
185 }
186
188 template < typename STRUCT_CONSTRAINT >
189 INLINE typename GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::iterator
190 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::begin() const {
191 return legal_changes_.cbegin();
192 }
193
195 template < typename STRUCT_CONSTRAINT >
196 INLINE const typename GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::iterator&
197 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::end() const {
198 return legal_changes_.cend();
199 }
200
202 template < typename STRUCT_CONSTRAINT >
203 INLINE void
204 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcAddition& change) {
205 }
206
208 template < typename STRUCT_CONSTRAINT >
209 INLINE void
210 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcDeletion& change) {
211 }
212
214 template < typename STRUCT_CONSTRAINT >
215 INLINE void
216 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcReversal& change) {
217 }
218
220 template < typename STRUCT_CONSTRAINT >
221 INLINE void
222 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const GraphChange& change) {
223 }
224
226 template < typename STRUCT_CONSTRAINT >
227 INLINE void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::notifyGetCompleted() {
228 if (legal_changes_.size()) legal_changes_.clear();
229 }
230
232 template < typename STRUCT_CONSTRAINT >
233 INLINE void
234 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::setMaxNbThreads(Size nb) noexcept {
235 if (nb == 0) nb = gum::getNumberOfThreads();
236 _max_threads_number_ = nb;
237 }
238
240 template < typename STRUCT_CONSTRAINT >
241 INLINE STRUCT_CONSTRAINT&
242 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::constraint() const noexcept {
243 return *constraint_;
244 }
245
246 } /* namespace learning */
247
248} /* namespace gum */
249
250#endif /* DOXYGEN_SHOULD_SKIP_THIS */
GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
unsigned int getNumberOfThreads()
returns the max number of threads used by default when entering the next parallel region
STL namespace.