1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GGGGino\WarehousePath\Breadcrumb; |
4
|
|
|
|
5
|
|
|
use GGGGino\WarehousePath\Entity\Place; |
6
|
|
|
|
7
|
|
|
class BreadthFirstBreadcrumb implements BreadcrumbInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Main algorithm to find the shortest path. |
11
|
|
|
* |
12
|
|
|
* @todo: nel futuro quando ci sarà un magazzino grande, spezzare il magazzino prendendo solo il quadrato contenente i vari punti |
13
|
|
|
* @todo: permettere la modifica al volo del peso delle locazioni |
14
|
|
|
* |
15
|
|
|
* @param Place $startPlace |
16
|
|
|
* @param Place $endPlace if not null do early exit |
17
|
|
|
*/ |
18
|
|
|
public function createBreadcrumb(Place $startPlace, Place $endPlace = null) |
19
|
|
|
{ |
20
|
|
|
$frontier = array(); |
21
|
|
|
array_push($frontier, $startPlace); |
22
|
|
|
|
23
|
|
|
while (!empty($frontier)) { |
24
|
|
|
/** @var Place $current */ |
25
|
|
|
$current = array_shift($frontier); |
26
|
|
|
|
27
|
|
|
/** @var Place $vicino */ |
28
|
|
|
foreach ($current->getWalkableNeighbors() as $vicino) { |
29
|
|
|
$tempCost = $current->getCurrentWeight() + $vicino->getOriginalWeight(); |
30
|
|
|
|
31
|
|
|
if ($vicino->isVisited() && $tempCost < $vicino->getCurrentWeight()) { |
32
|
|
|
$vicino->setCurrentWeight($tempCost); |
33
|
|
|
$vicino->setWalkingCameFrom($current); |
34
|
|
|
array_push($frontier, $vicino); |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($vicino->isVisited()) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$vicino->setVisited(true); |
43
|
|
|
|
44
|
|
|
$vicino->increaseCurrentWeight($current->getCurrentWeight()); |
45
|
|
|
$vicino->setWalkingCameFrom($current); |
46
|
|
|
|
47
|
|
|
array_push($frontier, $vicino); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$current->setVisited(true); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |