@@ -24,13 +24,13 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | public function computeRoute() |
| 26 | 26 | { |
| 27 | - if(! $this->graph instanceof NodeGraph) |
|
| 27 | + if (!$this->graph instanceof NodeGraph) |
|
| 28 | 28 | { |
| 29 | 29 | throw new \RuntimeException('Invalid Graph'); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | $vertexes = $this->graph->getVertexes(); |
| 33 | - if(count($vertexes) <= 2) |
|
| 33 | + if (count($vertexes) <= 2) |
|
| 34 | 34 | { |
| 35 | 35 | return new NodePath($vertexes); |
| 36 | 36 | } |
@@ -40,15 +40,15 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | $pathMap = new NodeMap(); |
| 42 | 42 | $pathMap->insert($vertexFrom, $vertexTo); |
| 43 | - while(! empty($vertexes)) |
|
| 43 | + while (!empty($vertexes)) |
|
| 44 | 44 | { |
| 45 | 45 | $minimalVertexTo = null; |
| 46 | 46 | $minimalEdge = INF; |
| 47 | 47 | $edges = $this->graph->getEdgesFrom($vertexFrom); |
| 48 | - foreach($edges as $vertexToId => $edge) |
|
| 48 | + foreach ($edges as $vertexToId => $edge) |
|
| 49 | 49 | { |
| 50 | 50 | $vertexTo = $this->graph->getVertex($vertexToId); |
| 51 | - if(! $pathMap->exists($vertexTo) && $edge < $minimalEdge) |
|
| 51 | + if (!$pathMap->exists($vertexTo) && $edge < $minimalEdge) |
|
| 52 | 52 | { |
| 53 | 53 | $minimalVertexTo = $vertexTo; |
| 54 | 54 | $minimalEdge = $edge; |
@@ -30,15 +30,15 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | public function computeRoute() |
| 32 | 32 | { |
| 33 | - if(! $this->graph instanceof NodeGraph) |
|
| 33 | + if (!$this->graph instanceof NodeGraph) |
|
| 34 | 34 | { |
| 35 | 35 | throw new \RuntimeException('Invalid Graph'); |
| 36 | 36 | } |
| 37 | - if(! $this->existingRoute instanceof NodePath) |
|
| 37 | + if (!$this->existingRoute instanceof NodePath) |
|
| 38 | 38 | { |
| 39 | 39 | throw new \RuntimeException('Invalid ExistingRoute'); |
| 40 | 40 | } |
| 41 | - if(count($this->existingRoute) < 2) |
|
| 41 | + if (count($this->existingRoute) < 2) |
|
| 42 | 42 | { |
| 43 | 43 | return $this->existingRoute; |
| 44 | 44 | } |
@@ -48,17 +48,17 @@ discard block |
||
| 48 | 48 | do |
| 49 | 49 | { |
| 50 | 50 | $improvementMade = false; |
| 51 | - foreach($this->existingRoute->getKeys() as $iKey) |
|
| 51 | + foreach ($this->existingRoute->getKeys() as $iKey) |
|
| 52 | 52 | { |
| 53 | - foreach($this->existingRoute->getKeys() as $kKey) |
|
| 53 | + foreach ($this->existingRoute->getKeys() as $kKey) |
|
| 54 | 54 | { |
| 55 | - if($iKey == $kKey) |
|
| 55 | + if ($iKey == $kKey) |
|
| 56 | 56 | { |
| 57 | 57 | continue; |
| 58 | 58 | } |
| 59 | 59 | $newRoute = $this->twoOptSwap($iKey, $kKey); |
| 60 | 60 | $newDistance = $this->graph->computeLength($newRoute); |
| 61 | - if($newDistance < $existingDistance) |
|
| 61 | + if ($newDistance < $existingDistance) |
|
| 62 | 62 | { |
| 63 | 63 | $this->existingRoute = $newRoute; |
| 64 | 64 | $existingDistance = $newDistance; |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | - } while($improvementMade); |
|
| 71 | + } while ($improvementMade); |
|
| 72 | 72 | |
| 73 | 73 | return $this->existingRoute; |
| 74 | 74 | } |
@@ -76,13 +76,13 @@ discard block |
||
| 76 | 76 | private function twoOptSwap($iKey, $kKey) |
| 77 | 77 | { |
| 78 | 78 | $newRouteNodes = array(); |
| 79 | - foreach($this->existingRoute->getKeys() as $key) |
|
| 79 | + foreach ($this->existingRoute->getKeys() as $key) |
|
| 80 | 80 | { |
| 81 | 81 | if ($key === $iKey) |
| 82 | 82 | { |
| 83 | 83 | $newRouteNodes[] = $this->existingRoute->getKey($kKey); |
| 84 | 84 | } |
| 85 | - elseif($key === $kKey) |
|
| 85 | + elseif ($key === $kKey) |
|
| 86 | 86 | { |
| 87 | 87 | $newRouteNodes[] = $this->existingRoute->getKey($iKey); |
| 88 | 88 | } |
@@ -30,15 +30,15 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | public function computeRoute() |
| 32 | 32 | { |
| 33 | - if(! $this->graph instanceof NodeGraph) |
|
| 33 | + if (!$this->graph instanceof NodeGraph) |
|
| 34 | 34 | { |
| 35 | 35 | throw new \RuntimeException('Invalid Graph'); |
| 36 | 36 | } |
| 37 | - if(! $this->existingRoute instanceof NodePath) |
|
| 37 | + if (!$this->existingRoute instanceof NodePath) |
|
| 38 | 38 | { |
| 39 | 39 | throw new \RuntimeException('Invalid ExistingRoute'); |
| 40 | 40 | } |
| 41 | - if(count($this->existingRoute) < 3) |
|
| 41 | + if (count($this->existingRoute) < 3) |
|
| 42 | 42 | { |
| 43 | 43 | return $this->existingRoute; |
| 44 | 44 | } |
@@ -48,19 +48,19 @@ discard block |
||
| 48 | 48 | do |
| 49 | 49 | { |
| 50 | 50 | $improvementMade = false; |
| 51 | - foreach($this->existingRoute->getKeys() as $iKey) |
|
| 51 | + foreach ($this->existingRoute->getKeys() as $iKey) |
|
| 52 | 52 | { |
| 53 | - foreach($this->existingRoute->getKeys() as $jKey) |
|
| 53 | + foreach ($this->existingRoute->getKeys() as $jKey) |
|
| 54 | 54 | { |
| 55 | - foreach($this->existingRoute->getKeys() as $kKey) |
|
| 55 | + foreach ($this->existingRoute->getKeys() as $kKey) |
|
| 56 | 56 | { |
| 57 | - if($iKey == $jKey || $iKey == $kKey || $jKey == $kKey) |
|
| 57 | + if ($iKey == $jKey || $iKey == $kKey || $jKey == $kKey) |
|
| 58 | 58 | { |
| 59 | 59 | continue; |
| 60 | 60 | } |
| 61 | 61 | $newRoute = $this->threeOptSwap($iKey, $jKey, $kKey); |
| 62 | 62 | $newDistance = $this->graph->computeLength($newRoute); |
| 63 | - if($newDistance < $existingDistance) |
|
| 63 | + if ($newDistance < $existingDistance) |
|
| 64 | 64 | { |
| 65 | 65 | $this->existingRoute = $newRoute; |
| 66 | 66 | $existingDistance = $newDistance; |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | } |
| 74 | - } while($improvementMade); |
|
| 74 | + } while ($improvementMade); |
|
| 75 | 75 | |
| 76 | 76 | return $this->existingRoute; |
| 77 | 77 | } |
@@ -79,17 +79,17 @@ discard block |
||
| 79 | 79 | private function threeOptSwap($iKey, $jKey, $kKey) |
| 80 | 80 | { |
| 81 | 81 | $newRouteNodes = array(); |
| 82 | - foreach($this->existingRoute->getKeys() as $key) |
|
| 82 | + foreach ($this->existingRoute->getKeys() as $key) |
|
| 83 | 83 | { |
| 84 | 84 | if ($key === $iKey) |
| 85 | 85 | { |
| 86 | 86 | $newRouteNodes[] = $this->existingRoute->getKey($kKey); |
| 87 | 87 | } |
| 88 | - elseif($key === $jKey) |
|
| 88 | + elseif ($key === $jKey) |
|
| 89 | 89 | { |
| 90 | 90 | $newRouteNodes[] = $this->existingRoute->getKey($iKey); |
| 91 | 91 | } |
| 92 | - elseif($key === $kKey) |
|
| 92 | + elseif ($key === $kKey) |
|
| 93 | 93 | { |
| 94 | 94 | $newRouteNodes[] = $this->existingRoute->getKey($jKey); |
| 95 | 95 | } |
@@ -20,9 +20,9 @@ discard block |
||
| 20 | 20 | { |
| 21 | 21 | $matrix = $this->convertToMatrix($syntax); |
| 22 | 22 | |
| 23 | - foreach($matrix as $x => $line) |
|
| 23 | + foreach ($matrix as $x => $line) |
|
| 24 | 24 | { |
| 25 | - foreach($line as $y => $char) |
|
| 25 | + foreach ($line as $y => $char) |
|
| 26 | 26 | { |
| 27 | 27 | $matrix[$x][$y] = ($char !== self::WALL) ? 1 : 0; |
| 28 | 28 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | { |
| 36 | 36 | $matrix = array_filter(explode("\n", $syntax)); |
| 37 | 37 | |
| 38 | - foreach($matrix as $key => $line) |
|
| 38 | + foreach ($matrix as $key => $line) |
|
| 39 | 39 | { |
| 40 | 40 | $matrix[$key] = array_filter(str_split($line)); |
| 41 | 41 | } |
@@ -48,9 +48,9 @@ discard block |
||
| 48 | 48 | $syntax = ''; |
| 49 | 49 | |
| 50 | 50 | $nodes = $grid->getNodes(); |
| 51 | - foreach($nodes as $line) |
|
| 51 | + foreach ($nodes as $line) |
|
| 52 | 52 | { |
| 53 | - foreach($line as $node) |
|
| 53 | + foreach ($line as $node) |
|
| 54 | 54 | { |
| 55 | 55 | $syntax .= ($node->isWalkable() ? self::FREE : self::WALL); |
| 56 | 56 | } |
@@ -65,23 +65,23 @@ discard block |
||
| 65 | 65 | $syntax = ''; |
| 66 | 66 | |
| 67 | 67 | $nodes = $grid->getNodes(); |
| 68 | - foreach($nodes as $line) |
|
| 68 | + foreach ($nodes as $line) |
|
| 69 | 69 | { |
| 70 | - foreach($line as $node) |
|
| 70 | + foreach ($line as $node) |
|
| 71 | 71 | { |
| 72 | - if(! $node->isWalkable()) |
|
| 72 | + if (!$node->isWalkable()) |
|
| 73 | 73 | { |
| 74 | 74 | $syntax .= self::WALL; |
| 75 | 75 | } |
| 76 | - elseif($node->toString() == $path->getStartNode()->toString()) |
|
| 76 | + elseif ($node->toString() == $path->getStartNode()->toString()) |
|
| 77 | 77 | { |
| 78 | 78 | $syntax .= self::IN; |
| 79 | 79 | } |
| 80 | - elseif($node->toString() == $path->getEndNode()->toString()) |
|
| 80 | + elseif ($node->toString() == $path->getEndNode()->toString()) |
|
| 81 | 81 | { |
| 82 | 82 | $syntax .= self::OUT; |
| 83 | 83 | } |
| 84 | - elseif($path->contains($node)) |
|
| 84 | + elseif ($path->contains($node)) |
|
| 85 | 85 | { |
| 86 | 86 | $syntax .= self::STEP; |
| 87 | 87 | } |
@@ -99,12 +99,12 @@ discard block |
||
| 99 | 99 | public function findAndCreateNode($syntax, $charToFind) |
| 100 | 100 | { |
| 101 | 101 | $xCount = count($syntax); |
| 102 | - for($x = 0; $x < $xCount; $x++) |
|
| 102 | + for ($x = 0; $x < $xCount; $x++) |
|
| 103 | 103 | { |
| 104 | 104 | $yCount = count($syntax[$x]); |
| 105 | - for($y = 0; $y < $yCount; $y++) |
|
| 105 | + for ($y = 0; $y < $yCount; $y++) |
|
| 106 | 106 | { |
| 107 | - if($syntax[$x][$y] === $charToFind) |
|
| 107 | + if ($syntax[$x][$y] === $charToFind) |
|
| 108 | 108 | { |
| 109 | 109 | return new Node($x, $y); |
| 110 | 110 | } |
@@ -116,12 +116,12 @@ discard block |
||
| 116 | 116 | { |
| 117 | 117 | $nodes = array(); |
| 118 | 118 | $xCount = count($syntax); |
| 119 | - for($x = 0; $x < $xCount; $x++) |
|
| 119 | + for ($x = 0; $x < $xCount; $x++) |
|
| 120 | 120 | { |
| 121 | 121 | $yCount = count($syntax[$x]); |
| 122 | - for($y = 0; $y < $yCount; $y++) |
|
| 122 | + for ($y = 0; $y < $yCount; $y++) |
|
| 123 | 123 | { |
| 124 | - if($syntax[$x][$y] === $charToFind) |
|
| 124 | + if ($syntax[$x][$y] === $charToFind) |
|
| 125 | 125 | { |
| 126 | 126 | $nodes[] = new Node($x, $y); |
| 127 | 127 | } |
@@ -135,32 +135,32 @@ discard block |
||
| 135 | 135 | { |
| 136 | 136 | $deltas = array( |
| 137 | 137 | array(-1, -1), array(-1, +0), array(-1, +1), |
| 138 | - array(+0, -1), array(+0, +1), |
|
| 138 | + array(+0, -1), array(+0, +1), |
|
| 139 | 139 | array(+1, -1), array(+1, +0), array(+1, +1), |
| 140 | 140 | ); |
| 141 | 141 | |
| 142 | 142 | $node = $this->findAndCreateNode($matrix, self::IN); |
| 143 | 143 | $target = $this->findAndCreateNode($matrix, self::OUT); |
| 144 | 144 | $path = array($node); |
| 145 | - while($node->getId() !== $target->getId()) |
|
| 145 | + while ($node->getId() !== $target->getId()) |
|
| 146 | 146 | { |
| 147 | 147 | $newNode = null; |
| 148 | - foreach($deltas as $delta) |
|
| 148 | + foreach ($deltas as $delta) |
|
| 149 | 149 | { |
| 150 | 150 | $x = $node->getX() + $delta[0]; |
| 151 | 151 | $y = $node->getY() + $delta[1]; |
| 152 | 152 | |
| 153 | - if(! array_key_exists($x, $matrix)) |
|
| 153 | + if (!array_key_exists($x, $matrix)) |
|
| 154 | 154 | { |
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - if(! array_key_exists($y, $matrix[$x])) |
|
| 158 | + if (!array_key_exists($y, $matrix[$x])) |
|
| 159 | 159 | { |
| 160 | 160 | continue; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - if($matrix[$x][$y] === self::STEP || $matrix[$x][$y] === self::OUT) |
|
| 163 | + if ($matrix[$x][$y] === self::STEP || $matrix[$x][$y] === self::OUT) |
|
| 164 | 164 | { |
| 165 | 165 | $matrix[$x][$y] = self::FREE; |
| 166 | 166 | $newNode = new Node($x, $y); |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | } |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - if(! $newNode instanceof Node) |
|
| 171 | + if (!$newNode instanceof Node) |
|
| 172 | 172 | { |
| 173 | 173 | throw new \RuntimeException('Path is not continous in the grid'); |
| 174 | 174 | } |