From 04c2f34fc043298407a29e4890f20118d5ffbe9f Mon Sep 17 00:00:00 2001 From: EdmondTongyou Date: Sat, 26 Feb 2022 12:25:43 -0800 Subject: [PATCH] Rework #2 --- wolfgoatcabbage.py | 69 +++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/wolfgoatcabbage.py b/wolfgoatcabbage.py index c22da86a8..f3db74693 100644 --- a/wolfgoatcabbage.py +++ b/wolfgoatcabbage.py @@ -3,63 +3,68 @@ class WolfGoatCabbage(Problem): # 0 denotes that the object is on the west bank # 1 denotes that the object is on the east bank # Farmer must always move per action - def __init__(self, initial=({"F", "G", "W", "C"}, {"", "", "", ""}), goal=({"", "", "", ""}, {"F", "G", "W", "C"})): + def __init__(self, initial=(("F", "G", "W", "C"), ("", "", "", "")), goal=(("", "", "", ""), ("F", "G", "W", "C"))): super().__init__(initial, goal) def actions(self, state): - possible_actions = [{"F", "G"}, - {"F", "W"}, - {"F", "C"}, - {"F"} + possible_actions = [["FG"], + ["FW"], + ["FC"], + ["F"] ] - if (state == ({"F", "W", "G", "C"}, {"", "", "", ""})): - possible_actions.remove({"F", "W"}, - {"F", "C"}) + if (state == (["F", "W", "G", "C"], ["", "", "", ""])): + possible_actions.remove(["FW"]) + possible_actions.remove(["FC"]) - elif (state == ({"", "W", "", "C"}, {"F", "", "G", ""}) or ({"F", "", "G", ""}, {"", "W", "", "C"})): - possible_actions.remove({"F", "W"}, - {"F", "C"}) + elif (state == (["", "W", "", "C"], ["F", "", "G", ""]) or (["F", "", "G", ""], ["", "W", "", "C"])): + possible_actions.remove(["FW"]) + possible_actions.remove(["FC"]) - elif (state == ({"F" , "W" , "", "C" }, {"", "", "G", ""}) or ({"" , "" , "G", "" }, {"F", "W", "", "C"})): - possible_actions.remove({"F" , "G" }) + elif (state == (["F" , "W" , "", "C" ], ["", "", "G", ""]) or (["" , "" , "G", "" ], ["F", "W", "", "C"])): + possible_actions.remove(["FG" ]) - elif (state == ({"F", "W", "G", "" }, {"", "", "","C"}) or ({"" , "" , "", "C" }, {"F", "W", "G", ""})): - possible_actions.remove({"F" , "C" }, - {"F"}) + elif (state == (["F", "W", "G", "" ], ["", "", "","C"]) or (["" , "" , "", "C" ], ["F", "W", "G", ""])): + possible_actions.remove(["FC" ]) + possible_actions.remove(["F"]) - elif (state == ({"F" , "" , "G", "C" }, {"", "W", "", ""}) or ({"" , "W" , "", "" }, {"F", "", "G", "C"})): - possible_actions.remove({"F", "W"}, - {"F"}) + elif (state == (["F" , "" , "G", "C" ], ["", "W", "", ""]) or (["" , "W" , "", "" ], ["F", "", "G", "C"])): + possible_actions.remove(["FW"]) + possible_actions.remove(["F"]) else: return - return frozenset(possible_actions) + return possible_actions def result(self, state, action): - new_state = set(state) + new_state = list(state) illegal_states = ( # All Illegal States - ({"", "", "G" , "C"} , {"F", "W", "", ""}) , - ({"F", "W", "" , ""} , {"", "", "G", "C"}) , - ({"", "W", "G" , "C"} , {"F", "", "", ""}) , - ({"F", "", "" , ""} , {"", "W", "G", "C"}), - ({"", "W", "G" , ""} , {"F", "", "", "C"}) , - ({"F", "", "" , "C"} , {"", "W", "G", ""}) , + (["", "", "G" , "C"] , ["F", "W", "", ""]) , + (["F", "W", "" , ""] , ["", "", "G", "C"]) , + (["", "W", "G" , "C"] , ["F", "", "", ""]) , + (["F", "", "" , ""] , ["", "W", "G", "C"]), + (["", "W", "G" , ""] , ["F", "", "", "C"]) , + (["F", "", "" , "C"] , ["", "W", "G", ""]) , ) if (state in illegal_states): return - new_state = action.actions(new_state) - return frozenset(new_state) + if (action == "FG"): + return + elif (action == "FW"): + return + elif (action == "FC"): + return + elif (action == "F"): + return + + return tuple(new_state) def goal_test(self, state): - if isinstance(self.goal, list): - return is_in(state, self.goal) - else: return state == self.goal if __name__ == '__main__':