-
Couldn't load subscription status.
- Fork 97
Breeder GA #1359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
francastagna
wants to merge
7
commits into
master
Choose a base branch
from
feature/breeder-ga
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−2
Open
Breeder GA #1359
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ef9641
added breeder-ga & tests
francastagna 1a02307
Update EMConfig.kt
francastagna 347777a
added BreederGA as a possible algorithm option in options.md
francastagna 72f098f
run ConfigToMarkdown
francastagna b40537e
added BreederGA to Main.kt
francastagna 9b527ea
Merge branch 'master' into feature/breeder-ga
francastagna aefedc2
Merge branch 'master' into feature/breeder-ga
francastagna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
core/src/main/kotlin/org/evomaster/core/search/algorithms/BreederGeneticAlgorithm.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package org.evomaster.core.search.algorithms | ||
|
|
||
| import org.evomaster.core.EMConfig | ||
| import org.evomaster.core.search.Individual | ||
| import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual | ||
| import kotlin.math.max | ||
|
|
||
| /** | ||
| * Breeder Genetic Algorithm (BGA) | ||
| * | ||
| * Differences vs Standard GA: | ||
| * - Uses truncation selection to build a parents pool P'. | ||
| * - At each step, creates two offspring from two random parents in P', | ||
| * then randomly selects ONE of the 2 offspring to add to the next population. | ||
| */ | ||
| class BreederGeneticAlgorithm<T> : AbstractGeneticAlgorithm<T>() where T : Individual { | ||
|
|
||
| override fun getType(): EMConfig.Algorithm { | ||
| return EMConfig.Algorithm.BreederGA | ||
| } | ||
|
|
||
| override fun searchOnce() { | ||
| beginGeneration() | ||
| frozenTargets = archive.notCoveredTargets() | ||
| val n = config.populationSize | ||
|
|
||
| // Elitism base for next generation | ||
| val nextPop = formTheNextPopulation(population) | ||
|
|
||
| // Build parents pool P' by truncation on current population | ||
| val parentsPool = buildParentsPoolByTruncation(population) | ||
|
|
||
| while (nextPop.size < n) { | ||
| beginStep() | ||
| val p1 = randomness.choose(parentsPool) | ||
| val p2 = randomness.choose(parentsPool) | ||
|
|
||
| // Work on copies | ||
| val o1 = p1.copy() | ||
| val o2 = p2.copy() | ||
|
|
||
| if (randomness.nextBoolean(config.xoverProbability)) { | ||
| xover(o1, o2) | ||
| } | ||
| if (randomness.nextBoolean(config.fixedRateMutation)) { | ||
| mutate(o1) | ||
| } | ||
| if (randomness.nextBoolean(config.fixedRateMutation)) { | ||
| mutate(o2) | ||
| } | ||
|
|
||
| // Randomly pick one child to carry over | ||
| var chosen = o1 | ||
| if (!randomness.nextBoolean()) { | ||
| chosen = o2 | ||
| } | ||
| nextPop.add(chosen) | ||
|
|
||
| if (!time.shouldContinueSearch()) { | ||
| endStep() | ||
| break | ||
| } | ||
| endStep() | ||
| } | ||
|
|
||
| population.clear() | ||
| population.addAll(nextPop) | ||
| endGeneration() | ||
| } | ||
|
|
||
| private fun buildParentsPoolByTruncation(pop: List<WtsEvalIndividual<T>>): List<WtsEvalIndividual<T>> { | ||
| if (pop.isEmpty()) { | ||
| return pop | ||
| } | ||
|
|
||
| val sorted = pop.sortedByDescending { score(it) } | ||
| val k = max(config.breederParentsMin, (sorted.size * config.breederTruncationFraction).toInt()) | ||
| return sorted.take(k) | ||
| } | ||
| } |
179 changes: 179 additions & 0 deletions
179
core/src/test/kotlin/org/evomaster/core/search/algorithms/BreederGeneticAlgorithmTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package org.evomaster.core.search.algorithms | ||
|
|
||
| import com.google.inject.Injector | ||
| import com.google.inject.Key | ||
| import com.google.inject.Module | ||
| import com.google.inject.TypeLiteral | ||
| import com.netflix.governator.guice.LifecycleInjector | ||
| import org.evomaster.core.BaseModule | ||
| import org.evomaster.core.EMConfig | ||
| import org.evomaster.core.TestUtils | ||
| import org.evomaster.core.search.algorithms.onemax.OneMaxIndividual | ||
| import org.evomaster.core.search.algorithms.onemax.OneMaxModule | ||
| import org.evomaster.core.search.algorithms.onemax.OneMaxSampler | ||
| import org.evomaster.core.search.algorithms.observer.GARecorder | ||
| import org.evomaster.core.search.service.ExecutionPhaseController | ||
| import org.evomaster.core.search.service.Randomness | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| import org.junit.jupiter.api.Assertions.* | ||
|
|
||
| class BreederGeneticAlgorithmTest { | ||
|
|
||
| private lateinit var injector: Injector | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| injector = LifecycleInjector.builder() | ||
| .withModules(* arrayOf<Module>(OneMaxModule(), BaseModule())) | ||
| .build().createInjector() | ||
| } | ||
|
|
||
| // Verifies that the Breeder GA can find the optimal solution for the OneMax problem | ||
| @Test | ||
| fun testBreederGeneticAlgorithmFindsOptimum() { | ||
| TestUtils.handleFlaky { | ||
| val breederGA = injector.getInstance( | ||
| Key.get( | ||
| object : TypeLiteral<BreederGeneticAlgorithm<OneMaxIndividual>>() {}) | ||
| ) | ||
|
|
||
| val config = injector.getInstance(EMConfig::class.java) | ||
| config.maxEvaluations = 10000 | ||
| config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS | ||
|
|
||
| val epc = injector.getInstance(ExecutionPhaseController::class.java) | ||
| epc.startSearch() | ||
| val solution = breederGA.search() | ||
| epc.finishSearch() | ||
|
|
||
| assertTrue(solution.individuals.size == 1) | ||
| assertEquals(OneMaxSampler.DEFAULT_N.toDouble(), solution.overall.computeFitnessScore(), 0.001) | ||
| } | ||
| } | ||
|
|
||
| // Verifies that BGA forms next generation as elites + chosen children from truncation | ||
| @Test | ||
| fun testNextGenerationIsElitesPlusTruncationChildren() { | ||
| TestUtils.handleFlaky { | ||
| val breederGA = injector.getInstance( | ||
| Key.get( | ||
| object : TypeLiteral<BreederGeneticAlgorithm<OneMaxIndividual>>() {}) | ||
| ) | ||
|
|
||
| val rec = GARecorder<OneMaxIndividual>() | ||
| breederGA.addObserver(rec) | ||
|
|
||
| val config = injector.getInstance(EMConfig::class.java) | ||
| injector.getInstance(Randomness::class.java).updateSeed(42) | ||
|
|
||
| config.populationSize = 4 | ||
| config.elitesCount = 2 | ||
| config.xoverProbability = 1.0 | ||
| config.fixedRateMutation = 1.0 | ||
| config.gaSolutionSource = EMConfig.GASolutionSource.POPULATION | ||
| config.maxEvaluations = 100_000 | ||
| config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS | ||
|
|
||
| breederGA.setupBeforeSearch() | ||
|
|
||
| val pop = breederGA.getViewOfPopulation() | ||
| val expectedElites = pop.sortedByDescending { it.calculateCombinedFitness() }.take(2) | ||
|
|
||
| breederGA.searchOnce() | ||
|
|
||
| val nextPop = breederGA.getViewOfPopulation() | ||
|
|
||
| // population size preserved | ||
| assertEquals(config.populationSize, nextPop.size) | ||
|
|
||
| // elites are present in next population | ||
| assertTrue(nextPop.any { it === expectedElites[0] }) | ||
| assertTrue(nextPop.any { it === expectedElites[1] }) | ||
|
|
||
| // number of iterations equals children added = populationSize - elites | ||
| val iterations = config.populationSize - config.elitesCount | ||
| assertEquals(iterations, rec.xoCalls.size) | ||
|
|
||
| // each iteration produced (o1,o2); exactly one should be carried over | ||
| rec.xoCalls.forEach { (o1, o2) -> | ||
| assertTrue(nextPop.any { it === o1 } || nextPop.any { it === o2 }) | ||
| } | ||
|
|
||
| // two mutations per iteration (one per offspring) | ||
| assertEquals(2 * iterations, rec.mutated.size) | ||
| } | ||
| } | ||
|
|
||
| // Edge Case: CrossoverProbability=0 and MutationProbability=1 on BGA | ||
| @Test | ||
| fun testNoCrossoverWhenProbabilityZero_BGA() { | ||
| TestUtils.handleFlaky { | ||
| val breederGA = injector.getInstance( | ||
| Key.get( | ||
| object : TypeLiteral<BreederGeneticAlgorithm<OneMaxIndividual>>() {}) | ||
| ) | ||
|
|
||
| val rec = GARecorder<OneMaxIndividual>() | ||
| breederGA.addObserver(rec) | ||
|
|
||
| val config = injector.getInstance(EMConfig::class.java) | ||
| config.populationSize = 4 | ||
| config.elitesCount = 0 | ||
| config.xoverProbability = 0.0 // disable crossover | ||
| config.fixedRateMutation = 1.0 // force mutation | ||
| config.gaSolutionSource = EMConfig.GASolutionSource.POPULATION | ||
| config.maxEvaluations = 100_000 | ||
| config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS | ||
|
|
||
| breederGA.setupBeforeSearch() | ||
| breederGA.searchOnce() | ||
|
|
||
| val nextPop = breederGA.getViewOfPopulation() | ||
| assertEquals(config.populationSize, nextPop.size) | ||
|
|
||
| // crossover disabled | ||
| assertEquals(0, rec.xoCalls.size) | ||
| // should apply two mutations per iteration (mutation probability = 1) | ||
| assertEquals(2 * config.populationSize, rec.mutated.size) | ||
| } | ||
| } | ||
|
|
||
| // Edge Case: MutationProbability=0 and CrossoverProbability=1 on BGA | ||
| @Test | ||
| fun testNoMutationWhenProbabilityZero_BGA() { | ||
| TestUtils.handleFlaky { | ||
| val breederGA = injector.getInstance( | ||
| Key.get( | ||
| object : TypeLiteral<BreederGeneticAlgorithm<OneMaxIndividual>>() {}) | ||
| ) | ||
|
|
||
| val rec = GARecorder<OneMaxIndividual>() | ||
| breederGA.addObserver(rec) | ||
|
|
||
| val config = injector.getInstance(EMConfig::class.java) | ||
| config.populationSize = 4 | ||
| config.elitesCount = 0 | ||
| config.xoverProbability = 1.0 // force crossover | ||
| config.fixedRateMutation = 0.0 // disable mutation | ||
| config.gaSolutionSource = EMConfig.GASolutionSource.POPULATION | ||
| config.maxEvaluations = 100_000 | ||
| config.stoppingCriterion = EMConfig.StoppingCriterion.ACTION_EVALUATIONS | ||
|
|
||
| breederGA.setupBeforeSearch() | ||
| breederGA.searchOnce() | ||
|
|
||
| val nextPop = breederGA.getViewOfPopulation() | ||
| assertEquals(config.populationSize, nextPop.size) | ||
|
|
||
| // crossovers happen once per iteration (mutation probability = 1) | ||
| assertEquals(config.populationSize, rec.xoCalls.size) | ||
|
|
||
| // mutations disabled | ||
| assertEquals(0, rec.mutated.size) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be
@PercentageAsProbability