Skip to content

Commit

Permalink
Handles the case when no legal operations are possible [refs #1348]
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurankan committed Nov 11, 2020
1 parent 84046ca commit 836a44c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pgmpy/estimators/HillClimbSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ def estimate(
iteration = range(int(max_iter))

# Step 2: For each iteration, find the best scoring operation and
# do that to the current model.
# do that to the current model. If no legal operation is
# possible, sets best_operation=None.
for _ in iteration:
best_operation, best_score_delta = max(
self._legal_operations(
Expand All @@ -297,6 +298,7 @@ def estimate(
fixed_edges,
),
key=lambda t: t[1],
default=(None, None),
)

if best_operation is None or best_score_delta < epsilon:
Expand Down
19 changes: 19 additions & 0 deletions pgmpy/tests/test_estimators/test_HillClimbSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,25 @@ def test_estimate_titanic(self):
in self.est_titanic2.estimate(fixed_edges=[("Pclass", "Survived")]).edges()
)

def test_no_legal_operation(self):
data = pd.DataFrame(
[
[1, 0, 0, 1, 0, 0, 1, 1, 0],
[1, 0, 1, 0, 0, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 1, 0, 0],
],
columns=list("ABCDEFGHI"),
)
est = HillClimbSearch(data)
best_model = est.estimate(
fixed_edges=[("A", "B"), ("B", "C")], white_list=[("F", "C")]
)
self.assertEqual(
set(best_model.edges()), set([("A", "B"), ("B", "C"), ("F", "C")])
)

def tearDown(self):
del self.rand_data
del self.est_rand
Expand Down

0 comments on commit 836a44c

Please sign in to comment.