|
| 1 | +package org.evomaster.core.search.algorithms |
| 2 | + |
| 3 | +import com.google.inject.Inject |
| 4 | +import org.evomaster.core.EMConfig |
| 5 | +import org.evomaster.core.search.Individual |
| 6 | +import org.evomaster.core.search.algorithms.wts.WtsEvalIndividual |
| 7 | +import org.evomaster.client.java.instrumentation.shared.ObjectiveNaming |
| 8 | +import org.evomaster.core.search.service.IdMapper |
| 9 | + |
| 10 | +/** |
| 11 | + * Linearly Independent Path-based Search (LIPS). |
| 12 | + * |
| 13 | + * A single-objective GA that optimizes one branch target at a time. |
| 14 | + * |
| 15 | + * - Initializes a random individual i and build the initial population P = random ∪ {i}. |
| 16 | + * - Maintains a current branch target. |
| 17 | + * - Per-target budget is a fair share of the global TIME/ACTIONS budget; switches target when the target is covered or its budget is exhausted. |
| 18 | + */ |
| 19 | +class LIPSAlgorithm<T> : AbstractGeneticAlgorithm<T>() where T : Individual { |
| 20 | + |
| 21 | + @Inject |
| 22 | + private lateinit var idMapper: IdMapper |
| 23 | + |
| 24 | + private var currentTarget: Int? = null |
| 25 | + private lateinit var budget: LipsBudget |
| 26 | + |
| 27 | + override fun getType(): EMConfig.Algorithm = EMConfig.Algorithm.LIPS |
| 28 | + |
| 29 | + override fun initPopulation() { |
| 30 | + population.clear() |
| 31 | + // 1) Generate Random Individual |
| 32 | + val i = sampleSuite() |
| 33 | + // 2) P <- RandomPopulation(ps-1) ∪ {i} |
| 34 | + population.add(i) |
| 35 | + while (population.size < config.populationSize) { |
| 36 | + population.add(sampleSuite()) |
| 37 | + } |
| 38 | + // Initialize budget manager and first per-target budget using current uncovered targets |
| 39 | + budget = LipsBudget(config, time) |
| 40 | + val initUncoveredSize = archive.notCoveredTargets().size |
| 41 | + budget.budgetLeftForCurrentTarget = budget.computePerTargetBudget(initUncoveredSize) |
| 42 | + } |
| 43 | + |
| 44 | + override fun searchOnce() { |
| 45 | + beginGeneration() |
| 46 | + // record budget usage for this generation |
| 47 | + val startActions = time.evaluatedActions |
| 48 | + val startSeconds = time.getElapsedSeconds() |
| 49 | + |
| 50 | + // Compute uncovered goals |
| 51 | + val uncovered = archive.notCoveredTargets() |
| 52 | + |
| 53 | + // current target is null if covered by previous generation or out of budget |
| 54 | + // Pick target if null, or if previously covered (check coverage directly) |
| 55 | + val needNewTarget = currentTarget == null || archive.isCovered(currentTarget!!) |
| 56 | + if (needNewTarget) { |
| 57 | + val target = lastUncoveredBranchTargetId() |
| 58 | + currentTarget = target |
| 59 | + // Initialize budget for this NEW target |
| 60 | + budget.budgetLeftForCurrentTarget = budget.computePerTargetBudget(uncovered.size) |
| 61 | + } |
| 62 | + |
| 63 | + // Focus scoring on the single selected target if present; otherwise use global fitness |
| 64 | + if (currentTarget == null) { |
| 65 | + frozenTargets = emptySet() |
| 66 | + } else { |
| 67 | + frozenTargets = setOf(currentTarget!!) |
| 68 | + } |
| 69 | + |
| 70 | + val n = config.populationSize |
| 71 | + val nextPop: MutableList<WtsEvalIndividual<T>> = formTheNextPopulation(population) |
| 72 | + |
| 73 | + while (nextPop.size < n) { |
| 74 | + beginStep() |
| 75 | + |
| 76 | + val p1 = tournamentSelection() |
| 77 | + val p2 = tournamentSelection() |
| 78 | + |
| 79 | + val o1 = p1.copy() |
| 80 | + val o2 = p2.copy() |
| 81 | + |
| 82 | + if (randomness.nextBoolean(config.xoverProbability)) { |
| 83 | + xover(o1, o2) |
| 84 | + } |
| 85 | + if (randomness.nextBoolean(config.fixedRateMutation)) { |
| 86 | + mutate(o1) |
| 87 | + } |
| 88 | + if (randomness.nextBoolean(config.fixedRateMutation)) { |
| 89 | + mutate(o2) |
| 90 | + } |
| 91 | + |
| 92 | + nextPop.add(o1) |
| 93 | + nextPop.add(o2) |
| 94 | + |
| 95 | + // Stop if global budget or target budget is up |
| 96 | + val usedForTarget = budget.usedForCurrentTarget(startActions, startSeconds) |
| 97 | + if (!time.shouldContinueSearch() || usedForTarget >= budget.budgetLeftForCurrentTarget) { |
| 98 | + endStep() |
| 99 | + break |
| 100 | + } |
| 101 | + endStep() |
| 102 | + } |
| 103 | + |
| 104 | + population.clear() |
| 105 | + population.addAll(nextPop) |
| 106 | + |
| 107 | + // Update budget usage for this target |
| 108 | + budget.updatePerTargetBudget(startActions, startSeconds) |
| 109 | + |
| 110 | + // Switch target if covered or out of budget |
| 111 | + val coveredNow = population.any { score(it) >= 1.0 } |
| 112 | + val switching = budget.shouldSwitchTarget(coveredNow) |
| 113 | + if (switching) currentTarget = null |
| 114 | + |
| 115 | + endGeneration() |
| 116 | + } |
| 117 | + |
| 118 | + fun lastUncoveredBranchTargetId(): Int? { |
| 119 | + val snapshot = archive.getSnapshotOfBestIndividuals() |
| 120 | + if (snapshot.isEmpty()) return null |
| 121 | + |
| 122 | + // Iterate targets by numeric id in descending order |
| 123 | + val orderedIds = snapshot.keys.sortedDescending() |
| 124 | + |
| 125 | + for (targetId in orderedIds) { |
| 126 | + val description = idMapper.getDescriptiveId(targetId) |
| 127 | + val isBranch = description.startsWith(ObjectiveNaming.BRANCH) |
| 128 | + val covered = archive.isCovered(targetId) |
| 129 | + if (isBranch) { |
| 130 | + if (!covered) { |
| 131 | + return targetId |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return null |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments