Exemplo n.º 1
0
        public void calibration(String hero, HSCardsParser parser, HoningStoneBuilder builder, HSCombosParser combosParser, Dictionary <String, double[]> cardDatasetFull, Dictionary <String, CardObject> cartTable, HSCardOperator op)
        {
            net = new HoningNetwork <string>();

            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);

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

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

            fixedBayes = new HSHoningBayes(hero.ToLower(), ref combosParser, ref net, ref cardDatasetFull, ref cartTable, 100000);
            fixedBayes.SaveKNNDataset(hero + "_data.dataset");
            Dataset fixedDataset = new Dataset(hero + "_data.dataset", ',');

            fixedKNN = new KNNEfficiency(fixedDataset);

            dic = net.getNetwork();
            selfAbilityFilter = op.GenerateAbilityFilter();
        }
Exemplo n.º 2
0
        public HoningNetwork <String> populateNetwork(String file_path)
        {
            // Configura builder
            HoningStoneBuilder builder = new HoningStoneBuilder();

            // Apenas leitura do json
            String json_network = builder.ReadHoningStone(file_path);

            // Popula e retorna rede pronta para uso
            return(builder.BuildNetwork(json_network));
        }
Exemplo n.º 3
0
        public void Instantiate(String jsonWithTypes, String ALYCOMBOOK, String allCardsWithAbility)
        {
            liteParser   = new HSCardsParser(jsonWithTypes);
            fullParser   = new HSCardsParser(allCardsWithAbility, 0);
            fullCParser  = new HSCombosParser(); fullCParser.PopulateFromJson(ALYCOMBOOK);
            HSBuilder    = new HoningStoneBuilder();
            cardOperator = new HSCardOperator();
            cartTable    = new Dictionary <string, CardObject>();
            cardsDataSet = cardOperator.generateCardVectors(fullParser, out dataID);

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

            honingOBJS = new Dictionary <string, HS_HONING_OBJ>();
            heroes     = new String[9];
            heroes[0]  = "Shaman"; heroes[1] = "Mage"; heroes[2] = "Warrior"; heroes[3] = "Druid"; heroes[4] = "Rogue"; heroes[5] = "Priest"; heroes[6] = "Paladin"; heroes[7] = "Warlock"; heroes[8] = "Hunter";
            for (int i = 0; i < 9; i++)
            {
                HS_HONING_OBJ h_obj = new HS_HONING_OBJ();
                h_obj.calibration(heroes[i], liteParser, HSBuilder, fullCParser, cardsDataSet, cartTable, cardOperator);
                honingOBJS.Add(heroes[i], h_obj);
            }

            dataID.Clear();
            liteParser.objects.Clear();
            fullParser.objects.Clear();
            fullCParser.combos_by_quantity.Clear();

            dataID                         = null;
            liteParser.objects             = null;
            fullParser.objects             = null;
            fullCParser.combos_by_quantity = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        void ValidationTests()
        {
            HSCardsParser      parser  = new HSCardsParser("jsonWithTypes.json");
            HoningStoneBuilder builder = new HoningStoneBuilder();

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

            // 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("ALYCOMBOOK.json");

            int maxComboSize = 10;
            //int maxMana         = 50;
            int maxCombos = 100;

            Random rand = new Random();

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

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

                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 cardTable);

                fixedBayes.SaveKNNDataset("ARSDataset.dataset");

                Dataset       fixedDataset = new Dataset("ARSDataset.dataset", ',');
                KNNEfficiency fixedKNN     = new KNNEfficiency(fixedDataset);

                HSHoningBayes dynamicBayes = new HSHoningBayes(hero.ToLower(), ref combosParser, ref net, ref cardTable);

                List <String> Terminals = net.getTerminalList();

                // Random Honing config
                HSCardExpasionConfiguration config = new HSCardExpasionConfiguration(fixedBayes, fixedKNN);
                config.cutByManaCost            = false;
                config.max_lowerlevel_to_expand = 1;
                config.giver_inflation          = false;

                // Guided Honing config
                HSCardExpasionConfiguration configGuided = new HSCardExpasionConfiguration(fixedBayes, fixedKNN);
                configGuided.cutByManaCost            = false;
                configGuided.max_lowerlevel_to_expand = 1;
                configGuided.giver_inflation          = false;

                // Tests i and ii control variables
                double ihs  = 0.0f;
                double iihs = 0.0f;

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

                // Tests i, ii, iii, iv, v control variables
                int           mana = 50;
                double        surprise;
                double        efficiency;
                double        creativity;
                List <String> combo;
                // Tests i, ii, iii, iv, v control variables

                int combosize = 5;

                for (int combos = 0; combos < maxCombos; combos++)
                {
                    //for (int mana = 2; mana <= maxMana; mana++)
                    //{
                    //for (int combosize = 2; combosize <= maxComboSize; combosize++)
                    // {
                    // Honing shared seed
                    int    RandomicSeed = rand.Next(Terminals.Count);
                    String seed         = Terminals[RandomicSeed];

                    //--------------------------------------------------------------------------------------------------------------------------
                    // Surpresa estática
                    // (i)totalmente aleatorio (sem honing)

                    /* List<String> randomComboList = new List<String>();
                     * while(randomComboList.Count < combosize)
                     * {
                     *   int randNode = rand.Next(Terminals.Count);
                     *   randomComboList.Add(Terminals[randNode]);
                     * }
                     *
                     * // Surprise
                     * ComboNode node = ToComboNode(randomComboList);
                     *
                     * fixedBayes.CalculateSurprise(ref node, 1, out surprise_vec, out surprise, false);
                     *
                     * // update surprise
                     * if (surprise > ihs)
                     *   ihs = surprise;
                     *
                     * // Calculate efficiency
                     * fixedBayes.GenerateComboVector(ref node, out comboArray);
                     * Instance target = new Instance(comboArray);
                     * efficiency = fixedKNN.getKNearestWinrates(target, 5);
                     * efficiency /= 100;
                     *
                     * // Calculate creativity
                     * creativity = ((surprise / ihs) + efficiency) / 2;
                     *
                     * //--------------------------------------------------------------------------------------------------------------------------
                     *
                     * // (ii)honing velho aleatorio
                     * Dictionary<String, String> shcombolist = new Dictionary<String, String>();
                     * Dictionary<String, HoningNode<String>> honingOut;
                     * List<String> bridges;
                     * net.getMfList(seed, out bridges);
                     * net.recruitNeurds(bridges, out honingOut, "terminal");
                     *
                     * List<String> comboH = new List<string>();
                     *
                     * int limit = combosize;
                     * if(honingOut.Count < combosize)
                     *   limit = honingOut.Count;
                     * for (int i = 0; i < limit; i++)
                     *   comboH.Add(honingOut.ElementAt(i).Key);
                     *
                     * // Surprise
                     * node = ToComboNode(comboH);
                     *
                     * fixedBayes.CalculateSurprise(ref node, 1, out surprise_vec, out surprise, false);
                     *
                     * // update surprise
                     * if (surprise > iihs)
                     *   iihs = surprise;
                     *
                     * // Calculate efficiency
                     * fixedBayes.GenerateComboVector(ref node, out comboArray);
                     * target = new Instance(comboArray);
                     * efficiency = fixedKNN.getKNearestWinrates(target, 5);
                     * efficiency /= 100;
                     *
                     * // Calculate creativity
                     * creativity = ((surprise / iihs) + efficiency) / 2;
                     *
                     * //--------------------------------------------------------------------------------------------------------------------------
                     *
                     * // (iii)honing novo aleatorio
                     * HoningTest(
                     *   config,
                     *   fixedBayes,
                     *   fixedKNN,
                     *   mana,
                     *   combosize,
                     *   net,
                     *   cardTable,
                     *   seed,
                     *   op,
                     *   ExpansionGeneralPolitics.Random,
                     *   out combo,
                     *   out creativity,
                     *   out efficiency,
                     *   out surprise);*/

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

                    // (iv)busca pela aresta com maior valor eficiência + surpresa (GULOSO)
                    HoningTest(
                        configGuided,
                        fixedBayes,
                        fixedKNN,
                        mana,
                        combosize,
                        net,
                        cardTable,
                        seed,
                        op,
                        ExpansionGeneralPolitics.Weight,
                        out combo,
                        out creativity,
                        out efficiency,
                        out surprise);

                    Console.WriteLine("Cluster honing finished runing.");
                    //--------------------------------------------------------------------------------------------------------------------------


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

                    // Surpresa dinâmica
                    // (i)totalmente aleatorio (sem honing)
                    // (ii)honing velho aleatorio
                    // (iii)honing novo aleatorio
                    // (iv)busca pela aresta com maior valor eficiência + surpresa
                    //}
                }
                //}
            }
        }
Exemplo n.º 5
0
        public void PopulateFromHoningNetwork(ref HSDecksParser decks, ref Dictionary <String, CardObject> cardTable, int mana_mult)
        {
            combos_per_card    = 10;
            combos_by_quantity = new Dictionary <string, Dictionary <int, List <ComboNode> > >();

            // Hero loop
            foreach (String hero in decks.decks_by_hero.Keys)
            {
                // Decks loop
                foreach (HSDeckInfo deckinfo in decks.decks_by_hero[hero])
                {
                    // Generate honingNet for the deck
                    HoningNetwork <String> deck_net      = new HoningNetwork <string>();
                    List <CardObject>      cards_objects = new List <CardObject>();
                    HoningStoneBuilder     builder       = new HoningStoneBuilder();

                    // Generate card table info to populate the honingNetwork
                    foreach (String card in deckinfo.cards)
                    {
                        if (cardTable.ContainsKey(card))
                        {
                            cards_objects.Add(cardTable[card]);
                        }
                    }

                    builder.PopulateFromCardData(ref deck_net, ref cards_objects);
                    builder.BuildThirdLevel(ref deck_net, ref cards_objects);

                    // Generate all combos
                    HSCardOperator op = new HSCardOperator();
                    HSCardExpasionConfiguration conf = new HSCardExpasionConfiguration(null, null);

                    for (int j = 1; j < mana_mult; j++)
                    {
                        // Mana to spend on combo
                        conf.total_mana = 10 * j;

                        // Same deck insertion flag
                        bool same_deck = false;
                        foreach (String card in deckinfo.cards)
                        {
                            for (int i = 0; i < combos_per_card; i++)
                            {
                                List <List <String> > combo;
                                List <String>         sCombo;
                                op.GreedyExpansionDelegated(
                                    ref deck_net,
                                    ref conf,
                                    ref cardTable,
                                    ExpansionGeneralPolitics.Random,
                                    PriorityPolitics.Random,
                                    PriorityPolitics.Random,
                                    card,
                                    out combo,
                                    out sCombo);

                                // Create new combo object(Node) or get an existing one
                                Dictionary <String, String> comboToFind = new Dictionary <string, string>();
                                foreach (String c in sCombo)
                                {
                                    comboToFind.Add(c, c);
                                }

                                if (comboToFind.Count == 0)
                                {
                                    continue;
                                }

                                ComboNode new_combo = FindCombo(ref comboToFind, hero);

                                if (new_combo == null)
                                {
                                    new_combo = new ComboNode();
                                    if (!combos_by_quantity.ContainsKey(hero))
                                    {
                                        combos_by_quantity.Add(hero, new Dictionary <int, List <ComboNode> >());
                                    }
                                    if (!combos_by_quantity[hero].ContainsKey(comboToFind.Count))
                                    {
                                        combos_by_quantity[hero].Add(comboToFind.Count, new List <ComboNode>());
                                    }


                                    // Configura winrate for this combo
                                    float comboWinrate;
                                    float.TryParse(deckinfo.winrate, out comboWinrate);
                                    new_combo.winrates.Add(comboWinrate);
                                    new_combo.combo = comboToFind;

                                    // Add the combo to the master holder
                                    combos_by_quantity[hero][comboToFind.Count].Add(new_combo);
                                }
                                else
                                {
                                    if (!same_deck && new_combo.combo.Count != 0)
                                    {
                                        float comboWinrate;
                                        float.TryParse(deckinfo.winrate, out comboWinrate);
                                        new_combo.winrates.Add(comboWinrate);
                                    }
                                }

                                same_deck = true;
                            }
                        } // end foreach card
                    }     // end for manda
                }         // end foreach deck
            }             // end foreach hero
        }
        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();
            }
        }
        void ValidationTests(String cardsJson, String combosFile, String knnDatase, int k)
        {
            HSCardsParser      parser  = new HSCardsParser(cardsJson);
            HoningStoneBuilder builder = new HoningStoneBuilder();

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

            // 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();

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

                List <String> honingCombo;
                List <String> highestComboI   = new List <String>();
                List <String> highestComboII  = new List <String>();;
                List <String> highestComboIII = new List <String>();;

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

                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 cardTable);

                //fixedBayes.SaveKNNDataset("ARSDataset.dataset");

                Dataset       fixedDataset = new Dataset(knnDatase, ',');
                KNNEfficiency fixedKNN     = new KNNEfficiency(fixedDataset);

                HSHoningBayes dynamicBayes = new HSHoningBayes(hero.ToLower(), ref combosParser, ref net, ref cardTable);

                Dictionary <String, HoningNode <String> > dic = net.getNetwork();
                Dictionary <String, String> selfAbilityFilter = op.GenerateAbilityFilter();
                Dictionary <String, String> TerminalsDic      = op.GetRealComboPotential(ref dic, ref cardTable, ref selfAbilityFilter, 3);
                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        creativity;
                List <String> combo = new List <string>();
                // Tests i, ii, iii, iv, v control variables

                // Test I
                double highestCreativityA = 0.0;

                // Test II
                double highestCreativityB = 0.0;

                // Test III
                double highestCreativityC = 0.0;

                double highSurp = 0.0;

                double highestEfficience = 0.0;

                String seed = "";

                foreach (String c in Terminals)
                {
                    Console.WriteLine("Hero: " + hero);
                    Console.WriteLine("Seed: " + c);

                    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, out surprise, false);

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

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

                    if (efficiency > highestEfficience)
                    {
                        highestEfficience = efficiency;
                    }

                    // Calculate creativity
                    creativity = op.CalculateCreativity(surprise / highSurp, efficiency);

                    // Write in file
                    bool update = false;
                    if (creativity > highestCreativityA)
                    {
                        highestCreativityA = creativity;
                        update             = true;
                    }

                    Console.WriteLine("I: " + creativity + " " + surprise / highSurp + " " + efficiency);
                    file.WriteLine("I: " + creativity + " " + surprise + " " + efficiency);
                    foreach (String st in randomComboList)
                    {
                        Console.Write(st + "|");
                        file.Write(st + "|");
                        if (update)
                        {
                            highestComboI.Add(st);
                        }
                    }
                    Console.WriteLine();
                    file.WriteLine();

                    //--------------------------------------------------------------------------------------------------------------------------
                    // (ii)honing novo aleatorio
                    honingCombo = op.GenerateCardClusterRandom(c, ref cardTable, ref net, ref selfAbilityFilter, ref fixedBayes, ref fixedKNN, mana, k,
                                                               highSurp,
                                                               out creativity,
                                                               out surprise,
                                                               out efficiency).Keys.ToList();

                    update = false;
                    if (creativity > highestCreativityB)
                    {
                        highestCreativityB = creativity;
                        update             = true;
                    }

                    if (efficiency > highestEfficience)
                    {
                        highestEfficience = efficiency;
                    }

                    file.WriteLine("II: " + creativity + " " + surprise + " " + efficiency);
                    Console.WriteLine("II: " + creativity + " " + surprise + " " + efficiency);
                    foreach (String st in honingCombo)
                    {
                        Console.Write(st + "|");
                        file.Write(st + "|");
                        if (update)
                        {
                            highestComboI.Add(st);
                        }
                    }
                    file.WriteLine();
                    Console.WriteLine();
                    //--------------------------------------------------------------------------------------------------------------------------
                    // (iii)honing novo (E+S)
                    honingCombo = op.GenerateCardCluster(c, ref cardTable, ref net, ref selfAbilityFilter, ref fixedBayes, ref fixedKNN, mana, k,
                                                         ref highSurp,
                                                         out creativity,
                                                         out surprise,
                                                         out efficiency).Keys.ToList();

                    update = false;
                    if (creativity > highestCreativityC)
                    {
                        highestCreativityC = creativity;
                        update             = true;
                    }

                    if (efficiency > highestEfficience)
                    {
                        highestEfficience = efficiency;
                    }

                    file.WriteLine("III: " + creativity + " " + surprise + " " + efficiency);
                    Console.WriteLine("III: " + creativity + " " + surprise + " " + efficiency);
                    foreach (String st in honingCombo)
                    {
                        Console.Write(st + "|");
                        file.Write(st + "|");
                        if (update)
                        {
                            highestComboI.Add(st);
                        }
                    }
                    file.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("----------------------------------------------------------------");
                }

                file.Close();
            }
        }
Exemplo n.º 8
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();
        }