1 /**
2  *
3  *   Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
4  *   info_at_agrum_dot_org
5  *
6  *  This library is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Lesser General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public License
17  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The K2 algorithm
24  *
25  * @author Christophe GONZALES(_at_AMU) and Pierre-Henri WUILLEMIN(_at_LIP6)
26  */
27 
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32   namespace learning {
33 
34     /// default constructor
K2()35     INLINE K2::K2() {
36       GUM_CONSTRUCTOR(K2);
37       ;
38     }
39 
40     /// copy constructor
K2(const K2 & from)41     INLINE K2::K2(const K2& from) : GreedyHillClimbing(from), _order_(from._order_) {
42       GUM_CONS_CPY(K2);
43     }
44 
45     /// move constructor
K2(K2 && from)46     INLINE K2::K2(K2&& from) :
47         GreedyHillClimbing(std::move(from)), _order_(std::move(from._order_)) {
48       GUM_CONS_MOV(K2);
49     }
50 
51     /// destructor
~K2()52     INLINE K2::~K2() {
53       GUM_DESTRUCTOR(K2);
54       ;
55     }
56 
57     /// copy operator
58     INLINE K2& K2::operator=(const K2& from) {
59       if (this != &from) {
60         GreedyHillClimbing::operator=(from);
61         _order_                     = from._order_;
62       }
63       return *this;
64     }
65 
66     /// move operator
67     INLINE K2& K2::operator=(K2&& from) {
68       if (this != &from) {
69         GreedyHillClimbing::operator=(std::move(from));
70         _order_                     = std::move(from._order_);
71       }
72       return *this;
73     }
74 
75     /// sets the order on the variables
setOrder(const Sequence<NodeId> & order)76     INLINE void K2::setOrder(const Sequence< NodeId >& order) { _order_ = order; }
77 
78     /// sets the order on the variables
setOrder(const std::vector<NodeId> & order)79     INLINE void K2::setOrder(const std::vector< NodeId >& order) {
80       _order_.clear();
81       for (const auto node: order) {
82         _order_.insert(node);
83       }
84     }
85 
86     /// returns the current order
order()87     INLINE const Sequence< NodeId >& K2::order() const noexcept { return _order_; }
88 
89     /** @brief checks that the order passed to K2 is coherent with the variables
90      * as specified by their modalities */
_checkOrder_(const std::vector<Size> & modal)91     INLINE void K2::_checkOrder_(const std::vector< Size >& modal) {
92       if (modal.size() != _order_.size()) {
93         GUM_ERROR(InvalidArgument,
94                   "the number of elements in the order given "
95                   "to K2 is not the same as the number of nodes");
96       }
97       bool order_ok = true;
98       for (const auto node: _order_) {
99         if (node >= _order_.size()) {
100           order_ok = false;
101           break;
102         }
103       }
104       if (!order_ok) {
105         GUM_ERROR(InvalidArgument,
106                   "there exist at least one node in the order "
107                   "given to K2 that has no domain size");
108       }
109     }
110 
111     /// returns the approximation policy of the learning algorithm
approximationScheme()112     INLINE ApproximationScheme& K2::approximationScheme() {
113       return GreedyHillClimbing::approximationScheme();
114     }
115 
116   } /* namespace learning */
117 
118 } /* namespace gum */
119 
120 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
121