You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/06/21 00:17:36 UTC
svn commit: r786918 -
/commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java
Author: sebb
Date: Sat Jun 20 22:17:36 2009
New Revision: 786918
URL: http://svn.apache.org/viewvc?rev=786918&view=rev
Log:
Fix thread-safety issues
- code failed to use synch. read
- code could potentially use two different random Generators if another thread changed it
Modified:
commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java?rev=786918&r1=786917&r2=786918&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/genetics/GeneticAlgorithm.java Sat Jun 20 22:17:36 2009
@@ -24,7 +24,7 @@
* of the algorithm can be configured for a specific problem.
*
* @since 2.0
- * @version $Revision:$ $Date:$
+ * @version $Revision$ $Date$
*/
public class GeneticAlgorithm {
@@ -34,6 +34,7 @@
* Use {@link #setRandomGenerator(RandomGenerator)} to supply an alternative
* to the default JDK-provided PRNG.
*/
+ //@GuardedBy("this")
private static RandomGenerator randomGenerator = new JDKRandomGenerator();
/**
@@ -74,7 +75,7 @@
* @param crossoverRate The crossover rate as a percentage (0-1 inclusive)
* @param mutationPolicy The {@link MutationPolicy}
* @param mutationRate The mutation rate as a percentage (0-1 inclusive)
- * @param selectionPolicy The {@link selectionPolicy}
+ * @param selectionPolicy The {@link SelectionPolicy}
*/
public GeneticAlgorithm(
CrossoverPolicy crossoverPolicy, double crossoverRate,
@@ -134,18 +135,20 @@
public Population nextGeneration(Population current) {
Population nextGeneration = current.nextGeneration();
+ RandomGenerator randGen = getRandomGenerator();
+
while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) {
// select parent chromosomes
ChromosomePair pair = getSelectionPolicy().select(current);
// crossover?
- if (randomGenerator.nextDouble() < getCrossoverRate()) {
+ if (randGen.nextDouble() < getCrossoverRate()) {
// apply crossover policy to create two offspring
pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond());
}
// mutation?
- if (randomGenerator.nextDouble() < getMutationRate()) {
+ if (randGen.nextDouble() < getMutationRate()) {
// apply mutation policy to the chromosomes
pair = new ChromosomePair(
getMutationPolicy().mutate(pair.getFirst()),