mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace phpOMS\Math\Optimization\TSP;
|
|
|
|
class BruteForce
|
|
{
|
|
const LIMIT = 22;
|
|
private $cityPool = null;
|
|
|
|
|
|
public function __construct(CityPool $pool)
|
|
{
|
|
$this->cityPool = $pool;
|
|
|
|
if ($this->cityPool->count() > self::LIMIT) {
|
|
throw new \Exception('64 bit overflow');
|
|
}
|
|
}
|
|
|
|
public function getSolution(\int $limit = 1) : Population
|
|
{
|
|
$population = new Population($this->cityPool, $limit, true);
|
|
$cities = $this->cityPool->getCities();
|
|
|
|
$this->bruteForce($cities, new Tour($this->cityPool, false), $population);
|
|
|
|
return $population;
|
|
}
|
|
|
|
private function bruteForce(array $cities, Tour $tour, Population $population)
|
|
{
|
|
if (count($cities) === 0) {
|
|
$population->addTour($tour);
|
|
}
|
|
|
|
for ($i = 0; $i < count($cities); $i++) {
|
|
$extended = clone $tour;
|
|
$extended->addCity($cities[$i]);
|
|
unset($cities[$i]);
|
|
|
|
if ($population->getUnfittest()->getFitness() > $extended->getFitness()) {
|
|
continue;
|
|
}
|
|
|
|
$this->bruteForce($cities, $extended, $population);
|
|
}
|
|
}
|
|
}
|