Esempio n. 1
0
def main():
    max_generations = 100
    max_not_improved = 10

    # Creating initial population
    pop = Population( size = 100,
            crossover = 0.4,
            mutation = 0.05,
            elitism = 0.05,
            imigration = 0.3,
            tournament_size = 10,
            debug = False)

    # Generations without improvements
    no_improvements = 0
    for i in range(max_generations):
        print 'Generation %d' % (i+1)

        print 'Calculating fitness'
        pop.evaluate()
        pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
        else:
            no_improvements = 0

        if no_improvements == max_not_improved:
            break

        print 'Evolving'
        pop.evolve()
        print

    if no_improvements == max_not_improved:
        print '%d generations without improvement' % (max_not_improved)

    print 'Best solution:'
    pop.show_first()
def main():
    max_generations = 100
    max_not_improved = 10

    # Creating initial population
    pop = Population( size = 100,
            crossover = 0.4,
            mutation = 0.05,
            elitism = 0.05,
            imigration = 0.3,
            tournament_size = 10,
            debug = False)

    # Generations without improvements
    no_improvements = 0
    for i in range(max_generations):
        #print 'Generation %d' %(i)

        print 'Calculating fitness'
        pop.evaluate()
        pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
        else:
            no_improvements = 0

        if no_improvements == max_not_improved:
            break

        print 'Evolving'
        pop.evolve()
        print

    if no_improvements == max_not_improved:
        print '%d generations without improvement' % (max_not_improved)

    print 'Best solution:'
    pop.show_first()
Esempio n. 3
0
def main(filename=None, ports=[5000]):
    if not filename:
        print 'Using default parameters'
        params = {
            'max_generations': 200,
            'max_not_improved': 20,
            'size': 100,
            'crossover': 0.3,
            'mutation': 0.05,
            'elitism': 0.2,
            'imigration': 0.2,
            'tour_size': 8,
            'local': None
        }
    else:
        print 'Loading parameters from %s\n' % (filename)
        params = util.parse_json(filename)

    # Creating initial population
    pop = Population(size=params['size'],
                     crossover=params['crossover'],
                     mutation=params['mutation'],
                     elitism=params['elitism'],
                     imigration=params['imigration'],
                     tour_size=params['tour_size'],
                     local=params['local'])

    sockets = []
    for port in ports:
        sock = util.open_socket(int(port))
        sockets.append(sock)
    Chromo.sockets = sockets

    Chromo.cross_op = params['cross_op']

    # Generations without improvements
    no_improv = 0
    for i in range(params['max_generations']):
        print 'Generation %d' % (i + 1)

        print 'Calculating fitness'
        for sock in Chromo.sockets:
            util.send_msg(sock, 'NEWGEN\n')
        best = pop.evaluate()
        for sock in Chromo.sockets:
            util.send_msg(sock, 'ENDGEN\n')
        print 'main:current_best ', best
        #pop.plot_evolution()

        if not pop.improved:
            no_improvements += 1
            print "Didn't improve:", no_improvements
            if no_improvements == params['max_not_improved']:
                print 'Reached limit for not improved generations'
                break
        else:
            no_improvements = 0

        print 'Evolving'
        pop.evolve()
        print

    print '\nBest solution:'
    #pop.show_first()
    print 'Fitness: ', best[0]
    print 'Genes: ', Chromo.to_str(best[1]), '\''

    # Closing connection
    for sock in Chromo.sockets:
        util.send_msg(sock, 'ENDGA\n')
        sock.close()