|
| 1 | +package org.evomaster.core.search.algorithms |
| 2 | + |
| 3 | +import org.evomaster.core.EMConfig |
| 4 | +import org.evomaster.core.search.Individual |
| 5 | +import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual |
| 6 | + |
| 7 | +/** |
| 8 | + * Cellular Genetic Algorithm (cGA). |
| 9 | + * |
| 10 | + * The population is organized on a virtual grid defined by a neighborhood model |
| 11 | + * (e.g., ring, 5-linear, compact 9, compact 13). For each cell i in the current |
| 12 | + * generation, the algorithm performs a localized evolutionary step restricted to |
| 13 | + * i's neighborhood: |
| 14 | + * |
| 15 | + * 1) Build i's neighborhood according to the configured model (with wrap-around). |
| 16 | + * 2) Select two parents via tournament restricted to that neighborhood. |
| 17 | + * 3) Create two offspring and apply crossover with probability |
| 18 | + * `xoverProbability`. |
| 19 | + * 4) Retain the best offspring by fitness and apply mutation with probability |
| 20 | + * `fixedRateMutation`. |
| 21 | + * 5) Replace the individual at position i with the best between the mutated offspring |
| 22 | + * and the current individual at i (local elitism). |
| 23 | + * |
| 24 | + * One pass over all cells produces the next population. The process repeats until the |
| 25 | + * global stopping criterion is met. |
| 26 | + */ |
| 27 | +class CellularGeneticAlgorithm<T> : AbstractGeneticAlgorithm<T>() where T : Individual { |
| 28 | + |
| 29 | + override fun getType(): EMConfig.Algorithm { |
| 30 | + return EMConfig.Algorithm.CellularGA |
| 31 | + } |
| 32 | + |
| 33 | + override fun searchOnce() { |
| 34 | + beginGeneration() |
| 35 | + // Freeze targets for current generation |
| 36 | + frozenTargets = archive.notCoveredTargets() |
| 37 | + |
| 38 | + val n = population.size |
| 39 | + val next: MutableList<WtsEvalIndividual<T>> = mutableListOf() |
| 40 | + |
| 41 | + for (i in 0 until n) { |
| 42 | + beginStep() |
| 43 | + val p = population[i] |
| 44 | + |
| 45 | + val neighbors = getNeighborhood(i) |
| 46 | + |
| 47 | + // Cellular tournament selection within neighborhood |
| 48 | + val x = neighborhoodTournament(neighbors) |
| 49 | + val y = neighborhoodTournament(neighbors) |
| 50 | + |
| 51 | + val o1 = x.copy() |
| 52 | + val o2 = y.copy() |
| 53 | + if (randomness.nextBoolean(config.xoverProbability)) { |
| 54 | + xover(o1, o2) |
| 55 | + } |
| 56 | + |
| 57 | + val o: WtsEvalIndividual<T> = if (score(o1) >= score(o2)) o1 else o2 |
| 58 | + |
| 59 | + if (randomness.nextBoolean(config.fixedRateMutation)) { |
| 60 | + mutate(o) |
| 61 | + } |
| 62 | + |
| 63 | + val bestLocal: WtsEvalIndividual<T> = if (score(o) >= score(p)) o else p |
| 64 | + next.add(bestLocal) |
| 65 | + endStep() |
| 66 | + } |
| 67 | + |
| 68 | + population.clear() |
| 69 | + population.addAll(next) |
| 70 | + endGeneration() |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Runs tournament selection restricted to a neighborhood subset. |
| 75 | + */ |
| 76 | + private fun neighborhoodTournament(neighbors: List<WtsEvalIndividual<T>>): WtsEvalIndividual<T> { |
| 77 | + val sel = selectionStrategy.select(neighbors, config.tournamentSize, randomness, ::score) |
| 78 | + observers.forEach { it.onSelection(sel) } |
| 79 | + return sel |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the neighborhood list for a given index based on the configured model. |
| 84 | + * The returned list always includes the current cell ("center") and neighbors, |
| 85 | + * in the order customary for the chosen model. |
| 86 | + */ |
| 87 | + private fun getNeighborhood(index: Int): List<WtsEvalIndividual<T>> { |
| 88 | + val model = config.cgaNeighborhoodModel |
| 89 | + val neighborhood = Neighborhood<T>(population.size) |
| 90 | + if (model == EMConfig.CGANeighborhoodModel.RING) { |
| 91 | + return neighborhood.ringTopology(population, index) |
| 92 | + } |
| 93 | + if (model == EMConfig.CGANeighborhoodModel.L5) { |
| 94 | + return neighborhood.linearFive(population, index) |
| 95 | + } |
| 96 | + if (model == EMConfig.CGANeighborhoodModel.C9) { |
| 97 | + return neighborhood.compactNine(population, index) |
| 98 | + } |
| 99 | + return neighborhood.compactThirteen(population, index) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
0 commit comments