Exemplo n.º 1
0
def _local_search(problem,
                  fringe_expander,
                  iterations_limit=0,
                  fringe_size=1,
                  random_initial_states=False):
    '''
    Basic algorithm for all local search algorithms.
    '''
    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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
    def _expander(fringe, iteration, viewer):
        fitness = [x.value for x in fringe]
        sampler = InverseTransformSampler(fitness, fringe)
        new_generation = []

        expanded_nodes = []
        expanded_neighbors = []

        for _ in fringe:
            node1 = sampler.sample()
            node2 = sampler.sample()
            child = problem.crossover(node1.state, node2.state)
            action = 'crossover'
            if random.random() < mutation_chance:
                # Noooouuu! she is... he is... *IT* is a mutant!
                child = problem.mutate(child)
                action += '+mutation'

            child_node = SearchNodeValueOrdered(state=child,
                                                problem=problem,
                                                action=action)
            new_generation.append(child_node)

            expanded_nodes.append(node1)
            expanded_neighbors.append([child_node])
            expanded_nodes.append(node2)
            expanded_neighbors.append([child_node])

        if viewer:
            viewer.event('expanded', expanded_nodes, expanded_neighbors)

        fringe.clear()
        for node in new_generation:
            fringe.append(node)
Exemplo n.º 4
0
    def _expander(fringe, iteration):
        fitness = [x.value() for x in fringe]
        sampler = InverseTransformSampler(fitness, fringe)
        new_generation = []
        for _ in fringe:
            node1 = sampler.sample()
            node2 = sampler.sample()
            child = problem.crossover(node1.state, node2.state)
            if random.random() < mutation_chance:
                # Noooouuu! she is... he is... *IT* is a mutant!
                child = problem.mutate(child)
            new_generation.append(child)

        fringe.clear()
        for s in new_generation:
            fringe.append(SearchNodeValueOrdered(state=s, problem=problem))
Exemplo n.º 5
0
    def test_search_node_value_sorted(self):
        n1 = SearchNodeValueOrdered(problem=self.problem, state='iab')
        n2 = SearchNodeValueOrdered(problem=self.problem, state='iba')

        self.assertTrue(n1 < n2)
        self.assertFalse(n2 < n1)