Beispiel #1
0
def search(queue, initialState, factory, goalTest, maxdepth=float('inf'), maxcost=float('inf')) :
    closedList = {}
    nodesEnqueued = 1
    nodesDequeued = 0
    nodesExpanded = 0
    queue.insert(initialState)
    ### you complete this.
    ### While there are states in the queue,
    ###   1. Dequeue
    ###   2. If this is the goal, stop
    ###   3. If not, insert in the closed list and generate successors
    ###   4. If successors are not in the closed list, enqueue them.

    found = False
    while not queue.isEmpty():
        current_state = queue.pop()
        nodesDequeued += 1

        if current_state.isGoal(goalTest):
            found = True
            break

        newf = searchQueues.f(current_state, goal_vertex)
        if (current_state.depth >= maxdepth) or (newf > maxcost):
            successors = []
            if newf < queue.mincost : queue.mincost = newf
        else:
            successors = factory.successors(current_state)

        closedList[current_state.vertex.name] = current_state
        nodesExpanded += len(successors)

        for succ_state in successors:
            if (not succ_state.vertex.name in closedList) or (closedList[succ_state.vertex.name] > succ_state):
                queue.insert(succ_state)
                nodesEnqueued += 1

    if not found:
        print "Search failed!"
        print "============================="
        return found

    print "nodes enqueued: %s" % nodesEnqueued
    print "nodes dequeued: %s" % nodesDequeued
    print "nodes expanded: %s" % nodesExpanded
    printSolution(current_state)

    # Find the max depth in closedList
    max_depth = 0
    for name, state in closedList.iteritems():
        if state.depth > max_depth: max_depth = state.depth
    print "search max depth: %s" % max_depth
    print "============================="

    return found
Beispiel #2
0
    # init node
    init_vertex = graph.vertex_list[args.initialState]
    init_state = Node(init_vertex, None, 0, 0)
    goal_name = args.goal       # [Hack] Global var for goalTest()
    goal_vertex = graph.vertex_list[goal_name]
    if args.search != 'IDAStar': queue = getattr(searchQueues, args.search + 'Queue')(goal_vertex)
    factory = NodeFactory(graph)

    if args.search == 'DFS':
        if args.isIterative:
            print "Searching from %s to %s by %s iteratively with %s limit..." % (args.initialState, args.goal, args.search, args.depthLimit)
            depth_limit = min(args.depthLimit, len(graph.vertex_list))
            for depth_limit in range(0, depth_limit):
                print "Now trying with depth limit %s..." % (depth_limit)
                if search(queue, init_state, factory, goalTest, depth_limit): break
        else:
            print "Searching from %s to %s by %s with depth limit %s..." % (args.initialState, args.goal, args.search, args.depthLimit)
            search(queue, init_state, factory, goalTest, maxdepth=args.depthLimit)
    elif args.search == 'IDAStar':
        f = searchQueues.f(init_state, goal_vertex)
        print "Searching from %s to %s by %s with depth limit %s and cost limit %f..." % (args.initialState, args.goal, args.search, args.depthLimit, f)
        while True:
            print "Now trying with cost limit %f..." % f
            queue = searchQueues.DFSQueue(goal_vertex)
            res = search(queue, init_state, factory, goalTest, maxdepth=args.depthLimit, maxcost=f)
            if (res) or (queue.mincost == float('inf')): break
            f = queue.mincost
    else:
        print "Searching from %s to %s by %s..." % (args.initialState, args.goal, args.search)
        search(queue, init_state, factory, goalTest)