예제 #1
0
        private void OutputAvg(GeneticTS geneticTS, List <List <double> > bestMaster, List <List <double> > avgMaster, string experimentName)
        {
            if (!File.Exists("output.csv"))
            {
                File.Create("output.csv").Close();
            }
            StreamWriter data = new StreamWriter("output.csv", true);

            string[] expInfo = geneticTS.GetExperimentInfo();
            foreach (var line in expInfo)
            {
                data.WriteLine(line);
            }
            data.WriteLine("");
            data.WriteLine(experimentName);
            data.WriteLine("Best Fitness " + "," + "Average Fitness");
            //Depth
            for (int i = 0; i < bestMaster.Max(x => x.Count) - 1; i++)
            {
                //Breadth
                double avgBest = 0;
                double avgAvg  = 0;
                for (int k = 0; k < bestMaster.Count; k++)
                {
                    avgBest += (i >= bestMaster[k].Count) ? bestMaster[k].Last() : bestMaster[k][i];
                    avgAvg  += (i >= avgMaster[k].Count) ? avgMaster[k].Last() : avgMaster[k][i];
                }
                data.WriteLine(avgBest / bestMaster.Count + "," + avgAvg / avgMaster.Count);
            }
            data.WriteLine("");
            data.Close();
            data.Dispose();
        }
예제 #2
0
        private void Output1(GeneticTS geneticTS, List <double> bestMaster, List <double> avgMaster, string experimentName)
        {
            if (!File.Exists("output.csv"))
            {
                File.Create("output.csv").Close();
            }
            StreamWriter data = new StreamWriter("output.csv", true);

            string[] expInfo = geneticTS.GetExperimentInfo();
            foreach (var line in expInfo)
            {
                data.WriteLine(line);
            }
            data.WriteLine("");
            data.WriteLine(experimentName);
            data.WriteLine("Best Fitness " + "," + "Average Fitness");
            for (int i = 0; i < bestMaster.Count; i++)
            {
                data.WriteLine(bestMaster[i] + ", " + avgMaster[i]);
            }
            data.WriteLine("");
            data.Close();
            data.Dispose();
        }
예제 #3
0
        public Program()
        {
            //Setup
            if (File.Exists("output.csv"))
            {
                File.Delete("output.csv");
            }
            List <double[, ]> datasets = new List <double[, ]> {
                ulyssess22TSP, eil51TSP
            };
            string s = "";

            do
            {
                Console.Clear();
                Console.WriteLine("1. Output all permutations. 2. Output custom run");
                s = Console.ReadLine();
            }while (s != "1" && s != "2");
            switch (s)
            {
            case "1":
                double[,] set = eil51TSP;
                int           mPopSize        = 1000;
                int           mGenerationSpan = 1000;
                int           tSize           = 5;
                int           round           = 5;
                int           startCity       = 1;
                bool          useConvergence  = true;
                ElitismMode   elitMode        = ElitismMode.top10P;
                CrossoverType crossoverType   = CrossoverType.PMX;
                double        crossoverRate   = 1.0;
                double        mutateRate      = 0.1;
                string        seed            = "SampleRandomSeed";
                City[]        cit             = new City[set.GetLength(0)];
                for (int i = 0; i < set.GetLength(0); i++)
                {
                    cit[i] = new City(set[i, 1], set[i, 2]);
                }
                GeneticTS g1 = new GeneticTS(
                    elitMode,
                    crossoverType,
                    crossoverRate,
                    mutateRate,
                    mPopSize,
                    mGenerationSpan,
                    seed.GetHashCode(),
                    tSize,
                    startCity,
                    cit,
                    useConvergence,
                    round,
                    CalculateSafeZone(elitMode, mPopSize)
                    );
                Output1(g1, g1.bestFitnesses, g1.avgFitnesses, "CustomRun");
                break;

            case "2":
                List <double> crossovers = new List <double> {
                    1, .9
                };
                List <double> mutations = new List <double> {
                    0, .1, 1
                };

                int  maxPopSize        = 1000;
                int  maxGenerationSpan = 1000;
                int  tournamentSize    = 5;
                int  roundDigits       = 5;
                int  startCityIndex    = 1;
                bool allowConvergence  = true;
                foreach (double[,] dataset in datasets)
                {
                    foreach (CrossoverType crossType in Enum.GetValues(typeof(CrossoverType)))
                    {
                        foreach (double crossRate in crossovers)
                        {
                            foreach (double mutRate in mutations)
                            {
                                foreach (ElitismMode eMode in Enum.GetValues(typeof(ElitismMode)))
                                {
                                    GeneticTS             geneticTS = null;
                                    List <List <double> > setBest   = new List <List <double> >();
                                    List <List <double> > setAvg    = new List <List <double> >();
                                    string experimentName           = string.Format("SET: {0} Elietism: {1} Crossover Type: {2} Crossover Rate: {3} Mutation Rate: {4}",
                                                                                    dataset.GetLength(0), eMode, crossType, crossRate, mutRate);

                                    Console.WriteLine("Starting " + experimentName);
                                    for (int b = 0; b < randomSeeds.Length; b++)
                                    {
                                        City[] cities = new City[dataset.GetLength(0)];
                                        for (int i = 0; i < dataset.GetLength(0); i++)
                                        {
                                            cities[i] = new City(dataset[i, 1], dataset[i, 2]);
                                        }
                                        //Call GeneticTS
                                        geneticTS = new GeneticTS(
                                            eMode,
                                            crossType,
                                            crossRate,
                                            mutRate,
                                            maxPopSize,
                                            maxGenerationSpan,
                                            randomSeeds[b].GetHashCode(),
                                            tournamentSize,
                                            startCityIndex,
                                            cities,
                                            allowConvergence,
                                            roundDigits,
                                            CalculateSafeZone(eMode, maxPopSize)
                                            );

                                        setBest.Add(geneticTS.bestFitnesses);
                                        setAvg.Add(geneticTS.avgFitnesses);
                                    }
                                    OutputAvg(geneticTS, setBest, setAvg, experimentName);
                                }
                            }
                        }
                    }
                }
                break;
            }
        }
예제 #4
0
 public Crossover(GeneticTS geneticTS)
 {
     this.geneticTS = geneticTS;
 }