Esempio n. 1
0
def two_point(p1, p2):
    # one or two point crossover
    point1 = rng.randrange(1, len(p1.measures))
    point2 = rng.randrange(1, len(p1.measures))

    while point1 == point2:
        point1 = rng.randrange(1, len(p1.measures))
        point2 = rng.randrange(1, len(p1.measures))
    if point1 > point2:
        temp = point2
        point2 = point1
        point1 = temp

    c1 = Individual([], None)
    c2 = Individual([], None)

    p1_copy = copy.deepcopy(p1)
    p2_copy = copy.deepcopy(p2)

    for i in range(0, point1):
        c1.measures.append(p1_copy.measures[i])
        c2.measures.append(p2_copy.measures[i])
    for i in range(point1, point2):
        c1.measures.append(p2_copy.measures[i])
        c2.measures.append(p1_copy.measures[i])
    for i in range(point2, len(p1_copy.measures)):
        c1.measures.append(p1_copy.measures[i])
        c2.measures.append(p2_copy.measures[i])

    if c1.measures[0] is p1.measures[0]:
        print("Child measure has reference to parent measure")
    return c1, c2
Esempio n. 2
0
    def create(self, genotype=None):
        if genotype is None:
            genotype = []
            for i in range(0, config.length):
                genotype += self.to_binary(random.randint(0, config.alphabet-1), math.ceil(math.log2(config.alphabet)))

        return Individual(config.phenotype_convertor, config.fitness_evaluator, config.mutation_strategy, genotype,
                          config.crossover_strategy)
Esempio n. 3
0
    def create(self, genotype=None):
        gene_length = config.gene_length

        if genotype is None:
            genotype = numpy.random.choice([0, 1],
                                           size=(gene_length, )).tolist()

        return Individual(config.phenotype_convertor, config.fitness_evaluator,
                          config.mutation_strategy, genotype,
                          config.crossover_strategy)
Esempio n. 4
0
def uniform(p1, p2):
    c1 = Individual([], None)
    c2 = Individual([], None)
    p1_copy = copy.deepcopy(p1)
    p2_copy = copy.deepcopy(p2)

    for i in range(len(p1_copy.measures)):
        p = rng.choice([True, False])
        if p:
            c1.measures.append(p2_copy.measures[i])
            c2.measures.append(p1_copy.measures[i])
        else:
            c1.measures.append(p1_copy.measures[i])
            c2.measures.append(p2_copy.measures[i])

    if c1.measures[0] is p1.measures[0] or c1.measures[0] is p2.measures[0]:
        print("Child measure has reference to parent measure")

    return c1, c2
Esempio n. 5
0
def one_point(p1, p2):
    if constants.NUM_OF_MEASURES == 2:
        point1 = 1
    else:
        point1 = rng.randrange(1, len(p1.measures))

    c1 = Individual([], None)
    c2 = Individual([], None)
    p1_copy = copy.deepcopy(p1)
    p2_copy = copy.deepcopy(p2)

    for i in range(0, point1):
        c1.measures.append(p1_copy.measures[i])
        c2.measures.append(p2_copy.measures[i])
    for i in range(point1, len(p1_copy.measures)):
        c1.measures.append(p2_copy.measures[i])
        c2.measures.append(p1_copy.measures[i])

    if c1.measures[0] is p1.measures[0] or c1.measures is p2.measures[0]:
        print("Child measure has reference to parent measure")
    return c1, c2
Esempio n. 6
0
 def create_population(self, n):
     '''
     Create's an initial population. An individual's genotype is created and retrieved from
     GenoTypeFactory. The genotype is initalized to a completely random genome.
     '''
     population = []
     for i in range(n):
         genotype = GenotypeFactory.make_genotype(genotype=self.genotype,
                                                  config=self.config)
         genotype.init_random_genotype(self.genome_length)
         individual = Individual(genotype, self.translator)
         population.append(individual)
     return population
Esempio n. 7
0
    def create(self, genotype=None):
        length = self.ann.weights_count() + self.ann.neurons_count() * 3

        if genotype is None:
            genotype = self.genotype_coder.generate_init_genotype()

        phenotype_convertor = BeerTrackerPhenotypeConvertor(
            length, self.genotype_coder)
        fitness_evaluator = BeerTrackerFitnessEvaluator(self.world, self.ann)
        mutation_strategy = BinaryVectorInversionMutation(0.05)
        crossover_strategy = FlatlandOnePointCrossover(0.8, length)

        return Individual(phenotype_convertor, fitness_evaluator,
                          mutation_strategy, genotype, crossover_strategy)
Esempio n. 8
0
    def create(self, genotype=None):
        length = self.ann.weights_count() + self.ann.biased_neurons_count()

        if genotype is None:
            # lecture06-slide21: it is recommended to initialize weights from interval [-0,1; 0,1]
            vals = [x for x in range(2**self.bits_per_weight) if abs(2*x/(2**self.bits_per_weight)-1) <= 0.1]
            genotype = []
            for i in range(0, length):
                genotype += self.to_binary(random.choice(vals), self.bits_per_weight)

        phenotype_convertor = FlatlandPhenotypeConvertor(length)
        fitness_evaluator = FlatlandFitnessEvaluator(self.flatland, self.ann)
        mutation_strategy = BinaryVectorInversionMutation(0.01)
        crossover_strategy = FlatlandOnePointCrossover(0.8, length)

        return Individual(phenotype_convertor, fitness_evaluator, mutation_strategy, genotype, crossover_strategy)