Esempio n. 1
0
        static void Main(string[] args)
        {
            Random rand = new Random();

            //double result = RandomSearch.Run(300, rand);
            //Console.WriteLine("Random search: " + result);


            //double result2 = HillClimbing.Run(300, rand);
            //Console.WriteLine("Hill climbing: " + result2);

            //double result3 = TabooSearch.Run(300, rand, 2);
            //Console.WriteLine("Taboo search: " + result3);

            //int min = -1;
            //int max = 2;
            //uint precision = 4;
            //for(int i=0;i<100;i++)
            //{
            //    Solution s = new Solution(min, max, precision, rand);
            //    s.Initialize();
            //    Console.WriteLine(s.ToReal());
            //}

            //GA ga = new GA(30, rand);
            //ga.Run(1000,0.9,0.05);
            ProblemTSP.ReadProblem("TSP30.TXT");
            GA_TSP ga = new GA_TSP(30, rand);

            ga.Run(1000, 0.9, 0.05);

            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Random rand     = new Random();
            int    sampSize = 30;

            double[,] results = new double[5, sampSize];

/*
 *          for (int i = 0; i < sampSize; i++)
 *          {
 *              results[0,i] = RandomSearch.Run(1000, rand);
 *              results[1,i] = HillClimbing.Run(1000, rand);
 *              results[2,i] = TabooSearch.Run(1000, rand, 4);
 *              GA ga = new GA(20, 10, 0.05, rand);
 *              results[3,i] = ga.Run(50);
 *              SimulatedAnnealing sa = new SimulatedAnnealing(-1, 2, 6, rand);
 *              results[4,i] = sa.Run(1000, 20, rand);
 *          }
 *
 *          try
 *          {
 *
 *              FileStream fileStream = File.Open("meta_results.txt", FileMode.Create, FileAccess.Write);
 *              StreamWriter fileWriter = new StreamWriter(fileStream);
 *              fileWriter.WriteLine("randSearch;hillClimb;taboo;ga;sa;");
 *
 *              for (int i = 0; i < sampSize; i++)
 *              {
 *                  for (int j = 0; j < 5; j++)
 *                  {
 *                      fileWriter.Write(results[j,i]+";");
 *                  }
 *                  fileWriter.WriteLine("");
 *              }
 *
 *              fileWriter.Flush();
 *              fileWriter.Close();
 *          }
 *          catch (IOException ioe)
 *          {
 *              Console.WriteLine(ioe);
 *          }
 */

            //TSP

            Problem_TSP problem = new Problem_TSP();

            problem.ReadProblem("TSP30.txt");
            GA_TSP ga_tsp;


            for (int i = 0; i < sampSize; i++)
            {
                ga_tsp        = new GA_TSP(50, 0.1, 0.8, rand);
                results[0, i] = ga_tsp.Run(50);

                ga_tsp        = new GA_TSP(50, 0.3, 0.8, rand);
                results[1, i] = ga_tsp.Run(50);

                ga_tsp        = new GA_TSP(50, 0.1, 0.5, rand);
                results[2, i] = ga_tsp.Run(50);

                ga_tsp        = new GA_TSP(25, 0.1, 0.8, rand);
                results[3, i] = ga_tsp.Run(100);

                ga_tsp        = new GA_TSP(100, 0.1, 0.8, rand);
                results[4, i] = ga_tsp.Run(25);
            }

            try
            {
                FileStream   fileStream = File.Open("TSP_results.txt", FileMode.Create, FileAccess.Write);
                StreamWriter fileWriter = new StreamWriter(fileStream);
                fileWriter.WriteLine("basic;highMutProb;lowCrossProb;moreIter;bigPop;");

                for (int i = 0; i < sampSize; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        fileWriter.Write(results[j, i] + ";");
                    }
                    fileWriter.WriteLine("");
                }

                fileWriter.Flush();
                fileWriter.Close();
            }
            catch (IOException ioe)
            {
                Console.WriteLine(ioe);
            }
        }