~ubuntu-branches/ubuntu/trusty/commons-math/trusty

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/genetics/GeneticAlgorithm.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 * of the algorithm can be configured for a specific problem.
25
25
 *
26
26
 * @since 2.0
27
 
 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
 
27
 * @version $Revision: 925812 $ $Date: 2010-03-21 11:49:31 -0400 (Sun, 21 Mar 2010) $
28
28
 */
29
29
public class GeneticAlgorithm {
30
30
 
31
31
    /**
32
32
     * Static random number generator shared by GA implementation classes.
33
 
     * Set the randomGenerator seed to get reproducible results.  
 
33
     * Set the randomGenerator seed to get reproducible results.
34
34
     * Use {@link #setRandomGenerator(RandomGenerator)} to supply an alternative
35
35
     * to the default JDK-provided PRNG.
36
36
     */
37
37
    //@GuardedBy("this")
38
38
    private static RandomGenerator randomGenerator = new JDKRandomGenerator();
39
 
    
40
 
    /**
41
 
     * Set the (static) random generator.
42
 
     * 
43
 
     * @param random random generator
44
 
     */
45
 
    public synchronized static void setRandomGenerator(RandomGenerator random) {
46
 
        randomGenerator = random;
47
 
    }
48
 
    
49
 
    /**
50
 
     * Returns the (static) random generator.
51
 
     * 
52
 
     * @return the static random generator shared by GA implementation classes
53
 
     */
54
 
    public synchronized static RandomGenerator getRandomGenerator() {
55
 
        return randomGenerator;
56
 
    }
57
 
      
 
39
 
58
40
    /** the crossover policy used by the algorithm. */
59
41
    private final CrossoverPolicy crossoverPolicy;
60
42
 
69
51
 
70
52
    /** the selection policy used by the algorithm. */
71
53
    private final SelectionPolicy selectionPolicy;
72
 
    
 
54
 
 
55
    /** the number of generations evolved to reach {@link StoppingCondition} in the last run. */
 
56
    private int generationsEvolved = 0;
 
57
 
73
58
    /**
74
59
     * @param crossoverPolicy The {@link CrossoverPolicy}
75
60
     * @param crossoverRate The crossover rate as a percentage (0-1 inclusive)
93
78
        this.mutationRate = mutationRate;
94
79
        this.selectionPolicy = selectionPolicy;
95
80
    }
96
 
    
 
81
 
 
82
    /**
 
83
     * Set the (static) random generator.
 
84
     *
 
85
     * @param random random generator
 
86
     */
 
87
    public static synchronized void setRandomGenerator(RandomGenerator random) {
 
88
        randomGenerator = random;
 
89
    }
 
90
 
 
91
    /**
 
92
     * Returns the (static) random generator.
 
93
     *
 
94
     * @return the static random generator shared by GA implementation classes
 
95
     */
 
96
    public static synchronized RandomGenerator getRandomGenerator() {
 
97
        return randomGenerator;
 
98
    }
 
99
 
97
100
    /**
98
101
     * Evolve the given population. Evolution stops when the stopping condition
 
102
     * is satisfied. Updates the {@link #getGenerationsEvolved() generationsEvolved}
 
103
     * property with the number of generations evolved before the StoppingCondition
99
104
     * is satisfied.
100
 
     * 
 
105
     *
101
106
     * @param initial the initial, seed population.
102
107
     * @param condition the stopping condition used to stop evolution.
103
108
     * @return the population that satisfies the stopping condition.
104
109
     */
105
110
    public Population evolve(Population initial, StoppingCondition condition) {
106
111
        Population current = initial;
 
112
        generationsEvolved = 0;
107
113
        while (!condition.isSatisfied(current)) {
108
114
            current = nextGeneration(current);
 
115
            generationsEvolved++;
109
116
        }
110
117
        return current;
111
118
    }
128
135
     *    <li>Return nextGeneration</li>
129
136
     *    </ol>
130
137
     * </p>
131
 
     * 
 
138
     *
132
139
     * @param current the current population.
133
140
     * @return the population for the next generation.
134
141
     */
136
143
        Population nextGeneration = current.nextGeneration();
137
144
 
138
145
        RandomGenerator randGen = getRandomGenerator();
139
 
        
 
146
 
140
147
        while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
141
148
            // select parent chromosomes
142
149
            ChromosomePair pair = getSelectionPolicy().select(current);
165
172
        }
166
173
 
167
174
        return nextGeneration;
168
 
    }    
169
 
    
 
175
    }
 
176
 
170
177
    /**
171
178
     * Returns the crossover policy.
172
179
     * @return crossover policy
206
213
    public SelectionPolicy getSelectionPolicy() {
207
214
        return selectionPolicy;
208
215
    }
209
 
        
 
216
 
 
217
    /**
 
218
     * Returns the number of generations evolved to
 
219
     * reach {@link StoppingCondition} in the last run.
 
220
     *
 
221
     * @return number of generations evolved
 
222
     * @since 2.1
 
223
     */
 
224
    public int getGenerationsEvolved() {
 
225
        return generationsEvolved;
 
226
    }
 
227
 
210
228
}