35 template <
typename GRAPH_CHANGES_
SELECTOR >
38 selector.setGraph(dag);
40 unsigned int nb_changes_applied = 0;
41 Idx applied_change_with_positive_score = 0;
48 std::vector< bool > impacted_queues(dag.
size(),
false);
52 double best_score = 0;
53 double current_score = 0;
54 double delta_score = 0;
57 applied_change_with_positive_score = 0;
60 std::vector< std::pair< NodeId, double > > ordered_queues =
61 selector.nodesSortedByBestScore();
63 for (
Idx j = 0; j < dag.
size(); ++j) {
64 NodeId i = ordered_queues[j].first;
66 if (!selector.empty(i)
67 && (!nb_changes_applied || (selector.bestScore(i) > 0))) {
72 switch (change.
type()) {
74 if (!impacted_queues[change.
node2()]
75 && selector.isChangeValid(change)) {
76 if (selector.bestScore(i) > 0) {
77 ++applied_change_with_positive_score;
78 }
else if (current_score > best_score) {
79 best_score = current_score;
88 delta_score += selector.bestScore(i);
89 current_score += selector.bestScore(i);
91 impacted_queues[change.
node2()] =
true;
92 selector.applyChangeWithoutScoreUpdate(change);
99 if (!impacted_queues[change.
node2()]
100 && selector.isChangeValid(change)) {
101 if (selector.bestScore(i) > 0) {
102 ++applied_change_with_positive_score;
103 }
else if (current_score > best_score) {
104 best_score = current_score;
113 delta_score += selector.bestScore(i);
114 current_score += selector.bestScore(i);
116 impacted_queues[change.
node2()] =
true;
117 selector.applyChangeWithoutScoreUpdate(change);
118 ++nb_changes_applied;
124 if ((!impacted_queues[change.
node1()])
125 && (!impacted_queues[change.
node2()])
126 && selector.isChangeValid(change)) {
127 if (selector.bestScore(i) > 0) {
128 ++applied_change_with_positive_score;
129 }
else if (current_score > best_score) {
130 best_score = current_score;
139 delta_score += selector.bestScore(i);
140 current_score += selector.bestScore(i);
143 impacted_queues[change.
node1()] =
true;
144 impacted_queues[change.
node2()] =
true;
145 selector.applyChangeWithoutScoreUpdate(change);
146 ++nb_changes_applied;
153 "edge modifications are not " 154 "supported by local search");
161 selector.updateScoresAfterAppliedChanges();
164 for (
auto iter = impacted_queues.begin(); iter != impacted_queues.end();
172 if (applied_change_with_positive_score) {
174 nb_changes_applied = 0;
187 if (current_score > best_score) {
195 template <
typename GUM_SCALAR,
196 typename GRAPH_CHANGES_SELECTOR,
197 typename PARAM_ESTIMATOR >
200 PARAM_ESTIMATOR& estimator,
A class that, given a structure and a parameter estimator returns a full Bayes net.
Class representing a Bayesian Network.
the classes to account for structure changes in a graph
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
Size size() const
alias for sizeNodes
void initApproximationScheme()
Initialise the scheme.
gum is the global namespace for all aGrUM entities
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
GraphChangeType type() const noexcept
returns the type of the operation
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
void stopApproximationScheme()
Stop the approximation scheme.
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
BayesNet< GUM_SCALAR > learnBN(GRAPH_CHANGES_SELECTOR &selector, PARAM_ESTIMATOR &estimator, DAG initial_dag=DAG())
learns the structure and the parameters of a BN
Size __MaxNbDecreasing
the max number of changes decreasing the score that we allow to apply
NodeId node2() const noexcept
returns the second node involved in the modification
Size Idx
Type for indexes.
A class that, given a structure and a parameter estimator returns a full Bayes net.
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
NodeId node1() const noexcept
returns the first node involved in the modification