public void NinjaFearLevelStringTests() { //Create our selector, indicating that we will be choosing between a set of strings. //This is fully generic and could be any type. var Selector = new WeightedSelector<string>(); var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. //As fear approaches 10, the monster is more likely to run. //Next we add our choices. The first parameter is the choice, the second is the weight. Selector.Add("Cast Heal", NinjaFearLevel); Selector.Add("Flee", NinjaFearLevel - 7); //Ninjas fight to the death... Usually. Selector.Add("Attack", 10 - NinjaFearLevel); //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). //This is where the magic happens. NinjaAction will be one of the choices we entered above. string NinjaAction = Selector.Select(); //This test is mostly for documentation, however this does have to be true: Assert.IsTrue(NinjaAction == "Cast Heal" || NinjaAction == "Flee" || NinjaAction == "Attack"); }
public void NinjaFearLevelStringTests() { //Create our selector, indicating that we will be choosing between a set of strings. //This is fully generic and could be any type. var Selector = new WeightedSelector <string>(); var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. //As fear approaches 10, the monster is more likely to run. //Next we add our choices. The first parameter is the choice, the second is the weight. Selector.Add("Cast Heal", NinjaFearLevel); Selector.Add("Flee", NinjaFearLevel - 7); //Ninjas fight to the death... Usually. Selector.Add("Attack", 10 - NinjaFearLevel); //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). //This is where the magic happens. NinjaAction will be one of the choices we entered above. string NinjaAction = Selector.Select(); //This test is mostly for documentation, however this does have to be true: Assert.IsTrue(NinjaAction == "Cast Heal" || NinjaAction == "Flee" || NinjaAction == "Attack"); }
private WeightedSelector <int> GetSelector() { var selector = new WeightedSelector <int>(_random); selector.Add(1, 0.48m); selector.Add(2, 0.38m); selector.Add(3, 0.09m); selector.Add(4, 0.05m); return(selector); }
private static WeightedSelector <int> GetSelector() { var selector = new WeightedSelector <int>(new TestRandom()); selector.Add(1, 0.48m); selector.Add(2, 0.38m); selector.Add(3, 0.09m); selector.Add(4, 0.05m); return(selector); }
public void Select([Values(1, 2)] int expected) { var selector = new WeightedSelector <int>(new Cheat.DebugRandom()); selector.Add(1, 0.5m); selector.Add(2, 0.5m); Assert.AreEqual(2, selector.Count); var result = selector.SelectV3(expected); Assert.AreEqual(expected, result.Count()); Assert.AreEqual(2 - expected, selector.Count); }
public void Probability_ExtremeInputs_Test() { var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); var Selector = new WeightedSelector <string>(); Selector.Add(Inputs); var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", Trials, Selector.ReadOnlyItems.Count, Selector.TotalWeight()); var ResultCounter = Helper.RunTrialsAndCountResults(); foreach (var Key in ResultCounter.Keys) { Helper.ExamineMetricsForKey(Key); } //Note that in this test, a ton of items will never be selected because there are so many. //If we did tens of millions of trials that would counter it, but it might take 30s+, which is //unecessary. Console.WriteLine("Expected {0} outputs, actual: {1}. Details: {2}", Inputs.Count, ResultCounter.Keys.Count, Helper.GetErrorMessage()); }
public static void Set(string id, DestinationsUpdate data) { WeightedSelector <DestinationsUpdate> ws; if (MemoryCache.Default.Contains(id)) { ws = Get(id); if (ws == null) { throw new NotImplementedException("Cache 'list' is NULL"); } // TODO: Fix bug where items can get added multiple times ws.Add(data, 1); } else { ws = new WeightedSelector <DestinationsUpdate>(); ws.Add(data, 1); } MemoryCache.Default.Set(new CacheItem(id, ws), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.MaxValue, Priority = CacheItemPriority.NotRemovable }); }
public void NinjaFearLevelTests() { var Selector = new WeightedSelector <MonsterAction>(); var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. //As fear approaches 10, the monster is more likely to run. var ActionCandidates = new List <WeightedItem <MonsterAction> >() { new WeightedItem <MonsterAction>(new MonsterAction("Cast Heal"), NinjaFearLevel), new WeightedItem <MonsterAction>(new MonsterAction("Flee"), NinjaFearLevel - 7), //Ninjas fight to the death... Usually. new WeightedItem <MonsterAction>(new MonsterAction("Attack"), 10 - NinjaFearLevel) }; //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). Selector.Add(ActionCandidates); var SelectedAction = Selector.Select(); //This test is mostly for documentation, however this does have to be true: Assert.IsTrue(SelectedAction.Name == "Cast Heal" || SelectedAction.Name == "Flee" || SelectedAction.Name == "Attack"); }
public void Probability_ExtremeInputs_Test() { var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); var Selector = new WeightedSelector<string>(); Selector.Add(Inputs); var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", Trials, Selector.ReadOnlyItems.Count, Selector.TotalWeight()); var ResultCounter = Helper.RunTrialsAndCountResults(); foreach (var Key in ResultCounter.Keys) Helper.ExamineMetricsForKey(Key); //Note that in this test, a ton of items will never be selected because there are so many. //If we did tens of millions of trials that would counter it, but it might take 30s+, which is //unecessary. Console.WriteLine("Expected {0} outputs, actual: {1}. Details: {2}", Inputs.Count, ResultCounter.Keys.Count, Helper.GetErrorMessage()); }
public void NinjaFearLevelTests() { var Selector = new WeightedSelector<MonsterAction>(); var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. //As fear approaches 10, the monster is more likely to run. var ActionCandidates = new List<WeightedItem<MonsterAction>>() { new WeightedItem<MonsterAction>(new MonsterAction("Cast Heal"), NinjaFearLevel), new WeightedItem<MonsterAction>(new MonsterAction("Flee"), NinjaFearLevel - 7), //Ninjas fight to the death... Usually. new WeightedItem<MonsterAction>(new MonsterAction("Attack"), 10 - NinjaFearLevel) }; //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). Selector.Add(ActionCandidates); var SelectedAction = Selector.Select(); //This test is mostly for documentation, however this does have to be true: Assert.IsTrue(SelectedAction.Name == "Cast Heal" || SelectedAction.Name == "Flee" || SelectedAction.Name == "Attack"); }
public void Probability_Simple_Test() { var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); var Selector = new WeightedSelector<string>(); Selector.Add(Inputs); var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", Trials, Selector.ReadOnlyItems.Count, Selector.TotalWeight()); var ResultCounter = Helper.RunTrialsAndCountResults(); foreach (var Key in ResultCounter.Keys) Helper.ExamineMetricsForKey(Key); Assert.IsTrue(ResultCounter.Keys.Count == Inputs.Count, string.Format("Expected {0} outputs, actual: {1}. Details: {2}", Inputs.Count, ResultCounter.Keys.Count, Helper.GetErrorMessage())); }
public static HashSet <int> SelectOption( EquipmentItemOptionSheet optionSheet, SkillSheet skillSheet, EquipmentItemSubRecipeSheet.Row subRecipe, IRandom random, Equipment equipment ) { var optionSelector = new WeightedSelector <EquipmentItemOptionSheet.Row>(random); var optionIds = new HashSet <int>(); // Skip sort subRecipe.Options because it had been already sorted in WeightedSelector.Select(); foreach (var optionInfo in subRecipe.Options) { if (!optionSheet.TryGetValue(optionInfo.Id, out var optionRow)) { continue; } optionSelector.Add(optionRow, optionInfo.Ratio); } IEnumerable <EquipmentItemOptionSheet.Row> optionRows = new EquipmentItemOptionSheet.Row[0]; try { optionRows = optionSelector.SelectV3(subRecipe.MaxOptionLimit); } catch (Exception e) when( e is InvalidCountException || e is ListEmptyException ) { return(optionIds); } finally { foreach (var optionRow in optionRows.OrderBy(r => r.Id)) { if (optionRow.StatType != StatType.NONE) { var statMap = GetStat(optionRow, random); equipment.StatsMap.AddStatAdditionalValue(statMap.StatType, statMap.Value); } else { var skill = GetSkill(optionRow, skillSheet, random); if (!(skill is null)) { equipment.Skills.Add(skill); } } optionIds.Add(optionRow.Id); } } return(optionIds); }
public void Probability_Simple_Test() { var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); var Selector = new WeightedSelector <string>(); Selector.Add(Inputs); var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", Trials, Selector.ReadOnlyItems.Count, Selector.TotalWeight()); var ResultCounter = Helper.RunTrialsAndCountResults(); foreach (var Key in ResultCounter.Keys) { Helper.ExamineMetricsForKey(Key); } Assert.IsTrue(ResultCounter.Keys.Count == Inputs.Count, string.Format("Expected {0} outputs, actual: {1}. Details: {2}", Inputs.Count, ResultCounter.Keys.Count, Helper.GetErrorMessage())); }
public void ReUseTest() { var Selector = new WeightedSelector<int>(); Selector.Add(1, 1); int Result1 = Selector.Select(); //There's only one choice - 1. It will always return. Assert.IsTrue(Result1 == 1); //Now re-use the same selector, but put an item in with so much weight that it will //always "win". Selector.Add(2, 5000000); //That's a heavy item. int Result2 = Selector.Select(); Assert.IsTrue(Selector.ReadOnlyItems.Count == 2); Assert.IsTrue(Result2 == 2); }
private WeightedSelector<string> BuildSelector() { var Selector = new WeightedSelector<string>(new SelectorOptions() { AllowDuplicates = false }); var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); Selector.Add(Inputs); return Selector; }
public void ReUseTest() { var Selector = new WeightedSelector <int>(); Selector.Add(1, 1); int Result1 = Selector.Select(); //There's only one choice - 1. It will always return. Assert.IsTrue(Result1 == 1); //Now re-use the same selector, but put an item in with so much weight that it will //always "win". Selector.Add(2, 5000000); //That's a heavy item. int Result2 = Selector.Select(); Assert.IsTrue(Selector.ReadOnlyItems.Count == 2); Assert.IsTrue(Result2 == 2); }
private WeightedSelector <string> BuildSelector() { var Selector = new WeightedSelector <string>(new SelectorOptions() { AllowDuplicates = false }); var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); Selector.Add(Inputs); return(Selector); }
//--------------------------------------------------------------------- // A helper function to shuffle a list of ActiveSties: Algorithm may be improved. // Sites are weighted for ignition in the Shuffle method, based on the respective inputs maps. // This function uses a fast open-source sort routine: 8/2019 private static WeightedSelector <ActiveSite> PreShuffleEther(ISiteVar <double> weightedSiteVar, out int numSites) { WeightedSelector <ActiveSite> wselector = new WeightedSelector <ActiveSite>(ModelCore); numSites = 0; foreach (ActiveSite site in PlugIn.ModelCore.Landscape.ActiveSites) { if (weightedSiteVar[site] > 0.0) { wselector.Add(site, ((int)weightedSiteVar[site])); numSites++; } } return(wselector); }
private Skill.Skill PostSelect(IRandom random, IEnumerable <Skill.Skill> skills) { var skillList = skills.ToList(); var defaultAttack = skillList.FirstOrDefault(x => x.SkillRow.Id == GameConfig.DefaultAttackId); if (defaultAttack == null) { throw new Exception("There is no default attack"); } if (skillList.Count == 1) // If there's only a default attack in skills { return(defaultAttack); } var sortedSkills = skillList .Where(x => x.SkillRow.Id != GameConfig.DefaultAttackId) .OrderBy(x => x.SkillRow.Id) .ToList(); var sumChance = sortedSkills.Sum(x => x.Chance); if (sumChance < 100 && sumChance <= random.Next(0, 100)) { return(defaultAttack); } var itemSelector = new WeightedSelector <Skill.Skill>(random); foreach (var skill in sortedSkills) { itemSelector.Add(skill, skill.Chance); } var selectedSkill = itemSelector.Select(1); return(selectedSkill.First()); }