Skip to content

Conversation

@francastagna
Copy link
Collaborator

@francastagna francastagna commented Nov 4, 2025

PR Summary

We implemented the Chemical Reaction Optimization (CRO) algorithm and added comprehensive tests.


Pseudocode:

image

Source: https://www.researchgate.net/publication/327166354_An_Empirical_Evaluation_of_Evolutionary_Algorithms_for_Unit_Test_Suite_Generation


Main Changes

  • New Algorithm (CroAlgorithm):

    • Implements CRO over whole-test-suite individuals (WtsEvalIndividual), modeling each suite as a molecule with potential energy (−fitness) and kinetic energy.
    • Maintains a global energy container (buffer) and enforces strict energy conservation at each step; compensates for external fitness drifts by adjusting the container.
    • Selects reaction path per step:
      • Uni-molecular path: either on-wall ineffective collision or decomposition (threshold on numCollisions).
      • Inter-molecular path (probability croMolecularCollisionRate): either synthesis (both molecules eligible by KE threshold) or inter-molecular ineffective collision.
    • Uses existing GA operators for variation: mutate and xover are reused; potential is defined as -calculateCombinedFitness().
    • Initializes molecules from GA population with croInitialKineticEnergy; tracks collisions per molecule to gate decomposition.
  • Reactor (CroReactor):

    • Encapsulates reactions and energy accounting with EnergyContext(container); provides:
      • onWallIneffectiveCollision: mutate copy; accept if energy surplus ≥ 0; split surplus between KE and container using retained-fraction ∈ [croKineticEnergyLossRate, 1].
      • decomposition: create two mutated offspring; if deficit, attempt borrow from container (random fractional product of container); split net KE between offspring.
      • intermolecularIneffectiveCollision: mutate both; accept if net surplus ≥ 0; split KE between the two.
      • synthesis: crossover two offspring, pick the fitter fused; accept if net surplus ≥ 0; assign KE=surplus, reset collisions; otherwise increment parents’ collisions.
    • Helper: computeEnergySurplus(totalBeforePotential, totalBeforeKinetic, totalAfterPotential).
  • Molecule model (CroMolecule):

    • New data class holding suite, kineticEnergy, numCollisions.
  • Config (EMConfig):

    • Added CRO to EMConfig.Algorithm.
    • Added CRO parameters (defaults in parentheses):
      • croMolecularCollisionRate (0.2): probability of binary reactions (0–1).
      • croKineticEnergyLossRate (0.2): lower bound of KE retained on on-wall (0–1).
      • croInitialKineticEnergy (1000.0): initial KE per molecule (≥ 0).
      • croDecompositionThreshold (500): min collisions before allowing decomposition (≥ 0).
      • croSynthesisThreshold (10.0): synthesis allowed if KE ≤ threshold (≥ 0).
  • Integration (Main.kt):

    • Wires EMConfig.Algorithm.CRO to CroAlgorithm<RestIndividual> in algorithm selection.
  • Docs (docs/options.md):

    • Added CRO options with descriptions and constraints in the options table.

Tests

  • Algorithm-level tests added (CroAlgorithmTest.kt):

    • Finds optimum on OneMax (testCroAlgorithmFindsOptimum).
    • Uni-molecular path:
      • Decomposition accepted → parent replaced by two offspring, collisions reset, KE ≥ 0.
      • Decomposition rejected (cannot borrow) → no replacement; parent collisions increment.
      • On-wall accepted → single molecule updated; collisions increment; KE/container updated.
      • On-wall rejected (negative net) → no changes.
    • Inter-molecular path:
      • Synthesis accepted → size decreases to 1; fused KE set; collisions reset.
      • Synthesis rejected (negative net) → size unchanged; both parents’ collisions increment.
      • Ineffective collision accepted → both replaced; collisions increment; KE split and ≥ 0.
      • Ineffective collision rejected → no changes.
  • Reactor unit tests added (CroReactorTest.kt):

    • computeEnergySurplus returns positive/zero/negative as expected.
    • Decomposition: borrow fails when container is zero (reject + parent collisions++); borrow succeeds with large container (accept + container decreases + KE distributed).
    • On-wall: negative net rejects with no changes; positive net updates KE/container and conserves energy.
    • Inter-molecular ineffective collision: negative net rejects; positive net splits KE and increments collisions, conserving energy.
    • Synthesis: negative net rejects and increments parent collisions; positive net returns fused offspring with KE=net and collisions reset.

@francastagna francastagna marked this pull request as ready for review November 7, 2025 13:54
@francastagna francastagna requested a review from arcuri82 November 7, 2025 13:54
@francastagna francastagna changed the title Chemical Reaction Optimization Algorithm CRO Algorithm Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants