Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/tutorial2/OurMutatorPipeline.java @ 6152

Last change on this file since 6152 was 6152, checked in by bfarka, 13 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 3.7 KB
Line 
1/*
2  Copyright 2006 by Sean Luke
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7
8package ec.app.tutorial2;
9import ec.vector.*;
10import ec.*;
11import ec.util.*;
12
13/*
14 * OurMutatorPipeline.java
15 */
16
17/**
18   OurMutatorPipeline is a BreedingPipeline which negates the sign of genes.
19   The individuals must be IntegerVectorIndividuals.  Because we're lazy,
20   we'll use the individual's species' mutation-probability parameter to tell
21   us whether or not to mutate a given gene.
22 
23   <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
24   (however many its source produces)
25
26   <p><b>Number of Sources</b><br>
27   1
28*/
29
30
31public class OurMutatorPipeline extends BreedingPipeline
32    {
33    public static final String P_OURMUTATION = "our-mutation";
34
35    // We have to specify a default base, even though we never use it
36    public Parameter defaultBase() { return VectorDefaults.base().push(P_OURMUTATION); }
37   
38    public static final int NUM_SOURCES = 1;
39
40    // Return 1 -- we only use one source
41    public int numSources() { return NUM_SOURCES; }
42
43    // We're supposed to create a most _max_ and at least _min_ individuals,
44    // drawn from our source and mutated, and stick them into slots in inds[]
45    // starting with the slot inds[start].  Let's do this by telling our
46    // source to stick those individuals into inds[] and then mutating them
47    // right there.
48    public int produce(final int min,
49        final int max,
50        final int start,
51        final int subpopulation,
52        final Individual[] inds,
53        final EvolutionState state,
54        final int thread)
55        {
56        // grab individuals from our source and stick 'em right into inds.
57        // we'll modify them from there
58        int n = sources[0].produce(min,max,start,subpopulation,inds,state,thread);
59
60
61        // should we bother?
62        if (!state.random[thread].nextBoolean(likelihood))
63            return reproduce(n, start, subpopulation, inds, state, thread, false);  // DON'T produce children from source -- we already did
64
65
66        // clone the individuals if necessary -- if our source is a BreedingPipeline
67        // they've already been cloned, but if the source is a SelectionMethod, the
68        // individuals are actual individuals from the previous population
69        if (!(sources[0] instanceof BreedingPipeline))
70            for(int q=start;q<n+start;q++)
71                inds[q] = (Individual)(inds[q].clone());
72
73        // Check to make sure that the individuals are IntegerVectorIndividuals and
74        // grab their species.  For efficiency's sake, we assume that all the
75        // individuals in inds[] are the same type of individual and that they all
76        // share the same common species -- this is a safe assumption because they're
77        // all breeding from the same subpopulation.
78
79        if (!(inds[start] instanceof IntegerVectorIndividual)) // uh oh, wrong kind of individual
80            state.output.fatal("OurMutatorPipeline didn't get an IntegerVectorIndividual." +
81                "The offending individual is in subpopulation " + subpopulation + " and it's:" + inds[start]);
82        IntegerVectorSpecies species = (IntegerVectorSpecies)(inds[start].species);
83
84        // mutate 'em!
85        for(int q=start;q<n+start;q++)
86            {
87            IntegerVectorIndividual i = (IntegerVectorIndividual)inds[q];
88            for(int x=0;x<i.genome.length;x++)
89                if (state.random[thread].nextBoolean(species.mutationProbability))
90                    i.genome[x] = -i.genome[x];
91            // it's a "new" individual, so it's no longer been evaluated
92            i.evaluated=false;
93            }
94
95        return n;
96        }
97
98    }
99   
100   
Note: See TracBrowser for help on using the repository browser.