CS156
Chris Pollett
Sep 18, 2017
def my_pow(x, y): """ Computes x raised to the power of y For example, >>> my_pow(3, 4) 81 Here are some base cases: >>> my_pow(0,0) 1 >>> my_pow(1,0) 1 >>> my_pow(2,0) 1 Since our method does something different when y is odd or even we have a test for each case: >>> my_pow(2,4) 16 >>> my_pow(2,5) 32 """ if y == 0: return 1 tmp = my_pow(x, y/2) tmp = tmp * tmp if y == 2 * (y / 2) : return tmp else : return tmp * x if __name__ == '__main__': #run as the main program from command line (as opposed to imported by #someone else import doctest doctest.testmod()
We argue the case where the nodes might form a DAG, the admissible case is similar.
Lemma. Suppose `h(n)` is a consistent heuristic, then the values of `f(n)` along any path are nondecreasing.
Proof. Suppose `n'` is a successor of `n`, then `g(n') = g(n) + c(n, a, n')` for some action `a` and we have:
`f(n') = g(n') + h(n') = g(n) + c(n, a, n') + h(n') mbox( (by consistency) ) geq g(n) + h(n) = f(n)`. QED.
Lemma. Whenever `A^star` selects a node `n` for expansion, the optimal path to that node has been found.
Proof. If this were not the case, there would have to be another frontier node `n'` on the optimal path from the start node to `n`. Here `n'` is on the frontier, as the frontier nodes of the graph always separate the unexplored region of the graph from the explored region, and if it was in the explored region we would have selected `n'` already on the path to `n` to get a lower solution. Since `f` is nondecreasing along any path, `n'` would have lower `f`-cost than `n` and would have been selected before `n`. QED.
It follows from these two lemmas that the sequence of nodes expanded by `A^star` is in non-decreasing order of `f(n)`. Hence, the first goal node selected for expansion must be optimal because `f` is the true cost for goal nodes and all later goal nodes will be at least as expensive. (QED optimality proof).
Which of the following is true?
function RBF-SEARCH(problem) returns a solution, or failure return RBFS(problem, MAKE-NODE(problem.INITIAL_STATE), infty) function RBFS(problem, node, f_limit) returns a solution, or failure and a new f-cost limit if problem.GOAL-TEST(node.STATE) then return SOLUTION(node) successors := [] for each action in problem.ACTIONS(node.STATE) do add CHILD-NODE(problem, node, action) into successors if successors is empty then return failure, infty for each s in successors do /* update f with value from previous search, if any */ s.f = max(s.g + s.h, node.f) loop do best := the lowest f-value node in successors if best.f > f_limit then return failure, best.f alternative := the second-lowest f-value among successors result, best.f := RBFS(problem, best, min(f_limit, alternative)) if result != failure then return result
function HILL_CLIMBING(problem) returns a state that is a local maximum current := MAKE_NODE(problem, INITIAL-STATE) loop do neighbor := a highest-valued successor of current if(neighbor.value ≤ current.value) return current.state current := neighbor