~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/ga/examples.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

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 = {'pop_size':250,'p_replace':.8,'p_cross': .8, 'p_mutate':'gene',
 
58
                'p_deviation': 0.,'gens':35,'rand_seed':0,'rand_alg':'CMRG'}
 
59
    galg.settings.update(settings)
 
60
    galg.evolve()
 
61
    print galg.pop.best()
 
62
    
 
63
if __name__ == '__main__':
 
64
    ex1()    
 
 
b'\\ No newline at end of file'