Exemple #1
0
def uniform_crossover(random, mom, dad, args):
    ux_bias = args.setdefault('ux_bias', 0.5)
    crossover_rate = args.setdefault('crossover_rate', 1.0)

    children = []
    if random.random() < crossover_rate:
        mom_genes = mom.group_genes(remove_noops=False)
        dad_genes = dad.group_genes(remove_noops=False)
        bro = copy.copy(dad_genes)
        sis = copy.copy(mom_genes)

        for i, (m, d) in enumerate(zip(mom_genes, dad_genes)):
            if random.random() < ux_bias:
                bro[i] = m
                sis[i] = d

        children.append(pyvotune.Genome(
            get_id(), initial_state=dad.initial_state, init_parts=bro,
            parent=dad))
        children.append(pyvotune.Genome(
            get_id(), initial_state=mom.initial_state, init_parts=sis,
            parent=mom))
    else:
        children.append(mom)
        children.append(dad)

    return children
Exemple #2
0
def uniform_crossover(random, mom, dad, args):
    ux_bias = args.setdefault('ux_bias', 0.5)
    crossover_rate = args.setdefault('crossover_rate', 1.0)

    children = []
    if random.random() < crossover_rate:
        mom_genes = mom.group_genes(remove_noops=False)
        dad_genes = dad.group_genes(remove_noops=False)
        bro = copy.copy(dad_genes)
        sis = copy.copy(mom_genes)

        for i, (m, d) in enumerate(zip(mom_genes, dad_genes)):
            if random.random() < ux_bias:
                bro[i] = m
                sis[i] = d

        children.append(
            pyvotune.Genome(get_id(),
                            initial_state=dad.initial_state,
                            init_parts=bro,
                            parent=dad))
        children.append(
            pyvotune.Genome(get_id(),
                            initial_state=mom.initial_state,
                            init_parts=sis,
                            parent=mom))
    else:
        children.append(mom)
        children.append(dad)

    return children
Exemple #3
0
    def test_scramble_mutation(self):
        @pyvotune.pint(
            range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        g1 = pyvotune.Genome(get_id())
        for i in range(5):
            g1.add_gene([i], P1)
        self.assertTrue(g1.assemble())

        r = random.Random()

        prev_val = g1[0][1]
        some_diff = False
        for i in range(10):
            (g2,) = pyvotune.variators.scramble_mutation(
                r, [g1], {'mutation_rate': 1.0})

            for orig, new in zip(g1, g2):
                if orig != new:
                    some_diff = True

        self.assertTrue(some_diff)
Exemple #4
0
def scramble_mutation(random, candidate, args):
    """
    Scrambles a candidate by switching around parts of the genome
    """
    rate = args.setdefault('mutation_rate', 0.1)

    if random.random() < rate:
        grouped_genes = candidate.group_genes(remove_noops=False)

        size = len(grouped_genes)
        p = random.randint(0, size - 1)
        q = random.randint(0, size - 1)

        p, q = min(p, q), max(p, q)

        s = grouped_genes[p:q + 1]

        random.shuffle(s)

        return pyvotune.Genome(
            get_id(), initial_state=candidate.initial_state,
            init_parts=grouped_genes[:p] + s[::-1] + grouped_genes[q + 1:],
            parent=candidate)
    else:
        return candidate
Exemple #5
0
def random_reset_mutation(random, candidate, args):
    """
    Varies a candidate by randomly resetting parameters
    """
    rate = args.setdefault('mutation_rate', 0.1)
    generator = args['pyvotune_generator']
    max_tries = args.setdefault('reset_max_tries', 25)

    grouped_genes = candidate.group_genes(remove_noops=False)
    mutant_genes = [random.random() for g in grouped_genes]

    for i in range(max_tries):
        mutant = pyvotune.Genome(
            get_id(),
            initial_state=candidate.initial_state,
            parent=candidate)

        for (gene, gene_param, param_values), r in zip(grouped_genes, mutant_genes):
            new_values = []

            if r < rate:
                replacement = random.choice(generator.gene_pool)
                vals = generator.get_gene_param_vals(replacement)

                mutant.add_gene(vals, replacement)
            else:
                mutant.add_gene(param_values, gene)

        if mutant.validate():
            return mutant

    return candidate
Exemple #6
0
def n_point_crossover(random, mom, dad, args):
    crossover_rate = args.setdefault('crossover_rate', 1.0)
    num_crossover_points = args.setdefault('num_crossover_points', 1)
    children = []
    if random.random() < crossover_rate:
        mom_genes = mom.group_genes(remove_noops=False)
        dad_genes = dad.group_genes(remove_noops=False)
        bro = copy.copy(dad_genes)
        sis = copy.copy(mom_genes)

        num_cuts = min(len(mom_genes) - 1, num_crossover_points)

        cut_points = random.sample(range(1, len(mom_genes)), num_cuts)

        cut_points.sort()

        normal = True
        for i, (m, d) in enumerate(zip(mom_genes, dad_genes)):
            if i in cut_points:
                normal = not normal

            if not normal:
                bro[i] = m
                sis[i] = d

        children.append(
            pyvotune.Genome(get_id(),
                            initial_state=dad.initial_state,
                            init_parts=bro,
                            parent=dad))
        children.append(
            pyvotune.Genome(get_id(),
                            initial_state=mom.initial_state,
                            init_parts=sis,
                            parent=mom))
    else:
        children.append(mom)
        children.append(dad)

    return children
Exemple #7
0
    def test_uniform(self):
        @pyvotune.pint(
            range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        g1 = pyvotune.Genome(get_id())
        g1.add_gene([0], P1)
        g1.add_gene([0], P1)
        self.assertTrue(g1.assemble())

        g2 = pyvotune.Genome(get_id())
        g2.add_gene([1], P1)
        g2.add_gene([1], P1)
        self.assertTrue(g2.assemble())

        r = random.Random()

        prev_val = g1[0][1]
        some_diff = False

        parents = [g1, g2]

        for i in range(1):
            children = pyvotune.variators.uniform_crossover(
                r, parents, {'ux_bias': 1.0})

            self.assertTrue(children)

            found_diff = False

            for child in children:
                for parent in parents:
                    for c, p in zip(child, parent):
                        if c != p:
                            found_diff = True

            self.assertTrue(found_diff)
Exemple #8
0
    def test_assembly(self):
        @pyvotune.pint(range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        @pyvotune.pbool()
        class P2:
            def __init__(self, boolparam):
                self.boolparam = boolparam

        g = pyvotune.Genome(get_id())
        g.add_gene([6], P1)
        self.assertFalse(g.assemble())

        g = pyvotune.Genome(get_id())
        g.add_gene([3], P1)
        self.assertTrue(g.assemble())

        g = pyvotune.Genome(get_id())
        g.add_gene([3], P1)
        g.add_gene([], P2)
        self.assertFalse(g.assemble())
Exemple #9
0
    def _generate(self):
        genome = Genome(get_id(), self.initial_state)

        for i in range(self.max_length):
            gene = self.next_gene(genome)

            if not gene:
                #log.debug(u"Generate: Failed, ran out of valid genes")
                return

            params = self.get_gene_param_vals(gene)
            genome.add_gene(params, gene)

        if genome.validate():
            return genome
Exemple #10
0
def n_point_crossover(random, mom, dad, args):
    crossover_rate = args.setdefault('crossover_rate', 1.0)
    num_crossover_points = args.setdefault('num_crossover_points', 1)
    children = []
    if random.random() < crossover_rate:
        mom_genes = mom.group_genes(remove_noops=False)
        dad_genes = dad.group_genes(remove_noops=False)
        bro = copy.copy(dad_genes)
        sis = copy.copy(mom_genes)

        num_cuts = min(len(mom_genes) - 1, num_crossover_points)

        cut_points = random.sample(range(1, len(mom_genes)), num_cuts)

        cut_points.sort()

        normal = True
        for i, (m, d) in enumerate(zip(mom_genes, dad_genes)):
            if i in cut_points:
                normal = not normal

            if not normal:
                bro[i] = m
                sis[i] = d

        children.append(pyvotune.Genome(
            get_id(), initial_state=dad.initial_state, init_parts=bro,
            parent=dad))
        children.append(pyvotune.Genome(
            get_id(), initial_state=mom.initial_state, init_parts=sis,
            parent=mom))
    else:
        children.append(mom)
        children.append(dad)

    return children
Exemple #11
0
    def test_assembly(self):
        @pyvotune.pint(
            range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        @pyvotune.pbool()
        class P2:
            def __init__(self, boolparam):
                self.boolparam = boolparam

        g = pyvotune.Genome(get_id())
        g.add_gene([6], P1)
        self.assertFalse(g.assemble())

        g = pyvotune.Genome(get_id())
        g.add_gene([3], P1)
        self.assertTrue(g.assemble())

        g = pyvotune.Genome(get_id())
        g.add_gene([3], P1)
        g.add_gene([], P2)
        self.assertFalse(g.assemble())
Exemple #12
0
    def _generate(self):
        genome = Genome(get_id(), self.initial_state)

        for i in range(self.max_length):
            gene = self.next_gene(genome)

            if not gene:
                #log.debug(u"Generate: Failed, ran out of valid genes")
                return

            params = self.get_gene_param_vals(gene)
            genome.add_gene(params, gene)

        if genome.validate():
            return genome
Exemple #13
0
    def test_random_reset(self):
        @pyvotune.pint(
            range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        @pyvotune.pint(
            range=(5, 10))
        class P2:
            def __init__(self, intparam):
                self.intparam = intparam

        gen = pyvotune.Generate(
            gene_pool=[P1, P2],
            max_length=2, noop_frequency=0.)

        g1 = pyvotune.Genome(get_id())
        g1.add_gene([3], P1)
        g1.add_gene([7], P2)
        self.assertTrue(g1.assemble())

        r = random.Random()

        prev_val = g1[0][1]
        some_diff = False
        for i in range(100):
            (g2,) = pyvotune.variators.random_reset_mutation(
                r, [g1], {
                    'mutation_rate': 1.0,
                    'pyvotune_generator': gen
                })
            new_val = g2[0][1]
            if g1[0] != g2[0] or g1[1] != g2[1]:
                some_diff = True

        self.assertTrue(some_diff)
Exemple #14
0
    def test_random_param_reset(self):
        @pyvotune.pint(
            range=(0, 5))
        class P1:
            def __init__(self, intparam):
                self.intparam = intparam

        g1 = pyvotune.Genome(get_id())
        g1.add_gene([3], P1)
        self.assertTrue(g1.assemble())

        r = random.Random()

        prev_val = g1[0][1]
        some_diff = False
        for i in range(100):
            (g2,) = pyvotune.variators.param_reset_mutation(
                r, [g1], {'mutation_rate': 1.0})
            new_val = g2[0][1]
            if new_val != prev_val:
                some_diff = True
            self.assertTrue(new_val >= 0 and new_val < 5)

        self.assertTrue(some_diff)
Exemple #15
0
def param_reset_mutation(random, candidate, args):
    """
    Varies a candidate by randomly resetting parameters
    """
    rate = args.setdefault('mutation_rate', 0.1)
    mutant = pyvotune.Genome(
        get_id(), initial_state=candidate.initial_state,
        parent=candidate)
    grouped_genes = candidate.group_genes(remove_noops=False)
    if not grouped_genes:
        print "Received invalid genome in mutator"
        print candidate

    for gene, gene_param, param_values in grouped_genes:
        new_values = []
        for param, value in zip(gene_param, param_values):
            if random.random() < rate:
                new_values.append(param.generate())
            else:
                new_values.append(value)

        mutant.add_gene(new_values, gene)

    return mutant