aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
score.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
62 // -------------------------------------------------------------------------
63 // Constructors and destructor are defined out-of-line (not INLINE) on
64 // purpose: under MSVC, a non-template dllexport'ed class has its
65 // inline-defined special members promoted to strong/eager symbol
66 // emission (instead of COMDAT), which causes LNK2005 duplicate-definition
67 // errors once several translation units include the inline body.
68 // -------------------------------------------------------------------------
69
72 const Prior& prior,
73 const std::vector< std::pair< std::size_t, std::size_t > >& ranges,
74 const Bijection< NodeId, std::size_t >& nodeId2columns) :
75 prior_(prior.clone()), counter_(parser, ranges, nodeId2columns) {
76 GUM_CONSTRUCTOR(Score);
77 }
78
80 Score::Score(const DBRowGeneratorParser& parser,
81 const Prior& prior,
82 const Bijection< NodeId, std::size_t >& nodeId2columns) :
83 prior_(prior.clone()), counter_(parser, nodeId2columns) {
84 GUM_CONSTRUCTOR(Score);
85 }
86
88 Score::Score(const Score& from) :
89 prior_(from.prior_->clone()), counter_(from.counter_), cache_(from.cache_),
90 use_cache_(from.use_cache_) {
91 GUM_CONS_CPY(Score);
92 }
93
95 Score::Score(Score&& from) :
96 prior_(from.prior_), counter_(std::move(from.counter_)), cache_(std::move(from.cache_)),
97 use_cache_(from.use_cache_) {
98 from.prior_ = nullptr;
99 GUM_CONS_MOV(Score);
100 }
101
103 Score::~Score() {
104 if (prior_ != nullptr) delete prior_;
105 GUM_DESTRUCTOR(Score);
106 }
107
109 Score& Score::operator=(const Score& from) {
110 if (this != &from) {
111 Prior* new_prior = from.prior_->clone();
112 RecordCounter new_counter = from.counter_;
113 ScoringCache new_cache = from.cache_;
114
115 if (prior_ != nullptr) delete prior_;
116
117 prior_ = new_prior;
118 counter_ = std::move(new_counter);
119 cache_ = std::move(new_cache);
120
121 use_cache_ = from.use_cache_;
122 }
123 return *this;
124 }
125
127 Score& Score::operator=(Score&& from) {
128 if (this != &from) {
129 std::swap(prior_, from.prior_);
130
131 counter_ = std::move(from.counter_);
132 cache_ = std::move(from.cache_);
133 use_cache_ = from.use_cache_;
134 }
135 return *this;
136 }
137
139
145 void Score::setRanges(const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
146 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
147 counter_.setRanges(new_ranges);
148 if (old_ranges != ranges()) clear();
149 }
150
152 void Score::clearRanges() {
153 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
154 counter_.clearRanges();
155 if (old_ranges != ranges()) clear();
156 }
157
159
163 std::vector< double > Score::marginalize_(const NodeId X_id,
164 const std::vector< double >& N_xyz) const {
165 // compute the domain sizes of the varible on the left hand side
166 // of the conditioning bar
167 const auto& nodeId2cols = this->counter_.nodeId2Columns();
168 const auto& database = this->counter_.database();
169 const std::size_t X_size
170 = database.domainSize(nodeId2cols.empty() ? X_id : nodeId2cols.second(X_id));
171
172 // determine the size of the output vector
173 std::size_t out_size = N_xyz.size() / X_size;
174
175 // allocate the output vector
176 std::vector< double > res(out_size, 0.0);
177
178 // fill the vector:
179 std::size_t xyz = std::size_t(0);
180 for (std::size_t z = std::size_t(0); z < out_size; ++z) {
181 for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
182 res[z] += N_xyz[xyz];
183 }
184 }
185
186 return res;
187 }
188
189
190 } /* namespace learning */
191
192} /* namespace gum */
193
194#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...
the base class for all a priori
Definition prior.h:84
Score(const DBRowGeneratorParser &parser, const Prior &external_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
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
the base class for all the scores used for learning (BIC, BDeu, etc)
the base class for all the scores used for learning (BIC, BDeu, etc)