예제 #1
0
def _local_search(problem, fringe_expander, iterations_limit=0, fringe_size=1,
                  random_initial_states=False):
    fringe = BoundedPriorityQueue(fringe_size)
    if random_initial_states:
        for _ in xrange(fringe_size):
            s = problem.generate_random_state()
            fringe.append(SearchNodeValueOrdered(state=s, problem=problem))
    else:
        fringe.append(SearchNodeValueOrdered(state=problem.initial_state,
                                             problem=problem))

    iteration = 0
    run = True
    best = None
    while run:
        old_best = fringe[0]
        fringe_expander(fringe, iteration)
        best = fringe[0]

        iteration += 1

        if iterations_limit and iteration >= iterations_limit:
            run = False
        elif old_best.value() >= best.value():
            run = False

    return best
예제 #2
0
def greedy(problem, graph_search=False, viewer=None):
    '''
    Greedy search.
    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeHeuristicOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)
예제 #3
0
def uniform_cost(problem, graph_search=False, viewer=None):
    '''
    Uniform cost search.
    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, and SearchProblem.cost.
    '''
    return _search(problem,
                   BoundedPriorityQueue(),
                   graph_search=graph_search,
                   node_factory=SearchNodeCostOrdered,
                   graph_replace_when_better=True,
                   viewer=viewer)
예제 #4
0
def astar(problem, graph_search=False, viewer=None):
    '''
    A* search.
    If graph_search=True, will avoid exploring repeated states.
    Requires: SearchProblem.actions, SearchProblem.result,
    SearchProblem.is_goal, SearchProblem.cost, and SearchProblem.heuristic.
    '''
    print ("I am in astar() now in traditional")
    return _search(problem,
                   BoundedPriorityQueue(), # this is defined in the utils
                   graph_search=graph_search,
                   node_factory=SearchNodeStarOrdered, # this is defined in the models. This is a class name so an object of this class is created as it is passed as an argument to a function
                   graph_replace_when_better=True,
                   viewer=viewer)
예제 #5
0
파일: methods.py 프로젝트: eguaio/simple-ai
def beam_search(problem, beam_size=100):
    fringe = BoundedPriorityQueue(beam_size)
    fringe.append(SearchNodeValueOrdered(state=problem.initial_state,
                                         problem=problem))
    while fringe:
        successors = BoundedPriorityQueue(beam_size)
        for node in fringe:
            if problem.is_goal(node.state):
                return node
            successors.extend(node.expand())
        fringe = successors
예제 #6
0
파일: local.py 프로젝트: mkiadi2002/astar
def _local_search(problem,
                  fringe_expander,
                  iterations_limit=0,
                  fringe_size=1,
                  random_initial_states=False,
                  stop_when_no_better=True,
                  viewer=None):
    '''
    Basic algorithm for all local search algorithms.
    '''
    if viewer:
        viewer.event('started')

    fringe = BoundedPriorityQueue(fringe_size)
    if random_initial_states:
        for _ in range(fringe_size):
            s = problem.generate_random_state()
            fringe.append(SearchNodeValueOrdered(state=s, problem=problem))
    else:
        fringe.append(
            SearchNodeValueOrdered(state=problem.initial_state,
                                   problem=problem))

    finish_reason = ''
    iteration = 0
    run = True
    best = None

    while run:
        if viewer:
            viewer.event('new_iteration', list(fringe))

        old_best = fringe[0]
        fringe_expander(fringe, iteration, viewer)
        best = fringe[0]

        iteration += 1

        if iterations_limit and iteration >= iterations_limit:
            run = False
            finish_reason = 'reaching iteration limit'
        elif old_best.value >= best.value and stop_when_no_better:
            run = False
            finish_reason = 'not being able to improve solution'

    if viewer:
        viewer.event('finished', fringe, best,
                     'returned after %s' % finish_reason)

    return best