Ejemplo n.º 1
0
        public int[] Solution(Cities cities)
        {
            List <int> tour = new List <int>();

            for (int i = 0; i < cities.NumCities; i++)
            {
                tour.Add(i);
            }
            double     best_score = cities.GetTotalDistance(tour.ToArray());
            List <int> best_tour  = new List <int>(tour);
            decimal    maxItr     = _maxTour;

            foreach (var perm in Permutate(tour, tour.Count))
            {
                double s = cities.GetTotalDistance(tour.ToArray());
                if (s < best_score)
                {
                    best_score = s;
                    best_tour  = new List <int>(perm as List <int>);
                }
                if (--maxItr < 0)
                {
                    break;
                }
            }
            _totalDistance = best_score;
            return(best_tour.ToArray());
        }
Ejemplo n.º 2
0
        public int[] Solution(Cities cities)
        {
            int[][]    ants       = InitAnts(numAnts, cities.NumCities); // initialize ants to random trails
            double[][] dists      = cities.GetArrayDistances();
            int[]      bestTrail  = BestTrail(ants, dists);              // determine the best initial trail
            double     bestLength = Length(bestTrail, dists);            // the length of the best trail

            double[][] pheromones = InitPheromones(cities.NumCities);
            int        time       = 0;

            while (time < maxTime)
            {
                UpdateAnts(ants, pheromones, dists);
                UpdatePheromones(pheromones, ants, dists);

                int[]  currBestTrail  = BestTrail(ants, dists);
                double currBestLength = Length(currBestTrail, dists);
                if (currBestLength < bestLength)
                {
                    bestLength = currBestLength;
                    bestTrail  = currBestTrail;
                }
                ++time;
            }
            _totalDistance = cities.GetTotalDistance(bestTrail);
            return(bestTrail);
        }