use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.
the class TestBooths method testBooths.
/**
* Testing NSGA-II with Booths Problem
*
* @throws IOException
* @throws NumberFormatException
*/
@Test
public void testBooths() throws NumberFormatException, IOException {
Properties.MUTATION_RATE = 1d / 2d;
ChromosomeFactory<?> factory = new RandomFactory(false, 2, -10.0, 10.0);
// GeneticAlgorithm<?> ga = new NSGAII(factory);
GeneticAlgorithm<?> ga = new NSGAII(factory);
BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
// BinaryTournament ts = new BinaryTournament();
ga.setSelectionFunction(ts);
ga.setCrossOverFunction(new SBXCrossover());
Problem p = new Booths();
final FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
ga.addFitnessFunction(f1);
// execute
ga.generateSolution();
List<Chromosome> chromosomes = (List<Chromosome>) ga.getPopulation();
Collections.sort(chromosomes, new Comparator<Chromosome>() {
@Override
public int compare(Chromosome arg0, Chromosome arg1) {
return Double.compare(arg0.getFitness(f1), arg1.getFitness(f1));
}
});
for (Chromosome chromosome : chromosomes) Assert.assertEquals(chromosome.getFitness(f1), 0.000, 0.001);
for (Chromosome chromosome : chromosomes) {
NSGAChromosome nsga_c = (NSGAChromosome) chromosome;
DoubleVariable x = (DoubleVariable) nsga_c.getVariables().get(0);
DoubleVariable y = (DoubleVariable) nsga_c.getVariables().get(1);
System.out.printf("%f,%f : %f\n", x.getValue(), y.getValue(), chromosome.getFitness(f1));
}
}
use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.
the class StatementsPopulationLimit method isPopulationFull.
/* (non-Javadoc)
* @see org.evosuite.ga.PopulationLimit#isPopulationFull(java.util.List)
*/
/**
* {@inheritDoc}
*/
@Override
public boolean isPopulationFull(List<? extends Chromosome> population) {
int numStatements = 0;
for (Chromosome chromosome : population) {
TestSuiteChromosome suite = (TestSuiteChromosome) chromosome;
numStatements += suite.totalLengthOfTestCases();
}
return numStatements >= Properties.POPULATION;
}
use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.
the class RelativeSuiteLengthBloatControl method iteration.
/**
* {@inheritDoc}
*
* Set current max length to max of best chromosome
*/
@Override
public void iteration(GeneticAlgorithm<?> algorithm) {
Chromosome best = algorithm.getBestIndividual();
if (best instanceof TestSuiteChromosome)
current_max = ((TestSuiteChromosome) best).totalLengthOfTestCases();
if (best instanceof TestChromosome)
current_max = ((TestChromosome) best).size();
best_fitness = best.getFitness();
}
use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.
the class UniformCrossOver method crossOver.
/**
* {@inheritDoc}
*/
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
if (parent1.size() < 2 || parent2.size() < 2) {
return;
}
int maxNumGenes = Math.min(parent1.size(), parent2.size());
Chromosome t1 = parent1.clone();
Chromosome t2 = parent2.clone();
for (int i = 0; i < maxNumGenes; i++) {
if (Randomness.nextDouble() <= Properties.CROSSOVER_RATE) {
parent1.crossOver(t2, i);
parent2.crossOver(t1, i);
}
}
}
use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.
the class TournamentSelection method getIndex.
/**
* {@inheritDoc}
*
* Perform the tournament on the population, return one index
*/
@Override
public int getIndex(List<T> population) {
int new_num = Randomness.nextInt(population.size());
int winner = new_num;
int round = 0;
while (round < Properties.TOURNAMENT_SIZE - 1) {
new_num = Randomness.nextInt(population.size());
Chromosome selected = population.get(new_num);
if (maximize) {
if (selected.getFitness() > population.get(winner).getFitness()) {
winner = new_num;
}
} else {
if (selected.getFitness() < population.get(winner).getFitness()) {
winner = new_num;
}
}
round++;
}
return winner;
}
Aggregations