aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
structuralConstraintTabuList.cpp
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
48
50
52#ifdef GUM_NO_INLINE
54#endif /* GUM_NO_INLINE */
55
56namespace gum {
57
58 namespace learning {
59
64 // insert the empty graph. The difference between the _tabuList_offset_ and
65 // the max offset in _graph_tabuList_ should always be equal to _tabuList_size_
67
68 GUM_CONSTRUCTOR(StructuralConstraintTabuList);
69 }
70
75 // compute the hash value of the diGraph
76 for (const auto& arc: graph.arcs()) {
77 const auto hash_arc = _hashArc_(arc.tail(), arc.head());
78 _current_graph_.first ^= hash_arc.first;
79 _current_graph_.second ^= hash_arc.second;
80 }
81
82 // insert the graph hash. The difference between the _tabuList_offset_ and
83 // the max offset in _graph_tabuList_ should always be equal to _tabuList_size_
85
86 GUM_CONSTRUCTOR(StructuralConstraintTabuList);
87 }
88
96
99 StructuralConstraintTabuList&& from) noexcept :
100 _graph_tabuList_(std::move(from._graph_tabuList_)),
101 _tabuList_offset_(from._tabuList_offset_), _tabuList_size_(from._tabuList_size_),
102 _current_graph_(std::move(from._current_graph_)) {
103 GUM_CONS_MOV(StructuralConstraintTabuList);
104 }
105
110
122
126 if (this != &from) {
127 _graph_tabuList_ = std::move(from._graph_tabuList_);
128 _tabuList_offset_ = from._tabuList_offset_;
129 _tabuList_size_ = from._tabuList_size_;
130 _current_graph_ = from._current_graph_;
131 }
132 return *this;
133 }
134
136 if (new_size == _tabuList_size_) return;
137
138 if (_graph_tabuList_.size() > new_size) {
139 // here, there are more graphs than allowed by the new size.
140 // So remove the oldest elements, so that only new_size elements remain
141 while (_graph_tabuList_.size() > new_size) {
144 }
145 } else {
146 // here, we should keep all the elements in _graph_tabuList_ but we must
147 // ensure that the difference between the max offset in _graph_tabuList_ and
148 // _tabuList_offset_ is equal to new_size
149 if (_tabuList_offset_ + _tabuList_size_ >= new_size) {
150 // we can modify _tabuList_offset_ so that the aforementioned difference
151 // is now equal to new_size
153 } else {
154 // basically, we should add _tabuList_size_ - new_size to _tabuList_offset_,
155 // except that the value of _tabuList_offset_ would be strictly negative,
156 // which is impossible for an attribute of type Size. Hence, here, instead
157 // of changing _tabuList_offset_, we will add (new_size - _tabuList_size_)
158 // to all the offsets in the tabu list
159 const Size delta = new_size - _tabuList_size_;
160 Bijection< GraphHash, Size > new_tabuList(2 * new_size);
161 for (auto iter = _graph_tabuList_.begin(); iter != _graph_tabuList_.end(); ++iter) {
162 new_tabuList.emplace(iter.first(), iter.second() + delta);
163 }
164 _graph_tabuList_ = std::move(new_tabuList);
165 }
166 }
167
168 _tabuList_size_ = new_size;
169 }
170
172 switch (change.type()) {
174 return checkArcAdditionAlone(change.node1(), change.node2());
175
177 return checkArcDeletionAlone(change.node1(), change.node2());
178
180 return checkArcReversalAlone(change.node1(), change.node2());
181
183 return checkArcTriangleDeletion1Alone(change.node1(), change.node2(), change.node3());
184
186 return checkArcTriangleDeletion2Alone(change.node1(), change.node2(), change.node3());
187
188 default :
190 "Graph change operation "
191 << change.typeAsString()
192 << "is not supported by the Tabu List structural constraint");
193 }
194 }
195
197 switch (change.type()) {
199 modifyGraphAlone(static_cast< const ArcAddition& >(change));
200 break;
201
203 modifyGraphAlone(static_cast< const ArcDeletion& >(change));
204 break;
205
207 modifyGraphAlone(static_cast< const ArcReversal& >(change));
208 break;
209
211 modifyGraphAlone(static_cast< const ArcTriangleDeletion1& >(change));
212 break;
213
215 modifyGraphAlone(static_cast< const ArcTriangleDeletion2& >(change));
216 break;
217
218 default :
220 "Graph change operation "
221 << change.typeAsString()
222 << " is not supported by Tabu List structural constraint")
223 }
224 }
225 } /* namespace learning */
226
227} /* namespace gum */
Base class for all oriented graphs.
Definition diGraph.h:132
Exception : operation not allowed.
The class for notifying learning algorithms of new arc additions.
The class for notifying learning algorithms of arc removals.
The class for notifying learning algorithms of arc reversals.
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node2->n...
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node1->n...
std::string typeAsString() const
returns a string corresponding to the type of the change
NodeId node1() const noexcept
returns the first node involved in the modification
GraphChangeType type() const noexcept
returns the type of the operation
NodeId node2() const noexcept
returns the second node involved in the modification
NodeId node3() const
returns the third node involved in the modification (if any)
The class imposing a N-sized tabu list as a structural constraints for learning algorithms.
void modifyGraphAlone(const ArcAddition &change)
notify the constraint of a modification of the graph
void setTabuListSize(Size new_size)
sets the size of the tabu list
bool checkArcTriangleDeletion1Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion1
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
Size _tabuList_offset_
the index of the oldest element
bool checkModificationAlone(const GraphChange &change) const
checks whether the constraints enable to perform a graph change
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
Bijection< GraphHash, Size > _graph_tabuList_
the tabu list
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
StructuralConstraintTabuList & operator=(const StructuralConstraintTabuList &from)
copy operator
bool checkArcTriangleDeletion2Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion2
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the class imposing a N-sized tabu list as a structural constraints for learning algorithms
#define GUM_STRUCTURAL_CONSTRAINT_TABU_LIST_DEFAULT_SIZE
the class imposing a N-sized tabu list as a structural constraints for learning algorithms