aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4UndiGraph_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(GraphChangesGenerator4UndiGraph);
60 }
61
63 template < typename STRUCT_CONSTRAINT >
64 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4UndiGraph(
65 const GraphChangesGenerator4UndiGraph& 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(GraphChangesGenerator4UndiGraph);
69 }
70
72 template < typename STRUCT_CONSTRAINT >
73 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4UndiGraph(
74 GraphChangesGenerator4UndiGraph&& 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(GraphChangesGenerator4UndiGraph);
79 }
80
82 template < typename STRUCT_CONSTRAINT >
83 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::~GraphChangesGenerator4UndiGraph() {
84 GUM_DESTRUCTOR(GraphChangesGenerator4UndiGraph);
85 }
86
88 template < typename STRUCT_CONSTRAINT >
89 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
90 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::operator=(
91 const GraphChangesGenerator4UndiGraph< 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 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
104 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::operator=(
105 GraphChangesGenerator4UndiGraph< 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 GraphChangesGenerator4UndiGraph< 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 edge additions
134 EdgeAddition edge_add(node1, node2);
135 if (!this->constraint_->isAlwaysInvalid(edge_add)) {
136 legal_changes[this_thread].insert(std::move(edge_add));
137 }
138
139 // try edge deletion
140 EdgeDeletion edge_del(node1, node2);
141 if (!this->constraint_->isAlwaysInvalid(edge_del)) {
142 legal_changes[this_thread].insert(std::move(edge_del));
143 }
144 }
145 }
146 }
147 ++i;
148 i %= nb_threads;
149 }
150 };
151
152 // launch the threads
153 ThreadExecutor::execute(nb_threads, threadedLegalSet);
154
155
156 // now store the changes into the protected vectors of the
157 // GraphChangesGenerator4UndiGraph
158 for (const auto& changes: legal_changes) {
159 for (const auto& change: changes) {
160 legal_changes_.insert(std::move(change));
161 }
162 }
163 }
164
166 template < typename STRUCT_CONSTRAINT >
167 INLINE void
168 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::setGraph(const UndiGraph& graph) {
169 // sets the current graph
170 graph_ = graph;
171
172 // generate the set of all changes
173 createChanges_();
174 }
175
177 template < typename STRUCT_CONSTRAINT >
178 INLINE void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::clearChanges() noexcept {
179 legal_changes_.clear();
180 }
181
183 template < typename STRUCT_CONSTRAINT >
184 INLINE typename GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::iterator
185 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::begin() const {
186 return legal_changes_.cbegin();
187 }
188
190 template < typename STRUCT_CONSTRAINT >
191 INLINE const typename GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::iterator&
192 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::end() const {
193 return legal_changes_.cend();
194 }
195
197 template < typename STRUCT_CONSTRAINT >
198 INLINE void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
199 const EdgeAddition& change) {}
200
202 template < typename STRUCT_CONSTRAINT >
203 INLINE void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
204 const EdgeDeletion& change) {}
205
207 template < typename STRUCT_CONSTRAINT >
208 INLINE void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
209 const GraphChange& change) {}
210
212 template < typename STRUCT_CONSTRAINT >
213 INLINE void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::notifyGetCompleted() {
214 if (legal_changes_.size()) legal_changes_.clear();
215 }
216
218 template < typename STRUCT_CONSTRAINT >
219 INLINE void
220 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::setMaxNbThreads(Size nb) noexcept {
221 if (nb == 0) nb = gum::getNumberOfThreads();
222 _max_threads_number_ = nb;
223 }
224
226 template < typename STRUCT_CONSTRAINT >
227 INLINE STRUCT_CONSTRAINT&
228 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::constraint() const noexcept {
229 return *constraint_;
230 }
231
232 } /* namespace learning */
233
234} /* namespace gum */
235
236#endif /* DOXYGEN_SHOULD_SKIP_THIS */
GraphChangesGenerator4UndiGraph(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.