Skip to content

Commit 2afadb9

Browse files
committed
Fix setting upper limit for TSP bruteforce via MST algorithm
1 parent 9a0f93b commit 2afadb9

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

src/TravelingSalesmanProblem/Base.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace Graphp\Algorithms\TravelingSalesmanProblem;
44

5-
use Fhaculty\Graph\Walk;
6-
use Fhaculty\Graph\Vertex;
7-
use Fhaculty\Graph\Edge\Base as Edge;
5+
use Fhaculty\Graph\Graph;
86
use Fhaculty\Graph\Set\Edges;
7+
use Fhaculty\Graph\Vertex;
8+
use Fhaculty\Graph\Walk;
99
use Graphp\Algorithms\Base as AlgorithmBase;
1010

1111
abstract class Base extends AlgorithmBase
1212
{
1313
/**
1414
* get resulting graph with the (first) best circle of edges connecting all vertices
1515
*
16-
* @throws Exception on error
16+
* @throws \Exception on error
1717
* @return Graph
18-
* @uses Tsp::getGraph()
19-
* @uses AlgorithmTsp::getEdges()
18+
* @uses self::getGraph()
19+
* @uses self::getEdges()
2020
* @uses Graph::createGraphCloneEdges()
2121
*/
2222
public function createGraph()
@@ -42,8 +42,8 @@ abstract protected function getVertexStart();
4242
* get (first) best circle connecting all vertices
4343
*
4444
* @return Walk
45-
* @uses AlgorithmTsp::getEdges()
46-
* @uses AlgorithmTsp::getVertexStart()
45+
* @uses self::getEdges()
46+
* @uses self::getVertexStart()
4747
* @uses Walk::factoryCycleFromEdges()
4848
*/
4949
public function getCycle()

src/TravelingSalesmanProblem/Bruteforce.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Graphp\Algorithms\TravelingSalesmanProblem;
44

5+
use Fhaculty\Graph\Edge\Base as Edge;
56
use Fhaculty\Graph\Exception\UnexpectedValueException;
6-
77
use Fhaculty\Graph\Exception\UnderflowException;
8-
98
use Fhaculty\Graph\Graph;
10-
use Fhaculty\Graph\Vertex;
119
use Fhaculty\Graph\Set\Edges;
10+
use Fhaculty\Graph\Vertex;
1211
use Graphp\Algorithms\TravelingSalesmanProblem\MinimumSpanningTree as AlgorithmTspMst;
1312

1413
class Bruteforce extends Base
@@ -44,7 +43,7 @@ class Bruteforce extends Base
4443
* upper limit to use for branch-and-bound (BNB)
4544
*
4645
* @var float|NULL
47-
* @see AlgorithmTspBruteforce::setUpperLimit()
46+
* @see self::setUpperLimit()
4847
*/
4948
private $upperLimit = NULL;
5049

@@ -72,8 +71,8 @@ public function __construct(Graph $graph)
7271
* this method can be used to optimize the algorithm by providing an upper
7372
* bound of when to stop branching any further.
7473
*
75-
* @param double $limit
76-
* @return AlgorithmTspBruteforce $this (chainable)
74+
* @param double $limit
75+
* @return self $this (chainable)
7776
*/
7877
public function setUpperLimit($limit)
7978
{
@@ -82,12 +81,18 @@ public function setUpperLimit($limit)
8281
return $this;
8382
}
8483

84+
/**
85+
* automatically sets upper limit to use for branch-and-bound from the MST heuristic
86+
*
87+
* @return self $this (chainable)
88+
* @uses AlgorithmTspMst
89+
*/
8590
public function setUpperLimitMst()
8691
{
8792
$alg = new AlgorithmTspMst($this->graph);
88-
$limit = $alg->createGraph()->getWeight();
93+
$this->upperLimit = $alg->getWeight();
8994

90-
return $this->setUpperLimit($limit);
95+
return $this;
9196
}
9297

9398
protected function getVertexStart()
@@ -104,7 +109,7 @@ protected function getGraph()
104109
/**
105110
* get resulting (first) best circle of edges connecting all vertices
106111
*
107-
* @throws Exception on error
112+
* @throws \Exception on error
108113
* @return Edges
109114
*/
110115
public function getEdges()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Graphp\Algorithms\TravelingSalesmanProblem\Bruteforce;
5+
6+
class BruteforceTest extends TestCase
7+
{
8+
public function testGetWeightReturnsExpectedWeightForSimpleCycle()
9+
{
10+
$graph = new Graph();
11+
$a = $graph->createVertex();
12+
$b = $graph->createVertex();
13+
$c = $graph->createVertex();
14+
$a->createEdgeTo($b)->setWeight(1);
15+
$b->createEdgeTo($c)->setWeight(2);
16+
$c->createEdgeTo($a)->setWeight(3);
17+
18+
$alg = new Bruteforce($graph);
19+
20+
$this->assertEquals(6, $alg->getWeight());
21+
}
22+
23+
public function testSetUpperLimitMstSetsExactLimitForSimpleCycle()
24+
{
25+
$graph = new Graph();
26+
$a = $graph->createVertex();
27+
$b = $graph->createVertex();
28+
$c = $graph->createVertex();
29+
$a->createEdgeTo($b)->setWeight(1);
30+
$b->createEdgeTo($c)->setWeight(2);
31+
$c->createEdgeTo($a)->setWeight(3);
32+
33+
$alg = new Bruteforce($graph);
34+
$alg->setUpperLimitMst();
35+
36+
$ref = new ReflectionProperty($alg, 'upperLimit');
37+
$ref->setAccessible(true);
38+
39+
$this->assertEquals(6, $ref->getValue($alg));
40+
}
41+
}

0 commit comments

Comments
 (0)