Ejemplo n.º 1
0
        private int CalculateSafeZone(ElitismMode eMode, int maxPopSize)
        {
            switch (eMode)
            {
            case ElitismMode.top10P:
                return((int)Math.Ceiling((double)maxPopSize * 0.1));

            case Program.ElitismMode.top1:
                return(1);

            default:
                return(0);
            }
        }
Ejemplo n.º 2
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;
            }
        }