예제 #1
0
        static private void AC(TSP tsp, int T, int NumberOfAnts, double alpha, double beta)
        {
            int            t = 0;
            List <int>     ListPositionAnts = Init(tsp.NrOrase);
            List <Furnica> Ants             = new List <Furnica>(InitAnts(ListPositionAnts, NumberOfAnts));

            double[,] Pheromone = new double[tsp.NrOrase, tsp.NrOrase];
            double[,] miu       = new double[tsp.NrOrase, tsp.NrOrase];
            Furnica BestAnt = new Furnica
            {
                Tur = new List <int>()
            };

            Console.Write("p: ");
            double p = Convert.ToDouble(Console.ReadLine());

            Console.Write("Q: ");
            int Q = Convert.ToInt32(Console.ReadLine());

            InitPheromone(Pheromone, tsp.NrOrase, tsp.NrOrase);
            InitMiu(miu, tsp);

            while (t < T)
            {
                for (int pas = 1; pas < tsp.NrOrase; pas++)
                {
                    for (int k = 0; k < NumberOfAnts; k++)
                    {
                        if (t == 0)
                        {
                            Ants[k].Tur.Add(StatetransitionRule(tsp, Ants[k], Pheromone, miu, alpha, beta, pas));
                        }
                        else
                        {
                            Ants[k].Tur[pas] = StatetransitionRule(tsp, Ants[k], Pheromone, miu, alpha, beta, pas);
                        }
                    }
                }
                UpdatePheromone(Pheromone, Ants, tsp, p, Q);
                BestAnt.Tur.Clear();
                BestAnt.Tur.AddRange(DetBestAnt(Ants, tsp));
                t++;
            }
            WriteAnts(Ants);
            WriteEvalAnts(Ants, tsp);
            WritePheromone(Pheromone);
            WriteSol(BestAnt.Tur);
            WriteEvalSol(BestAnt, tsp);
            Console.ReadKey();
        }
예제 #2
0
        static private List <Furnica> InitAnts(List <int> listPosition, int NumberOfAnts)
        {
            List <Furnica> Ants = new List <Furnica>();

            for (int position = 0; position < NumberOfAnts; position++)
            {
                Furnica Ant = new Furnica
                {
                    Tur = new List <int>()
                };
                Ant.Tur.Add(listPosition[position]);
                Ants.Add(Ant);
            }

            return(Ants);
        }
예제 #3
0
        static private int StatetransitionRule(TSP tsp, Furnica Ant, double[,] Pheromone, double[,] miu, double alpha, double beta, int pas)
        {
            int    BestCity = 0;
            double rnd      = new Random().NextDouble();

            double[] prob = new double[tsp.NrOrase];
            InitProb(prob, Pheromone, miu, Ant, alpha, beta, pas);

            for (int i = 0; i < prob.Length; i++)
            {
                double partialSum = InitPartialSum(prob, 0, i);
                if (rnd <= partialSum)
                {
                    BestCity = i;
                    break;
                }
            }

            return(BestCity);
        }
예제 #4
0
 static private void WriteEvalSol(Furnica ant, TSP tsp)
 {
     Console.WriteLine("Fitness = {0}", ant.Eval(tsp));
 }
예제 #5
0
        static private void InitProb(double[] prob, double[,] Pheromone, double[,] miu, Furnica Ant, double alpha, double beta, int pas)
        {
            int    index = Ant.Tur[pas - 1];
            double sum   = 0;

            for (int i = 0; i < prob.Length; i++)
            {
                if (Ant.Tur.FindIndex(0, pas, x => x == i) == -1)
                {
                    sum = sum + (Math.Pow(Pheromone[index, i], alpha) * Math.Pow(miu[index, i], beta));
                }
            }

            for (int i = 0; i < prob.Length; i++)
            {
                if (i != index && Ant.Tur.FindIndex(0, pas, x => x == i) == -1)
                {
                    prob[i] = (Math.Pow(Pheromone[index, i], alpha) * Math.Pow(miu[index, i], beta)) / sum;
                }
                else
                {
                    prob[i] = 0;
                }
            }
        }