aGrUM 3.1.1
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-2026 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-2026 *
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#pragma once
42
43
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
55 namespace learning {
56
58 template < typename STRUCT_CONSTRAINT >
60 STRUCT_CONSTRAINT& constraint) : constraint_(&constraint) {
61 GUM_CONSTRUCTOR(GraphChangesGenerator4UndiGraph);
62 }
63
65 template < typename STRUCT_CONSTRAINT >
66 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4UndiGraph(
67 const GraphChangesGenerator4UndiGraph& from) :
68 graph_(from.graph_), constraint_(from.constraint_), legal_changes_(from.legal_changes_),
69 _max_threads_number_(from._max_threads_number_) {
70 GUM_CONS_CPY(GraphChangesGenerator4UndiGraph);
71 }
72
74 template < typename STRUCT_CONSTRAINT >
75 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4UndiGraph(
76 GraphChangesGenerator4UndiGraph&& from) :
77 graph_(std::move(from.graph_)), constraint_(from.constraint_),
78 legal_changes_(std::move(from.legal_changes_)),
79 _max_threads_number_(from._max_threads_number_) {
80 GUM_CONS_MOV(GraphChangesGenerator4UndiGraph);
81 }
82
84 template < typename STRUCT_CONSTRAINT >
85 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::~GraphChangesGenerator4UndiGraph() {
86 GUM_DESTRUCTOR(GraphChangesGenerator4UndiGraph);
87 }
88
90 template < typename STRUCT_CONSTRAINT >
91 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
92 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::operator=(
93 const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from) {
94 if (this != &from) {
95 graph_ = from.graph_;
96 constraint_ = from.constraint_;
97 legal_changes_ = from.legal_changes_;
98 _max_threads_number_ = from._max_threads_number_;
99 }
100 return *this;
101 }
102
104 template < typename STRUCT_CONSTRAINT >
105 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
106 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::operator=(
107 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from) {
108 if (this != &from) {
109 graph_ = std::move(from.graph_);
110 constraint_ = std::move(from.constraint_);
111 legal_changes_ = std::move(from.legal_changes_);
112 _max_threads_number_ = from._max_threads_number_;
113 }
114 return *this;
115 }
116
118 template < typename STRUCT_CONSTRAINT >
119 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::createChanges_() {
120 legal_changes_.clear();
121
122 // for all the pairs of nodes, consider adding, reverse and removing arcs
123 // do it for each thread
124 const Size nb_threads = _max_threads_number_;
125 std::vector< Set< GraphChange > > legal_changes(nb_threads);
126
127 // create the lambda that will be used to fill the legal changes
128 auto threadedLegalSet = [this, &legal_changes](const std::size_t this_thread,
129 const std::size_t nb_threads) -> void {
130 Idx i = 0;
131 for (const auto node1: this->graph_) {
132 if (i == this_thread) {
133 for (const auto node2: this->graph_) {
134 if (node1 != node2) {
135 // try edge additions
136 EdgeAddition edge_add(node1, node2);
137 if (!this->constraint_->isAlwaysInvalid(edge_add)) {
138 legal_changes[this_thread].insert(std::move(edge_add));
139 }
140
141 // try edge deletion
142 EdgeDeletion edge_del(node1, node2);
143 if (!this->constraint_->isAlwaysInvalid(edge_del)) {
144 legal_changes[this_thread].insert(std::move(edge_del));
145 }
146 }
147 }
148 }
149 ++i;
150 i %= nb_threads;
151 }
152 };
153
154 // launch the threads
155 ThreadExecutor::execute(nb_threads, threadedLegalSet);
156
157
158 // now store the changes into the protected vectors of the
159 // GraphChangesGenerator4UndiGraph
160 for (const auto& changes: legal_changes) {
161 for (const auto& change: changes) {
162 legal_changes_.insert(std::move(change));
163 }
164 }
165 }
166
168 template < typename STRUCT_CONSTRAINT >
169 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::setGraph(const UndiGraph& graph) {
170 // sets the current graph
171 graph_ = graph;
172
173 // generate the set of all changes
174 createChanges_();
175 }
176
178 template < typename STRUCT_CONSTRAINT >
179 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::clearChanges() noexcept {
180 legal_changes_.clear();
181 }
182
184 template < typename STRUCT_CONSTRAINT >
185 typename GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::iterator
186 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::begin() const {
187 return legal_changes_.cbegin();
188 }
189
191 template < typename STRUCT_CONSTRAINT >
192 const typename GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::iterator&
193 GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::end() const {
194 return legal_changes_.cend();
195 }
196
198 template < typename STRUCT_CONSTRAINT >
199 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
200 const EdgeAddition& change) {}
201
203 template < typename STRUCT_CONSTRAINT >
204 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
205 const EdgeDeletion& change) {}
206
208 template < typename STRUCT_CONSTRAINT >
209 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::modifyGraph(
210 const GraphChange& change) {}
211
213 template < typename STRUCT_CONSTRAINT >
214 void GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::notifyGetCompleted() {
215 if (legal_changes_.size()) legal_changes_.clear();
216 }
217
219 template < typename STRUCT_CONSTRAINT >
220 void 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 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
The basic class for computing the set of undigraph changes allowed by the user to be executed by the ...
include the inlined functions if necessary
Definition CSVParser.h:55
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.