Ejemplo n.º 1
0
def main():
    population = create_initial_population()

    population = Evolver.sort_by_fitness(population)

    print('Initial population:')
    print_population(population)

    fitness_evolution = [(0, fitnesses(population))]

    try:
        for n in range(iteration):
            population = Evolver.evolve(population)

            print(n, fitnesses(population), end="    \r")

            if (n + 1) % (iteration // 4) == 0:
                fitness_evolution += [(n + 1, fitnesses(population))]

    except KeyboardInterrupt:
        pass

    finally:
        print('Final population:')
        print_population(population)

        plot_fitness_distributions(fitness_evolution)

        plt.show()
    def generate(self):
        logging.info(
            "***generate(generations, population, all_possible_genes, dataset)***"
        )
        t_start = datetime.datetime.now()
        t = time.time()

        evolver = Evolver(self.params)
        genomes = evolver.create_population(self.population)

        ofile = open(self.path + '/result.csv', "w")
        writer = csv.writer(ofile, delimiter=',')

        table_head = list()
        for p in self.params:
            table_head.append(str(p))

        table_head.append("accuracy")
        row = table_head
        writer.writerow(row)

        # Evolve the generation.
        for i in range(self.generations):
            logging.info("***Now in generation %d of %d***" %
                         (i + 1, self.generations))
            self.print_genomes(genomes)

            # Train and get accuracy for networks/genomes.
            self.train_genomes(genomes, writer)

            # Get the average accuracy for this generation.
            average_accuracy = self.get_average_accuracy(genomes)

            # Print out the average accuracy each generation.
            logging.info("Generation average: %.2f%%" %
                         (average_accuracy * 100))
            logging.info('-' * 80)

            # Evolve, except on the last iteration.
            if i != self.generations - 1:
                genomes = evolver.evolve(genomes)

        # Sort our final population according to performance.
        genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True)

        # Print out the top 5 networks/genomes.
        self.print_genomes(genomes[:5])

        ofile.close()
        total = time.time() - t
        m, s = divmod(total, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 24)
        t_stop = datetime.datetime.now()
        file = open(self.path + '/total_time.txt', 'w')
        file.write('Start : ' + str(t_start) + '\n')
        file.write('Stop : ' + str(t_stop) + '\n')
        file.write('Total :' + "%d days, %d:%02d:%02d" % (d, h, m, s) + '\n')
        file.close()
Ejemplo n.º 3
0
def get_evolver_and_sketch(source_image,
                           output_folder,
                           num_triangles,
                           save_frequency,
                           color_type,
                           start_from=None,
                           continue_run=False,
                           randomized=False):
    im = Image.open(source_image)
    im = im.convert(mode=color_type)
    assert not (start_from and continue_run
                ), "you should only use start_from or continue_run, not both"
    sketch = None
    e = None
    if continue_run:
        # find the latest file in the folder
        file_names = glob.glob(
            os.path.join(output_folder, "intermediate_???.txt"))
        best = -1
        for filename in file_names:
            num = int(filename[-7:-4])
            if num > best:
                best = num
        if best >= 0:
            filename = os.path.join(output_folder,
                                    "intermediate_%03d.txt" % best)
            module_logger.info(
                "Restarting evolution based on found file 'intermediate_%03d.txt'",
                best)
            sketch = Sketch.read(filename)
            e = Evolver(im,
                        output_folder,
                        num_triangles,
                        save_frequency,
                        color_type=color_type,
                        save_index=best + 1)

    # Preferred is to load from the auto-save, but in case it died while saving the above will still work
    filename = os.path.join(output_folder, "auto_save.txt")
    if os.path.exists(filename):
        sketch = Sketch.read(filename)

    if sketch is None:
        utils.clean_dir(output_folder)
        if start_from:
            sketch = Sketch.read(start_from)
        else:
            if randomized:
                seeder = RandomSeeder(im, output_folder, num_triangles,
                                      color_type)
            else:
                seeder = Seeder(im, output_folder, num_triangles, color_type)
            sketch = seeder.run()
        e = Evolver(im,
                    output_folder,
                    num_triangles,
                    save_frequency,
                    color_type=color_type)
    return e, sketch
Ejemplo n.º 4
0
class Test_Model(Model):
    def __init__(self, args, rng):
        super(Test_Model, self).__init__(args, rng)

    def get_summary(self):
        print "Running test model"

    def get_output(self):
        print self.evolver.current_code
        print "Model done"

    def run(self):
        trnas = 8
        aarss = 8
        phi = 0.9
        mu = .01
        msg_mu = .1
        pop = 100

        trna_names = ["tra_" + str(i) for i in xrange(trnas)]
        codon_names = ["codon_" + str(i) for i in xrange(trnas)]

        aars_names = ["aars_" + str(i) for i in xrange(aarss)]
        aa_names = ["aa_" + str(i) for i in xrange(aarss)]

        site_names = ["site_" + str(i) for i in xrange(aarss)]

        trna_space = Bit_TRNA_Space(trna_names, codon_names)
        aars_space = Id_AARS_Space(aars_names, aa_names)

        trna = range(trnas)
        trna[1] = 0
        trna[2] = 0
        code = Code(trna, range(aarss), trna_space, aars_space)

        site_types = Ring_Site_Types(phi, zip(aa_names, np.arange(0, 1, .125)),
                                     zip(site_names, np.arange(0, 1, .125)),
                                     [1] * len(site_names))

        self.evolver = Evolver(
            code, site_types, mu,
            get_ring_mutation_matrix(len(codon_names), msg_mu), pop, self.rng)

        mut = Code_Mutator(code, mu)

        trna[0] = 1
        code2 = Code(trna, range(aarss), trna_space, aars_space)

        #print mut.mutation_probability(code2)
        #print [trna_space.mutation_probability(0, i, mu) for i in xrange(8)]
        # mut = Code_Mutator(code, mu)
        # code_dict = mut.get_one_gene_mutation_probabilities()
        #for code in code_dict:
        #    print code, code_dict[code]
        for i in xrange(10000):
            self.evolver.step_time()
Ejemplo n.º 5
0
class Test_Model(Model):
    def __init__(self, args, rng):
        super(Test_Model, self).__init__(args, rng)

    def get_summary(self):
        print "Running test model"

    def get_output(self):
        print self.evolver.current_code
        print "Model done"

    def run(self):
        trnas = 8
        aarss = 8
        phi = 0.9
        mu = .01
        msg_mu = .1
        pop = 100

        trna_names = ["tra_" + str(i) for i in xrange(trnas)]
        codon_names = ["codon_" + str(i) for i in xrange(trnas)]

        aars_names = ["aars_" + str(i) for i in xrange(aarss)]
        aa_names = ["aa_" + str(i) for i in xrange(aarss)]

        site_names = ["site_" + str(i) for i in xrange(aarss)]

        trna_space = Bit_TRNA_Space(trna_names, codon_names)
        aars_space = Id_AARS_Space(aars_names, aa_names)

        trna = range(trnas)
        trna[1] = 0
        trna[2] = 0
        code = Code(trna, range(aarss), trna_space, aars_space)
        
        site_types = Ring_Site_Types(phi, zip(aa_names, np.arange(0, 1, .125)),
                                     zip(site_names, np.arange(0, 1, .125)), [1] * len(site_names))

        
        self.evolver = Evolver(code, site_types, mu,
                          get_ring_mutation_matrix(len(codon_names), msg_mu), pop, self.rng)

        mut = Code_Mutator(code, mu)

        trna[0] = 1
        code2 = Code(trna, range(aarss), trna_space, aars_space)
        
        #print mut.mutation_probability(code2)
        #print [trna_space.mutation_probability(0, i, mu) for i in xrange(8)]
        # mut = Code_Mutator(code, mu)
        # code_dict = mut.get_one_gene_mutation_probabilities()
        #for code in code_dict:
        #    print code, code_dict[code]
        for i in xrange(10000):
            self.evolver.step_time()
Ejemplo n.º 6
0
def test_sort_by_fitness_gives_entities_descending_order_by_fitness():
    entity_1 = Mock(fitness=1)
    entity_2 = Mock(fitness=2)
    entity_3 = Mock(fitness=3)
    population = Evolver.sort_by_fitness([entity_1, entity_3, entity_2])
    assert population == [entity_3, entity_2, entity_1]
    assert [entity.fitness for entity in population] == [3, 2, 1]
Ejemplo n.º 7
0
def generate(generations, population, all_possible_genes, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evolve the population
        population (int): Number of networks in each generation
        all_possible_genes (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    logging.info(
        "***generate(generations, population, all_possible_genes, dataset)***")

    evolver = Evolver(all_possible_genes)

    genomes = evolver.create_population(population)

    # Evolve the generation.
    for i in range(generations):

        logging.info("***Now in generation %d of %d***" % (i + 1, generations))

        print_genomes(genomes)

        # Train and get accuracy for networks/genomes.
        train_genomes(genomes, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(genomes)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-' * 80)  #-----------

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Evolve!
            genomes = evolver.evolve(genomes)

    # Sort our final population according to performance.
    genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks/genomes.
    print_genomes(genomes[:5])
Ejemplo n.º 8
0
def generate(generations, population, all_possible_genes, dataset):
    """Generate a network with the genetic algorithm.

    Args:
        generations (int): Number of times to evolve the population
        population (int): Number of networks in each generation
        all_possible_genes (dict): Parameter choices for networks
        dataset (str): Dataset to use for training/evaluating

    """
    logging.info("***generate(generations, population, all_possible_genes, dataset)***")
    
    evolver = Evolver(all_possible_genes)
    
    genomes = evolver.create_population(population)

    # Evolve the generation.
    for i in range( generations ):

        logging.info("***Now in generation %d of %d***" % (i + 1, generations))

        print_genomes(genomes)
        
        # Train and get accuracy for networks/genomes.
        train_genomes(genomes, dataset)

        # Get the average accuracy for this generation.
        average_accuracy = get_average_accuracy(genomes)

        # Print out the average accuracy each generation.
        logging.info("Generation average: %.2f%%" % (average_accuracy * 100))
        logging.info('-'*80) #-----------

        # Evolve, except on the last iteration.
        if i != generations - 1:
            # Evolve!
            genomes = evolver.evolve(genomes)

    # Sort our final population according to performance.
    genomes = sorted(genomes, key=lambda x: x.accuracy, reverse=True)

    # Print out the top 5 networks/genomes.
    print_genomes(genomes[:5])
Ejemplo n.º 9
0
class Evolver_Test(unittest.TestCase):
    def setUp(self):
        self.aars_space = AARS_Space(range(2), np.diag([1] * 2),
                                     get_uniform_mutation_matrix(2, .01))

        # mu is zero so that there is only 3 potential codes available
        # and so the code is forced to handle mutants with a fixation
        # probability of zero.
        self.trna_space = TRNA_Space(range(2), np.diag([1] * 2), np.diag([1] * 2),
                                     get_uniform_mutation_matrix(2, .01))

        pchem_vals = [.30, .70]
        self.site_types = Ring_Site_Types(.05, zip(range(2), pchem_vals),
                                          zip(range(2), pchem_vals), [1] * 2)

        message_mutation_matrix = get_uniform_mutation_matrix(2, .01)
        #print message_mutation_matrix.shape
        #print np.matrix([[.5, .5], [.5, .5]]).shape

        self.code = Code([1], [1], self.trna_space, self.aars_space)

        rng = Random()
        rng.seed(42)
        self.evolver = Evolver(self.code, self.site_types, message_mutation_matrix,
                               100, rng)

        
    def test_overall_fitness(self):
        """ Check if Eq5 SellaArdell 06 is implemented correctly. """
        self.assertAlmostEqual(.5 ** 2, self.evolver._overall_fitness([.5, .5]))

    def test_transition_probabilities(self):
        self.assertTrue(False, "Still need to implement this test.")

    def test_step_time(self):
        self.evolver.step_time()
        self.assertEqual(self.evolver._current_code, self.code)
Ejemplo n.º 10
0
    def setUp(self):
        self.aars_space = AARS_Space(range(2), np.diag([1] * 2),
                                     get_uniform_mutation_matrix(2, .01))

        # mu is zero so that there is only 3 potential codes available
        # and so the code is forced to handle mutants with a fixation
        # probability of zero.
        self.trna_space = TRNA_Space(range(2), np.diag([1] * 2), np.diag([1] * 2),
                                     get_uniform_mutation_matrix(2, .01))

        pchem_vals = [.30, .70]
        self.site_types = Ring_Site_Types(.05, zip(range(2), pchem_vals),
                                          zip(range(2), pchem_vals), [1] * 2)

        message_mutation_matrix = get_uniform_mutation_matrix(2, .01)
        #print message_mutation_matrix.shape
        #print np.matrix([[.5, .5], [.5, .5]]).shape

        self.code = Code([1], [1], self.trna_space, self.aars_space)

        rng = Random()
        rng.seed(42)
        self.evolver = Evolver(self.code, self.site_types, message_mutation_matrix,
                               100, rng)
Ejemplo n.º 11
0
def main():
    comm = MPI.COMM_WORLD
    size = comm.Get_size()
    if size == 1:
        print("Can not evolve on single node, 2 nodes required at least!")
        sys.exit(-1)

    parser = argparse.ArgumentParser(description='Distributed evolver')
    parser.add_argument(
        '-p',
        "--popsize",
        default=(size - 1),
        type=int,
        help='population size (default: equals to node count-1 [%d])' %
        (size - 1))
    parser.add_argument('-e',
                        "--epochs",
                        default=1,
                        type=int,
                        help='epochs count (default: 1)')
    parser.add_argument("-i", "--input", required=True, help='input bitmap')

    args = parser.parse_args()

    per_node = int(np.rint(args.popsize / (size - 1)))
    pop_size = per_node * (size - 1)

    global sample, reduced_sample

    im = Image.open(args.input)
    sample = np.array(im)[:, :, :3]
    sample = rgb2gray(sample) / 255.0

    reduced_sample = block_reduce(sample, kernel_size, func=np.mean)
    smpl = reduced_sample * 255
    img = smpl.astype(np.uint8)

    Image.fromarray(img).save("sample.png")

    evo = Evolver(args.epochs, pop_size, 0.5, 0.15, generator, fitness,
                  breeder, mutator, process_alpha)

    rank = comm.Get_rank()
    if rank == 0:
        if pop_size != args.popsize:
            print(
                "Population size adjusted to %d [%d per node x %d nodes + master]"
                % (pop_size, per_node, size - 1))
        evo.master()
    else:
        evo.slave()
Ejemplo n.º 12
0
def test_breed_population_gives_the_breed_of_each_entity():
    Entity = EntityBreadingWithOffspringSuffix
    new_population = Evolver.breed([Entity("one"), Entity("other")])
    assert "oneoffspring" in new_population
    assert "otheroffspring" in new_population
    assert len(new_population) == 2
Ejemplo n.º 13
0
import pygame
from arm import Arm
from conf import generations, population_size, screen_size
from evolver import Evolver
from painter import Painter
from pie import Pie
from rod import Rod
from testlab import TestLab, TestLabParams
from vec import Vec

pygame.init()

testlab_params = TestLabParams(anchor=Vec(50, screen_size / 2),
                               target=Vec(screen_size - 40, screen_size / 2),
                               world_bounds=Vec(screen_size, screen_size))
evolver = Evolver(generations, population_size, testlab_params)
testlab = TestLab(
    testlab_params,
    evolver.evolve(),
    Pie(),
)
painter = Painter(pygame.display.set_mode((screen_size, screen_size)))

clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False

    testlab.update()
Ejemplo n.º 14
0
def test_evolve_empty_population_gives_empty_one():
    assert Evolver.evolve([]) == []
Ejemplo n.º 15
0
def test_select_high_fitness_entities_gives_the_first_half_of_the_population():
    Entity = Mock
    first_half = [Entity(), Entity(), Entity()]
    second_half = [Entity(), Entity(), Entity()]
    assert first_half != second_half
    assert Evolver.select_high_fitness_entities(first_half + second_half) == first_half
Ejemplo n.º 16
0
        os.unlink(file_name)

    plt.savefig(file_name)


if __name__ == '__main__':

    pool_size = 128
    iterations = 1280
    new_brain_percent = .1

    brains = []
    iter = 0
    stats_per_iteration = []

    evolver = Evolver()

    while len(brains) < pool_size:
        brains.append(Brain())

    for loop in range(iterations):
        plot(stats_per_iteration)
        iter += 1
        mutants = []
        while (len(brains) + len(mutants)) < pool_size:
            if random() < new_brain_percent:
                mutants.append(Brain())
            else:
                parent_brain = sample(brains, 1)[0]
                mutant = parent_brain.getNewMutant(mutation_chance=.2,
                                                   max_mutation=.5)
Ejemplo n.º 17
0
def run_repo_model():
    """
    This function instantiates a model and can be used to gather results

    Argument 1 (int): Amount of iterations to run model
    Argument 2 (bool): Whether to generate the java files created during the simulation
    Argument 3 (int): Amount of simulations of the model to run
    """

    iterations = int(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_ITERATIONS
    simulations = int(
        sys.argv[3]) if len(sys.argv) > 1 else DEFAULT_SIMULATIONS

    PROBABILITIES['create_class'] = float(
        sys.argv[4]) if len(sys.argv) > 4 else PROBABILITIES['create_class']
    PROBABILITIES['create_method'] = float(
        sys.argv[5]) if len(sys.argv) > 5 else PROBABILITIES['create_method']
    PROBABILITIES['call_method'] = float(
        sys.argv[6]) if len(sys.argv) > 6 else PROBABILITIES['call_method']
    PROBABILITIES['update_method'] = float(
        sys.argv[7]) if len(sys.argv) > 7 else PROBABILITIES['update_method']
    PROBABILITIES['delete_method'] = float(
        sys.argv[8]) if len(sys.argv) > 8 else PROBABILITIES['delete_method']
    print(sys.argv)
    print(PROBABILITIES)
    assert (EXP_CONDITION in ['reproduce', 'no_rec', 'delete_state', 'MSR'])

    print(
        'Going to run {} simulations, with condition: {}, fitness method: {} and number of steps: {}'
        .format(simulations, EXP_CONDITION, FITNESS_METHOD, iterations))
    # Set add/delete probability
    if EXP_CONDITION == 'delete_state':
        add_prob = ADD_STATE
    else:
        add_prob = 1
    print('Statements are added with prob: {} and deleted with: {}'.format(
        add_prob, 1 - add_prob))

    # Create the output file
    filename = create_outputfile(iterations, add_prob)

    if LOGGING:
        os.makedirs('./vid', exist_ok=True)

    for sim in range(simulations):
        print('Simulation {} of {}'.format(sim + 1, simulations))

        model = Evolver(iterations, FITNESS_METHOD, PROBABILITIES,
                        EXP_CONDITION, add_prob, LOGGING)
        # model = Evolver()
        print('Model instantiated...\n')

        print('Running model...')

        start_time = time.time()
        model.run_model()

        print('Model run completed..!\nTook {} seconds.\n'.format(time.time() -
                                                                  start_time))

        gen = True if len(sys.argv) > 2 and sys.argv[2] == 'True' else False
        gather_results(model, gen)
        filename = append_outputfile_try(model, sim, filename, iterations,
                                         add_prob)
Ejemplo n.º 18
0
def test_evolve_gives_population_with_greater_fitness_sorted_by_fitness():
    Entity = EntityBreadingOffspringWithDoubleFitness
    assert Evolver.evolve([Entity(-1), Entity(-3), Entity(3)]) == [Entity(6), Entity(3), Entity(-1)]