Informed (Heuristic) Search

Simulated Annealing Search

Simulated Annealing (SA) is an optimization algorithm inspired by the annealing process in metallurgy, where materials are heated and slowly cooled to reach a stable state with minimal energy. It is used to find approximate solutions to optimization problems by iteratively exploring potential solutions and occasionally accepting worse solutions to escape local optima.
The goal in this example here is to find the minimum point of $f(x) = x²$, which is clearly at $x = 0$, where the function value is also $0$.

 
<?php

use app\classes\search\SimulatedAnnealing;

// Example of usage
$resultDebug ??= false;
$coolingRate ??= 0.99;
$stopTemperature ??= 0.1;

$initialSolution 9;
$sa = new SimulatedAnnealing(1000$coolingRate$stopTemperature);
$optimalSolution $sa->optimize($initialSolution5);

$debugResult $resultDebug $sa->printIterationLog(detailedtrue) : '--';

echo 
'Optimal Solution: ' $optimalSolution "\n";
Result Format:

Result: Memory: 0.393 Mb Time running: 0.002 sec.
Start temprature: 1000°
Stop temprature: 0.1°
Cooling Rate: 0.99
Optimal Solution: -0.064
Debug:
--