Esempio n. 1
0
        /// <summary>
        /// Choose the next node for an ant to visit.  This is based on probability.
        /// </summary>
        /// <param name="currentIndex">The step we are at in the path.</param>
        /// <param name="ant">The ant being evaluated.</param>
        /// <returns>The node we will move into.</returns>
        private int PickNextNode(int currentIndex, DiscreteAnt ant)
        {
            if (currentIndex == 0 || Random.NextDouble() < PR)
            {
                int index;
                do
                {
                    index = Random.NextInt(0, _graph.Count);
                } while (ant.WasVisited(index));
                return(index);
            }

            double[] prob = CalculateProbability(currentIndex, ant);

            double r   = Random.NextDouble();
            double sum = 0;

            for (int i = 0; i < _graph.Count; i++)
            {
                sum += prob[i];
                if (sum >= r)
                {
                    return(i);
                }
            }
            // should not happen!
            return(-1);
        }
Esempio n. 2
0
        /// <summary>
        /// Calculate the probability of a given ant moving to any of the next nodes.
        /// </summary>
        /// <param name="currentIndex">The index into the path.</param>
        /// <param name="ant">The ant.</param>
        /// <returns>The probability of moving to the next node.</returns>
        private double[] CalculateProbability(int currentIndex, DiscreteAnt ant)
        {
            double[] result = new double[_graph.Count];
            int      i      = ant.Path[currentIndex - 1];

            double d = 0.0;

            for (int l = 0; l < _graph.Count; l++)
            {
                if (!ant.WasVisited(l))
                {
                    d += Math.Pow(_pheromone[i][l], Alpha)
                         * Math.Pow(1.0 / _graph.Cost(i, l), Beta);
                }
            }


            for (int j = 0; j < _graph.Count; j++)
            {
                if (ant.WasVisited(j))
                {
                    result[j] = 0.0;
                }
                else
                {
                    double n = Math.Pow(_pheromone[i][j], Alpha)
                               * Math.Pow(1.0 / _graph.Cost(i, j), Beta);
                    result[j] = n / d;
                }
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Choose the next node for an ant to visit.  This is based on probability. 
        /// </summary>
        /// <param name="currentIndex">The step we are at in the path.</param>
        /// <param name="ant">The ant being evaluated.</param>
        /// <returns>The node we will move into.</returns>
        private int PickNextNode(int currentIndex, DiscreteAnt ant)
        {
            if (currentIndex == 0 || Random.NextDouble() < PR)
            {
                int index;
                do
                {
                    index = Random.NextInt(0, _graph.Count);
                } while (ant.WasVisited(index));
                return index;
            }

            double[] prob = CalculateProbability(currentIndex, ant);

            double r = Random.NextDouble();
            double sum = 0;
            for (int i = 0; i < _graph.Count; i++)
            {
                sum += prob[i];
                if (sum >= r)
                    return i;
            }
            // should not happen!
            return -1;
        }
Esempio n. 4
0
        /// <summary>
        /// Calculate the probability of a given ant moving to any of the next nodes. 
        /// </summary>
        /// <param name="currentIndex">The index into the path.</param>
        /// <param name="ant">The ant.</param>
        /// <returns>The probability of moving to the next node.</returns>
        private double[] CalculateProbability(int currentIndex, DiscreteAnt ant)
        {
            double[] result = new double[_graph.Count];
            int i = ant.Path[currentIndex - 1];

            double d = 0.0;
            for (int l = 0; l < _graph.Count; l++)
                if (!ant.WasVisited(l))
                    d += Math.Pow(_pheromone[i][l], Alpha)
                            * Math.Pow(1.0 / _graph.Cost(i, l), Beta);


            for (int j = 0; j < _graph.Count; j++)
            {
                if (ant.WasVisited(j))
                {
                    result[j] = 0.0;
                }
                else
                {
                    double n = Math.Pow(_pheromone[i][j], Alpha)
                            * Math.Pow(1.0 / _graph.Cost(i, j), Beta);
                    result[j] = n / d;
                }
            }
            return result;

        }