From 2a46e187bf22b16a746e5d4345c09e41a0d01c24 Mon Sep 17 00:00:00 2001 From: Kyungwon Chun Date: Mon, 30 Oct 2023 02:33:08 +0900 Subject: [PATCH 1/4] Define equality of MCState If __eq__ and __hash__ are not defined, duplicated MCStates will be piled up in the explored set. --- Chapter2/missionaries.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Chapter2/missionaries.py b/Chapter2/missionaries.py index 92a5692..733321f 100644 --- a/Chapter2/missionaries.py +++ b/Chapter2/missionaries.py @@ -34,6 +34,16 @@ def __str__(self) -> str: "The boat is on the {} bank.")\ .format(self.wm, self.wc, self.em, self.ec, ("west" if self.boat else "east")) + def __eq__(self, other) -> bool: + return (self.wm == other.wm) and (self.wc == other.wc) and \ + (self.em == other.em) and (self.ec == other.ec) and \ + (self.boat == other.boat) + + def __hash__(self): + state: int = self.wm * 1000 + self.wc * 100 + self.em * 10 + self.ec + state *= 1 if self.boat else -1 + return hash(state) + def goal_test(self) -> bool: return self.is_legal and self.em == MAX_NUM and self.ec == MAX_NUM From 95f2e0631dd41fa601ebceff85e51e7d5a1f72a2 Mon Sep 17 00:00:00 2001 From: Kyungwon Chun Date: Mon, 30 Oct 2023 02:47:36 +0900 Subject: [PATCH 2/4] Generalize hash func for arbitrary MAX_NUM --- Chapter2/missionaries.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Chapter2/missionaries.py b/Chapter2/missionaries.py index 733321f..ae0b009 100644 --- a/Chapter2/missionaries.py +++ b/Chapter2/missionaries.py @@ -39,10 +39,10 @@ def __eq__(self, other) -> bool: (self.em == other.em) and (self.ec == other.ec) and \ (self.boat == other.boat) - def __hash__(self): - state: int = self.wm * 1000 + self.wc * 100 + self.em * 10 + self.ec + def __hash__(self) -> int: + state: int = self.wm * MAX_NUM**3 + self.wc * MAX_NUM**2 + self.em * MAX_NUM + self.ec state *= 1 if self.boat else -1 - return hash(state) + return state def goal_test(self) -> bool: return self.is_legal and self.em == MAX_NUM and self.ec == MAX_NUM From 1b383d781e162be183f51d880053eb755e3e47bb Mon Sep 17 00:00:00 2001 From: Kyungwon Chun Date: Mon, 30 Oct 2023 02:53:58 +0900 Subject: [PATCH 3/4] Generate fixed size hash value --- Chapter2/missionaries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter2/missionaries.py b/Chapter2/missionaries.py index ae0b009..61c33a6 100644 --- a/Chapter2/missionaries.py +++ b/Chapter2/missionaries.py @@ -42,7 +42,7 @@ def __eq__(self, other) -> bool: def __hash__(self) -> int: state: int = self.wm * MAX_NUM**3 + self.wc * MAX_NUM**2 + self.em * MAX_NUM + self.ec state *= 1 if self.boat else -1 - return state + return hash(state) def goal_test(self) -> bool: return self.is_legal and self.em == MAX_NUM and self.ec == MAX_NUM From f7cdbb2521802696257247e589b7da7c78b0113a Mon Sep 17 00:00:00 2001 From: Kyungwon Chun Date: Mon, 30 Oct 2023 18:26:45 +0900 Subject: [PATCH 4/4] Make base number bigger than digit A base number should be bigger than a digit. --- Chapter2/missionaries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter2/missionaries.py b/Chapter2/missionaries.py index 61c33a6..565d610 100644 --- a/Chapter2/missionaries.py +++ b/Chapter2/missionaries.py @@ -40,7 +40,7 @@ def __eq__(self, other) -> bool: (self.boat == other.boat) def __hash__(self) -> int: - state: int = self.wm * MAX_NUM**3 + self.wc * MAX_NUM**2 + self.em * MAX_NUM + self.ec + state: int = self.wm * (MAX_NUM + 1)**3 + self.wc * (MAX_NUM + 1)**2 + self.em * (MAX_NUM + 1) + self.ec state *= 1 if self.boat else -1 return hash(state)