Beispiel #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
Beispiel #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)
Beispiel #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)
Beispiel #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
Beispiel #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
Beispiel #6
0
def duration_changes(individual: Individual):
    notes = individual.get_flattened_notes()
    durations = list(map(lambda x: x.duration.duration_value, notes))
    counter = 0
    for i in range(1, len(durations)):
        d_1 = durations[i - 1]
        d_2 = durations[i]

        if d_1 / d_2 >= 4.0:
            counter += 1

    return - (counter / (len(notes) - 1))
Beispiel #7
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
Beispiel #8
0
def similar_notes(individual: Individual):
    notes_per_measure = individual.get_notes_per_measure()
    fitness = 0.0
    for m in notes_per_measure:
        consecutive_notes_count = 0
        prev_note = m[0].pitch
        for i in range(1, len(m)):
            curr_note = m[i].pitch
            if prev_note == curr_note:
                consecutive_notes_count += 1
            prev_note = curr_note
        fitness -= consecutive_notes_count / len(m)
    return fitness / len(individual.measures)
Beispiel #9
0
def duration_patterns(individual: Individual):
    notes = individual.get_flattened_notes()
    durations = list(map(lambda x: x.duration.duration_name, notes))

    pattern_counter = find_patterns(durations)
    fitness = 0.0
    for k, v in pattern_counter.items():
        divider = len(notes) - k + 1

        fitness += v / divider
    if fitness == 0.0:
        return 0.0
    return fitness / len(pattern_counter.items())
Beispiel #10
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)
Beispiel #11
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)
Beispiel #12
0
def intervallic_patterns(individual: Individual):
    notes: [Note] = individual.get_flattened_notes()
    notes = list(filter(lambda x: x.pitch != 'REST', notes))
    intervals = []
    for i in range(1, len(notes)):
        n1 = notes[i - 1].to_music21_note()
        n2 = notes[i].to_music21_note()
        n1_scalestep = get_scale_position(n1.nameWithOctave)
        n2_scalestep = get_scale_position(n2.nameWithOctave)
        scalestep_interval = n2_scalestep - n1_scalestep
        intervals.append(scalestep_interval)
    pattern_length_counter = find_patterns(intervals)
    fitness = 0.0
    for k, v in pattern_length_counter.items():
        # Maximum number of k length patterns in piece
        divider = len(notes) - k + 1
        fitness += v / divider
    if fitness == 0.0:
        return 0.0
    return fitness / len(pattern_length_counter.items())