1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JMGQ\AStar\Benchmark; |
4
|
|
|
|
5
|
|
|
use JMGQ\AStar\AStar; |
6
|
|
|
use JMGQ\AStar\Benchmark\Result\Result; |
7
|
|
|
use JMGQ\AStar\Example\Terrain\DomainLogic; |
8
|
|
|
use JMGQ\AStar\Example\Terrain\Position; |
9
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
10
|
|
|
|
11
|
|
|
class BenchmarkRunner |
12
|
|
|
{ |
13
|
|
|
private ProgressBarInterface $progressBar; |
14
|
|
|
private TerrainGenerator $terrainGenerator; |
15
|
|
|
private Stopwatch $stopwatch; |
16
|
|
|
|
17
|
2 |
|
public function __construct(ProgressBarInterface $progressBar) |
18
|
|
|
{ |
19
|
2 |
|
$this->progressBar = $progressBar; |
20
|
2 |
|
$this->terrainGenerator = new TerrainGenerator(); |
21
|
2 |
|
$this->stopwatch = new Stopwatch(); |
22
|
2 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param int[] $sizes |
26
|
|
|
* @param int $iterations |
27
|
|
|
* @param int | null $seed |
28
|
|
|
* @return Result[] |
29
|
|
|
*/ |
30
|
2 |
|
public function run(array $sizes, int $iterations, ?int $seed): array |
31
|
|
|
{ |
32
|
2 |
|
$results = []; |
33
|
|
|
|
34
|
2 |
|
$steps = count($sizes) * $iterations; |
35
|
2 |
|
$this->progressBar->start($steps); |
36
|
|
|
|
37
|
2 |
|
foreach ($sizes as $size) { |
38
|
2 |
|
for ($i = 0; $i < $iterations; $i++) { |
39
|
2 |
|
$terrain = $this->terrainGenerator->generate($size, $size, $seed); |
40
|
2 |
|
$domainLogic = new DomainLogic($terrain); |
41
|
2 |
|
$aStar = new AStar($domainLogic); |
42
|
|
|
|
43
|
2 |
|
$start = new Position(0, 0); |
44
|
2 |
|
$goal = new Position($size - 1, $size - 1); |
45
|
|
|
|
46
|
2 |
|
$this->stopwatch->start('benchmark'); |
47
|
|
|
|
48
|
2 |
|
$solution = $aStar->run($start, $goal); |
49
|
|
|
|
50
|
2 |
|
$event = $this->stopwatch->stop('benchmark'); |
51
|
|
|
|
52
|
2 |
|
$solutionFound = !empty($solution); |
53
|
|
|
|
54
|
2 |
|
$results[] = new Result($size, (int) $event->getDuration(), $solutionFound); |
55
|
|
|
|
56
|
2 |
|
$this->stopwatch->reset(); |
57
|
|
|
|
58
|
2 |
|
$this->progressBar->advance(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$this->progressBar->finish(); |
63
|
|
|
|
64
|
2 |
|
return $results; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|