cityPool = $pool; if ($this->cityPool->count() > self::LIMIT) { throw new \Exception('Overflow'); } } /** * Calculate best routes. * * @param int $limit Amount of best routes * * @return Population * * @since 1.0.0 */ 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; } /** * Bruteforce best solutions. * * @param array $cities Cities * @param Tour $tour Current (potential) optimal tour * @param Population $population Population of tours * * @return Population * * @since 1.0.0 */ private function bruteForce(array $cities, Tour $tour, Population $population) { if (empty($cities)) { $population->addTour($tour); } $count = count($cities); for ($i = 0; $i < $count; $i++) { $extended = clone $tour; $extended->addCity($cities[$i]); unset($cities[$i]); if ($population->getUnfittest()->getFitness() > $extended->getFitness()) { continue; } $this->bruteForce($cities, $extended, $population); } } }