aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBRowGeneratorSet.cpp
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
41
48
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
54# ifdef GUM_NO_INLINE
56# endif /* GUM_NO_INLINE */
57
58namespace gum {
59
60 namespace learning {
61
64 _generators_(from._nb_generators_, nullptr), _nb_generators_(from._nb_generators_),
65 _setInputRow_performed_(from._nb_generators_, 0) {
66 // create the generators
67 for (std::size_t i = std::size_t(0); i < _nb_generators_; ++i)
68 _generators_[i] = from._generators_[i]->clone();
69
70 GUM_CONS_CPY(DBRowGeneratorSet);
71 }
72
74 DBRowGeneratorSet* DBRowGeneratorSet::clone() const { return new DBRowGeneratorSet(*this); }
75
77 void DBRowGeneratorSet::clear() {
78 // delete all the generators
79 for (auto gen: _generators_) {
80 delete gen;
81 }
82
83 // clear all the internal fields
84 _generators_.clear();
85 _nb_generators_ = std::size_t(0);
86 _output_row_ = nullptr;
87 _setInputRow_performed_.clear();
88 }
89
91 DBRowGeneratorSet::~DBRowGeneratorSet() {
92 GUM_DESTRUCTOR(DBRowGeneratorSet);
93 clear();
94 }
95
97 DBRowGeneratorSet& DBRowGeneratorSet::operator=(const DBRowGeneratorSet& from) {
98 if (this != &from) {
99 // produce the new generators
100 std::vector< DBRowGenerator* > new_generators(from._nb_generators_, nullptr);
101 for (std::size_t i = std::size_t(0); i < from._nb_generators_; ++i) {
102 try {
103 new_generators[i] = from._generators_[i]->clone();
104 } catch (...) {
105 for (std::size_t j = std::size_t(0); j < i; ++j) {
106 delete new_generators[j];
107 }
108 throw;
109 }
110 }
111
112 // create the setInputDBrow_performed vector
113 std::vector< int > setInputDBrow_performed(from._nb_generators_, 0);
114
115 // remove the old generators and copy the new ones
116 clear();
117 _generators_ = std::move(new_generators);
118 _nb_generators_ = from._nb_generators_;
119 _output_row_ = nullptr;
120 _setInputRow_performed_ = std::move(setInputDBrow_performed);
121 }
122
123 return *this;
124 }
125
127 DBRowGeneratorSet& DBRowGeneratorSet::operator=(DBRowGeneratorSet&& from) {
128 if (this != &from) {
129 // remove the old generators and move the new ones
130 clear();
131 _generators_ = std::move(from._generators_);
132 _nb_generators_ = from._nb_generators_;
133 _output_row_ = from._output_row_;
134 _setInputRow_performed_ = std::move(from._setInputRow_performed_);
135 }
136 return *this;
137 }
138
139 // try to produce a new row
140 bool DBRowGeneratorSet::_produceNextRow_(const DBRow< DBTranslatedValue >* input_row,
141 std::size_t i) {
142 // the generation of output rows can be viewed as the traversal of a
143 // tree: each node of the tree correspond to the input row received by
144 // a generator. So the root node is the row passed in argument to
145 // the setInputDBrow() Method. From these input rows, generators produce
146 // through their generate() method new output rows, which correspond to
147 // the input rows of the next level of the tree. If we traverse this tree
148 // in terms of generators rather than in terms of input rows, we should
149 // call once Method setInputDBrow() in order to update the generators
150 // data structures, and then, to call method generate() to create new
151 // output rows. When some generators are unable to produce output rows,
152 // we just need to backtrack in the tree
153
154 // for the ith generator, we set the new input DBRow passed in
155 // argument of setInputDBRow. We ask it to generate a new output row.
156 // If this can be done, the new output row is passed as a new input DBRow
157 // for the next generator, and so on. If a generator cannot produce any
158 // output row, we ask its predecessors to generate new rows (backtrack),
159 // until all the generators have been able to generate at least one output
160 // row (or no output row can be produced from input_row).
161 const DBRow< DBTranslatedValue >* row = input_row;
162 while (i != _nb_generators_) {
163 auto generator = _generators_[i];
164
165 // if we did not pass any row yet to the ith generator, do it
166 // else use method generate() to generate a new output row
167 if (_setInputRow_performed_[i] == 0) {
168 // pass the current row
169 const bool has_rows = generator->setInputRow(*row);
170
171 // if the generator could not create output rows, try to backtrack
172 if (!has_rows) {
173 if (i > std::size_t(0)) {
174 --i;
175 continue;
176 } else {
177 // here we were unable to generate output rows
178 _output_row_ = nullptr;
179 return false;
180 }
181 } else {
182 // here, the generator is able to generate output rows
183 // so, generate the first one
184 row = &(generator->generate());
185 _setInputRow_performed_[i] = 1;
186
187 // pass to the next generator
188 ++i;
189 }
190 } else {
191 // here, the generator has already performed its setInputDBRow call
192 // so we should ask it to generate a new row. If it cannot produce
193 // any more row, try to backtrack
194 if (generator->hasRows()) {
195 // get the new row
196 row = &(generator->generate());
197
198 // pass to the next generator
199 ++i;
200 } else {
201 // indicate that the next time we use this generator, we will have
202 // to use method setInputDBRow and backtrack
203 _setInputRow_performed_[i] = 0;
204 if (i > std::size_t(0)) {
205 --i;
206 continue;
207 } else {
208 // here we were unable to generate output rows
209 _output_row_ = nullptr;
210 return false;
211 }
212 }
213 }
214 }
215
216 // here row contains a row generated on a leaf of the row generation tree
217 // we should keep it when the user will ask for the next row to generate
218 _output_row_ = row;
219 return true;
220 }
221
222 } /* namespace learning */
223
224} /* namespace gum */
225
226#endif /* DOXYGEN_SHOULD_SKIP_THIS */
class for packing sets of generators
The class used to pack sets of generators.
DBRowGeneratorSet()
default constructor
std::mt19937 & generator()
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46