~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to scipy/sandbox/ga/examples.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from scipy import ga
 
2
 
 
3
def fitness_ex1(self):
 
4
    score = 0
 
5
    desired = ['a','b','c','d','b',-1,.25,.6, .8, 0]
 
6
    # compare letters
 
7
    actual = self.get_values()
 
8
    pairs = zip(actual[:5],desired[:5])
 
9
    for g,d in pairs:
 
10
        if g == d:
 
11
            score += 1
 
12
    pairs = zip(actual[5:],desired[5:])
 
13
    for g,d in pairs:
 
14
        diff = g - d
 
15
        score -= abs(diff)
 
16
    return score
 
17
 
 
18
def ex1():
 
19
    """ This example illustrates the main steps in setting
 
20
        up a genetic optimization:
 
21
 
 
22
          1. Specify the genes types used to encode your problem
 
23
          2. Group these genes into a genome.
 
24
             a.  Specify the fitness function that evaluates the genomes.
 
25
          3. Create a population of the genomes
 
26
          4. Specify the algorithm used to evolve the population
 
27
          5.
 
28
    """
 
29
 
 
30
    # 1. First scpecify your genes.  To gene types are
 
31
    #    currently supported, list_gene and float_gene
 
32
 
 
33
    #    A list gene chooses its value from a list of values.
 
34
    #    The list can contain any type of object.
 
35
    g1 = ga.gene.list_gene(['a','b','c','d'])
 
36
    #    Float genes take on a continuous value between two
 
37
    #    bounds.
 
38
    g2 = ga.gene.float_gene((-1.,1.))
 
39
    #    We'll replicate these genes several times to make a longer
 
40
    #    genome.
 
41
    all_genes = g1.replicate(5) + g2.replicate(5)
 
42
    # 2.  Create a specialized "list_genome" (as opposed to tree_genome)
 
43
    #     class with the desired fitness function.
 
44
    #     It's structure is defined by our gene list.
 
45
    class this_genome(ga.genome.list_genome):
 
46
        pass
 
47
    this_genome.performance = fitness_ex1
 
48
    gnm = this_genome(all_genes)
 
49
 
 
50
    # 3.  Create a population of the genomes.
 
51
    #
 
52
    pop = ga.population.population(gnm)
 
53
    # 4.  Now use the basic genetic algorithm to evolve the population
 
54
    #
 
55
    galg = ga.algorithm.galg(pop)
 
56
    # change a few settings
 
57
    settings = dict(
 
58
        pop_size = 250,
 
59
        p_replace = .8,
 
60
        p_cross = .8,
 
61
        p_mutate = 'gene',
 
62
        p_deviation = 0.,
 
63
        gens = 35,
 
64
        rand_seed = 0,
 
65
    )
 
66
    galg.settings.update(settings)
 
67
    galg.evolve()
 
68
    print galg.pop.best()
 
69
 
 
70
if __name__ == '__main__':
 
71
    ex1()