コード例 #1
0
ファイル: sum.py プロジェクト: ameya7295/geneflow
 def call(self, population):
     flat_pop = self._flatten_population(population)
     scores = B.sum(flat_pop, axis=-1)
     if self.max_sum_value:
         return scores / self.max_sum_value
     else:
         return scores
コード例 #2
0
ファイル: test_sum.py プロジェクト: ameya7295/geneflow
def test_sum2d():
    t = B.randint(0, 10, (10, 10, 10))
    v = Sum().call(t)

    result = []
    for r in t:
        result.append(B.sum(r, axis=-1))
    result = B.tensor(result)

    assert v.shape == (10, )
    for idx, a in enumerate(v):
        assert v[idx].all() == result[idx].all()
コード例 #3
0
    def call(self, population):
        # cosine only works on 1D array we need to flatten
        flat_pop = self._flatten_population(population)

        print('flat_pop', flat_pop.shape)
        print('ref_chromosome', self.ref_chromosome.shape)
        # numerator
        numerator = B.dot(flat_pop, self.ref_chromosome)

        # denominator (norm(u) *  norm(v))
        # ! B.norm is not broadcasted we need our own version
        population_norm = B.sum(B.abs(flat_pop)**2, axis=-1)**0.5
        denominator = population_norm * self.ref_norm

        print(numerator)
        print(denominator)
        return (numerator / denominator)
コード例 #4
0
def test_mutation2d_eager():
    pop_shape = (2, 4, 4)
    max_gene_value = 10
    min_gene_value = 0
    population_fraction = 1
    mutations_probability = (0.5, 0.5)
    min_mutation_value = 1
    max_mutation_value = 1

    population = B.randint(0, max_gene_value, pop_shape)

    # save original
    original_population = copy(population)
    cprint('[Initial genepool]', 'blue')
    cprint(original_population, 'blue')

    RM = RandomMutations2D(population_fraction=population_fraction,
                           mutations_probability=mutations_probability,
                           min_gene_value=min_gene_value,
                           max_gene_value=max_gene_value,
                           min_mutation_value=min_mutation_value,
                           max_mutation_value=max_mutation_value,
                           debug=True)
    population = RM(population)

    cprint('\n[Mutated genepool]', 'yellow')
    cprint(population, 'yellow')

    cprint('\n[Diff]', 'magenta')
    diff = population - original_population
    cprint(diff, 'magenta')

    assert B.is_tensor(population)
    assert population.shape == pop_shape
    assert B.max(diff) <= max_mutation_value
    for chromosome in diff:
        assert B.sum(chromosome) == 4
コード例 #5
0
def test_uniform_2Dcrossover_randomness_shape():
    GENOME_SHAPE = (10, 4, 4)
    population = B.randint(0, 1024, GENOME_SHAPE)
    population_fraction = 0.5
    crossover_probability = (0.5, 0.5)

    original_population = copy(population)
    OP = UniformCrossover2D(population_fraction, crossover_probability)
    population = OP(population)

    diff = B.clip(abs(population - original_population), 0, 1)
    print(diff)
    expected_mutations = original_population.shape[0] * population_fraction

    mutated_chromosomes = []
    for c in diff:
        if B.max(c):
            mutated_chromosomes.append(c)
    num_mutations = len(mutated_chromosomes)

    # sometime we have a collision so we use a delta
    assert abs(num_mutations - expected_mutations) < 2

    mutated_rows = crossover_probability[0] * GENOME_SHAPE[1]
    mutated_cells = crossover_probability[0] * GENOME_SHAPE[2]
    for cidx, c in enumerate(mutated_chromosomes):
        mr = 0
        mc = 0
        for r in c:
            s = B.sum(r)
            if s:
                mr += 1
                mc += s

        assert abs(mutated_rows - mr) < 2
        assert abs(mutated_cells - (mc // mutated_rows)) < 2