public void CalculateSurprise(ref ComboNode combo, int N, out double[] surpriseVector, ref Dictionary <String, double[]> cardVec, out double surpriseMean, bool updateBayes) { double[] comboVector; GenerateComboVector(ref combo, ref cardVec, out comboVector); surpriseMean = 0.0; surpriseVector = new double[varianceVector.Length]; for (int i = 0; i < varianceVector.Length; i++) { if (varianceVector[i] <= 0.0) { surpriseVector[i] = 0.0; } else { surpriseVector[i] = (N / (2 * varianceVector[i])) * (varianceVector[i] + Math.Pow((comboVector[i] - meanVector[i]), 2.0)); } surpriseMean += surpriseVector[i]; } //surpriseMean /= surpriseVector.Length; if (updateBayes) { bayesMatrix.Add(comboVector); CalculateStatistics(); } }
public ComboNode FindCombo(ref Dictionary <String, String> combo, String hero) { ComboNode combonode = new ComboNode(); if (!combos_by_quantity.ContainsKey(hero)) { return(null); } int comboSize = combo.Count; Dictionary <int, List <ComboNode> > comboDic = combos_by_quantity[hero]; if (!comboDic.ContainsKey(comboSize)) { return(null); } List <ComboNode> seekingObject = comboDic[comboSize]; foreach (ComboNode checker in seekingObject) { if (CheckComboAND(ref combo, ref checker.combo)) { combonode = checker; break; } } if (combonode.combo.Count == 0) { return(null); } return(combonode); }
public HSHoningBayes(String hero, ref HSCombosParser combosParser, ref HoningNetwork <String> net, ref Dictionary <String, double[]> cardDataset, ref Dictionary <String, CardObject> cardTable, int comboLimit) { this.net = net; this.cardTable = cardTable; bayesMatrix = new List <double[]>(); globalSurprise = 0.0f; int totalCombos = 0; foreach (int key in combosParser.combos_by_quantity[hero].Keys) { int comboPerQuantity = 0; foreach (ComboNode node in combosParser.combos_by_quantity[hero][key]) { if (comboPerQuantity == comboLimit) { break; } ComboNode combo = node; double[] bayesCombo; GenerateComboVector(ref combo, ref cardDataset, out bayesCombo); bayesMatrix.Add(bayesCombo); comboPerQuantity++; totalCombos++; } } winrates = new double[totalCombos]; // Populate winrates int currentCombo = 0; foreach (int key in combosParser.combos_by_quantity[hero].Keys) { int comboPerQuantity = 0; foreach (ComboNode node in combosParser.combos_by_quantity[hero][key]) { if (comboPerQuantity == comboLimit) { break; } winrates[currentCombo] = node.winrate_mean; comboPerQuantity++; currentCombo++; } } CalculateStatistics(); }
ComboNode ToComboNode(Dictionary <String, String> combo) { ComboNode node = new ComboNode(); foreach (String s in combo.Keys) { node.combo.Add(s, s); } return(node); }
public void PopulateFromJson(String path) { combos_by_quantity = new Dictionary <string, Dictionary <int, List <ComboNode> > >(); // Le arquivo json e retorna string inteira da rede String json = System.IO.File.ReadAllText(path); // Ler json com dados da rede dynamic stuff = JArray.Parse(json); foreach (dynamic hero in stuff) { String currentHero = hero.hero; int cut = 0; foreach (dynamic combo in hero.combos) { if (cut >= 100) { break; } ComboNode node = new ComboNode(); node.winrate_max = combo.winrate_max; node.winrate_min = combo.winrate_min; node.winrate_mean = combo.winrate_mean; node.winrate_median = combo.winrate_median; foreach (float w in combo.winrates) { node.winrates.Add(w); } foreach (String card in combo.cards) { node.combo.Add(card, card); } if (!combos_by_quantity.ContainsKey(currentHero)) { combos_by_quantity.Add(currentHero, new Dictionary <int, List <ComboNode> >()); } Dictionary <int, List <ComboNode> > dic = combos_by_quantity[currentHero]; if (!dic.ContainsKey(node.combo.Count)) { dic.Add(node.combo.Count, new List <ComboNode>()); } List <ComboNode> nodes = dic[node.combo.Count]; nodes.Add(node); cut++; } } }
ComboNode ToComboNode(List <String> combo) { ComboNode node = new ComboNode(); foreach (String s in combo) { if (!node.combo.ContainsKey(s)) { node.combo.Add(s, s); } } return(node); }
public void CalculateWinrates() { foreach (String key in combos_by_quantity.Keys) { foreach (int comboQuant in combos_by_quantity[key].Keys) { for (int i = 0; i < combos_by_quantity[key][comboQuant].Count; i++) { ComboNode node = combos_by_quantity[key][comboQuant][i]; node.winrates.Sort(); CalculeteStatistics(ref node); } } } }
public void GenerateComboVector(ref ComboNode combo, ref Dictionary <String, double[]> cardVec, out double[] HearthStoneBayesVector) { int vectorSize = cardVec.ElementAt(0).Value.Length; HearthStoneBayesVector = new double[vectorSize]; // populate vector foreach (String card in combo.combo.Keys) { double[] cardDataset = cardVec[card]; for (int i = 0; i < cardDataset.Length; i++) { HearthStoneBayesVector[i] += cardDataset[i]; } } HearthStoneBayesVector[vectorSize - 1] = combo.winrate_median; }
public void GenerateComboVector(ref ComboNode combo, out double[] HearthStoneBayesVector) { net.leafIDMapping(out id_mapping, out inverse_id_mapping); int vectorSize = id_mapping.Count; HearthStoneBayesVector = new double[vectorSize]; // populate vector foreach (String card in combo.combo.Keys) { // get all the card abilities and put into the vector CardObject cardobj = cardTable[card]; foreach (CardAbility ability in cardobj.abilities) { int vectorId = id_mapping[ability.ability]; float abilityValue; float.TryParse(ability.value, out abilityValue); HearthStoneBayesVector[vectorId] += abilityValue; } } }
public void CalculeteStatistics(ref ComboNode node) { float winRateMin = node.winrates.First(); float winRateMax = node.winrates.First(); foreach (float winrate in node.winrates) { node.winrate_mean += winrate; if (winrate >= winRateMax) { winRateMax = winrate; } if (winrate <= winRateMin) { winRateMin = winrate; } } node.winrate_mean /= node.winrates.Count; node.winrate_max = winRateMax; node.winrate_min = winRateMin; node.winrate_median = node.winrates[(int)(node.winrates.Count / 2)]; }
void HoningTest( HSCardExpasionConfiguration config, HSHoningBayes fixedBayes, KNNEfficiency fixedKNN, int mana, int maxCards, HoningNetwork <String> net, Dictionary <String, CardObject> cardTable, String seed, HSCardOperator op, ExpansionGeneralPolitics pol, out List <String> combo, out double creativity, out double efficiency, out double surprise) { config.maxCards = maxCards; config.total_mana = mana; List <List <String> > out_subcluster; List <String> out_combo; op.GreedyExpansionDelegated( ref net, ref config, ref cardTable, pol, PriorityPolitics.Random, PriorityPolitics.Random, seed, out out_subcluster, out out_combo); // Context filter int voidCount = 0; for (int i = 0; i < out_combo.Count; i++) { if (out_combo[i] == "") { voidCount++; } } while (voidCount > 0) { out_combo.Remove(""); voidCount--; } // Surprise ComboNode comboNode = ToComboNode(out_combo); double[] surpriseVector; fixedBayes.CalculateSurprise(ref comboNode, 1, out surpriseVector, out surprise, true); // Calculate efficiency Double[] efficiencyVector; config.bayes.GenerateComboVector(ref comboNode, out efficiencyVector); Instance knnInstance = new Instance(efficiencyVector); efficiency = config.knn.getKNearestWinrates(knnInstance, 5); // Percent efficiency /= 100; // Calculate creativity creativity = ((surprise / config.highestSurprise) + efficiency) / 2; // Combo return combo = out_combo; }
public void ToJson(String path) { System.IO.StreamWriter file = new System.IO.StreamWriter(path); //File start vector file.WriteLine("["); int c = 0; foreach (String key in combos_by_quantity.Keys) { //hero start file.WriteLine("{"); file.WriteLine("\"hero\":\"" + key + "\","); // Combos start file.WriteLine("\"combos\":["); int b = 0; foreach (int comboQuant in combos_by_quantity[key].Keys) { for (int i = 0; i < combos_by_quantity[key][comboQuant].Count; i++) { string lines = ""; ComboNode node = combos_by_quantity[key][comboQuant][i]; // combo open file.Write("{"); lines += "\"winrate_max\":\"" + node.winrate_max + "\","; lines += "\"winrate_min\":\"" + node.winrate_min + "\","; lines += "\"winrate_mean\":\"" + node.winrate_mean + "\","; lines += "\"winrate_median\":\"" + node.winrate_median + "\","; lines += "\"winrates\":["; for (int j = 0; j < node.winrates.Count; j++) { lines += "\"" + node.winrates[j] + "\""; if (j != node.winrates.Count - 1) { lines += ","; } } lines += "],"; lines += "\"cards\":["; for (int j = 0; j < node.combo.Count; j++) { lines += "\"" + node.combo.ElementAt(j).Value + "\""; if (j != node.combo.Count - 1) { lines += ","; } } lines += "]"; // combo internal file.WriteLine(lines); // combo close file.WriteLine("}"); if (b != combos_by_quantity[key].Count - 1) { file.WriteLine(","); } // if (i != combos_by_quantity[key][comboQuant].Count - 1) } b++; } //combos end file.WriteLine("]"); //end hero file.WriteLine("}"); if (c != combos_by_quantity.Count - 1) { file.WriteLine(","); } c++; } //File end vector file.WriteLine("]"); file.Close(); }
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(); } }
public List <String> InflateNeighbours( Dictionary <String, HoningNode <String> > workspace, Dictionary <String, CardObject> cardTable, HSCardExpasionConfiguration config, ExpansionGeneralPolitics politics, Dictionary <String, Boolean> markers, List <String> combo) { List <String> res = new List <string>(); // Inflation window int windowSize = 3; int displace = 0; Random dRange = new Random(); if (workspace.Count > windowSize) { displace = dRange.Next(workspace.Count - windowSize); for (int i = displace; i < displace + windowSize; i++) { res.Add(workspace.ElementAt(i).Value.holder); } } else { foreach (String k in workspace.Keys) { if (!markers.ContainsKey(k)) { res.Add(workspace[k].holder); } } } if (res.Count == 0) { return(res); } List <String> final_ret = new List <string>(); Boolean[] check = new Boolean[res.Count]; Boolean allCheckded = false; int cur = 0; switch (politics) { case ExpansionGeneralPolitics.Lowest_Mana: try { res.Sort( delegate(String a, String b) { if (a == null || b == null) { return(-1); } int Ia, Ib; Int32.TryParse(cardTable[a].cost, out Ia); Int32.TryParse(cardTable[b].cost, out Ib); if (Ia > Ib) { return(0); } return(-1); } ); } catch (ArgumentException) { } cur = 0; while (final_ret.Count < config.single_expasion_quant) { if (allCheckded) { break; } check[cur] = true; if (!markers.ContainsKey(res[cur])) { markers.Add(res[cur], true); final_ret.Add(res[cur]); } cur++; allCheckded = true; for (int i = 0; i < res.Count; i++) { if (check[i] == false) { allCheckded = false; break; } } } break; case ExpansionGeneralPolitics.Higher_Mana: try { res.Sort( delegate(String a, String b) { if (a == null || b == null) { return(-1); } int Ia, Ib; Int32.TryParse(cardTable[a].cost, out Ia); Int32.TryParse(cardTable[a].cost, out Ib); if (Ia < Ib) { return(0); } return(-1); } ); } catch (ArgumentException) { } cur = 0; while (final_ret.Count < config.single_expasion_quant) { if (allCheckded) { break; } check[cur] = true; if (!markers.ContainsKey(res[cur])) { markers.Add(res[cur], true); final_ret.Add(res[cur]); } cur++; allCheckded = true; for (int i = 0; i < res.Count; i++) { if (check[i] == false) { allCheckded = false; break; } } } break; case ExpansionGeneralPolitics.Random: // Roullet selection Random rand = new Random(); int total = config.single_expasion_quant; cur = 0; for (int i = 0; i < res.Count; i++) { check[i] = false; } while (total > 0) { if (allCheckded) { break; } if (cur >= res.Count) { cur = 0; } float t = (float)rand.NextDouble(); if (t >= config.single_expasion_quant_threshold) { check[cur] = true; if (!markers.ContainsKey(res[cur])) { markers.Add(res[cur], true); final_ret.Add(res[cur]); total--; } } cur++; allCheckded = true; for (int i = 0; i < res.Count; i++) { if (check[i] == false) { allCheckded = false; break; } } } break; case ExpansionGeneralPolitics.Weight: // Do nothing // Calculate edge weights from efficiency and surprise ComboNode node = new ComboNode(); foreach (String card in combo) { node.combo.Add(card, card); } total = config.single_expasion_quant; List <KeyValuePair <string, double> > card_creativity_list = new List <KeyValuePair <string, double> >(); String hs = ""; double localHighSurprise = 0.0; foreach (String s in res) { if (node.combo.ContainsKey(s)) { continue; } node.combo.Add(s, s); // Calculate efficiency Double[] comboArray; config.bayes.GenerateComboVector(ref node, out comboArray); Instance target = new Instance(comboArray); Double newWinrate = config.knn.getKNearestWinrates(target, 5); newWinrate /= 100; // Calculate surprise double[] surpriseVec; double surprise; config.bayes.CalculateSurprise(ref node, 1, out surpriseVec, out surprise, false); // Update globalSurprise if (surprise >= config.highestSurprise) { config.highestSurprise = surprise; } if (surprise >= localHighSurprise) { localHighSurprise = surprise; hs = s; } // Calculate criativity double creativityFactor = ((surprise / config.highestSurprise) + newWinrate) / 2; card_creativity_list.Add(new KeyValuePair <string, double>(s, creativityFactor)); node.combo.Remove(s); } // Sort creativity combo /*card_creativity_list.Sort(delegate(KeyValuePair<string, double> A, KeyValuePair<string, double> B) * { * if (A.Value < B.Value) * return 0; * return -1; * });*/ if (config.single_expasion_quant == 1) { if (!markers.ContainsKey(hs)) { final_ret.Add(hs); } } else { cur = 0; while (final_ret.Count < config.single_expasion_quant) { if (allCheckded) { break; } check[cur] = true; if (!markers.ContainsKey(card_creativity_list[cur].Key)) { markers.Add(card_creativity_list[cur].Key, true); final_ret.Add(card_creativity_list[cur].Key); } cur++; allCheckded = true; for (int i = 0; i < res.Count; i++) { if (check[i] == false) { allCheckded = false; break; } } } } break; } return(final_ret); }