Search in sources :

Example 56 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class SPEA2SystemTest method test.

public double[][] test(String targetClass) {
    Properties.CRITERION = new Criterion[2];
    Properties.CRITERION[0] = Criterion.RHO;
    Properties.CRITERION[1] = Criterion.AMBIGUITY;
    Properties.ALGORITHM = Algorithm.SPEA2;
    Properties.SELECTION_FUNCTION = Properties.SelectionFunction.BINARY_TOURNAMENT;
    Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
    Properties.SEARCH_BUDGET = 10;
    Properties.MINIMIZE = false;
    EvoSuite evosuite = new EvoSuite();
    Properties.TARGET_CLASS = targetClass;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    Assert.assertNotNull(result);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    final FitnessFunction<?> branch = ga.getFitnessFunctions().get(0);
    final FitnessFunction<?> rho = ga.getFitnessFunctions().get(1);
    List<Chromosome> population = new ArrayList<Chromosome>(ga.getBestIndividuals());
    double[][] front = new double[population.size()][2];
    for (int i = 0; i < population.size(); i++) {
        Chromosome c = population.get(i);
        front[i][0] = c.getFitness(branch);
        front[i][1] = c.getFitness(rho);
    }
    return front;
}
Also used : EvoSuite(org.evosuite.EvoSuite) ArrayList(java.util.ArrayList) Chromosome(org.evosuite.ga.Chromosome)

Example 57 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class TestSelectionOperators method testProportions.

@Ignore
@Test
public void testProportions() {
    boolean[] maximize = new boolean[] { false, true };
    SelectionFunction[] v = new SelectionFunction[] { new TournamentSelection(), new FitnessProportionateSelection(), new RankSelection() };
    final int N = 10;
    for (boolean b : maximize) {
        List<Chromosome> population = new LinkedList<Chromosome>();
        for (int i = 0; i < N; i++) {
            ExecutableChromosome ind = new TestChromosome();
            double fit = b ? N - i : i;
            // ind.setFitness(fit);
            ind.addFitness(null, fit);
            // Rank selection assumes the population in order, but for the others does not matter
            population.add(ind);
        }
        for (SelectionFunction sf : v) {
            sf.setMaximize(b);
            int[] counter = new int[N];
            for (int j = 0; j < 10000; j++) {
                int index = sf.getIndex(population);
                counter[index]++;
            }
            for (int j = 0; j < N - 1; j++) {
                Assert.assertTrue("" + counter[j] + " " + counter[j + 1], counter[j] > counter[j + 1]);
            }
            Assert.assertTrue(counter[N - 1] > 0);
        }
    }
}
Also used : RankSelection(org.evosuite.ga.operators.selection.RankSelection) Chromosome(org.evosuite.ga.Chromosome) TournamentSelection(org.evosuite.ga.operators.selection.TournamentSelection) SelectionFunction(org.evosuite.ga.operators.selection.SelectionFunction) FitnessProportionateSelection(org.evosuite.ga.operators.selection.FitnessProportionateSelection)

Example 58 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class ZDT1IntTest method testZDT1.

/**
 * Testing NSGA-II with ZDT1 Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testZDT1() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 30d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 30, 0.0, 1.0);
    GeneticAlgorithm<?> ga = new NSGAII(factory);
    BinaryTournamentSelectionCrowdedComparison ts = new BinaryTournamentSelectionCrowdedComparison();
    ts.setMaximize(false);
    ga.setSelectionFunction(ts);
    ga.setCrossOverFunction(new SBXCrossover());
    Problem p = new ZDT1();
    final FitnessFunction f1 = (FitnessFunction) p.getFitnessFunctions().get(0);
    final FitnessFunction f2 = (FitnessFunction) p.getFitnessFunctions().get(1);
    ga.addFitnessFunction(f1);
    ga.addFitnessFunction(f2);
    // 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));
        }
    });
    double[][] front = new double[Properties.POPULATION][2];
    int index = 0;
    for (Chromosome chromosome : chromosomes) {
        System.out.printf("%f,%f\n", chromosome.getFitness(f1), chromosome.getFitness(f2));
        front[index][0] = Double.valueOf(chromosome.getFitness(f1));
        front[index][1] = Double.valueOf(chromosome.getFitness(f2));
        index++;
    }
    // load True Pareto Front
    double[][] trueParetoFront = Metrics.readFront("ZDT1.pf");
    GenerationalDistance gd = new GenerationalDistance();
    double gdd = gd.evaluate(front, trueParetoFront);
    System.out.println("GenerationalDistance: " + gdd);
    Assert.assertEquals(gdd, 0.001, 0.001);
    Spacing sp = new Spacing();
    double spd = sp.evaluate(front);
    double spdt = sp.evaluate(trueParetoFront);
    System.out.println("SpacingFront (" + spd + ") - SpacingTrueFront (" + spdt + ") = " + Math.abs(spd - spdt));
    Assert.assertEquals(Math.abs(spd - spdt), 0.10, 0.10);
}
Also used : Chromosome(org.evosuite.ga.Chromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) Spacing(org.evosuite.ga.problems.metrics.Spacing) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) GenerationalDistance(org.evosuite.ga.problems.metrics.GenerationalDistance) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 59 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class TestBeales method testBeales.

/**
 * Testing NSGA-II with Beales Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testBeales() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 2d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 2, -4.5, 4.5);
    // 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 Beales();
    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.29, 0.01);
    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));
    }
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Example 60 with Chromosome

use of org.evosuite.ga.Chromosome in project evosuite by EvoSuite.

the class TestShere method testSphere.

/**
 * Testing NSGA-II with Sphere Problem
 *
 * @throws IOException
 * @throws NumberFormatException
 */
@Test
public void testSphere() throws NumberFormatException, IOException {
    Properties.MUTATION_RATE = 1d / 1d;
    ChromosomeFactory<?> factory = new RandomFactory(false, 1, Math.pow(-10.0, 3.0), Math.pow(10.0, 3.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 Sphere();
    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.00, 0.01);
    for (Chromosome chromosome : chromosomes) {
        NSGAChromosome nsga_c = (NSGAChromosome) chromosome;
        DoubleVariable x = (DoubleVariable) nsga_c.getVariables().get(0);
        System.out.printf("%f : %f\n", x.getValue(), chromosome.getFitness(f1));
    }
}
Also used : NSGAChromosome(org.evosuite.ga.NSGAChromosome) Chromosome(org.evosuite.ga.Chromosome) NSGAChromosome(org.evosuite.ga.NSGAChromosome) FitnessFunction(org.evosuite.ga.FitnessFunction) RandomFactory(org.evosuite.ga.metaheuristics.RandomFactory) NSGAII(org.evosuite.ga.metaheuristics.NSGAII) BinaryTournamentSelectionCrowdedComparison(org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison) Problem(org.evosuite.ga.problems.Problem) List(java.util.List) DoubleVariable(org.evosuite.ga.variables.DoubleVariable) SBXCrossover(org.evosuite.ga.operators.crossover.SBXCrossover) Test(org.junit.Test)

Aggregations

Chromosome (org.evosuite.ga.Chromosome)60 Test (org.junit.Test)41 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)32 ArrayList (java.util.ArrayList)29 Neighbourhood (org.evosuite.ga.Neighbourhood)20 FitnessFunction (org.evosuite.ga.FitnessFunction)15 List (java.util.List)13 SBXCrossover (org.evosuite.ga.operators.crossover.SBXCrossover)13 BinaryTournamentSelectionCrowdedComparison (org.evosuite.ga.operators.selection.BinaryTournamentSelectionCrowdedComparison)13 Problem (org.evosuite.ga.problems.Problem)13 NSGAChromosome (org.evosuite.ga.NSGAChromosome)12 NSGAII (org.evosuite.ga.metaheuristics.NSGAII)12 RandomFactory (org.evosuite.ga.metaheuristics.RandomFactory)12 GenerationalDistance (org.evosuite.ga.problems.metrics.GenerationalDistance)9 Spacing (org.evosuite.ga.problems.metrics.Spacing)9 TestChromosome (org.evosuite.testcase.TestChromosome)6 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)5 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)5 DoubleVariable (org.evosuite.ga.variables.DoubleVariable)4 EvoSuite (org.evosuite.EvoSuite)3