aGrUM 2.3.2
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-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
63 Score& Score::operator=(const Score& from) {
64 if (this != &from) {
65 Prior* new_prior = from.prior_->clone();
66 RecordCounter new_counter = from.counter_;
67 ScoringCache new_cache = from.cache_;
68
69 if (prior_ != nullptr) delete prior_;
70
71 prior_ = new_prior;
72 counter_ = std::move(new_counter);
73 cache_ = std::move(new_cache);
74
75 use_cache_ = from.use_cache_;
76 }
77 return *this;
78 }
79
82 if (this != &from) {
83 std::swap(prior_, from.prior_);
84
85 counter_ = std::move(from.counter_);
86 cache_ = std::move(from.cache_);
87 use_cache_ = from.use_cache_;
88 }
89 return *this;
90 }
91
93
99 void Score::setRanges(const std::vector< std::pair< std::size_t, std::size_t > >& new_ranges) {
100 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
101 counter_.setRanges(new_ranges);
102 if (old_ranges != ranges()) clear();
103 }
104
106 void Score::clearRanges() {
107 std::vector< std::pair< std::size_t, std::size_t > > old_ranges = ranges();
108 counter_.clearRanges();
109 if (old_ranges != ranges()) clear();
110 }
111
113
117 std::vector< double > Score::marginalize_(const NodeId X_id,
118 const std::vector< double >& N_xyz) const {
119 // compute the domain sizes of the varible on the left hand side
120 // of the conditioning bar
121 const auto& nodeId2cols = this->counter_.nodeId2Columns();
122 const auto& database = this->counter_.database();
123 const std::size_t X_size
124 = database.domainSize(nodeId2cols.empty() ? X_id : nodeId2cols.second(X_id));
125
126 // determine the size of the output vector
127 std::size_t out_size = N_xyz.size() / X_size;
128
129 // allocate the output vector
130 std::vector< double > res(out_size, 0.0);
131
132 // fill the vector:
133 std::size_t xyz = std::size_t(0);
134 for (std::size_t z = std::size_t(0); z < out_size; ++z) {
135 for (std::size_t x = std::size_t(0); x < X_size; ++x, ++xyz) {
136 res[z] += N_xyz[xyz];
137 }
138 }
139
140 return res;
141 }
142
143
144 } /* namespace learning */
145
146} /* namespace gum */
147
148#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The base class for all the scores used for learning (BIC, BDeu, etc).
Definition score.h:68
Prior * prior_
the expert knowledge a priorwe add to the score
Definition score.h:238
void clearRanges()
reset the ranges to the one range corresponding to the whole database
const std::vector< std::pair< std::size_t, std::size_t > > & ranges() const
returns the current ranges
void clear()
clears all the data structures from memory, including the cache
const DatabaseTable & database() const
return the database used by the score
Score & operator=(const Score &from)
copy operator
RecordCounter counter_
the record counter used for the counts over discrete variables
Definition score.h:241
void setRanges(const std::vector< std::pair< std::size_t, std::size_t > > &new_ranges)
sets new ranges to perform the counts used by the score
std::vector< double > marginalize_(const NodeId X_id, const std::vector< double > &N_xyz) const
returns a counting vector where variables are marginalized from N_xyz
bool use_cache_
a Boolean indicating whether we wish to use the cache
Definition score.h:247
ScoringCache cache_
the scoring cache
Definition score.h:244
Size NodeId
Type for node ids.
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 all the scores used for learning (BIC, BDeu, etc)
the base class for all the scores used for learning (BIC, BDeu, etc)