Beispiel #1
0
        static void Main(string[] args)
        {
            TSPFileLoader fileLoader = new TSPFileLoader();

            fileLoader.LoadAndListTSPFiles();
            TSPProblem problem = fileLoader.SelectAndLoadFile();

            //problem.PrintDistanceMatrix();

            Console.WriteLine("\nRandom Algorithm");
            Algorithm randomAlgorithm = new RandomAlgorithm(problem);

            randomAlgorithm.PerformAlgorithm();
            printAlgorithmResults(randomAlgorithm);

            Console.WriteLine("\nGreedy Algorithm");
            Algorithm greedyAlgorithm = new GreedyAlgorithm(problem);

            greedyAlgorithm.PerformAlgorithm();
            printAlgorithmResults(greedyAlgorithm);

            GeneticAlgorithmParameters parameters = readParameters();

            Algorithm geneticAlgorithm = new GeneticAlgorithm(problem)
            {
                Parameters = parameters,
                Selection  = new TournamentSelection(),
                Crossover  = new OrderedCrossover(),
                Mutation   = new InversionMutation()
            };

            geneticAlgorithm.PerformAlgorithm();
            printAlgorithmResults(geneticAlgorithm);
        }
        private IList <Individual> generateGreedyPopulation(int populationSize)
        {
            IList <Individual> individuals     = new List <Individual>(populationSize);
            GreedyAlgorithm    greedyAlgorithm = new GreedyAlgorithm(problem);

            IList <Individual> greedyIndividuals = greedyAlgorithm.PerformAlgorithm();

            individuals = individuals.Concat(greedyIndividuals.OrderBy(x => x.Fitness).Take(populationSize / 2)).ToList();

            for (int i = greedyIndividuals.Count; i < populationSize; i++)
            {
                individuals.Add(problem.GenerateRandomIndividual());
            }

            return(individuals);
        }