aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
paramEstimator.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
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 const DBRowGeneratorParser& parser,
65 const Prior& external_prior,
66 const Prior& score_internal_prior,
67 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
68 const Bijection< NodeId, std::size_t >& nodeId2columns) :
69 counter_(parser, ranges, nodeId2columns) {
70 // copy the a prioris
71 external_prior_ = external_prior.clone();
72 try {
73 score_internal_prior_ = score_internal_prior.clone();
74 } catch (...) {
75 delete external_prior_;
76 throw;
77 }
78
79 GUM_CONSTRUCTOR(ParamEstimator);
80 }
81
83 ParamEstimator::ParamEstimator(const DBRowGeneratorParser& parser,
84 const Prior& external_prior,
85 const Prior& score_internal_prior,
86 const Bijection< NodeId, std::size_t >& nodeId2columns) :
87 counter_(parser, nodeId2columns) {
88 // copy the a prioris
89 external_prior_ = external_prior.clone();
90 try {
91 score_internal_prior_ = score_internal_prior.clone();
92 } catch (...) {
93 delete external_prior_;
94 throw;
95 }
96
97 GUM_CONSTRUCTOR(ParamEstimator);
98 }
99
100 // Copy and move constructors are defined out-of-line (not INLINE) on
101 // purpose: see the comment in score.cpp for the MSVC LNK2005 rationale.
102
104 ParamEstimator::ParamEstimator(const ParamEstimator& from) :
105 external_prior_(from.external_prior_->clone()),
106 score_internal_prior_(from.score_internal_prior_->clone()), counter_(from.counter_) {
107 GUM_CONS_CPY(ParamEstimator);
108 }
109
111 ParamEstimator::ParamEstimator(ParamEstimator&& from) noexcept :
112 external_prior_(from.external_prior_), score_internal_prior_(from.score_internal_prior_),
113 counter_(std::move(from.counter_)) {
114 from.external_prior_ = nullptr;
115 from.score_internal_prior_ = nullptr;
116 GUM_CONS_MOV(ParamEstimator);
117 }
118
120 ParamEstimator::~ParamEstimator() {
121 if (external_prior_ != nullptr) delete external_prior_;
122 if (score_internal_prior_ != nullptr) delete score_internal_prior_;
123
124 GUM_DESTRUCTOR(ParamEstimator);
125 }
126
128 ParamEstimator& ParamEstimator::operator=(const ParamEstimator& from) {
129 if (this != &from) {
130 if (external_prior_ != nullptr) {
131 delete external_prior_;
132 external_prior_ = nullptr;
133 }
134 external_prior_ = from.external_prior_->clone();
135
136 if (score_internal_prior_ != nullptr) {
137 delete score_internal_prior_;
138 external_prior_ = nullptr;
139 }
140 score_internal_prior_ = from.score_internal_prior_->clone();
141
142 counter_ = from.counter_;
143 }
144 return *this;
145 }
146
148 ParamEstimator& ParamEstimator::operator=(ParamEstimator&& from) {
149 if (this != &from) {
150 external_prior_ = from.external_prior_;
151 score_internal_prior_ = from.score_internal_prior_;
152 counter_ = std::move(from.counter_);
153 from.external_prior_ = nullptr;
154 from.score_internal_prior_ = nullptr;
155 }
156 return *this;
157 }
158
160
166 void ParamEstimator::setRanges(
167 const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
168 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
169 counter_.setRanges(new_ranges);
170 if (old_ranges != ranges()) clear();
171 }
172
174 void ParamEstimator::clearRanges() {
175 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
176 counter_.clearRanges();
177 if (old_ranges != ranges()) clear();
178 }
179
180
181 } /* namespace learning */
182
183} /* namespace gum */
184
185#endif /* DOXYGEN_SHOULD_SKIP_THIS */
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
ParamEstimator(const DBRowGeneratorParser &parser, const Prior &external_prior, const Prior &_score_internal_prior, const std::vector< std::pair< std::size_t, std::size_t > > &ranges, const Bijection< NodeId, std::size_t > &nodeId2columns=Bijection< NodeId, std::size_t >())
default constructor
the base class for all a priori
Definition prior.h:84
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the base class for estimating parameters of CPTs
the base class for estimating parameters of CPTs