主要實現Chromosome接口的fitness方法java
package genetictest; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.genetics.AbstractListChromosome; import org.apache.commons.math3.genetics.BinaryChromosome; import org.apache.commons.math3.genetics.BinaryMutation; import org.apache.commons.math3.genetics.Chromosome; import org.apache.commons.math3.genetics.ElitisticListPopulation; import org.apache.commons.math3.genetics.FixedGenerationCount; import org.apache.commons.math3.genetics.GeneticAlgorithm; import org.apache.commons.math3.genetics.InvalidRepresentationException; import org.apache.commons.math3.genetics.OnePointCrossover; import org.apache.commons.math3.genetics.Population; import org.apache.commons.math3.genetics.StoppingCondition; import org.apache.commons.math3.genetics.TournamentSelection; public class GeneticAlgorithmTest { public static final int NUM_GENERATIONS = 100; public static void main(String[] args) { // initialize a new genetic algorithm GeneticAlgorithm ga = new GeneticAlgorithm( new OnePointCrossover<Integer>(), 1, new BinaryMutation(), 0.10, new TournamentSelection(21) ); // initial population Population initial = getInitialPopulation(); // stopping condition StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // run the algorithm Population finalPopulation = ga.evolve(initial, stopCond); // best chromosome from the final population Chromosome bestFinal = finalPopulation.getFittestChromosome(); System.out.println(bestFinal); } private static Population getInitialPopulation() { List<Chromosome> chromosomes = new ArrayList<>(); int populationLimit = 1000; double elitismRate = 0.1; int chromosomeCount = BagBinaryChromosome.WEIGHTS.length; for(int i = 0; i < populationLimit; i++){ List<Integer> temp = new ArrayList<>(chromosomeCount); for(int j = 0; j < chromosomeCount; j++){ temp.add(Math.random() < 0.5? 1:0); } chromosomes.add(new BagBinaryChromosome(temp)); } Population ret = new ElitisticListPopulation(chromosomes, populationLimit, elitismRate); return ret; } } class MyBinaryChromosome extends BinaryChromosome{ public MyBinaryChromosome(Integer[] representation) throws InvalidRepresentationException { super(representation); } public MyBinaryChromosome(List<Integer> representation) throws InvalidRepresentationException { super(representation); } @Override public double fitness() { double ret = 0.0; for(Integer val : this.getRepresentation()){ ret += Math.pow(val, 3); } return ret; } @Override public AbstractListChromosome<Integer> newFixedLengthChromosome(List<Integer> chromosomeRepresentation) { return new MyBinaryChromosome(chromosomeRepresentation); } } class BagBinaryChromosome extends BinaryChromosome{ public static final int[] WEIGHTS = {2,2,6,5,4,2,2,6,5,4}; public static final int[] VALUE = {6,3,5,4,6,6,3,5,4,6}; public static final int MAX_WEIGHT = 20; public BagBinaryChromosome(List<Integer> representation) throws InvalidRepresentationException { super(representation); } @Override public double fitness() { double ret = 0.0; int weight = 0; int index = 0; for(Integer val : this.getRepresentation()){ if(val.equals(1)){ ret += VALUE[index]; weight += WEIGHTS[index]; } index++; } if(weight > MAX_WEIGHT){ ret = 0.0; } return ret; } @Override public String toString(){ StringBuilder sb = new StringBuilder(super.toString()); int value = 0; int weight = 0; int index = 0; for(Integer val : this.getRepresentation()){ if(val.equals(1)){ value += VALUE[index]; weight += WEIGHTS[index]; } index++; } sb.append(", WEIGHT: ").append(weight).append(", value: ").append(value); return sb.toString(); } @Override public AbstractListChromosome<Integer> newFixedLengthChromosome(List<Integer> chromosomeRepresentation) { return new BagBinaryChromosome(chromosomeRepresentation); } }