Exemplo n.º 1
0
        private static void TestGA()
        {
            TSP tsp = VRPLoader.readTSP(vrps[0]);
            List <(double, double, double)> stats;
            List <int> bestSolution;

            (bestSolution, stats) = GA.Run(tsp, 100, 100, 0.7, 0.1, 5);
            Console.WriteLine("Solution: " + ListToString(bestSolution));
            Console.WriteLine("Distance: " + tsp.Evaluate(bestSolution).ToString());
        }
Exemplo n.º 2
0
        private static List <double> EvaluateAll(TSP evaluation, List <List <int> > population)
        {
            List <double> scores = new List <double>();

            foreach (var individual in population)
            {
                scores.Add(evaluation.Evaluate(individual));
            }
            return(scores);
        }
Exemplo n.º 3
0
        private static void LogStats()
        {
            string s = "problem;generation;best;worst;average\n";

            foreach (string filename in vrps)
            {
                TSP evaluation = VRPLoader.readTSP(filename);
                List <(double, double, double)> stats;
                (_, stats) = GA.Run(evaluation, 100, 100, 0.6, 0.3, 10);
                for (int i = 0; i < stats.Count; i++)
                {
                    s += filename + ";" + i.ToString() + ";" + stats[i].Item1.ToString() + ";" + stats[i].Item2.ToString() + ";" + stats[i].Item3.ToString() + "\n";
                }
            }
            File.WriteAllText("stats-test.csv", s);
        }
Exemplo n.º 4
0
        public static TSP readTSP(string filename)
        {
            var lines = File.ReadAllLines(filename);
            int size  = 0;
            List <(int, int, int)> coords = new List <(int, int, int)>();
            bool readingCoords            = false;

            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                if (readingCoords)
                {
                    try
                    {
                        coords.Add((int.Parse(words[1]), int.Parse(words[2]), int.Parse(words[3])));
                    }
                    catch (FormatException)
                    {
                        readingCoords = false;
                    }
                    catch (IndexOutOfRangeException)
                    {
                        readingCoords = false;
                    }
                }
                else if (words[0] == "DIMENSION")
                {
                    size = int.Parse(words[2]);
                }
                else if (words[0] == "NODE_COORD_SECTION")
                {
                    readingCoords = true;
                }
            }
            if (size == 0)
            {
                throw new FormatException();
            }
            return(TSP.FromEuclid2D(size, coords));
        }
Exemplo n.º 5
0
        private static List <double> TestPopSize(TSP evaluation, int repeats)
        {
            List <(double, double, double)> stats;
            List <int>    bestSolution;
            List <int>    range   = new List <int>();
            List <double> results = new List <double>();

            for (int i = 0; i < 10; i++)
            {
                range.Add(100 * (i + 1));
            }
            foreach (int prob in range)
            {
                List <double> scores = new List <double>();
                for (int i = 0; i < repeats; i++)
                {
                    (bestSolution, stats) = GA.Run(evaluation, prob, 100, 0.7, 0.1, 5);
                    scores.Add(stats[stats.Count - 1].Item1);
                }
                results.Add(Median(scores));
            }
            File.WriteAllText(".\\popsize-test.csv", ResultsToString(range, results));
            return(results);
        }
Exemplo n.º 6
0
 public static List <int> GetRandomSolution(TSP evaluation)
 {
     return(Enumerable.Range(0, evaluation.size).ToList().OrderBy(x => rng.Next()).ToList());
 }