Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d313f00
feat(core/ga): freeze targets per generation; add GASolutionSource + …
fncastagna Oct 6, 2025
8fb5bbf
fix cp error in StandardGeneticAlgorithmTest.kt
fncastagna Oct 6, 2025
8288e69
add tests to StandardGeneticAlgorithmTest.kt
fncastagna Oct 9, 2025
b5e7283
added publisher observer pattern
fncastagna Oct 9, 2025
916290a
rename crossover and mutator operator method names
fncastagna Oct 10, 2025
23c0060
applyMutation renamed to mutateIndividual and DefaultCrossoveOperator…
fncastagna Oct 15, 2025
f8cd208
added DefaultMutationOperator javadoc
fncastagna Oct 15, 2025
d111925
added TournamentSelectionStrategy javadocs
fncastagna Oct 15, 2025
19f5c41
fix matching brackets
fncastagna Oct 15, 2025
08e98b0
add tournament selection strategy javadocs
fncastagna Oct 15, 2025
a0dc76c
deleted accidentaly commited file MonotonicGeneticAlgorithmTest
fncastagna Oct 15, 2025
0259fe2
Merge pull request #3 from francastagna/feature/genetics-algorithms
francastagna Oct 15, 2025
46e4164
Merge branch 'master' into master
francastagna Oct 15, 2025
8749385
remove GA bindings from BaseModule
fncastagna Oct 17, 2025
7bcac3b
improve explanation of GASolutionSource i
fncastagna Oct 17, 2025
7edce0d
remove injection of GA operators, changed inline for loop to multilin…
fncastagna Oct 17, 2025
e931072
added suite to GA operators package name
fncastagna Oct 17, 2025
14ffc21
added @BeforeEach to tests in StandardGeneticAlgorithmTest
fncastagna Oct 17, 2025
28d9e72
added suite to GA operators package name
fncastagna Oct 17, 2025
40869f9
minor fixes
fncastagna Oct 17, 2025
4c82ab9
compilation and test fixes
fncastagna Oct 17, 2025
013a774
renamed mutation operator
fncastagna Oct 17, 2025
7bf221f
Merge pull request #7 from francastagna/feature/standard-genetic-algo…
francastagna Oct 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/kotlin/org/evomaster/core/EMConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2852,6 +2852,19 @@ class EMConfig {

fun isEnabledAIModelForResponseClassification() = aiModelForResponseClassification != AIResponseClassifierModel.NONE

/**
* Source to build the final GA solution when evolving full test suites (not single tests).
* ARCHIVE: use current behavior (take tests from the archive).
* POPULATION: for GA algorithms, take the best suite (individual) from the final population.
*/
enum class GASolutionSource { ARCHIVE, POPULATION }

/**
* Controls how GA algorithms produce the final solution.
* Default preserves current behavior.
*/
var gaSolutionSource: GASolutionSource = GASolutionSource.ARCHIVE

private var disabledOracleCodesList: List<FaultCategory>? = null

fun getDisabledOracleCodesList(): List<FaultCategory> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ package org.evomaster.core.search.algorithms
import org.evomaster.core.search.Individual
import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual
import org.evomaster.core.search.service.SearchAlgorithm
import org.evomaster.core.EMConfig
import org.evomaster.core.search.Solution
import com.google.inject.Inject
import org.evomaster.core.search.algorithms.strategy.suite.CrossoverOperator
import org.evomaster.core.search.algorithms.strategy.suite.MutationEvaluationOperator
import org.evomaster.core.search.algorithms.strategy.suite.DefaultMutationEvaluationOperator
import org.evomaster.core.search.algorithms.strategy.suite.SelectionStrategy
import org.evomaster.core.search.algorithms.strategy.suite.TournamentSelectionStrategy
import org.evomaster.core.search.algorithms.strategy.suite.DefaultCrossoverOperator
import org.evomaster.core.search.algorithms.observer.GAObserver

/**
* Abstract base class for implementing Genetic Algorithms (GAs) in EvoMaster.
Expand All @@ -24,6 +34,29 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi
/** The current population of evaluated individuals (test suites). */
protected val population: MutableList<WtsEvalIndividual<T>> = mutableListOf()

/**
* Frozen set of targets (coverage objectives) to score against during the current generation.
* Captured at the start of a generation and kept constant until the generation ends.
*/
protected var frozenTargets: Set<Int> = emptySet()

protected var selectionStrategy: SelectionStrategy = TournamentSelectionStrategy()

protected var crossoverOperator: CrossoverOperator = DefaultCrossoverOperator()

protected var mutationOperator: MutationEvaluationOperator = DefaultMutationEvaluationOperator()

/** Optional observers for GA events (test/telemetry). */
protected val observers: MutableList<GAObserver<T>> = mutableListOf()

fun addObserver(observer: GAObserver<T>) {
observers.add(observer)
}

fun removeObserver(observer: GAObserver<T>) {
observers.remove(observer)
}

/**
* Called once before the search begins. Clears any old population and initializes a new one.
*/
Expand Down Expand Up @@ -59,7 +92,7 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi
val nextPop: MutableList<WtsEvalIndividual<T>> = mutableListOf()

if (config.elitesCount > 0 && population.isNotEmpty()) {
val sortedPopulation = population.sortedByDescending { it.calculateCombinedFitness() }
val sortedPopulation = population.sortedByDescending { score(it) }
val elites = sortedPopulation.take(config.elitesCount)
nextPop.addAll(elites)
}
Expand All @@ -78,30 +111,17 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi
* This method modifies the individual in-place.
*/
protected fun mutate(wts: WtsEvalIndividual<T>) {
val op = randomness.choose(listOf("del", "add", "mod"))
val n = wts.suite.size

when (op) {
"del" -> if (n > 1) {
val i = randomness.nextInt(n)
wts.suite.removeAt(i)
}

"add" -> if (n < config.maxSearchSuiteSize) {
ff.calculateCoverage(sampler.sample(), modifiedSpec = null)?.run {
archive.addIfNeeded(this)
wts.suite.add(this)
}
}

"mod" -> {
val i = randomness.nextInt(n)
val ind = wts.suite[i]

getMutatator().mutateAndSave(ind, archive)
?.let { wts.suite[i] = it }
}
}
mutationOperator.mutateEvaluateAndArchive(
wts,
config,
randomness,
getMutatator(),
ff,
sampler,
archive
)
// notify observers
observers.forEach { it.onMutation(wts) }
}

/**
Expand All @@ -111,28 +131,28 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi
* (bounded by the size of the smaller suite).
*/
protected fun xover(x: WtsEvalIndividual<T>, y: WtsEvalIndividual<T>) {
val nx = x.suite.size
val ny = y.suite.size

val splitPoint = randomness.nextInt(Math.min(nx, ny))

(0..splitPoint).forEach {
val k = x.suite[it]
x.suite[it] = y.suite[it]
y.suite[it] = k
}
crossoverOperator.applyCrossover(x, y, randomness)
// notify observers
observers.forEach { it.onCrossover(x, y) }
}

/**
* Allows tests or callers to override GA operators without DI.
*/
fun useSelectionStrategy(strategy: SelectionStrategy) { this.selectionStrategy = strategy }
fun useCrossoverOperator(operator: CrossoverOperator) { this.crossoverOperator = operator }
fun useMutationOperator(operator: MutationEvaluationOperator) { this.mutationOperator = operator }

/**
* Selects one individual using tournament selection.
*
* A subset of the population is randomly selected, and the individual with the
* highest fitness among them is chosen. Falls back to random selection if needed.
*/
protected fun tournamentSelection(): WtsEvalIndividual<T> {
val selectedIndividuals = randomness.choose(population, config.tournamentSize)
val bestIndividual = selectedIndividuals.maxByOrNull { it.calculateCombinedFitness() }
return bestIndividual ?: randomness.choose(population)
val sel = selectionStrategy.select(population, config.tournamentSize, randomness, ::score)
observers.forEach { it.onSelection(sel) }
return sel
}

/**
Expand All @@ -141,13 +161,15 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi
* Each element is generated by sampling and evaluated via the fitness function.
* Stops early if the time budget is exceeded.
*/
private fun sampleSuite(): WtsEvalIndividual<T> {
protected fun sampleSuite(): WtsEvalIndividual<T> {
val n = 1 + randomness.nextInt(config.maxSearchSuiteSize)
val suite = WtsEvalIndividual<T>(mutableListOf())

for (i in 1..n) {
ff.calculateCoverage(sampler.sample(), modifiedSpec = null)?.run {
archive.addIfNeeded(this)
if (config.gaSolutionSource != EMConfig.GASolutionSource.POPULATION) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this? @jgaleotti did we explicitly speak about this? i don't remember... ie, whether we want to evaluated test-level archive for suite generation

archive.addIfNeeded(this)
}
suite.suite.add(this)
}

Expand All @@ -158,4 +180,57 @@ abstract class AbstractGeneticAlgorithm<T> : SearchAlgorithm<T>() where T : Indi

return suite
}

/**
* Combined fitness of a suite computed only over [frozenTargets] when set; otherwise full combined fitness.
*/
protected fun score(w: WtsEvalIndividual<T>): Double {
if (w.suite.isEmpty()) return 0.0

// Explicitly use full combined fitness when solution source is POPULATION
if (config.gaSolutionSource == EMConfig.GASolutionSource.POPULATION) {
return w.calculateCombinedFitness()
}

if (frozenTargets.isEmpty()) {
return w.calculateCombinedFitness()
}

val fv = w.suite.first().fitness.copy()
w.suite.forEach { ei -> fv.merge(ei.fitness) }
val view = fv.getViewOfData()
var sum = 0.0
frozenTargets.forEach { t ->
val comp = view[t]
if (comp != null) sum += comp.score
}
return sum
}

/**
* For GA algorithms, optionally build the final solution from the final population
* instead of the archive, controlled by config.gaSolutionSource.
*/
override fun buildSolution(): Solution<T> {
return if (config.gaSolutionSource == EMConfig.GASolutionSource.POPULATION) {
val best = population.maxByOrNull { it.calculateCombinedFitness() }
val individuals = (best?.suite ?: mutableListOf())
Solution(
individuals.toMutableList(),
config.outputFilePrefix,
config.outputFileSuffix,
org.evomaster.core.output.Termination.NONE,
listOf(),
listOf()
)
} else {
super.buildSolution()
}
}

/**
* Exposes a read-only view of the current population for observability/tests.
* Returns an immutable copy to prevent external mutations of internal state.
*/
fun getViewOfPopulation(): List<WtsEvalIndividual<T>> = population.toList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ open class StandardGeneticAlgorithm<T> : AbstractGeneticAlgorithm<T>() where T :
* Terminates early if the time budget is exceeded.
*/
override fun searchOnce() {
// Freeze objectives for this generation
frozenTargets = archive.notCoveredTargets()
val n = config.populationSize

// Generate the base of the next population (e.g., elitism or re-selection of fit individuals)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.evomaster.core.search.algorithms.observer

import org.evomaster.core.search.Individual
import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual

/**
* Observer for GA internal events used primarily for testing/telemetry.
* Default methods are no-ops so listeners can implement only what they need.
*/
interface GAObserver<T : Individual> {
/** Called when one parent is selected. */
fun onSelection(sel: WtsEvalIndividual<T>) {}
/** Called immediately after crossover is applied to [x] and [y]. */
fun onCrossover(x: WtsEvalIndividual<T>, y: WtsEvalIndividual<T>) {}

/** Called immediately after mutation is applied to [wts]. */
fun onMutation(wts: WtsEvalIndividual<T>) {}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.evomaster.core.search.algorithms.strategy.suite

import org.evomaster.core.search.Individual
import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual
import org.evomaster.core.search.service.Randomness

interface CrossoverOperator {
fun <T : Individual> applyCrossover(x: WtsEvalIndividual<T>, y: WtsEvalIndividual<T>, randomness: Randomness)
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.evomaster.core.search.algorithms.strategy.suite

import org.evomaster.core.search.Individual
import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual
import org.evomaster.core.search.service.Randomness
import kotlin.math.min



/**
* Default crossover operator for GA test suites.
*
* Behavior:
* - Takes the two suites as input, named x and y.
* - Picks a split point uniformly at random in [0, min(len(x), len(y)) - 1].
* - Swaps all elements from index 0 up to the split point (i ∈ [0, split]).
*/
class DefaultCrossoverOperator : CrossoverOperator {
override fun <T : Individual> applyCrossover(
x: WtsEvalIndividual<T>,
y: WtsEvalIndividual<T>,
randomness: Randomness
) {
val nx = x.suite.size
val ny = y.suite.size
val splitPoint = randomness.nextInt(min(nx, ny))
(0..splitPoint).forEach {
val k = x.suite[it]
x.suite[it] = y.suite[it]
y.suite[it] = k
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.evomaster.core.search.algorithms.strategy.suite

import org.evomaster.core.EMConfig
import org.evomaster.core.search.Individual
import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual
import org.evomaster.core.search.service.Archive
import org.evomaster.core.search.service.FitnessFunction
import org.evomaster.core.search.service.Randomness
import org.evomaster.core.search.service.Sampler
import org.evomaster.core.search.service.mutator.Mutator

/**
* Default mutation operator acting at the test suite level for GA.
*
* Behavior:
* - Randomly applies one of: "del", "add", "mod" (selected randomly)
* - del: if size > 1, remove a random test from the suite.
* - add: if size < maxSearchSuiteSize, sample + evaluate; add new test to suite (and archive if needed).
* - mod: mutate one random test in the suite; re-evaluate (or mutateAndSave if GASolutionSource = ARCHIVE).
*/
class DefaultMutationEvaluationOperator : MutationEvaluationOperator {
companion object {
private const val OP_DELETE = "del"
private const val OP_ADD = "add"
private const val OP_MOD = "mod"
}

override fun <T : Individual> mutateEvaluateAndArchive(
wts: WtsEvalIndividual<T>,
config: EMConfig,
randomness: Randomness,
mutator: Mutator<T>,
ff: FitnessFunction<T>,
sampler: Sampler<T>,
archive: Archive<T>
) {
val op = randomness.choose(listOf(OP_DELETE, OP_ADD, OP_MOD))
val n = wts.suite.size

when (op) {
OP_DELETE -> if (n > 1) {
val i = randomness.nextInt(n)
wts.suite.removeAt(i)
}

OP_ADD -> if (n < config.maxSearchSuiteSize) {
ff.calculateCoverage(sampler.sample(), modifiedSpec = null)?.run {
if (config.gaSolutionSource == EMConfig.GASolutionSource.ARCHIVE) {
archive.addIfNeeded(this)
}
wts.suite.add(this)
}
}

OP_MOD -> {
val i = randomness.nextInt(n)
val ind = wts.suite[i]

if (config.gaSolutionSource == EMConfig.GASolutionSource.ARCHIVE) {
mutator.mutateAndSave(ind, archive)?.let { wts.suite[i] = it }
} else {
val mutated = mutator.mutate(ind)
ff.calculateCoverage(mutated, modifiedSpec = null)?.let { wts.suite[i] = it }
}
}
}
}
}


Loading