~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/genetics/BinaryMutation.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:
22
22
/**
23
23
 * Mutation for {@link BinaryChromosome}s. Randomly changes one gene.
24
24
 *
25
 
 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
 
25
 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
26
26
 * @since 2.0
27
27
 */
28
28
public class BinaryMutation implements MutationPolicy {
34
34
     */
35
35
    public Chromosome mutate(Chromosome original) {
36
36
        if (!(original instanceof BinaryChromosome)) {
37
 
            throw new IllegalArgumentException("Binary mutation works on BinaryChromosome only."); 
 
37
            throw new IllegalArgumentException("Binary mutation works on BinaryChromosome only.");
38
38
        }
39
 
        
 
39
 
40
40
        BinaryChromosome origChrom = (BinaryChromosome) original;
41
41
        List<Integer> newRepr = new ArrayList<Integer>(origChrom.getRepresentation());
42
 
        
 
42
 
43
43
        // randomly select a gene
44
44
        int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength());
45
45
        // and change it
46
46
        newRepr.set(geneIndex, origChrom.getRepresentation().get(geneIndex) == 0 ? 1 : 0);
47
 
        
 
47
 
48
48
        Chromosome newChrom = origChrom.newFixedLengthChromosome(newRepr);
49
49
        return newChrom;
50
50
    }