aGrUM 3.1.1
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-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(GraphChangesGenerator4DiGraph);
62 }
63
65 template < typename STRUCT_CONSTRAINT >
66 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4DiGraph(
67 const GraphChangesGenerator4DiGraph& 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(GraphChangesGenerator4DiGraph);
71 }
72
74 template < typename STRUCT_CONSTRAINT >
75 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4DiGraph(
76 GraphChangesGenerator4DiGraph&& 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(GraphChangesGenerator4DiGraph);
81 }
82
84 template < typename STRUCT_CONSTRAINT >
85 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::~GraphChangesGenerator4DiGraph() {
86 GUM_DESTRUCTOR(GraphChangesGenerator4DiGraph);
87 }
88
90 template < typename STRUCT_CONSTRAINT >
91 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
92 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::operator=(
93 const GraphChangesGenerator4DiGraph< 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 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
106 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::operator=(
107 GraphChangesGenerator4DiGraph< 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 GraphChangesGenerator4DiGraph< 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 arc additions
136 ArcAddition arc_add(node1, node2);
137 if (!this->constraint_->isAlwaysInvalid(arc_add)) {
138 legal_changes[this_thread].insert(std::move(arc_add));
139 }
140
141 // try arc deletion
142 ArcDeletion arc_del(node1, node2);
143 if (!this->constraint_->isAlwaysInvalid(arc_del)) {
144 legal_changes[this_thread].insert(std::move(arc_del));
145 }
146
147 // try arc reversal
148 ArcReversal arc_rev(node1, node2);
149 if (!this->constraint_->isAlwaysInvalid(arc_rev)) {
150 legal_changes[this_thread].insert(std::move(arc_rev));
151 }
152 }
153 }
154 }
155 ++i;
156 i %= nb_threads;
157 }
158 };
159
160 // launch the threads
161 ThreadExecutor::execute(nb_threads, threadedLegalSet);
162
163
164 // now store the changes into the protected vectors of the
165 // GraphChangesGenerator4DiGraph
166 for (const auto& changes: legal_changes) {
167 for (const auto& change: changes) {
168 legal_changes_.insert(std::move(change));
169 }
170 }
171 }
172
174 template < typename STRUCT_CONSTRAINT >
175 void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::setGraph(const DiGraph& graph) {
176 // sets the current graph
177 graph_ = graph;
178
179 // generate the set of all changes
180 createChanges_();
181 }
182
184 template < typename STRUCT_CONSTRAINT >
185 void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::clearChanges() noexcept {
186 legal_changes_.clear();
187 }
188
190 template < typename STRUCT_CONSTRAINT >
191 typename GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::iterator
192 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::begin() const {
193 return legal_changes_.cbegin();
194 }
195
197 template < typename STRUCT_CONSTRAINT >
198 const typename GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::iterator&
199 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::end() const {
200 return legal_changes_.cend();
201 }
202
204 template < typename STRUCT_CONSTRAINT >
205 void
206 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcAddition& change) {
207 }
208
210 template < typename STRUCT_CONSTRAINT >
211 void
212 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcDeletion& change) {
213 }
214
216 template < typename STRUCT_CONSTRAINT >
217 void
218 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const ArcReversal& change) {
219 }
220
222 template < typename STRUCT_CONSTRAINT >
223 void
224 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::modifyGraph(const GraphChange& change) {
225 }
226
228 template < typename STRUCT_CONSTRAINT >
229 void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::notifyGetCompleted() {
230 if (legal_changes_.size()) legal_changes_.clear();
231 }
232
234 template < typename STRUCT_CONSTRAINT >
235 void GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::setMaxNbThreads(Size nb) noexcept {
236 if (nb == 0) nb = gum::getNumberOfThreads();
237 _max_threads_number_ = nb;
238 }
239
241 template < typename STRUCT_CONSTRAINT >
242 STRUCT_CONSTRAINT&
243 GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >::constraint() const noexcept {
244 return *constraint_;
245 }
246
247 } /* namespace learning */
248
249} /* namespace gum */
250
251#endif /* DOXYGEN_SHOULD_SKIP_THIS */
GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
The basic class for computing the set of digraph changes allowed by the user to be executed by the le...
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.