aGrUM  0.14.2
structuralConstraintDAG_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
26 
27 # include <agrum/agrum.h>
30 
31 namespace gum {
32 
33  namespace learning {
34 
36  INLINE void StructuralConstraintDAG::setGraphAlone(const DiGraph& graph) {
37  // check that the digraph has no cycle
38  DAG g;
39 
40  for (auto node : graph)
41  g.addNodeWithId(node);
42 
43  for (auto& arc : graph.arcs())
44  g.addArc(arc.tail(), arc.head());
45 
47  }
48 
50  INLINE void StructuralConstraintDAG::setGraphAlone(Size nb_nodes) {
51  DAG g;
52 
53  for (NodeId i = 0; i < nb_nodes; ++i) {
54  g.addNodeWithId(i);
55  }
56 
58  }
59 
62  NodeId y) const {
64  }
65 
68  NodeId y) const {
70  }
71 
74  NodeId y) const {
76  }
77 
80  const ArcAddition& change) const {
81  return checkArcAdditionAlone(change.node1(), change.node2());
82  }
83 
86  const ArcDeletion& change) const {
87  return checkArcDeletionAlone(change.node1(), change.node2());
88  }
89 
92  const ArcReversal& change) const {
93  return checkArcReversalAlone(change.node1(), change.node2());
94  }
95 
98  const GraphChange& change) const {
99  switch (change.type()) {
101  return checkArcAdditionAlone(change.node1(), change.node2());
102 
104  return checkArcDeletionAlone(change.node1(), change.node2());
105 
107  return checkArcReversalAlone(change.node1(), change.node2());
108 
109  default:
110  GUM_ERROR(OperationNotAllowed,
111  "edge modifications are not "
112  "supported by StructuralConstraintDAG");
113  }
114  }
115 
117  INLINE void
118  StructuralConstraintDAG::modifyGraphAlone(const ArcAddition& change) {
119  _DAG__cycle_detector.addArc(change.node1(), change.node2());
120  }
121 
123  INLINE void
124  StructuralConstraintDAG::modifyGraphAlone(const ArcDeletion& change) {
125  _DAG__cycle_detector.eraseArc(change.node1(), change.node2());
126  }
127 
129  INLINE void
130  StructuralConstraintDAG::modifyGraphAlone(const ArcReversal& change) {
131  _DAG__cycle_detector.reverseArc(change.node1(), change.node2());
132  }
133 
135  INLINE void
136  StructuralConstraintDAG::modifyGraphAlone(const GraphChange& change) {
137  switch (change.type()) {
139  modifyGraphAlone(reinterpret_cast< const ArcAddition& >(change));
140  break;
141 
143  modifyGraphAlone(reinterpret_cast< const ArcDeletion& >(change));
144  break;
145 
147  modifyGraphAlone(reinterpret_cast< const ArcReversal& >(change));
148  break;
149 
150  default:
151  GUM_ERROR(OperationNotAllowed,
152  "edge modifications are not supported by DAG constraints");
153  }
154  }
155 
157  INLINE bool
158  StructuralConstraintDAG::isAlwaysInvalidAlone(const GraphChange&) const {
159  return false;
160  }
161 
163  INLINE void StructuralConstraintDAG::setGraph(const DAG& graph) {
164  constraints::setGraph(graph);
166  }
167 
169  INLINE void StructuralConstraintDAG::setGraph(Size nb_nodes) {
171  setGraphAlone(nb_nodes);
172  }
173 
174 // include all the methods applicable to the whole class hierarchy
175 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintDAG
177 # undef GUM_CONSTRAINT_CLASS_NAME
178 
179  } /* namespace learning */
180 
181 } /* namespace gum */
182 
183 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
bool checkModificationAlone(const ArcAddition &change) const
checks whether the constraints enable to add an arc
void setGraph(const DAG &graph)
sets a new graph from which we will perform checkings
void setGraph(Size nb_nodes)
sets a new empty graph from which we will perform checkings
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
void eraseArc(NodeId x, NodeId y)
removes an arc from the current DAG
void addArc(NodeId x, NodeId y)
adds a new arc to the current DAG
void modifyGraphAlone(const ArcAddition &change)
notify the constraint of a modification of the graph
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
void setDAG(const DAG &dag)
sets the initial DAG from which changes shall be applied
the base class for structural constraints used by learning algorithms that learn a directed graph str...
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph...
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
bool hasCycleFromAddition(NodeId x, NodeId y) const noexcept
indicates whether an arc addition would create a cycle
bool hasCycleFromReversal(NodeId x, NodeId y) const noexcept
indicates wether an arc reversal would create a cycle
bool hasCycleFromDeletion(NodeId x, NodeId y) const noexcept
indicates whether an arc deletion would create a cycle
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Size NodeId
Type for node ids.
Definition: graphElements.h:97
DAGCycleDetector _DAG__cycle_detector
the cycle detector used to check quickly graph modifications
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
void reverseArc(NodeId x, NodeId y)
reverses an arc from the DAG