Example #1
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);
        }
Example #2
0
        /// <summary>
        /// Load cities from the text file representing the adjacency matrix
        /// </summary>
        private void LoadCities(Cities cities)
        {
            double[][] arrayDistances = cities.GetArrayDistances();
            int        numCities      = cities.NumCities;

            distances = new double[numCities, numCities];
            for (int i = 0; i < numCities; i++)
            {
                for (int j = 0; j < numCities; j++)
                {
                    distances[i, j] = (double)arrayDistances[i][j];
                }

                //the number of rows in this matrix represent the number of cities
                //we are representing each city by an index from 0 to N - 1
                //where N is the total number of cities
                currentOrder.Add(i);
            }

            if (currentOrder.Count < 1)
            {
                throw new Exception("No cities to order.");
            }
        }