|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Algorithms\TravelingSalesman; |
|
4
|
|
|
|
|
5
|
|
|
use Letournel\PathFinder\AlgorithmTravelingSalesman; |
|
6
|
|
|
use Letournel\PathFinder\Core\Node; |
|
7
|
|
|
use Letournel\PathFinder\Core\NodeGraph; |
|
8
|
|
|
use Letournel\PathFinder\Core\NodePath; |
|
9
|
|
|
|
|
10
|
|
|
class TwoOpt implements AlgorithmTravelingSalesman |
|
11
|
|
|
{ |
|
12
|
|
|
/* |
|
13
|
|
|
* For more info see |
|
14
|
|
|
* http://en.wikipedia.org/wiki/2-opt |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
private |
|
18
|
|
|
$existingRoute, |
|
|
|
|
|
|
19
|
|
|
$graph; |
|
20
|
|
|
|
|
21
|
|
|
public function setExistingRoute(NodePath $existingRoute) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->existingRoute = $existingRoute; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setGraph(NodeGraph $graph) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->graph = $graph; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function computeRoute() |
|
32
|
|
|
{ |
|
33
|
|
|
if(! $this->graph instanceof NodeGraph) |
|
34
|
|
|
{ |
|
35
|
|
|
throw new \RuntimeException('Invalid Graph'); |
|
36
|
|
|
} |
|
37
|
|
|
if(! $this->existingRoute instanceof NodePath) |
|
38
|
|
|
{ |
|
39
|
|
|
throw new \RuntimeException('Invalid ExistingRoute'); |
|
40
|
|
|
} |
|
41
|
|
|
if(count($this->existingRoute) < 2) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->existingRoute; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$existingDistance = $this->graph->computeLength($this->existingRoute); |
|
47
|
|
|
|
|
48
|
|
|
do |
|
49
|
|
|
{ |
|
50
|
|
|
$improvementMade = false; |
|
51
|
|
|
foreach($this->existingRoute->getKeys() as $iKey) |
|
52
|
|
|
{ |
|
53
|
|
|
foreach($this->existingRoute->getKeys() as $kKey) |
|
54
|
|
|
{ |
|
55
|
|
|
if($iKey == $kKey) |
|
56
|
|
|
{ |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
$newRoute = $this->twoOptSwap($iKey, $kKey); |
|
60
|
|
|
$newDistance = $this->graph->computeLength($newRoute); |
|
61
|
|
View Code Duplication |
if($newDistance < $existingDistance) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$this->existingRoute = $newRoute; |
|
64
|
|
|
$existingDistance = $newDistance; |
|
65
|
|
|
$improvementMade = true; |
|
66
|
|
|
|
|
67
|
|
|
break 2; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} while($improvementMade); |
|
72
|
|
|
|
|
73
|
|
|
return $this->existingRoute; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function twoOptSwap($iKey, $kKey) |
|
77
|
|
|
{ |
|
78
|
|
|
$newRouteNodes = array(); |
|
79
|
|
|
foreach($this->existingRoute->getKeys() as $key) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($key === $iKey) |
|
82
|
|
|
{ |
|
83
|
|
|
$newRouteNodes[] = $this->existingRoute->getKey($kKey); |
|
84
|
|
|
} |
|
85
|
|
|
elseif($key === $kKey) |
|
86
|
|
|
{ |
|
87
|
|
|
$newRouteNodes[] = $this->existingRoute->getKey($iKey); |
|
88
|
|
|
} |
|
89
|
|
|
else |
|
90
|
|
|
{ |
|
91
|
|
|
$newRouteNodes[] = $this->existingRoute->getKey($key); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new NodePath($newRouteNodes); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.