From c462d52e0de7f84dff29f947f75bc232cdd91466 Mon Sep 17 00:00:00 2001 From: Josiah Date: Sat, 30 Sep 2017 19:17:00 -0400 Subject: [PATCH] So in order to have effective problem solving algorithms, I need node objects that can break up the states spaces into usable pieces --- josiah_exercises/3_3_vacuum.py | 20 ++++++++++++ josiah_exercises/QueenProblem.py | 56 ++++++++++++++++++++++++++++++++ josiah_exercises/jNode.py | 0 3 files changed, 76 insertions(+) create mode 100644 josiah_exercises/3_3_vacuum.py create mode 100644 josiah_exercises/QueenProblem.py create mode 100644 josiah_exercises/jNode.py diff --git a/josiah_exercises/3_3_vacuum.py b/josiah_exercises/3_3_vacuum.py new file mode 100644 index 000000000..0fe31b510 --- /dev/null +++ b/josiah_exercises/3_3_vacuum.py @@ -0,0 +1,20 @@ +from search import Problem + + +class Vacuum(Problem): + def __init__(self, initial, goal=None): + """The constructor specifies the initial state, and possibly a goal + state, if there is a unique goal. Your subclass's constructor can add + other arguments.""" + super().__init__(initial, goal) + self.initial = initial + self.goal = goal + + def actions(self, state): + pass + + def result(self, state, action): + pass + + def value(self, state): + pass diff --git a/josiah_exercises/QueenProblem.py b/josiah_exercises/QueenProblem.py new file mode 100644 index 000000000..5dfb68e15 --- /dev/null +++ b/josiah_exercises/QueenProblem.py @@ -0,0 +1,56 @@ +from search import Problem + +from utils import ( + is_in, argmin, argmax, argmax_random_tie, probability, weighted_sampler, + memoize, print_table, DataFile, Stack, FIFOQueue, PriorityQueue, name +) + +class QueenProblem(Problem): + def __init__(self, initial, goal=None): + """The constructor specifies the initial state, and possibly a goal + state, if there is a unique goal. Your subclass's constructor can add + other arguments.""" + if initial is None: + initial = goal * None + super().__init__(initial, goal) + self.initial = initial + self.goal = goal + + def actions(self, state): + """Return the actions that can be executed in the given + state. The result would typically be a list, but if there are + many actions, consider yielding them one at a time in an + iterator, rather than building them all at once.""" + raise NotImplementedError + + def result(self, state, action): + """Return the state that results from executing the given + action in the given state. The action must be one of + self.actions(state).""" + raise NotImplementedError + + def goal_test(self, state): + """Return True if the state is a goal. The default method compares the + state to self.goal or checks for state in self.goal if it is a + list, as specified in the constructor. Override this method if + checking against a single self.goal is not enough.""" + if isinstance(self.goal, list): + return is_in(state, self.goal) + else: + return state == self.goal + + def path_cost(self, c, state1, action, state2): + """Return the cost of a solution path that arrives at state2 from + state1 via action, assuming cost c to get up to state1. If the problem + is such that the path doesn't matter, this function will only look at + state2. If the path does matter, it will consider c and maybe state1 + and action. The default method costs 1 for every step in the path.""" + return c + 1 + + def value(self, state): + """For optimization problems, each state has a value. Hill-climbing + and related algorithms try to maximize this value.""" + raise NotImplementedError + + +# someSearchMethod(QueenProblem(0, goal=8)) diff --git a/josiah_exercises/jNode.py b/josiah_exercises/jNode.py new file mode 100644 index 000000000..e69de29bb