Exemplo n.º 1
0
        public static List <Wynik> testAIO(int counter, Item[] item, double back_volume, String fileName)
        {
            List <Wynik>      wyniki     = new List <Wynik>();
            List <ICrossover> crossovers = new List <ICrossover>();

            crossovers.Add(new CycleCrossover());
            crossovers.Add(new CutAndSpliceCrossover());
            crossovers.Add(new OnePointCrossover());
            crossovers.Add(new OrderBasedCrossover());
            crossovers.Add(new OrderedCrossover());
            crossovers.Add(new PartiallyMappedCrossover());
            crossovers.Add(new PositionBasedCrossover());
            crossovers.Add(new ThreeParentCrossover());
            crossovers.Add(new TwoPointCrossover());
            crossovers.Add(new UniformCrossover());

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("Wartość spakowanych itemków\tWaga spakowanych itemków\tCzas pakowania\tCzy poprawne\tTyp crossovera\tRozmiar populacji\tSzansa na crossover\tSzansa na mutację\tRozmiar tournamentu\r\n");
            foreach (ICrossover crossover in crossovers)
            {
                for (int iPop = 5; iPop < 500; iPop += 5)
                {
                    for (double iCrossChance = 0.05f; iCrossChance < 1.0f; iCrossChance += 0.05f)
                    {
                        for (double iMutChance = 0.05f; iMutChance < 1.0f; iMutChance += 0.01f)
                        {
                            for (int iTurSize = 2; iTurSize < 50; iTurSize += 1)
                            {
                                for (int rp = 0; rp < repeatCount; rp++)
                                {
                                    if (showCurrent)
                                    {
                                        if (Experiments.doOutput)
                                        {
                                            System.Console.WriteLine("testDifferentCrossovers  " + rp);
                                        }
                                    }
                                    Boolean isOK = true;
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("Typ krosołwera: " + crossover.GetType().Name);
                                    }

                                    wynikGA w = new wynikGA
                                    {
                                        ga = null,
                                        ch = null
                                    };

                                    try
                                    {
                                        w = GeneticsMethods.GeneticExt(counter - 1, item, back_volume, iPop, crossover, iCrossChance, iMutChance, iTurSize);
                                    }
                                    catch (System.ArgumentException e)
                                    {
                                        if (Experiments.doOutput)
                                        {
                                            System.Console.WriteLine("BUMMMMMM");
                                        }
                                        //if (Experiments.doOutput) System.Console.WriteLine("BUMMMMMM " + crossover.GetType().Name + " " + e.ToString());
                                        continue;
                                    }
                                    catch (System.Exception e)
                                    {
                                        if (Experiments.doOutput)
                                        {
                                            System.Console.WriteLine("BUMMMMMM");
                                        }
                                        //if (Experiments.doOutput) System.Console.WriteLine("BUMMMMMM " + crossover.GetType().Name + " " + e.ToString());
                                        continue;
                                    }

                                    Wynik wynik = new Wynik();


                                    double[] wn = w.ch.ToFloatingPoints();
                                    double   pack_items_volume = 0;
                                    double   pack_items_value  = 0;
                                    for (int j = 0; j < wn.Length; j++)
                                    {
                                        pack_items_volume += wn[j] * item[j].item_volume;
                                        pack_items_value  += wn[j] * item[j].item_value;
                                        if (item[j].item_volume < 0)
                                        {
                                            isOK = false;
                                        }
                                    }
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("Objetosc spakowanych rzeczy: " + (pack_items_volume.ToString("F")));
                                    }
                                    wynik.items_volume = pack_items_volume;
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("Czas: " + w.ga.TimeEvolving.TotalSeconds);
                                    }
                                    wynik.time = w.ga.TimeEvolving.TotalSeconds;
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("Wartosc: " + pack_items_value.ToString("F"));
                                    }
                                    wynik.items_value = pack_items_value;
                                    if (pack_items_volume > back_volume)
                                    {
                                        wynik.czypoprawny = false; if (Experiments.doOutput)
                                        {
                                            System.Console.WriteLine("niepoprawny");
                                        }
                                    }
                                    else
                                    {
                                        wynik.czypoprawny = true; if (Experiments.doOutput)
                                        {
                                            System.Console.WriteLine("poprawny");
                                        }
                                    }
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("");
                                    }
                                    if (Experiments.doOutput)
                                    {
                                        System.Console.WriteLine("");
                                    }
                                    stringBuilder.Append(System.Math.Round(pack_items_value, 4) + "\t" + System.Math.Round(pack_items_volume, 4) + "\t" + System.Math.Round(w.ga.TimeEvolving.TotalSeconds, 4) + "\t" + (isOK ? "1" : "0") + "\t" + crossover.GetType().Name + "\t" + iPop + "\t" + iCrossChance + "\t" + iMutChance + "\t" + iTurSize + "\r\n");

                                    wyniki.Add(wynik);
                                }
                            }
                        }
                    }
                }
            }
            DataTransfer.saveFileWithResults(fileName, stringBuilder);
            return(wyniki);
        }
Exemplo n.º 2
0
        public static List <Wynik> testCommon(int counter, Item[] item, double back_volume, String fileName)
        {
            List <Wynik> wyniki = new List <Wynik>();

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("Wartość spakowanych itemków\tWaga spakowanych itemków\tCzas pakowania\tCzy poprawne\r\n");
            for (int rp = 0; rp < repeatCount * 8; rp++)
            {
                Wynik wynik = new Wynik();
                if (showCurrent)
                {
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("testCommon  " + rp);
                    }
                }
                Boolean isOK = true;
                wynikGA w    = GeneticsMethods.Genetic(counter - 1, item, back_volume);



                double[] wn = w.ch.ToFloatingPoints();
                double   pack_items_volume = 0;
                double   pack_items_value  = 0;
                for (int j = 0; j < wn.Length; j++)
                {
                    pack_items_volume += wn[j] * item[j].item_volume;
                    pack_items_value  += wn[j] * item[j].item_value;
                    if (item[j].item_volume < 0)
                    {
                        isOK = false;
                    }
                }



                //  if (Experiments.doOutput) System.Console.WriteLine("Objetosc plecaka: " + (back_volume.ToString("F")));
                if (Experiments.doOutput)
                {
                    System.Console.WriteLine("Objetosc spakowanych rzeczy: " + (pack_items_volume.ToString("F")));
                }
                wynik.items_volume = pack_items_volume;
                if (Experiments.doOutput)
                {
                    System.Console.WriteLine("Czas: " + w.ga.TimeEvolving.TotalSeconds);
                }
                wynik.time = w.ga.TimeEvolving.TotalSeconds;

                /*
                 * for (int j = 0; j < wn.Length; j++) System.Console.Write(item[j].item_volume + "\t");
                 * if (Experiments.doOutput) System.Console.WriteLine("");
                 * for (int j = 0; j < wn.Length; j++) System.Console.Write(wn[j] + "\t");
                 * if (Experiments.doOutput) System.Console.WriteLine("");
                 * for (int j = 0; j < wn.Length; j++) System.Console.Write(MathF.Round((float)(item[j].item_volume * wn[j]), 2) + "\t");
                 */
                // if (Experiments.doOutput) System.Console.WriteLine("");
                // if (Experiments.doOutput) System.Console.WriteLine("CrossoverProbability: " + w.ga.CrossoverProbability.ToString("F"));
                //  if (Experiments.doOutput) System.Console.WriteLine("GenerationsNumber: " + w.ga.GenerationsNumber.ToString("F"));
                // if (Experiments.doOutput) System.Console.WriteLine("MutationProbability: " + w.ga.MutationProbability.ToString("F"));
                if (Experiments.doOutput)
                {
                    System.Console.WriteLine("Wartosc: " + pack_items_value.ToString("F"));
                }
                wynik.items_value = pack_items_value;
                if (pack_items_volume > back_volume)
                {
                    wynik.czypoprawny = false; if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("niepoprawny");
                    }
                }
                else
                {
                    wynik.czypoprawny = true; if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("poprawny");
                    }
                }
                wyniki.Add(wynik);
                if (Experiments.doOutput)
                {
                    System.Console.WriteLine("");
                }
                if (Experiments.doOutput)
                {
                    System.Console.WriteLine("");
                }
                stringBuilder.Append(System.Math.Round(pack_items_value, 4) + "\t" + System.Math.Round(pack_items_volume, 4) + "\t" + System.Math.Round(w.ga.TimeEvolving.TotalSeconds, 4) + "\t" + (isOK ? "1" : "0") + "\r\n");
            }
            DataTransfer.saveFileWithResults(fileName, stringBuilder);
            return(wyniki);
        }
Exemplo n.º 3
0
        public static List <Wynik> testTournamentSize(int counter, Item[] item, double back_volume, String fileName)
        {
            List <Wynik> wyniki = new List <Wynik>();

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("Wartość spakowanych itemków\tWaga spakowanych itemków\tCzas pakowania\tCzy poprawne\tRozmiar tournamentu\r\n");
            for (int iTurSize = 2; iTurSize < 50; iTurSize += 1)
            {
                for (int rp = 0; rp < repeatCount; rp++)
                {
                    Wynik wynik = new Wynik();
                    if (showCurrent)
                    {
                        if (Experiments.doOutput)
                        {
                            System.Console.WriteLine("testTournamentSize  " + iTurSize + "  " + rp);
                        }
                    }
                    Boolean isOK = true;
                    wynikGA w    = GeneticsMethods.GeneticExt(counter - 1, item, back_volume, tournamentSize: iTurSize);



                    double[] wn = w.ch.ToFloatingPoints();
                    double   pack_items_volume = 0;
                    double   pack_items_value  = 0;
                    for (int j = 0; j < wn.Length; j++)
                    {
                        pack_items_volume += wn[j] * item[j].item_volume;
                        pack_items_value  += wn[j] * item[j].item_value;
                        if (item[j].item_volume < 0)
                        {
                            isOK = false;
                        }
                    }
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("Rozmiar tournamentu: " + iTurSize);
                    }
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("Objetosc spakowanych rzeczy: " + (pack_items_volume.ToString("F")));
                    }
                    wynik.items_volume = pack_items_volume;
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("Czas: " + w.ga.TimeEvolving.TotalSeconds);
                    }
                    wynik.time = w.ga.TimeEvolving.TotalSeconds;
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("Wartosc: " + pack_items_value.ToString("F"));
                    }
                    wynik.items_value = pack_items_value;
                    if (pack_items_volume > back_volume)
                    {
                        wynik.czypoprawny = false; if (Experiments.doOutput)
                        {
                            System.Console.WriteLine("niepoprawny");
                        }
                    }
                    else
                    {
                        wynik.czypoprawny = true; if (Experiments.doOutput)
                        {
                            System.Console.WriteLine("poprawny");
                        }
                    }
                    wyniki.Add(wynik);
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("");
                    }
                    if (Experiments.doOutput)
                    {
                        System.Console.WriteLine("");
                    }
                    if (pack_items_volume > back_volume)
                    {
                        isOK = false;
                    }
                    stringBuilder.Append(System.Math.Round(pack_items_value, 4) + "\t" + System.Math.Round(pack_items_volume, 4) + "\t" + System.Math.Round(w.ga.TimeEvolving.TotalSeconds, 4) + "\t" + (isOK ? "1" : "0") + "\t" + iTurSize + "\r\n");
                }
            }
            DataTransfer.saveFileWithResults(fileName, stringBuilder);
            return(wyniki);
        }
Exemplo n.º 4
0
        public static wynikGA Genetic(int counter, Item[] dane, double back_volume)
        {
            double[] minValue       = new double[counter];
            double[] maxValue       = new double[counter];
            int[]    totalBits      = new int[counter];
            int[]    fractionDigits = new int[counter];


            for (int i = 0; i < counter; i++)
            {
                minValue[i]       = 0;
                maxValue[i]       = 1;
                totalBits[i]      = 16;
                fractionDigits[i] = 4;
            }

            var selection  = new TournamentSelection(50, true);
            var crossover  = new TwoPointCrossover();
            var mutation   = new FlipBitMutation();
            var chromosome = new FloatingPointChromosome(minValue, maxValue, totalBits, fractionDigits);
            // var fitobj = new MyProblemFitness(dane, chromosome, back_volume);
            //var fitness = fitobj.Evaluate;
            var population = new Population(500, 500, chromosome);

            var fitness = new FuncFitness((c) =>
            {
                var fc = c as FloatingPointChromosome;
                //var _items = dane;
                //var _bvol = back_volume;

                /* var values = fc.ToFloatingPoints();
                 * var x1 = values[0];
                 * var y1 = values[1];
                 * var x2 = values[2];
                 * var y2 = values[3];
                 * return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
                 *
                 */

                //chromosome.ReplaceGenes(0, chromosome.GetGenes());
                double[] proc_wag          = fc.ToFloatingPoints();
                double suma_spakowanych    = 0;
                double wartosc_spakowanych = 0;
                for (int i = 0; i < proc_wag.Length; i++)
                {
                    suma_spakowanych    += dane[i].item_volume * proc_wag[i];
                    wartosc_spakowanych += dane[i].item_value * proc_wag[i];
                }

                if (suma_spakowanych <= back_volume)
                {
                    double temp = (wartosc_spakowanych * (suma_spakowanych / back_volume));
                    if (temp < 0)
                    {
                        if (Experiments.doOutput)
                        {
                            System.Console.WriteLine("LOL ");
                        }
                    }
                }


                return((suma_spakowanych <= back_volume) ? (wartosc_spakowanych * (suma_spakowanych / back_volume)) : (-suma_spakowanych));
            });



            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

            ga.Termination = new GenerationNumberTermination(100);

            if (Experiments.doOutput)
            {
                System.Console.WriteLine("GA running...");
            }
            ga.MutationProbability  = 0.05f;
            ga.CrossoverProbability = 0.20f;
            //ga.Selection.
            ga.Start();

            // FITNESS  if (Experiments.doOutput) System.Console.WriteLine("Best solution found has fitness = " + ga.BestChromosome.Fitness);
            wynikGA w = new wynikGA();

            w.ga = ga;
            w.ch = chromosome;
            w.ch.ReplaceGenes(0, ga.BestChromosome.GetGenes());

            return(w);
        }