Package com.trimga.matingstrategies

Source Code of com.trimga.matingstrategies.ChromosomeUtilities

/*
Copyright 2009 Jon Tait

This file is part of TrimGA.

TrimGA is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

TrimGA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with TrimGA.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.trimga.matingstrategies;

import java.util.ArrayList;
import java.util.List;

import com.trimga.Chromosome;

public class ChromosomeUtilities {
 
  public static List<Chromosome> makeCrossoverChild(List<Chromosome> parent1,
      List<Chromosome> parent2)
  {
    List<Chromosome> childChromosomeList = new ArrayList<Chromosome>();
   
    int shortestChromosomes = parent1.size() <= parent2.size() ?
        parent1.size() : parent2.size();
   
    double crossoverCheckFrequency = Math.random();
    boolean useParent1 = Math.random() < 0.5;
    for(int i=0; i<shortestChromosomes; i++) {
      if(Math.random() < crossoverCheckFrequency) {
        useParent1 = Math.random() < 0.5;
      }
      if(useParent1) {
        childChromosomeList.add(parent1.get(i));
      } else {
        childChromosomeList.add(parent2.get(i));
      }
    }
   
    return childChromosomeList;
  }
 
  public static void randomlyPluckOutChromosomes(List<Chromosome> chromosomeList) {
    int numChromosomesToRemove = (int)Math.round(Math.sqrt(chromosomeList.size()) * Math.random() * Math.random());
    for(int i=0; i<numChromosomesToRemove; i++) {
      try {
        int randomIndex = (int)Math.round(chromosomeList.size() * Math.random());
        chromosomeList.remove(randomIndex);
      } catch(RuntimeException ignore) {}
    }
  }
 
  public static void randomlyAddGeneratedChromosomes(List<Chromosome> chromosomeList) {
    int numChromosomesToAdd = (int)Math.round(Math.sqrt(chromosomeList.size()) * Math.random() * Math.random());
    if(chromosomeList.size() > 0) {
      Chromosome aChromosome = chromosomeList.get(0);
      for(int i=0; i<numChromosomesToAdd; i++) {
        try {
          int insertionIndex = (int) (Math.random() * chromosomeList.size());
          chromosomeList.add(insertionIndex, aChromosome.generateRandomChromosome());
        } catch(RuntimeException ignore) {}
      }
    }
  }

  public static void randomlyMutateChromosomes(List<Chromosome> chromosomeList) {
    int numChromosomesToMutate = (int)Math.round(Math.sqrt(chromosomeList.size()) * Math.random() * Math.random());
      for(int i=0; i<numChromosomesToMutate; i++) {
        try {
          int randomIndex = (int)Math.round(chromosomeList.size() * Math.random());
        Chromosome chromosomeToMutate = chromosomeList.remove(randomIndex);
          chromosomeList.add(chromosomeToMutate.generateMutation());
        } catch(RuntimeException ignore) {}
      }
  }
}
TOP

Related Classes of com.trimga.matingstrategies.ChromosomeUtilities

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.