public function matching_shortest_routes()
    {
        return $this->shortest_routes;
    }
    // the shortest possible distance to travel
    public function shortest_distance()
    {
        return $this->shortest_distance;
    }
    // returns an array of all the possible routes
    public function routes()
    {
        return $this->all_routes;
    }
}
$tsp = new TSP();
$db = new PDO('mysql:host=localhost;dbname=binapp', 'root', '');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$tsp->add('newquay', 50.413608, -5.083364);
$tsp->add('london', 51.500152, -0.126236);
$tsp->add('birmingham', 52.483003, -1.893561);
$tsp->add('manchester', 53.480712, -2.234377);
$tsp->compute();
echo 'Shortest Distance: ' . $tsp->shortest_distance();
echo '<br />Shortest Route: ';
print_r($tsp->shortest_route());
echo '<br />Num Routes: ' . count($tsp->routes());
echo '<br />Matching shortest Routes: ';
print_r($tsp->matching_shortest_routes());
echo '<br />All Routes: ';
print_r($tsp->routes());
示例#2
0
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad / $R);
$minLat = $lat - rad2deg($rad / $R);
// compensate for degrees longitude getting smaller with increasing latitude
$maxLon = $lon + rad2deg($rad / $R / cos(deg2rad($lat)));
$minLon = $lon - rad2deg($rad / $R / cos(deg2rad($lat)));
$sql = "Select Id, Latitude,Longitutde, Fill,\n                acos(sin(:lat)*sin(radians(Latitude)) + cos(:lat)*cos(radians(Latitude))*cos(radians(Longitutde)-:lon)) * :R As D\n            From (\n                Select Id, Fill, Latitude, Longitutde\n                From bins\n                Where Latitude Between :minLat And :maxLat\n                  And Longitutde Between :minLon And :maxLon\n            ) As FirstCut\n            Where acos(sin(:lat)*sin(radians(Latitude)) + cos(:lat)*cos(radians(Latitude))*cos(radians(Longitutde)-:lon)) * :R < :rad\n            Order by D";
$params = array('lat' => deg2rad($lat), 'lon' => deg2rad($lon), 'minLat' => $minLat, 'minLon' => $minLon, 'maxLat' => $maxLat, 'maxLon' => $maxLon, 'rad' => $rad, 'R' => $R);
$points = $db->prepare($sql);
$points->execute($params);
//$myArray = array();
//$myArray["bins"] = array();
//$tsp->add("Source",$lat,$lon);    trying it for ssource
while ($row = $points->fetch(PDO::FETCH_ASSOC)) {
    //array_push($myArray["bins"], $row);
    $tsp->add($row["Id"], $row["Latitude"], $row["Longitutde"]);
    print $row["Id"] . "\t";
}
//$tsp->add('test1',50.413708,-3.083364);
//$tsp->add('london',51.500152,-0.126236);
//$tsp->add('birmingham',52.483003,-1.893561);
//$tsp->add('manchester',53.480712,-2.234377);
//$tsp->add('newquay',50.413608,-5.083364);
$tsp->compute();
//echo 'Shortest Distance: '.$tsp->shortest_distance();
//echo '<br />Shortest Route: ';
print_r($tsp->shortest_route());
//echo '<br />Num Routes: '.count($tsp->routes());
//echo '<br />Matching shortest Routes: ';
//print_r($tsp->matching_shortest_routes());
//echo '<br />All Routes: ';