aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
score_inl.h
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#pragma once
42
43
49#include <agrum/BN/learning/scores/score.h> // to ease IDE parser
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52namespace gum {
53
54 namespace learning {
55
56 // Constructors and destructor are defined out-of-line in score.cpp
57 // on purpose -- see the comment there.
58
61
64
66 INLINE bool Score::isGumNumberOfThreadsOverriden() const {
68 }
69
72 INLINE void Score::setMinNbRowsPerThread(const std::size_t nb) const {
74 }
75
77 INLINE std::size_t Score::minNbRowsPerThread() const { return counter_.minNbRowsPerThread(); }
78
80 INLINE const std::vector< std::pair< std::size_t, std::size_t > >& Score::ranges() const {
81 return counter_.ranges();
82 }
83
85 INLINE double Score::score(const NodeId var) {
86 IdCondSet idset(var, empty_ids_, true);
87 if (use_cache_) {
88 if (auto ptr_score = cache_.tryGet(idset)) { return *ptr_score; }
89 double the_score = score_(idset);
90 cache_.insert(std::move(idset), the_score);
91 return the_score;
92 } else {
93 return score_(std::move(idset));
94 }
95 }
96
98
101 INLINE double Score::score(const NodeId var, const std::vector< NodeId >& rhs_ids) {
102 IdCondSet idset(var, rhs_ids, false);
103 if (use_cache_) {
104 if (auto ptr_score = cache_.tryGet(idset)) { return *ptr_score; }
105 const double the_score = score_(idset);
106 cache_.insert(std::move(idset), the_score);
107 return the_score;
108 } else {
109 return score_(idset);
110 }
111 }
112
114 INLINE void Score::clear() {
115 counter_.clear();
116 cache_.clear();
117 }
118
120 INLINE void Score::clearCache() { cache_.clear(); }
121
123 INLINE void Score::useCache(const bool on_off) { use_cache_ = on_off; }
124
126 INLINE bool Score::isUsingCache() const { return use_cache_; }
127
129 INLINE const Bijection< NodeId, std::size_t >& Score::nodeId2Columns() const {
130 return counter_.nodeId2Columns();
131 }
132
134 INLINE const DatabaseTable& Score::database() const { return counter_.database(); }
135
136 } /* namespace learning */
137
138} /* namespace gum */
139
140#endif /* DOXYGEN_SHOULD_SKIP_THIS */
bool isGumNumberOfThreadsOverriden() const override
indicates whether the class containing this ThreadNumberManager set its own number of threads
Size getNumberOfThreads() const override
returns the current max number of threads used by the class containing this ThreadNumberManager
The class representing a tabular database as used by learning tasks.
std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
const std::vector< std::pair< std::size_t, std::size_t > > & ranges() const
returns the current ranges
const DatabaseTable & database() const
returns the database on which we perform the counts
void clear()
clears all the last database-parsed counting from memory
const Bijection< NodeId, std::size_t > & nodeId2Columns() const
returns the mapping from ids to column positions in the database
void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context
void setNumberOfThreads(Size nb) override
sets the number max of threads that can be used
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
double score(const NodeId var)
returns the score of a single node
const DatabaseTable & database() const
return the database used by the score
virtual std::size_t minNbRowsPerThread() const
returns the minimum of rows that each thread should process
const Bijection< NodeId, std::size_t > & nodeId2Columns() const
return the mapping between the columns of the database and the node ids
const std::vector< NodeId > empty_ids_
an empty vector
Definition score.h:250
RecordCounter counter_
the record counter used for the counts over discrete variables
Definition score.h:241
bool isUsingCache() const
indicates whether the score uses a cache
void setNumberOfThreads(Size nb) override
sets the number max of threads that can be used
virtual double score_(const IdCondSet &idset)=0
returns the score for a given IdCondSet
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
bool isGumNumberOfThreadsOverriden() const override
indicates whether the user set herself the number of threads
virtual void setMinNbRowsPerThread(const std::size_t nb) const
changes the number min of rows a thread should process in a multithreading context
void clearCache()
clears the current cache
void useCache(const bool on_off)
turn on/off the use of a cache of the previously computed score
Size getNumberOfThreads() const override
returns the current max number of threads of the scheduler
void insert(const IdCondSet &idset, double score)
insert a new score into the cache
void clear()
removes all the stored scores
optional_ref< double > tryGet(const IdCondSet &idset)
returns a pointer to a given score, or nullptr if not cached
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55
class GUM_SHARED_PUBLIC IdCondSet
Definition idCondSet.h:68
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)