aGrUM 2.3.2
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-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 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
101 ParamEstimator::~ParamEstimator() {
102 if (external_prior_ != nullptr) delete external_prior_;
103 if (score_internal_prior_ != nullptr) delete score_internal_prior_;
104
105 GUM_DESTRUCTOR(ParamEstimator);
106 }
107
109 ParamEstimator& ParamEstimator::operator=(const ParamEstimator& from) {
110 if (this != &from) {
111 if (external_prior_ != nullptr) {
112 delete external_prior_;
113 external_prior_ = nullptr;
114 }
115 external_prior_ = from.external_prior_->clone();
116
117 if (score_internal_prior_ != nullptr) {
118 delete score_internal_prior_;
119 external_prior_ = nullptr;
120 }
121 score_internal_prior_ = from.score_internal_prior_->clone();
122
123 counter_ = from.counter_;
124 }
125 return *this;
126 }
127
129 ParamEstimator& ParamEstimator::operator=(ParamEstimator&& from) {
130 if (this != &from) {
131 external_prior_ = from.external_prior_;
132 score_internal_prior_ = from.score_internal_prior_;
133 counter_ = std::move(from.counter_);
134 from.external_prior_ = nullptr;
135 from.score_internal_prior_ = nullptr;
136 }
137 return *this;
138 }
139
141
147 void ParamEstimator::setRanges(
148 const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
149 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
150 counter_.setRanges(new_ranges);
151 if (old_ranges != ranges()) clear();
152 }
153
155 void ParamEstimator::clearRanges() {
156 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
157 counter_.clearRanges();
158 if (old_ranges != ranges()) clear();
159 }
160
161
162 } /* namespace learning */
163
164} /* namespace gum */
165
166#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:83
include the inlined functions if necessary
Definition CSVParser.h:54
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