aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
ConstraintBasedLearning.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
53#ifndef GUM_CONSTRAINT_BASED_LEARNING_H
54#define GUM_CONSTRAINT_BASED_LEARNING_H
55
56#include <limits>
57#include <sstream>
58#include <string>
59#include <vector>
60
61#include <agrum/config.h>
62
65
66#define GUM_SL_EMIT(x, y, action, explain) \
67 { \
68 std::ostringstream action_stream; \
69 action_stream << action; \
70 std::ostringstream explain_stream; \
71 explain_stream << explain; \
72 GUM_EMIT4(onStructuralModification, x, y, action_stream.str(), explain_stream.str()); \
73 }
74
75namespace gum {
76
77 namespace learning {
78
79 using ThreePoints = std::tuple< NodeId, NodeId, NodeId >;
80
98 public:
99 // ##########################################################################
101 // ##########################################################################
103
105 explicit ConstraintBasedLearning(int maxLog);
108 ~ConstraintBasedLearning() override;
109
111
114
115 // ##########################################################################
117 // ##########################################################################
119
120 void setForbiddenGraph(const gum::DiGraph& forbidGraph);
121 void setMandatoryGraph(const gum::DAG& mandaGraph);
123 void addConstraints(const HashTable< std::pair< NodeId, NodeId >, char >& constraints);
124 const std::vector< Arc > latentVariables() const;
125
127
128 // ##########################################################################
130 // ##########################################################################
132
135
138
140 template < GUM_Numeric GUM_SCALAR = double, typename PARAM_ESTIMATOR >
141 BayesNet< GUM_SCALAR > learnBN(PARAM_ESTIMATOR& estimator, MixedGraph graph);
142
144
146
147 protected:
148 // ##########################################################################
150 // ##########################################################################
152
154
156
158
159 // ##########################################################################
161 // ##########################################################################
163
164 static bool _existsDirectedPath_(const MixedGraph& graph, NodeId n1, NodeId n2);
165 static bool _existsNonTrivialDirectedPath_(const MixedGraph& graph, NodeId n1, NodeId n2);
167 std::vector< ThreePoints > unshieldedTriples_(const MixedGraph& graph);
168
170
174 MixedGraph initGraph_(const MixedGraph& template_graph);
175
177
179
180 // ##########################################################################
182 // ##########################################################################
184
185 bool isForbiddenArc_(NodeId x, NodeId y) const;
186 bool isForbiddenEdge_(NodeId x, NodeId y) const;
187 bool isMaxIndegree_(const MixedGraph& graph, NodeId x);
188 bool isArcValid_(const MixedGraph& graph, NodeId x, NodeId y);
189
191
192 // ##########################################################################
194 // ##########################################################################
195
199 std::vector< Arc > _latentCouples_;
200 int _maxLog_{100};
201 const std::vector< NodeId > _emptySet_;
202
203 private:
204 gum::Size _maxIndegree_{std::numeric_limits< gum::Size >::max()};
205 };
206
207 } /* namespace learning */
208
209} /* namespace gum */
210
212
213#endif /* GUM_CONSTRAINT_BASED_LEARNING_H */
Meek rules for orienting edges in a mixed graph.
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme(bool verbosity=false)
Base class for dag.
Definition DAG.h:121
Base class for all oriented graphs.
Definition diGraph.h:132
The class for generic Hash Tables.
Definition hashTable.h:640
Applies Meek's orientation rules to propagate arc directions in a mixed graph.
Definition MeekRules.h:67
Base class for mixed graphs.
Definition mixedGraph.h:146
Base class for partially directed acyclic graphs.
Definition PDAG.h:130
virtual MixedGraph learnSkeleton(MixedGraph graph)=0
Signaler< gum::NodeId, gum::NodeId, std::string, std::string > onStructuralModification
void setMandatoryGraph(const gum::DAG &mandaGraph)
std::vector< ThreePoints > unshieldedTriples_(const MixedGraph &graph)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
static bool _existsNonTrivialDirectedPath_(const MixedGraph &graph, NodeId n1, NodeId n2)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
gum::MeekRules meekRules_
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
bool isForbiddenEdge_(NodeId x, NodeId y) const
bool isMaxIndegree_(const MixedGraph &graph, NodeId x)
void setForbiddenGraph(const gum::DiGraph &forbidGraph)
const std::vector< Arc > latentVariables() const
bool isArcValid_(const MixedGraph &graph, NodeId x, NodeId y)
void orientDoubleHeadedArcs_(MixedGraph &mg)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
PDAG learnPDAG(MixedGraph graph)
learns the essential graph (CPDAG)
HashTable< std::pair< NodeId, NodeId >, char > _initialMarks_
static bool _existsDirectedPath_(const MixedGraph &graph, NodeId n1, NodeId n2)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
BayesNet< GUM_SCALAR > learnBN(PARAM_ESTIMATOR &estimator, MixedGraph graph)
learns structure then estimates parameters
void addConstraints(const HashTable< std::pair< NodeId, NodeId >, char > &constraints)
virtual MixedGraph learnMixedStructure(MixedGraph graph)=0
ConstraintBasedLearning & operator=(const ConstraintBasedLearning &)
DAG learnDAG(MixedGraph graph)
learns a DAG
void applyStructuralConstraints_(MixedGraph &graph)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
MixedGraph initGraph_(const MixedGraph &template_graph)
Builds a complete MixedGraph on the nodes of template_graph, minus edges forbidden by structural cons...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
std::tuple< NodeId, NodeId, NodeId > ThreePoints
gum is the global namespace for all aGrUM entities
Definition agrum.h:46