コード例 #1
0
        void ValidationTests(String cardsJson, String combosFile, int k)
        {
            HSCardsParser      parser  = new HSCardsParser(cardsJson);
            HoningStoneBuilder builder = new HoningStoneBuilder();

            Dictionary <String, CardObject> cardTable = new Dictionary <string, CardObject>();
            HSCardOperator op = new HSCardOperator();

            HSCardsParser                 fullParser = new HSCardsParser("allCardsWithAbility.json", 0);
            Dictionary <String, int>      dataID;
            Dictionary <String, double[]> cardDatasetFull = op.generateCardVectors(fullParser, out dataID);

            // Populate all card table
            foreach (string key in parser.objects.Keys)
            {
                List <CardObject> cards_objects = parser.objects[key];
                builder.GenCardTableObject(ref cards_objects, ref cardTable);
            }

            HSCombosParser combosParser = new HSCombosParser();

            //combosParser.PopulateFromHoningNetwork(ref decksParser, ref cardTable, 5);
            combosParser.PopulateFromJson(combosFile);

            Random rand = new Random();

            List <CardObject> neutral = parser.objects["Neutral"];

            foreach (String hero in parser.objects.Keys)
            {
                // To write results
                System.IO.StreamWriter file = new System.IO.StreamWriter(hero + "_results.dat");
                if (hero != "Mage")
                {
                    continue;
                }

                List <String> honingCombo;

                HoningNetwork <String> net       = new HoningNetwork <string>();
                List <CardObject>      heroCards = parser.objects[hero];

                builder.PopulateFromCardData(ref net, ref heroCards);
                builder.BuildThirdLevel(ref net, ref heroCards);
                builder.PopulateFromCardData(ref net, ref neutral);
                builder.BuildThirdLevel(ref net, ref neutral);

                HSHoningBayes fixedBayes = new HSHoningBayes(hero.ToLower(), ref combosParser, ref net, ref cardDatasetFull, ref cardTable, 100000);

                fixedBayes.SaveKNNDataset(hero + "_data.dataset");

                Dataset fixedDataset = new Dataset(hero + "_data.dataset", ',');

                KNNEfficiency fixedKNN = new KNNEfficiency(fixedDataset);

                Dictionary <String, HoningNode <String> > dic = net.getNetwork();
                Dictionary <String, String> selfAbilityFilter = op.GenerateAbilityFilter();
                Dictionary <String, String> TerminalsDic      = op.GetComboPotential(ref dic, ref cardTable, ref selfAbilityFilter, 123123);
                List <String> Terminals = TerminalsDic.Keys.ToList();

                double[] surprise_vec;
                Double[] comboArray;
                // Tests i and ii control variables

                // Tests i, ii, iii, iv, v control variables
                int           mana = 10;
                double        surprise;
                double        efficiency;
                double        fitness;
                double        creativity;
                double        normCreativity;
                double        normSurprise;
                double        normEfficiency;
                List <String> combo    = new List <string>();
                double        highSurp = 0.0;
                double        minSurp  = double.MaxValue;
                double        highEff  = 0.0;
                double        minEff   = double.MaxValue;
                // Tests i, ii, iii, iv, v control variables

                String seed = "";

                // Calibrating surprise
                Console.WriteLine("----------------------------------------------------------------------");
                Console.WriteLine("-                       Calibrating surprise!                        -");
                for (int i = 0; i < 100; i++)
                {
                    int           totalMana       = 0;
                    List <String> randomComboList = new List <String>();
                    int           manaCost        = 0;
                    while (totalMana < mana)
                    {
                        int randNode = rand.Next(Terminals.Count);
                        Int32.TryParse(cardTable[Terminals[randNode]].cost, out manaCost);
                        if (manaCost + totalMana <= mana)
                        {
                            randomComboList.Add(Terminals[randNode]);
                            totalMana += manaCost;
                        }
                    }

                    // Surprise
                    ComboNode node = ToComboNode(randomComboList);
                    fixedBayes.CalculateSurprise(ref node, 1, out surprise_vec, ref cardDatasetFull, out surprise, false);

                    // Calculate efficiency
                    fixedBayes.GenerateComboVector(ref node, ref cardDatasetFull, out comboArray);
                    Instance target = new Instance(comboArray);
                    efficiency = fixedKNN.getKNearestWinrates(target, k);

                    if (surprise > highSurp)
                    {
                        highSurp = surprise;
                    }
                    if (surprise < minSurp)
                    {
                        minSurp = surprise;
                    }

                    if (efficiency > highEff)
                    {
                        highEff = efficiency;
                    }
                    if (efficiency < minEff)
                    {
                        minEff = efficiency;
                    }
                }

                Console.WriteLine("-                       Surprise calibrated!                         -");

                foreach (String c in Terminals)
                {
                    Console.WriteLine("----------------------------------------------------------------------");

                    Console.WriteLine("Hero: " + hero);
                    Console.WriteLine("Seed: " + c);

                    Console.WriteLine();

                    file.WriteLine("----------------------------------------------------------------------");
                    file.WriteLine();
                    file.WriteLine("Hero: " + hero);
                    file.WriteLine("Seed: " + c);

                    // Test all reacheable seeds
                    seed = c;

                    //--------------------------------------------------------------------------------------------------------------------------
                    // (i)totalmente aleatorio (sem honing)
                    int           totalMana       = 0;
                    List <String> randomComboList = new List <String>();
                    randomComboList.Add(seed.ToLower());
                    int manaCost = 0;
                    Int32.TryParse(cardTable[seed.ToLower()].cost, out manaCost);
                    totalMana += manaCost;
                    while (totalMana < mana)
                    {
                        int randNode = rand.Next(Terminals.Count);
                        Int32.TryParse(cardTable[Terminals[randNode]].cost, out manaCost);
                        if (manaCost + totalMana <= mana)
                        {
                            randomComboList.Add(Terminals[randNode]);
                            totalMana += manaCost;
                        }
                    }

                    // Surprise
                    ComboNode node = ToComboNode(randomComboList);

                    fixedBayes.CalculateSurprise(ref node, 1, out surprise_vec, ref cardDatasetFull, out surprise, false);

                    // Calculate efficiency
                    fixedBayes.GenerateComboVector(ref node, ref cardDatasetFull, out comboArray);
                    Instance target = new Instance(comboArray);
                    efficiency = fixedKNN.getKNearestWinrates(target, k);

                    // Calculate creativity
                    fitness = op.CalculateFitness(surprise, ref highSurp, ref minSurp, out normSurprise, ref highEff, ref minEff, out normEfficiency, efficiency);

                    creativity     = surprise + efficiency;
                    normCreativity = normSurprise + normEfficiency;

                    Console.WriteLine("Test I:\n");
                    Console.WriteLine("Fitness: " + fitness + "\nRaw creativity: " + creativity + "\nNormalized creativity: " + normCreativity + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    Console.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    Console.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    Console.WriteLine("Cards:\n");
                    file.WriteLine("Test I:\n");
                    file.WriteLine("Fitness: " + creativity + "\nRaw creativity: " + surprise + efficiency + "\nNormalized creativity: " + normEfficiency + normSurprise + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    file.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    file.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    file.WriteLine();
                    file.WriteLine("Cards: ");
                    file.WriteLine();
                    foreach (String st in randomComboList)
                    {
                        Console.Write("(" + st + ") ");
                        file.Write("(" + st + ") ");
                    }
                    Console.WriteLine("\n");
                    file.WriteLine("\n");

                    //--------------------------------------------------------------------------------------------------------------------------
                    // (ii)honing novo aleatorio
                    honingCombo = op.GenerateCardClusterRandom(
                        c,
                        ref cardTable,
                        ref net,
                        ref selfAbilityFilter,
                        ref fixedBayes,
                        ref fixedKNN,
                        ref cardDatasetFull,
                        mana,
                        k,
                        ref highSurp,
                        ref minSurp,
                        ref highEff,
                        ref minEff,
                        out fitness,
                        out surprise,
                        out efficiency,
                        out normSurprise,
                        out normEfficiency).Keys.ToList();

                    creativity     = surprise + efficiency;
                    normCreativity = normSurprise + normEfficiency;

                    Console.WriteLine("Test II:\n");
                    Console.WriteLine("Fitness: " + fitness + "\nRaw creativity: " + creativity + "\nNormalized creativity: " + normCreativity + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    Console.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    Console.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    Console.WriteLine("Cards:\n");
                    file.WriteLine("Test I:\n");
                    file.WriteLine("Fitness: " + creativity + "\nRaw creativity: " + surprise + efficiency + "\nNormalized creativity: " + normEfficiency + normSurprise + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    file.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    file.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    file.WriteLine();
                    file.WriteLine("Cards: ");
                    file.WriteLine();
                    foreach (String st in honingCombo)
                    {
                        Console.Write("(" + st + ") ");
                        file.Write("(" + st + ") ");
                    }
                    Console.WriteLine("\n");
                    file.WriteLine("\n");

                    //--------------------------------------------------------------------------------------------------------------------------


                    // (iii)honing novo (E+S)
                    honingCombo = op.GenerateCardCluster(
                        c,
                        ref cardTable,
                        ref net,
                        ref selfAbilityFilter,
                        ref fixedBayes,
                        ref fixedKNN,
                        ref cardDatasetFull,
                        mana,
                        k,
                        ref highSurp,
                        ref minSurp,
                        ref highEff,
                        ref minEff,
                        out fitness,
                        out surprise,
                        out efficiency,
                        out normSurprise,
                        out normEfficiency).Keys.ToList();

                    creativity     = surprise + efficiency;
                    normCreativity = normSurprise + normEfficiency;

                    Console.WriteLine("Test III:\n");
                    Console.WriteLine("Fitness: " + fitness + "\nRaw creativity: " + creativity + "\nNormalized creativity: " + normCreativity + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    Console.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    Console.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    Console.WriteLine("Cards:\n");
                    file.WriteLine("Test I:\n");
                    file.WriteLine("Fitness: " + creativity + "\nRaw creativity: " + surprise + efficiency + "\nNormalized creativity: " + normEfficiency + normSurprise + "\nSurprise " + surprise + "\nNormalized surprise: " + normSurprise + "\nEfficiency: " + efficiency + "\nNormalized efficiency: " + normEfficiency);
                    file.WriteLine("Highest surprise: " + highSurp + "\nLowest surprise: " + minSurp);
                    file.WriteLine("Highest efficiency: " + highEff + "\nLowest efficiency: " + minEff + "\n");
                    file.WriteLine();
                    file.WriteLine("Cards: ");
                    file.WriteLine();
                    foreach (String st in honingCombo)
                    {
                        Console.Write("(" + st + ") ");
                        file.Write("(" + st + ") ");
                    }
                    Console.WriteLine("\n");
                    file.WriteLine("\n");
                }

                file.Close();
            }
        }
コード例 #2
0
        void ValidationTests(String cardsJson, String combosFile, int k)
        {
            HSCardsParser      parser  = new HSCardsParser(cardsJson);
            HoningStoneBuilder builder = new HoningStoneBuilder();

            Dictionary <String, CardObject> cardTable = new Dictionary <string, CardObject>();
            HSCardOperator op = new HSCardOperator();

            HSCardsParser                 fullParser = new HSCardsParser("allCardsWithAbility.json", 0);
            Dictionary <String, int>      dataID;
            Dictionary <String, double[]> cardDatasetFull = op.generateCardVectors(fullParser, out dataID);

            // Populate all card table
            foreach (string key in parser.objects.Keys)
            {
                List <CardObject> cards_objects = parser.objects[key];
                builder.GenCardTableObject(ref cards_objects, ref cardTable);
            }

            HSCombosParser combosParser = new HSCombosParser();

            //combosParser.PopulateFromHoningNetwork(ref decksParser, ref cardTable, 5);
            combosParser.PopulateFromJson(combosFile);

            Random rand = new Random();

            List <CardObject> neutral = parser.objects["Neutral"];

            String currentDirectory = "results";

            bool exists = System.IO.Directory.Exists(currentDirectory);

            if (!exists)
            {
                System.IO.Directory.CreateDirectory(currentDirectory);
            }

            // boxplot files
            System.IO.StreamWriter Fitness            = new System.IO.StreamWriter(currentDirectory + "//fit.txt");
            System.IO.StreamWriter Efficiency         = new System.IO.StreamWriter(currentDirectory + "//eff.txt");
            System.IO.StreamWriter Surprise           = new System.IO.StreamWriter(currentDirectory + "//sur.txt");
            System.IO.StreamWriter Creativity         = new System.IO.StreamWriter(currentDirectory + "//crea.txt");
            System.IO.StreamWriter fileNormEfficiency = new System.IO.StreamWriter(currentDirectory + "//norm_eff.txt");
            System.IO.StreamWriter fileNormSurprise   = new System.IO.StreamWriter(currentDirectory + "//norm_sur.txt");
            System.IO.StreamWriter fileNormCreativity = new System.IO.StreamWriter(currentDirectory + "//norm_crea.txt");

            Fitness.WriteLine("Hero Algorithm Value");
            Efficiency.WriteLine("Hero Algorithm Value");
            Surprise.WriteLine("Hero Algorithm Value");
            Creativity.WriteLine("Hero Algorithm Value");
            fileNormEfficiency.WriteLine("Hero Algorithm Value");
            fileNormSurprise.WriteLine("Hero Algorithm Value");
            fileNormCreativity.WriteLine("Hero Algorithm Value");

            foreach (String hero in parser.objects.Keys)
            {
                if (hero == "Neutral")
                {
                    continue;
                }

                List <String> honingCombo;

                HoningNetwork <String> net       = new HoningNetwork <string>();
                List <CardObject>      heroCards = parser.objects[hero];

                builder.PopulateFromCardData(ref net, ref heroCards);
                builder.BuildThirdLevel(ref net, ref heroCards);
                builder.PopulateFromCardData(ref net, ref neutral);
                builder.BuildThirdLevel(ref net, ref neutral);

                HSHoningBayes fixedBayes = new HSHoningBayes(hero.ToLower(), ref combosParser, ref net, ref cardDatasetFull, ref cardTable, 100000);

                fixedBayes.SaveKNNDataset(hero + "_data.dataset");

                Dataset fixedDataset = new Dataset(hero + "_data.dataset", ',');

                KNNEfficiency fixedKNN = new KNNEfficiency(fixedDataset);

                Dictionary <String, HoningNode <String> > dic = net.getNetwork();
                Dictionary <String, String> selfAbilityFilter = op.GenerateAbilityFilter();
                Dictionary <String, String> TerminalsDic      = op.GetComboPotential(ref dic, ref cardTable, ref selfAbilityFilter, 10);
                List <String> Terminals = TerminalsDic.Keys.ToList();

                // Tests i, ii, iii, iv, v control variables
                int           mana = 10;
                double        surprise;
                double        efficiency;
                double        fitness;
                double        creativity;
                double        normCreativity;
                double        normSurprise;
                double        normEfficiency;
                List <String> combo    = new List <string>();
                double        highSurp = 0.0;
                double        minSurp  = double.MaxValue;
                double        highEff  = 0.0;
                double        minEff   = double.MaxValue;
                // Tests i, ii, iii, iv, v control variables

                String seed = "";

                Console.WriteLine("----------------------------------------------------------------------");
                Console.WriteLine("-                       Loading calibrated surprise!                 -");

                string[] lines = System.IO.File.ReadAllLines(hero + "_calibration.cal");
                char[]   delim = new char[1];
                delim[0] = ' ';
                String highSurprise   = lines[0].Split(delim).Last();
                String minSurprise    = lines[1].Split(delim).Last();
                String highEfficiency = lines[2].Split(delim).Last();
                String minEfficiency  = lines[3].Split(delim).Last();

                double.TryParse(highSurprise, out highSurp);
                double.TryParse(minSurprise, out minSurp);
                double.TryParse(highEfficiency, out highEff);
                double.TryParse(minEfficiency, out minEff);

                int seedCount         = 1;
                int totalTestsPerSeed = 10;

                // Foreach percentage
                for (int percentage = 20; percentage <= 100; percentage += 20)
                {
                    // To write results
                    System.IO.StreamWriter resultFile = new System.IO.StreamWriter(currentDirectory + "//" + hero + "_results_II_" + percentage + ".dat");

                    // To calculate statistics
                    List <result> listres = new List <result>();

                    foreach (String c in Terminals)
                    {
                        //String c = Terminals.First();
                        Console.WriteLine("----------------------------------------------------------------------");

                        Console.WriteLine("Hero: " + hero);
                        Console.WriteLine("Seed: " + c);
                        Console.WriteLine("Seed " + seedCount + " of " + Terminals.Count);

                        Console.WriteLine();

                        // Test all reacheable seeds
                        seed = c;

                        for (int i = 0; i < totalTestsPerSeed; i++)
                        {
                            // (iii)honing novo (E+S)
                            honingCombo = op.GenerateCardCluster(
                                c,
                                ref cardTable,
                                ref net,
                                ref selfAbilityFilter,
                                ref fixedBayes,
                                ref fixedKNN,
                                ref cardDatasetFull,
                                mana,
                                k,
                                percentage,
                                false,
                                ref highSurp,
                                ref minSurp,
                                ref highEff,
                                ref minEff,
                                out fitness,
                                out surprise,
                                out efficiency,
                                out normSurprise,
                                out normEfficiency).Keys.ToList();

                            creativity     = surprise + efficiency;
                            normCreativity = normSurprise + normEfficiency;

                            result res = new result(honingCombo, fitness, creativity, normCreativity, surprise, normSurprise, efficiency, normEfficiency, highSurp, minSurp, highEff, minEff);
                            listres.Add(res);
                        }

                        seedCount++;
                    }

                    // Renormalize and recalibrate limits
                    List <result> renormalizedResults = renormalizeSimple(listres, ref highSurp, ref minSurp, ref minEff, ref highEff);

                    // Write to file
                    foreach (result r in renormalizedResults)
                    {
                        r.writeIntoFile(resultFile);
                    }

                    //extractViableSeeds(op, hero, 0, 1, meanFitness, meanEfficiency, meanSurprise, meanCreativity);
                    writeForBoxPlot(hero, renormalizedResults, "II" + percentage, Fitness, Efficiency, Surprise, Creativity, fileNormEfficiency, fileNormSurprise, fileNormCreativity);

                    resultFile.Close();
                } // end statistics
            }     // end hero

            // boxplot closing
            Fitness.Close();
            Efficiency.Close();
            Surprise.Close();
            Creativity.Close();
            fileNormCreativity.Close();
            fileNormEfficiency.Close();
            fileNormSurprise.Close();
        }