-
Notifications
You must be signed in to change notification settings - Fork 0
/
SalesmanAnt.php
127 lines (105 loc) · 3.7 KB
/
SalesmanAnt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace App\Module\TSP\Application\Problem;
use App\Module\TSP\Domain\Constant;
use App\Shared\Domain\DistanceMap;
use Ramsey\Collection\Collection;
use Ramsey\Collection\CollectionInterface;
use Ramsey\Collection\Map\TypedMap;
use Random\Randomizer;
use UnexpectedValueException;
/**
* @see https://www.baeldung.com/java-ant-colony-optimization
*/
class SalesmanAnt implements AntInterface
{
/**
* @var CollectionInterface<string>
*/
private CollectionInterface $path;
private ?string $currentLocation;
public function __construct(
private readonly string $initialLocation,
private readonly DistanceMap $distanceMap,
private readonly PheromoneMatrix $pheromoneMatrix,
private readonly float $alpha = Constant::DEFAULT_ALPHA,
private readonly float $beta = Constant::DEFAULT_BETA,
private readonly int $distanceCoefficient = Constant::DEFAULT_DISTANCE_COEFFICIENT,
) {
$this->path = new Collection('string');
$this->currentLocation = null;
}
public function start(): void
{
$this->applyInitialLocation();
while ($this->path->count() < $this->distanceMap->count()) {
$nextLocation = $this->findNextLocation();
$this->currentLocation = $nextLocation;
$this->path->add($nextLocation);
}
$this->applyInitialLocation();
}
/**
* @return CollectionInterface<string>
*/
public function getPath(): CollectionInterface
{
return clone $this->path;
}
public function getLength(): float
{
$length = 0.0;
for ($i = 0; $i < $this->path->count() - 1; ++$i) {
$length += $this->distanceMap->getDistance($this->path[$i], $this->path[$i + 1])->getValue();
}
return $length;
}
private function findNextLocation(): string
{
$probabilities = new TypedMap('string', 'float');
$total = 0.0;
foreach ($this->getUnvisitedLocations() as $locationId) {
$pheromone = $this->pheromoneMatrix->getPheromone($this->currentLocation, $locationId);
$invertedDistance = 1 / ($this->distanceMap->getDistance($this->currentLocation, $locationId)->getValue()
/ $this->distanceCoefficient);
$probability = pow($pheromone, $this->alpha) * pow($invertedDistance, $this->beta);
$probabilities->put($locationId, $probability);
$total += $probability;
}
if (!$probabilities->isEmpty()) {
$randomValue = (new Randomizer())->getFloat(0, $total);
$currentValue = 0.0;
foreach ($probabilities as $locationId => $probability) {
$currentValue += $probability;
/* @phpstan-ignore-next-line */
if ($currentValue >= $randomValue) {
return $locationId;
}
}
}
return $this->getFirstUnvisitedLocation();
}
private function applyInitialLocation(): void
{
$this->currentLocation = $this->initialLocation;
$this->path->add($this->initialLocation);
}
private function getFirstUnvisitedLocation(): string
{
foreach ($this->distanceMap->keys() as $locationId) {
if (!$this->path->contains($locationId)) {
return $locationId;
}
}
throw new UnexpectedValueException('No matching location found!');
}
/**
* @return string[]
*/
private function getUnvisitedLocations(): array
{
return array_values(array_filter(
$this->distanceMap->keys(),
fn (string $locationId) => !$this->path->contains($locationId)
));
}
}