|
| 1 | +package org.evomaster.core.search.algorithms |
| 2 | + |
| 3 | +import com.google.inject.Injector |
| 4 | +import com.google.inject.Key |
| 5 | +import com.google.inject.Module |
| 6 | +import com.google.inject.TypeLiteral |
| 7 | +import com.netflix.governator.guice.LifecycleInjector |
| 8 | +import org.evomaster.core.BaseModule |
| 9 | +import org.evomaster.core.EMConfig |
| 10 | +import org.evomaster.core.TestUtils |
| 11 | +import org.evomaster.core.search.algorithms.observer.GARecorder |
| 12 | +import org.evomaster.core.search.algorithms.onemax.OneMaxIndividual |
| 13 | +import org.evomaster.core.search.algorithms.onemax.OneMaxModule |
| 14 | +import org.evomaster.core.search.algorithms.onemax.OneMaxSampler |
| 15 | +import org.evomaster.core.search.service.ExecutionPhaseController |
| 16 | +import org.junit.jupiter.api.Assertions.* |
| 17 | +import org.junit.jupiter.api.BeforeEach |
| 18 | +import org.junit.jupiter.api.Test |
| 19 | + |
| 20 | +class MuPlusLambdaEvolutionaryAlgorithmTest { |
| 21 | + |
| 22 | + private lateinit var injector: Injector |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + fun setUp() { |
| 26 | + injector = LifecycleInjector.builder() |
| 27 | + .withModules(* arrayOf<Module>(OneMaxModule(), BaseModule())) |
| 28 | + .build().createInjector() |
| 29 | + } |
| 30 | + |
| 31 | + // Verifies that the (μ+λ) EA can find the optimal solution for the OneMax problem |
| 32 | + @Test |
| 33 | + fun testMuPlusLambdaEAFindsOptimum() { |
| 34 | + TestUtils.handleFlaky { |
| 35 | + val ea = injector.getInstance( |
| 36 | + Key.get(object : TypeLiteral<MuPlusLambdaEvolutionaryAlgorithm<OneMaxIndividual>>() {}) |
| 37 | + ) |
| 38 | + |
| 39 | + val config = injector.getInstance(EMConfig::class.java) |
| 40 | + config.populationSize = 5 |
| 41 | + config.muPlusLambdaOffspringSize = 10 |
| 42 | + config.maxEvaluations = 10000 |
| 43 | + config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 44 | + |
| 45 | + val epc = injector.getInstance(ExecutionPhaseController::class.java) |
| 46 | + epc.startSearch() |
| 47 | + val solution = ea.search() |
| 48 | + epc.finishSearch() |
| 49 | + |
| 50 | + assertEquals(1, solution.individuals.size) |
| 51 | + assertEquals(OneMaxSampler.DEFAULT_N.toDouble(), solution.overall.computeFitnessScore(), 0.001) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Edge Case: CrossoverProbability=0 and MutationProbability=1 |
| 56 | + @Test |
| 57 | + fun testNoCrossoverWhenProbabilityZero_MuPlusEA() { |
| 58 | + TestUtils.handleFlaky { |
| 59 | + val ea = injector.getInstance( |
| 60 | + Key.get(object : TypeLiteral<MuPlusLambdaEvolutionaryAlgorithm<OneMaxIndividual>>() {}) |
| 61 | + ) |
| 62 | + |
| 63 | + val rec = GARecorder<OneMaxIndividual>() |
| 64 | + ea.addObserver(rec) |
| 65 | + |
| 66 | + val config = injector.getInstance(EMConfig::class.java) |
| 67 | + config.gaSolutionSource = EMConfig.GASolutionSource.POPULATION |
| 68 | + config.maxEvaluations = 100_000 |
| 69 | + config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 70 | + config.populationSize = 5 |
| 71 | + config.muPlusLambdaOffspringSize = 10 // divisible by mu |
| 72 | + config.xoverProbability = 0.0 // disable crossover |
| 73 | + config.fixedRateMutation = 1.0 // force mutation |
| 74 | + |
| 75 | + ea.setupBeforeSearch() |
| 76 | + ea.searchOnce() |
| 77 | + |
| 78 | + val nextPop = ea.getViewOfPopulation() |
| 79 | + // population remains of size mu in (μ+λ) EA |
| 80 | + assertEquals(config.populationSize, nextPop.size) |
| 81 | + |
| 82 | + // crossover disabled (and not used by this EA anyway) |
| 83 | + assertEquals(0, rec.xoCalls.size) |
| 84 | + // λ offspring mutated |
| 85 | + assertEquals(config.muPlusLambdaOffspringSize, rec.mutated.size) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Edge Case: MutationProbability=0 and CrossoverProbability=1 |
| 90 | + @Test |
| 91 | + fun testNoMutationWhenProbabilityZero_MuPlusEA() { |
| 92 | + TestUtils.handleFlaky { |
| 93 | + val ea = injector.getInstance( |
| 94 | + Key.get(object : TypeLiteral<MuPlusLambdaEvolutionaryAlgorithm<OneMaxIndividual>>() {}) |
| 95 | + ) |
| 96 | + |
| 97 | + val rec = GARecorder<OneMaxIndividual>() |
| 98 | + ea.addObserver(rec) |
| 99 | + |
| 100 | + val config = injector.getInstance(EMConfig::class.java) |
| 101 | + config.gaSolutionSource = EMConfig.GASolutionSource.POPULATION |
| 102 | + config.maxEvaluations = 100_000 |
| 103 | + config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS |
| 104 | + config.populationSize = 5 |
| 105 | + config.muPlusLambdaOffspringSize = 10 // divisible by mu |
| 106 | + config.xoverProbability = 1.0 // force crossover (not used in this EA) |
| 107 | + config.fixedRateMutation = 0.0 // disable mutation |
| 108 | + |
| 109 | + ea.setupBeforeSearch() |
| 110 | + ea.searchOnce() |
| 111 | + |
| 112 | + val nextPop = ea.getViewOfPopulation() |
| 113 | + // population remains of size mu in (μ+λ) EA |
| 114 | + assertEquals(config.populationSize, nextPop.size) |
| 115 | + |
| 116 | + // crossovers are not used in (μ+λ) EA |
| 117 | + assertEquals(0, rec.xoCalls.size) |
| 118 | + |
| 119 | + // mutations disabled |
| 120 | + assertEquals(0, rec.mutated.size) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // One iteration properties: population size, best-µ selection, mutation count |
| 125 | + @Test |
| 126 | + fun testNextGenerationIsTheBestMuOfParentsUnionOffspring() { |
| 127 | + TestUtils.handleFlaky { |
| 128 | + val ea = injector.getInstance( |
| 129 | + Key.get(object : TypeLiteral<MuPlusLambdaEvolutionaryAlgorithm<OneMaxIndividual>>() {}) |
| 130 | + ) |
| 131 | + |
| 132 | + val rec = GARecorder<OneMaxIndividual>() |
| 133 | + ea.addObserver(rec) |
| 134 | + |
| 135 | + val config = injector.getInstance(EMConfig::class.java) |
| 136 | + config.populationSize = 5 |
| 137 | + config.muPlusLambdaOffspringSize = 10 // divisible by mu -> perParent = 2 |
| 138 | + config.xoverProbability = 0.0 // not used in (µ+λ) |
| 139 | + config.fixedRateMutation = 1.0 // force mutation on all offspring |
| 140 | + |
| 141 | + // initialize population and snapshot parents |
| 142 | + ea.setupBeforeSearch() |
| 143 | + val parents = ea.getViewOfPopulation().toList() |
| 144 | + |
| 145 | + // run a single generation |
| 146 | + ea.searchOnce() |
| 147 | + |
| 148 | + val finalPop = ea.getViewOfPopulation() |
| 149 | + val mu = config.populationSize |
| 150 | + |
| 151 | + // 1) population size remains µ |
| 152 | + assertEquals(mu, finalPop.size) |
| 153 | + |
| 154 | + // 2) final population equals best-µ of parents ∪ offspring (compare scores) |
| 155 | + val offspring = rec.mutated.toList() |
| 156 | + val expectedScores = (parents + offspring) |
| 157 | + .map { ea.score(it) } |
| 158 | + .sortedDescending() |
| 159 | + .take(mu) |
| 160 | + val finalScores = finalPop |
| 161 | + .map { ea.score(it) } |
| 162 | + .sortedDescending() |
| 163 | + assertEquals(expectedScores, finalScores) |
| 164 | + |
| 165 | + // 3) with fixedRateMutation=1, mutations equal number of created offspring |
| 166 | + val perParent = config.muPlusLambdaOffspringSize / config.populationSize |
| 167 | + val expectedMutations = perParent * config.populationSize |
| 168 | + assertEquals(expectedMutations, rec.mutated.size) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | + |
0 commit comments