|
| 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 | + * LIPS (Linearly Independent Path-based Search). |
| 9 | + * Single-objective GA that optimizes one target (branch) at a time. |
| 10 | + * |
| 11 | + * Simplified adaptation: |
| 12 | + * - Picks a current target from the uncovered set. |
| 13 | + * - Freezes scoring on that target only. |
| 14 | + * - Runs one GA generation to evolve the population towards the current target. |
| 15 | + * - Adds discovered individuals to archive; when targets are covered, picks a new target. |
| 16 | + */ |
| 17 | +class LIPSAlgorithm<T> : AbstractGeneticAlgorithm<T>() where T : Individual { |
| 18 | + |
| 19 | + private var currentTarget: Int? = null |
| 20 | + |
| 21 | + override fun getType(): EMConfig.Algorithm = EMConfig.Algorithm.LIPS |
| 22 | + |
| 23 | + override fun searchOnce() { |
| 24 | + beginGeneration() |
| 25 | + |
| 26 | + // Compute uncovered goals |
| 27 | + val uncovered = archive.notCoveredTargets() |
| 28 | + if (uncovered.isEmpty()) { |
| 29 | + endGeneration() |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + // Pick target if none, or if previously covered |
| 34 | + if (currentTarget == null || !uncovered.contains(currentTarget)) { |
| 35 | + currentTarget = uncovered.last() |
| 36 | + } |
| 37 | + |
| 38 | + // Focus scoring on the single selected target |
| 39 | + frozenTargets = setOf(currentTarget!!) |
| 40 | + |
| 41 | + // Ensure population exists |
| 42 | + if (population.isEmpty()) { |
| 43 | + initPopulation() |
| 44 | + } |
| 45 | + |
| 46 | + val n = config.populationSize |
| 47 | + val nextPop: MutableList<WtsEvalIndividual<T>> = formTheNextPopulation(population) |
| 48 | + |
| 49 | + while (nextPop.size < n) { |
| 50 | + beginStep() |
| 51 | + |
| 52 | + val p1 = tournamentSelection() |
| 53 | + val p2 = tournamentSelection() |
| 54 | + |
| 55 | + val o1 = p1.copy() |
| 56 | + val o2 = p2.copy() |
| 57 | + |
| 58 | + if (randomness.nextBoolean(config.xoverProbability)) { |
| 59 | + xover(o1, o2) |
| 60 | + } |
| 61 | + if (randomness.nextBoolean(config.fixedRateMutation)) { |
| 62 | + mutate(o1) |
| 63 | + } |
| 64 | + if (randomness.nextBoolean(config.fixedRateMutation)) { |
| 65 | + mutate(o2) |
| 66 | + } |
| 67 | + |
| 68 | + // Keep best wrt frozen target |
| 69 | + val a = if (score(o1) >= score(o2)) o1 else o2 |
| 70 | + nextPop.add(a) |
| 71 | + |
| 72 | + // Stop if time is up |
| 73 | + if (!time.shouldContinueSearch()) { |
| 74 | + endStep() |
| 75 | + break |
| 76 | + } |
| 77 | + endStep() |
| 78 | + } |
| 79 | + |
| 80 | + population.clear() |
| 81 | + population.addAll(nextPop) |
| 82 | + |
| 83 | + // If target got covered (score 1 for any in pop), refresh target next time |
| 84 | + val coveredNow = population.any { score(it) >= 1.0 } |
| 85 | + if (coveredNow) { |
| 86 | + currentTarget = null |
| 87 | + } |
| 88 | + |
| 89 | + endGeneration() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
0 commit comments