├── Ether.WeightedSelector ├── WeightedItem.cs ├── Algorithm │ ├── SingleSelector.cs │ ├── BinarySearchOptimizer.cs │ ├── MultiSelector.cs │ └── SelectorBase.cs ├── SelectorOptions.cs ├── Ether.WeightedSelector.csproj ├── Extensions │ └── ExtensionMethods.cs └── WeightedSelector.cs ├── Ether.WeightedSelector.Tests ├── Ether.WeightedSelector.Tests.csproj ├── Helpers │ ├── InputBuilder.cs │ └── ProbabilityHelpers.cs ├── ReuseTests.cs ├── MultiSelect_NoDuplicates_Tests.cs ├── MultiSelect_WithAllowDuplicates_Tests.cs ├── NinjaStringTests.cs ├── NinjaTests.cs ├── Probability_Simple_Tests.cs ├── Probability_ExtremeWeight_Tests.cs └── Probability_ExtremeInputs_Tests.cs ├── license.txt ├── README.md ├── Ether.WeightedSelector.sln ├── WebEssentials-Settings.json └── .gitignore /Ether.WeightedSelector/WeightedItem.cs: -------------------------------------------------------------------------------- 1 | namespace Ether.WeightedSelector 2 | { 3 | public class WeightedItem 4 | { 5 | public int Weight; 6 | public readonly T Value; 7 | internal int CumulativeWeight; //used for binary chop/search. 8 | 9 | public WeightedItem(T value, int weight) 10 | { 11 | Value = value; 12 | Weight = weight; 13 | CumulativeWeight = 0; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Algorithm/SingleSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ether.WeightedSelector.Algorithm 4 | { 5 | internal class SingleSelector : SelectorBase 6 | { 7 | internal SingleSelector(WeightedSelector weightedSelector) : base(weightedSelector) 8 | { 9 | } 10 | 11 | internal T Select() 12 | { 13 | var Items = WeightedSelector.Items; 14 | 15 | if (Items.Count == 0) 16 | throw new InvalidOperationException("There were no items to select from."); 17 | 18 | return ExecuteSelect(Items).Value; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Ether.WeightedSelector.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Helpers/InputBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Ether.WeightedSelector.Tests.Helpers 5 | { 6 | internal class InputBuilder 7 | { 8 | public static List> CreateInputs(int minInputs, int maxInputs, int minWeight, int maxWeight) 9 | { 10 | var Gen = new Random(); 11 | var InputCount = Gen.Next(minInputs, maxInputs); 12 | var Result = new List>(); 13 | 14 | for (var i = 1; i <= InputCount; i++) 15 | { 16 | var Item = new WeightedItem(GetInputName(), 17 | Gen.Next(minWeight, maxWeight)); 18 | Result.Add(Item); 19 | } 20 | 21 | return Result; 22 | } 23 | 24 | private static string GetInputName() 25 | { 26 | return Guid.NewGuid().ToString(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Algorithm/BinarySearchOptimizer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Ether.WeightedSelector.Algorithm 7 | { 8 | public class BinarySearchOptimizer 9 | { 10 | public static int[] GetCumulativeWeights(List> items) 11 | { 12 | //For binary search, we do some setup ahead of time here... We need to build an array of 13 | //cumulative weights. So if our items had weights of: 3, 5, 3, 2 the array would be: 3, 8, 11, 13 14 | 15 | int RunningWeight = 0; 16 | int Index = 0; 17 | var ResultArray = new int[items.Count() + 1]; 18 | 19 | foreach (var Item in items) 20 | { 21 | RunningWeight += Item.Weight; 22 | ResultArray[Index] = RunningWeight; 23 | 24 | Index ++; 25 | } 26 | 27 | return ResultArray; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/SelectorOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Ether.WeightedSelector 7 | { 8 | public class SelectorOptions 9 | { 10 | /// 11 | /// This only impacts MultiSelect. Turning it on will allow result set to contain duplicates, otherwise 12 | /// we will never return the same item twice. 13 | /// 14 | public Boolean AllowDuplicates { get; set; } 15 | 16 | /// 17 | /// If this is false and you add an item with a weight of zero or less, that will throw an exception. If it's true, 18 | /// the item will just be ignored (not added). This is often convenient. 19 | /// 20 | public Boolean DropZeroWeightItems { get; set; } 21 | 22 | public SelectorOptions() 23 | { 24 | AllowDuplicates = false; 25 | DropZeroWeightItems = true; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/ReuseTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Tests.Helpers; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests 9 | { 10 | [TestClass] 11 | public class ReUseTests 12 | { 13 | 14 | [TestMethod] 15 | public void ReUseTest() 16 | { 17 | var Selector = new WeightedSelector(); 18 | Selector.Add(1, 1); 19 | int Result1 = Selector.Select(); 20 | 21 | //There's only one choice - 1. It will always return. 22 | Assert.IsTrue(Result1 == 1); 23 | 24 | //Now re-use the same selector, but put an item in with so much weight that it will 25 | //always "win". 26 | Selector.Add(2, 5000000); //That's a heavy item. 27 | int Result2 = Selector.Select(); 28 | 29 | Assert.IsTrue(Selector.ReadOnlyItems.Count == 2); 30 | Assert.IsTrue(Result2 == 2); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) [2014] [Brian MacKay] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What is WeightedSelector.NET? 2 | ============== 3 | 4 | WeightedSelector.NET is a .NET Standard 2 project (which means it should work most anywhere) that lets you assign weights to a set of choices, then make decisions based on each choice's proportion of the total weight. 5 | 6 | This is useful for scenarios where choices are made based on complicated but quantifiable factors, or where you need to choose between a number of reasonable choices in a way that appears semi-random. Great examples of the latter case include suggestion engines and game AI. 7 | 8 | WeightedSelector.NET's API is easy to pick up and fun to use. In one of the examples, we implement a game AI that decides between attacking, fleeing, and casting a heal spell based on a dynamic "fear" factor... In 6 lines of code! 9 | 10 | How can I get started? 11 | ============== 12 | 13 | Check out the getting started guide. 14 | 15 | Where can I get it? 16 | ============== 17 | 18 | First, install NuGet. Then, install WeightedSelector.NET from the package manager console: 19 | 20 | >PM> Install-Package Ether.WeightedSelector 21 | 22 | WeightedSelector.NET is Copyright © 2014 Brian MacKay, Kinetiq, and other contributors under the MIT license. 23 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Ether.WeightedSelector.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | true 6 | 1.2.0 7 | Brian MacKay 8 | Kinetiq 9 | https://github.com/kinetiq/Ether.WeightedSelector 10 | https://github.com/kinetiq/Ether.WeightedSelector 11 | WeightedSelector.NET lets you assign weights to a set of choices, then make intelligent decisions based on each choice's proportion of the total weight. 12 | 13 | This simple concept is useful for machine learning scenarios where choices need to be made based on dynamic factors. Great examples include suggestion engines and game AI. 14 | 15 | In one of the examples, we implement a game AI that decides between attacking, fleeing, and casting a heal spell based on a dynamic "fear" factor... In 6 lines of code. Free to use under MIT License. 16 | 2014-2019 17 | c# weighted selector selection machine learning ai decision decide 18 | Updated to .NET Standard 2 - thanks to Burton Rheutan for the contribution! 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/MultiSelect_NoDuplicates_Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Tests.Helpers; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests 9 | { 10 | [TestClass] 11 | public class MultiSelect_NoDuplicates_Tests 12 | { 13 | private const int Cycles = 100; 14 | private const int MinInputs = 10; 15 | private const int MaxInputs = 50; 16 | private const int MinWeight = 1; 17 | private const int MaxWeight = 3; 18 | 19 | [TestMethod] 20 | public void MultiSelect_NoDuplicates_Test() 21 | { 22 | 23 | for (var i = 0; i < Cycles; i++) 24 | { 25 | var Selector = BuildSelector(); 26 | 27 | var Rng = new Random(); 28 | var SelectionsRequested = Rng.Next(1, MinInputs); //Have to make sure we don't ask for more than we create. 29 | 30 | var SelectionList = Selector.SelectMultiple(SelectionsRequested); 31 | 32 | Assert.IsTrue(SelectionList.Count == SelectionsRequested); 33 | Assert.IsTrue(SelectionList.Distinct().Count() == SelectionsRequested); 34 | } 35 | } 36 | 37 | private WeightedSelector BuildSelector() 38 | { 39 | var Selector = new WeightedSelector(new SelectorOptions() { AllowDuplicates = false }); 40 | var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); 41 | Selector.Add(Inputs); 42 | 43 | return Selector; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Extensions/ExtensionMethods.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Ether.WeightedSelector.Extensions 7 | { 8 | public static class ExtensionMethods 9 | { 10 | public static int AverageWeight(this WeightedSelector selector) 11 | { 12 | return selector.Items.Count == 0 ? 0 : (int) selector.Items.Average(t => t.Weight); 13 | } 14 | 15 | public static int Count(this WeightedSelector selector) 16 | { 17 | return selector.Items.Count(); 18 | } 19 | 20 | public static int MaxWeight(this WeightedSelector selector) 21 | { 22 | return selector.Items.Count == 0 ? 0 : selector.Items.Max(t => t.Weight); 23 | } 24 | 25 | public static int MinWeight(this WeightedSelector selector) 26 | { 27 | return selector.Items.Count == 0 ? 0 : selector.Items.Min(t => t.Weight); 28 | } 29 | 30 | public static int TotalWeight(this WeightedSelector selector) 31 | { 32 | return selector.Items.Count == 0 ? 0 : selector.Items.Sum(t => t.Weight); 33 | } 34 | 35 | #region "Sorting" 36 | public static List> ListByWeightDescending(this WeightedSelector selector) 37 | { 38 | var Result = (from Item in selector.Items 39 | orderby Item.Weight descending 40 | select Item).ToList(); 41 | 42 | return Result; 43 | } 44 | 45 | public static List> ListByWeightAscending(this WeightedSelector selector) 46 | { 47 | var Result = (from Item in selector.Items 48 | orderby Item.Weight ascending 49 | select Item).ToList(); 50 | 51 | return Result; 52 | } 53 | #endregion 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/MultiSelect_WithAllowDuplicates_Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Tests.Helpers; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests 9 | { 10 | [TestClass] 11 | public class MultiSelectWithAllowDuplicatesTests 12 | { 13 | //Tests multiple selection using randomized parameters. 14 | 15 | private const int Cycles = 100; 16 | private const int MinInputs = 10; 17 | private const int MaxInputs = 50; 18 | private const int MinWeight = 1; 19 | private const int MaxWeight = 3; 20 | 21 | [TestMethod] 22 | public void MultiSelect_WithAllowDuplicates_Test() 23 | { 24 | 25 | for (var i = 0; i < Cycles; i++) 26 | { 27 | var Selector = BuildSelector(); 28 | 29 | var Rnd = new Random(); 30 | var SelectionsRequested = Rnd.Next(MaxInputs, MaxInputs * 2); //Ask for way more results than we have items, 31 | //to make the point that items aren't being removed as we select them. 32 | 33 | var SelectionList = Selector.SelectMultiple(SelectionsRequested); 34 | 35 | Assert.IsTrue(SelectionList.Count == SelectionsRequested); //We have as many selections as we requested. 36 | Assert.IsTrue(Selector.ReadOnlyItems.Count < SelectionsRequested); //We have more selections than we have source items (due to duplicates). 37 | } 38 | } 39 | 40 | private WeightedSelector BuildSelector() 41 | { 42 | var Selector = new WeightedSelector(new SelectorOptions() { AllowDuplicates = true }); 43 | var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); 44 | Selector.Add(Inputs); 45 | 46 | return Selector; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/NinjaStringTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Tests.Helpers; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests 9 | { 10 | [TestClass] 11 | public class NinjaStringTests 12 | { 13 | 14 | [TestMethod] 15 | public void NinjaFearLevelStringTests() 16 | { 17 | //Create our selector, indicating that we will be choosing between a set of strings. 18 | //This is fully generic and could be any type. 19 | var Selector = new WeightedSelector(); 20 | 21 | var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. 22 | //As fear approaches 10, the monster is more likely to run. 23 | 24 | //Next we add our choices. The first parameter is the choice, the second is the weight. 25 | Selector.Add("Cast Heal", NinjaFearLevel); 26 | Selector.Add("Flee", NinjaFearLevel - 7); //Ninjas fight to the death... Usually. 27 | Selector.Add("Attack", 10 - NinjaFearLevel); 28 | 29 | //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). 30 | //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). 31 | //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). 32 | 33 | //This is where the magic happens. NinjaAction will be one of the choices we entered above. 34 | string NinjaAction = Selector.Select(); 35 | 36 | //This test is mostly for documentation, however this does have to be true: 37 | Assert.IsTrue(NinjaAction == "Cast Heal" || 38 | NinjaAction == "Flee" || 39 | NinjaAction == "Attack"); 40 | } 41 | 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Algorithm/MultiSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Ether.WeightedSelector.Algorithm 5 | { 6 | internal class MultiSelector : SelectorBase 7 | { 8 | 9 | internal MultiSelector(WeightedSelector weightedSelector) : base(weightedSelector) 10 | { 11 | } 12 | 13 | internal List Select(int count) 14 | { 15 | Validate(count); 16 | 17 | //Create a shallow clone of the our items, because we're going to be removing 18 | //items from the list in some cases, and we want to preserve the original. 19 | var Items = new List>(WeightedSelector.Items); 20 | var ResultList = new List(); 21 | 22 | do 23 | { 24 | WeightedItem Item = null; 25 | 26 | if (WeightedSelector.Options.AllowDuplicates) 27 | Item = ExecuteSelect(Items); //Use binary search if we can. 28 | else 29 | //Force linear search, since AllowDuplicates currently breaks binary search. 30 | Item = ExecuteSelectWithLinearSearch(Items); 31 | 32 | 33 | ResultList.Add(Item.Value); 34 | 35 | if (!WeightedSelector.Options.AllowDuplicates) 36 | Items.Remove(Item); 37 | 38 | } while (ResultList.Count < count); 39 | 40 | return ResultList; 41 | } 42 | 43 | private void Validate(int count) 44 | { 45 | 46 | if (count <= 0) 47 | throw new InvalidOperationException("Count must be > 0."); 48 | 49 | var Items = WeightedSelector.Items; 50 | 51 | if (Items.Count == 0) 52 | throw new InvalidOperationException("There were no items to select from."); 53 | 54 | if (!WeightedSelector.Options.AllowDuplicates && Items.Count < count) 55 | throw new InvalidOperationException("There aren't enough items in the collection to take " + count); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/NinjaTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Tests.Helpers; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests 9 | { 10 | [TestClass] 11 | public class NinjaTests 12 | { 13 | 14 | [TestMethod] 15 | public void NinjaFearLevelTests() 16 | { 17 | var Selector = new WeightedSelector(); 18 | 19 | var NinjaFearLevel = 5; //A value from 0 to 10, where 10 is the most afraid. 20 | //As fear approaches 10, the monster is more likely to run. 21 | 22 | var ActionCandidates = new List>() 23 | { 24 | new WeightedItem(new MonsterAction("Cast Heal"), NinjaFearLevel), 25 | new WeightedItem(new MonsterAction("Flee"), NinjaFearLevel - 7), //Ninjas fight to the death... Usually. 26 | new WeightedItem(new MonsterAction("Attack"), 10 - NinjaFearLevel) 27 | }; 28 | 29 | //So, if fear is 0, ninja will not cast heal (0) and will not flee (-7), he will always attack (10). 30 | //If fear is 5, ninja might cast heal (5/50%) and will never flee (-2). He might attack (5/50%). 31 | //If fear is 10, ninja will probably cast heal (10/76%) and might flee (3/23%). He's too afraid to attack (0/0%). 32 | 33 | Selector.Add(ActionCandidates); 34 | 35 | var SelectedAction = Selector.Select(); 36 | 37 | //This test is mostly for documentation, however this does have to be true: 38 | Assert.IsTrue(SelectedAction.Name == "Cast Heal" || 39 | SelectedAction.Name == "Flee" || 40 | SelectedAction.Name == "Attack"); 41 | } 42 | 43 | 44 | } 45 | 46 | public class MonsterAction 47 | { 48 | public string Name = string.Empty; 49 | 50 | public MonsterAction(string name) 51 | { 52 | this.Name = name; 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Probability_Simple_Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Extensions; 6 | using Ether.WeightedSelector.Tests.Helpers; 7 | using Microsoft.VisualStudio.TestTools.UnitTesting; 8 | 9 | namespace Ether.WeightedSelector.Tests 10 | { 11 | [TestClass] 12 | public class ProbabilitySimpleTests 13 | { 14 | //This test runs a million trials with randomized parameters, and tests to make sure the 15 | //distribution of selections is somewhat close to the weight proportions. For instance, 16 | //if one item's weight makes up 33% of a test's total weight, after a million tests it 17 | //should be selected about 33% of the time. 18 | 19 | private const int Trials = 1000000; //We do a lot of tests. 20 | 21 | //AcceptableDeviation gives us some margin for small statistical anomolies. For instance, 22 | //given the example above, maybe the item is selected 34% or even 37% of the time. That's an acceptable 23 | //abnormality. We could close the gap by running even more tests. 24 | private const int AcceptableDeviation = 4; 25 | 26 | //Range of weighted items going in. 27 | private const int MinInputs = 2; 28 | private const int MaxInputs = 4; 29 | 30 | //Range of weights each item can have. 31 | private const int MinWeight = 1; 32 | private const int MaxWeight = 3; 33 | 34 | [TestMethod] 35 | public void Probability_Simple_Test() 36 | { 37 | var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); 38 | var Selector = new WeightedSelector(); 39 | Selector.Add(Inputs); 40 | 41 | var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); 42 | 43 | 44 | Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", 45 | Trials, 46 | Selector.ReadOnlyItems.Count, 47 | Selector.TotalWeight()); 48 | 49 | var ResultCounter = Helper.RunTrialsAndCountResults(); 50 | 51 | foreach (var Key in ResultCounter.Keys) 52 | Helper.ExamineMetricsForKey(Key); 53 | 54 | Assert.IsTrue(ResultCounter.Keys.Count == Inputs.Count, 55 | string.Format("Expected {0} outputs, actual: {1}. Details: {2}", 56 | Inputs.Count, 57 | ResultCounter.Keys.Count, 58 | Helper.GetErrorMessage())); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Probability_ExtremeWeight_Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Extensions; 6 | using Ether.WeightedSelector.Tests.Helpers; 7 | using Microsoft.VisualStudio.TestTools.UnitTesting; 8 | 9 | namespace Ether.WeightedSelector.Tests 10 | { 11 | [TestClass] 12 | public class ProbabilityExtremeWeightTests 13 | { 14 | //This test runs a million trials with randomized parameters, and tests to make sure the 15 | //distribution of selections is somewhat close to the weight proportions. For instance, 16 | //if one item's weight makes up 33% of a test's total weight, after a million tests it 17 | //should be selected about 33% of the time. 18 | 19 | private const int Trials = 1000000; //We do a million tests. 20 | 21 | //AcceptableDeviation gives us some margin for small statistical anomolies. For instance, 22 | //given the example above, maybe the item is selected 34% or even 35% of the time. That's an acceptable 23 | //abnormality. We could close the gap by running even more tests. 24 | private const int AcceptableDeviation = 5; //turned deviation up a bit because of the vast range of weights. 25 | 26 | //Range of weighted items going in. 27 | private const int MinInputs = 3; 28 | private const int MaxInputs = 4; 29 | 30 | //Range of weights each item can have. 31 | private const int MinWeight = 10000; 32 | private const int MaxWeight = 1000000; 33 | 34 | [TestMethod] 35 | public void Probability_ExtremeWeight_Test() 36 | { 37 | var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); 38 | var Selector = new WeightedSelector(); 39 | Selector.Add(Inputs); 40 | 41 | var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); 42 | 43 | 44 | Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", 45 | Trials, 46 | Selector.ReadOnlyItems.Count, 47 | Selector.TotalWeight()); 48 | 49 | var ResultCounter = Helper.RunTrialsAndCountResults(); 50 | 51 | foreach (var Key in ResultCounter.Keys) 52 | Helper.ExamineMetricsForKey(Key); 53 | 54 | Assert.IsTrue(ResultCounter.Keys.Count == Inputs.Count, 55 | string.Format("Expected {0} outputs, actual: {1}. Details: {2}", 56 | Inputs.Count, 57 | ResultCounter.Keys.Count, 58 | Helper.GetErrorMessage())); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26124.0 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ether.WeightedSelector", "Ether.WeightedSelector\Ether.WeightedSelector.csproj", "{8A5C83B2-B2F3-4019-8998-1CA38FBED88F}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ether.WeightedSelector.Tests", "Ether.WeightedSelector.Tests\Ether.WeightedSelector.Tests.csproj", "{4B897D9D-A476-4A86-812F-0C259DE49E7A}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Debug|x64 = Debug|x64 14 | Debug|x86 = Debug|x86 15 | Release|Any CPU = Release|Any CPU 16 | Release|x64 = Release|x64 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|x64.ActiveCfg = Debug|Any CPU 26 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|x64.Build.0 = Debug|Any CPU 27 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|x86.ActiveCfg = Debug|Any CPU 28 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Debug|x86.Build.0 = Debug|Any CPU 29 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|x64.ActiveCfg = Release|Any CPU 32 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|x64.Build.0 = Release|Any CPU 33 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|x86.ActiveCfg = Release|Any CPU 34 | {8A5C83B2-B2F3-4019-8998-1CA38FBED88F}.Release|x86.Build.0 = Release|Any CPU 35 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|x64.ActiveCfg = Debug|Any CPU 38 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|x64.Build.0 = Debug|Any CPU 39 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|x86.ActiveCfg = Debug|Any CPU 40 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Debug|x86.Build.0 = Debug|Any CPU 41 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|x64.ActiveCfg = Release|Any CPU 44 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|x64.Build.0 = Release|Any CPU 45 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|x86.ActiveCfg = Release|Any CPU 46 | {4B897D9D-A476-4A86-812F-0C259DE49E7A}.Release|x86.Build.0 = Release|Any CPU 47 | EndGlobalSection 48 | EndGlobal 49 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Probability_ExtremeInputs_Tests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Extensions; 6 | using Ether.WeightedSelector.Tests.Helpers; 7 | using Microsoft.VisualStudio.TestTools.UnitTesting; 8 | 9 | namespace Ether.WeightedSelector.Tests 10 | { 11 | [TestClass] 12 | public class ProbabilityExtremeInputsTests 13 | { 14 | //This test runs a million trials with randomized parameters, and tests to make sure the 15 | //distribution of selections is somewhat close to the weight proportions. For instance, 16 | //if one item's weight makes up 33% of a test's total weight, after a million tests it 17 | //should be selected about 33% of the time. 18 | 19 | private const int Trials = 50000; //Fewer tests on this one; extreme inputs is time consuming. 20 | 21 | //AcceptableDeviation gives us some margin for small statistical anomolies. For instance, 22 | //given the example above, maybe the item is selected 34% or even 35% of the time. That's an acceptable 23 | //abnormality. We could close the gap by running even more tests. 24 | private const int AcceptableDeviation = 4; 25 | 26 | //Range of weighted items going in. 27 | private const int MinInputs = 2000; 28 | private const int MaxInputs = 3000; 29 | 30 | //Range of weights each item can have. 31 | private const int MinWeight = 3; 32 | private const int MaxWeight = 6; 33 | 34 | [TestMethod] 35 | public void Probability_ExtremeInputs_Test() 36 | { 37 | var Inputs = InputBuilder.CreateInputs(MinInputs, MaxInputs, MinWeight, MaxWeight); 38 | var Selector = new WeightedSelector(); 39 | Selector.Add(Inputs); 40 | 41 | var Helper = new ProbabilityHelpers(Selector, Inputs, Trials, AcceptableDeviation); 42 | 43 | Console.WriteLine("Running {0} trials with {1} items (total weight: {2})", 44 | Trials, 45 | Selector.ReadOnlyItems.Count, 46 | Selector.TotalWeight()); 47 | 48 | var ResultCounter = Helper.RunTrialsAndCountResults(); 49 | 50 | foreach (var Key in ResultCounter.Keys) 51 | Helper.ExamineMetricsForKey(Key); 52 | 53 | //Note that in this test, a ton of items will never be selected because there are so many. 54 | //If we did tens of millions of trials that would counter it, but it might take 30s+, which is 55 | //unecessary. 56 | Console.WriteLine("Expected {0} outputs, actual: {1}. Details: {2}", 57 | Inputs.Count, 58 | ResultCounter.Keys.Count, 59 | Helper.GetErrorMessage()); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/Algorithm/SelectorBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Ether.WeightedSelector.Extensions; 5 | 6 | namespace Ether.WeightedSelector.Algorithm 7 | { 8 | internal abstract class SelectorBase 9 | { 10 | protected readonly WeightedSelector WeightedSelector; 11 | private readonly Random Rng; 12 | 13 | internal SelectorBase(WeightedSelector weightedSelector) 14 | { 15 | WeightedSelector = weightedSelector; 16 | Rng = new Random(); 17 | } 18 | 19 | protected int GetSeed(List> items) 20 | { 21 | var TopRange = items.Sum(i => i.Weight) + 1; 22 | return Rng.Next(1, TopRange); 23 | } 24 | 25 | /// 26 | /// Execute selection using the binary search algorithm, which is the fastest way. 27 | /// 28 | protected WeightedItem ExecuteSelect(List> items) 29 | { 30 | if (items.Count == 0) 31 | throw new InvalidOperationException("Tried to do a select, but WeightedItems was emtpy."); 32 | 33 | //Choose an item based on each item's proportion of the total weight. 34 | var Seed = GetSeed(items); 35 | 36 | return BinarySearch(items, Seed); 37 | } 38 | 39 | /// 40 | /// Select, and force the slower Linear Search algorithm. 41 | /// 42 | protected WeightedItem ExecuteSelectWithLinearSearch(List> items) 43 | { 44 | //Note that this is only really useful for multiselect with !allowduplicates, which removes 45 | //items from the list as it goes. Just haven't put the effort into getting binary search to work 46 | //in those conditions. 47 | 48 | if (items.Count == 0) 49 | throw new InvalidOperationException("Tried to do a select, but WeightedItems was emtpy."); 50 | 51 | //Choose an item based on each item's proportion of the total weight. 52 | var Seed = GetSeed(items); 53 | 54 | return LinearSearch(items, Seed); 55 | } 56 | 57 | 58 | private WeightedItem LinearSearch(IEnumerable> items, int seed) 59 | { 60 | var RunningCount = 0; 61 | 62 | foreach (var Item in items) 63 | { 64 | RunningCount += Item.Weight; 65 | 66 | if (seed <= RunningCount) 67 | return Item; 68 | } 69 | 70 | throw new InvalidOperationException("There was no result during SimpleSearch. This should never happen."); 71 | } 72 | 73 | private WeightedItem BinarySearch(List> items, int seed) 74 | { 75 | int Index = Array.BinarySearch(WeightedSelector.CumulativeWeights, seed); 76 | 77 | //If there's a near match, IE our array is (1, 5, 9) and we search for 3, BinarySearch 78 | //returns a negative number that is one less than the first index great than our search. 79 | if (Index < 0) 80 | Index = (Index*-1) - 1; 81 | 82 | return items[Index]; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Ether.WeightedSelector/WeightedSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.ObjectModel; 4 | using System.Runtime.InteropServices; 5 | using Ether.WeightedSelector.Algorithm; 6 | 7 | namespace Ether.WeightedSelector 8 | { 9 | public class WeightedSelector 10 | { 11 | internal readonly List> Items = new List>(); 12 | public readonly SelectorOptions Options; 13 | 14 | internal int[] CumulativeWeights = null; //used for binary search. 15 | private Boolean IsCumulativeWeightsStale; //forces recalc of CumulativeWeights any time our list of WeightedItems changes. 16 | 17 | public WeightedSelector(SelectorOptions options = null) 18 | { 19 | if (options == null) 20 | options = new SelectorOptions(); 21 | 22 | this.Options = options; 23 | IsCumulativeWeightsStale = false; 24 | } 25 | 26 | #region "Add/Remove" 27 | public void Add(WeightedItem item) 28 | { 29 | if (item.Weight <= 0) 30 | { 31 | if (Options.DropZeroWeightItems) 32 | return; //"drop" the item, that is don't add it. 33 | else 34 | throw new InvalidOperationException("Scores must be => 0."); 35 | } 36 | 37 | IsCumulativeWeightsStale = true; 38 | Items.Add(item); 39 | } 40 | 41 | public void Add(IEnumerable> items) 42 | { 43 | foreach (var Item in items) 44 | { 45 | this.Add(Item); 46 | } 47 | } 48 | 49 | public void Add(T item, int weight) 50 | { 51 | this.Add(new WeightedItem(item, weight)); 52 | } 53 | 54 | public void Remove(WeightedItem item) 55 | { 56 | IsCumulativeWeightsStale = true; 57 | Items.Remove(item); 58 | } 59 | #endregion 60 | 61 | #region "Selection API" 62 | 63 | /// 64 | /// Execute the selection algorithm, returning one result. 65 | /// 66 | public T Select() 67 | { 68 | CalculateCumulativeWeights(); 69 | 70 | var Selector = new SingleSelector(this); 71 | return Selector.Select(); 72 | } 73 | 74 | /// 75 | /// Execute the selection algorithm, returning multiple results. 76 | /// 77 | public List SelectMultiple(int count) 78 | { 79 | CalculateCumulativeWeights(); 80 | 81 | var Selector = new MultiSelector(this); 82 | return Selector.Select(count); 83 | } 84 | 85 | private void CalculateCumulativeWeights() 86 | { 87 | if (!IsCumulativeWeightsStale) //If it's not stale, we can skip this! 88 | return; 89 | 90 | IsCumulativeWeightsStale = false; 91 | CumulativeWeights = BinarySearchOptimizer.GetCumulativeWeights(Items); 92 | } 93 | #endregion 94 | 95 | #region "Public Properties" 96 | 97 | /// 98 | /// Read-only collection of WeightedItems. 99 | /// 100 | public ReadOnlyCollection> ReadOnlyItems 101 | { 102 | get { return new ReadOnlyCollection>(this.Items); } 103 | } 104 | 105 | #endregion 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Ether.WeightedSelector.Tests/Helpers/ProbabilityHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Ether.WeightedSelector.Extensions; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | 8 | namespace Ether.WeightedSelector.Tests.Helpers 9 | { 10 | class ProbabilityHelpers 11 | { 12 | private Dictionary ResultCounter; 13 | private readonly List> Inputs; 14 | private readonly WeightedSelector Selector; 15 | private readonly int Trials; 16 | private readonly int AcceptableDeviation; 17 | 18 | public ProbabilityHelpers(WeightedSelector selector, 19 | List> inputs, 20 | int trials, 21 | int acceptableDeviation) 22 | { 23 | this.Selector = selector; 24 | this.Inputs = inputs; 25 | this.Trials = trials; 26 | this.AcceptableDeviation = acceptableDeviation; 27 | } 28 | 29 | public Dictionary RunTrialsAndCountResults() 30 | { 31 | //Do [trials] selections, and dump the number of hits for each item into a dictionary. 32 | var Results = new Dictionary(); 33 | 34 | for (var i = 0; i < Trials; i++) 35 | { 36 | string Decision = Selector.Select(); 37 | 38 | if (!Results.ContainsKey(Decision)) 39 | Results[Decision] = 1; 40 | else 41 | Results[Decision] += 1; 42 | } 43 | 44 | this.ResultCounter = Results; 45 | 46 | return Results; 47 | } 48 | 49 | public void ExamineMetricsForKey(string key) 50 | { 51 | decimal WeightProportion = GetWeightProportion(key); 52 | decimal SelectionProportion = GetSelectionProportion(key); 53 | 54 | Console.WriteLine("{0}", key); 55 | Console.WriteLine(" Hits: {0} ({3}% of total) Weight {1} ({2}% of total)", 56 | ResultCounter[key], 57 | GetWeight(key), 58 | Math.Round(WeightProportion, 3), 59 | Math.Round(SelectionProportion, 3)); 60 | 61 | Assert.IsTrue(WeightProportion >= (SelectionProportion - AcceptableDeviation) && 62 | WeightProportion <= (SelectionProportion + AcceptableDeviation), 63 | string.Format("Expected between {0}% and {1}%. Actual: {2}%", SelectionProportion - AcceptableDeviation, 64 | SelectionProportion + AcceptableDeviation, 65 | WeightProportion)); 66 | } 67 | 68 | private decimal GetWeightProportion(string key) 69 | { 70 | //What % of the total weight does this key's weight represent? 71 | return ((decimal)ResultCounter[key] / Trials) * 100; 72 | } 73 | 74 | private decimal GetSelectionProportion(string key) 75 | { 76 | //Over all of our tests, how many times did we select this key? 77 | var Total = Selector.TotalWeight(); 78 | 79 | var Item = (from WeightedItem W in Selector.ReadOnlyItems 80 | where W.Value == key 81 | select W).First(); 82 | 83 | return ((decimal)Item.Weight / Total) * 100; 84 | } 85 | 86 | private decimal GetWeight(string key) 87 | { 88 | var Item = (from WeightedItem W in Selector.ReadOnlyItems 89 | where W.Value == key 90 | select W).First(); 91 | 92 | return Item.Weight; 93 | } 94 | 95 | 96 | public string GetErrorMessage() 97 | { 98 | //If an item didn't generate any hits at all, it won't show up in ResultCounter. This grabs some details. 99 | var Builder = new StringBuilder(); 100 | 101 | foreach (var Key in Inputs) 102 | { 103 | if (!ResultCounter.ContainsKey(Key.Value)) 104 | Builder.AppendLine(string.Format("Missing {0}, Weight: {1}", Key.Value, Key.Weight)); 105 | } 106 | 107 | return Builder.ToString(); 108 | } 109 | 110 | 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /WebEssentials-Settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "BrowserLink": { 3 | "CssIgnorePatterns": "bootstrap*; reset.css; normalize.css; jquery*; toastr*; foundation*; animate*; inuit*; elements*; ratchet*; hint*; flat-ui*; 960*; skeleton*", 4 | "EnableMenu": true, 5 | "EnablePixelPushing": true, 6 | "ShowMenu": false 7 | }, 8 | "CodeGen": { 9 | "AddTypeScriptReferencePath": true, 10 | "CamelCaseEnumerationValues": false, 11 | "CamelCasePropertyNames": false, 12 | "CamelCaseTypeNames": false 13 | }, 14 | "CoffeeScript": { 15 | "CompileOnBuild": false, 16 | "CompileOnSave": true, 17 | "GenerateSourceMaps": false, 18 | "LintOnBuild": false, 19 | "LintOnSave": true, 20 | "LintResultLocation": "Message", 21 | "MinifyInPlace": false, 22 | "OutputDirectory": null, 23 | "ProcessSourceMapsForEditorEnhancements": true, 24 | "ShowPreviewPane": true, 25 | "WrapClosure": true 26 | }, 27 | "Css": { 28 | "AdjustRelativePaths": true, 29 | "AutoMinify": true, 30 | "Autoprefix": false, 31 | "AutoprefixerBrowsers": null, 32 | "GzipMinifiedFiles": false, 33 | "MakeMinified": true, 34 | "OutputDirectory": null, 35 | "RunOnBuild": false, 36 | "ShowBrowserTooltip": true, 37 | "ShowInitialInherit": false, 38 | "ShowUnsupported": true, 39 | "SyncBase64ImageValues": true, 40 | "SyncVendorValues": true, 41 | "ValidateEmbedImages": false, 42 | "ValidateStarSelector": false, 43 | "ValidateVendorSpecifics": false, 44 | "ValidateZeroUnit": false, 45 | "ValidationLocation": "Messages" 46 | }, 47 | "General": { 48 | "AllMessagesToOutputWindow": false, 49 | "KeepImportantComments": false, 50 | "SvgPreviewPane": true 51 | }, 52 | "Html": { 53 | "AutoMinify": true, 54 | "EnableAngularValidation": true, 55 | "EnableBootstrapValidation": true, 56 | "EnableEnterFormat": true, 57 | "EnableFoundationValidation": true, 58 | "GzipMinifiedFiles": false, 59 | "ImageDropFormats": [ 60 | { 61 | "HtmlFormat": "\"\"", 62 | "Name": "Simple Image Tag" 63 | }, 64 | { 65 | "HtmlFormat": "
\"\"
", 66 | "Name": "Enclosed in Div" 67 | }, 68 | { 69 | "HtmlFormat": "
  • \"\"
  • ", 70 | "Name": "Enclosed as List Item" 71 | }, 72 | { 73 | "HtmlFormat": "
    ", 74 | "Name": "Inline CSS" 75 | } 76 | ], 77 | "MakeMinified": true, 78 | "OutputDirectory": null, 79 | "RunOnBuild": false 80 | }, 81 | "JavaScript": { 82 | "AutoMinify": true, 83 | "BlockCommentCompletion": false, 84 | "GenerateSourceMaps": true, 85 | "GzipMinifiedFiles": false, 86 | "LintOnBuild": false, 87 | "LintOnSave": true, 88 | "LintResultLocation": "Message", 89 | "MakeMinified": true, 90 | "OutputDirectory": null, 91 | "RunOnBuild": false 92 | }, 93 | "Less": { 94 | "CompileOnBuild": false, 95 | "CompileOnSave": true, 96 | "EnableChainCompilation": true, 97 | "GenerateSourceMaps": false, 98 | "MinifyInPlace": false, 99 | "OutputDirectory": null, 100 | "ProcessSourceMapsForEditorEnhancements": true, 101 | "ShowPreviewPane": true, 102 | "StrictMath": false 103 | }, 104 | "LiveScript": { 105 | "CompileOnBuild": false, 106 | "CompileOnSave": true, 107 | "GenerateSourceMaps": true, 108 | "MinifyInPlace": false, 109 | "OutputDirectory": null, 110 | "ProcessSourceMapsForEditorEnhancements": true, 111 | "ShowPreviewPane": true, 112 | "WrapClosure": true 113 | }, 114 | "Markdown": { 115 | "AutoHyperlink": true, 116 | "AutoNewLines": true, 117 | "CompileOnBuild": false, 118 | "CompileOnSave": true, 119 | "EncodeProblemUrlCharacters": true, 120 | "GenerateXHTML": true, 121 | "LinkEmails": true, 122 | "MinifyInPlace": false, 123 | "OutputDirectory": null, 124 | "ShowPreviewPane": true, 125 | "StrictBoldItalic": true 126 | }, 127 | "Scss": { 128 | "CompileOnBuild": false, 129 | "CompileOnSave": true, 130 | "EnableChainCompilation": true, 131 | "GenerateSourceMaps": true, 132 | "MinifyInPlace": false, 133 | "OutputDirectory": null, 134 | "OutputStyle": "Expanded", 135 | "ProcessSourceMapsForEditorEnhancements": true, 136 | "ShowPreviewPane": true 137 | }, 138 | "Sprite": { 139 | "CssOutputDirectory": null, 140 | "IsVertical": true, 141 | "LessOutputDirectory": null, 142 | "Optimize": true, 143 | "RunOnBuild": false, 144 | "ScssOutputDirectory": null, 145 | "UseAbsoluteUrl": false, 146 | "UseFullPathForIdentifierName": true 147 | }, 148 | "SweetJs": { 149 | "CompileOnBuild": false, 150 | "CompileOnSave": true, 151 | "GenerateSourceMaps": true, 152 | "MinifyInPlace": false, 153 | "OutputDirectory": null, 154 | "ProcessSourceMapsForEditorEnhancements": true, 155 | "ShowPreviewPane": true 156 | }, 157 | "TypeScript": { 158 | "LintOnBuild": false, 159 | "LintOnSave": false, 160 | "LintResultLocation": "Message", 161 | "ShowPreviewPane": false 162 | } 163 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | 33 | # Visual Studio 2015/2017 cache/options directory 34 | .vs/ 35 | # Uncomment if you have tasks that create the project's static files in wwwroot 36 | #wwwroot/ 37 | 38 | # Visual Studio 2017 auto generated files 39 | Generated\ Files/ 40 | 41 | # MSTest test Results 42 | [Tt]est[Rr]esult*/ 43 | [Bb]uild[Ll]og.* 44 | 45 | # NUNIT 46 | *.VisualState.xml 47 | TestResult.xml 48 | 49 | # Build Results of an ATL Project 50 | [Dd]ebugPS/ 51 | [Rr]eleasePS/ 52 | dlldata.c 53 | 54 | # Benchmark Results 55 | BenchmarkDotNet.Artifacts/ 56 | 57 | # .NET Core 58 | project.lock.json 59 | project.fragment.lock.json 60 | artifacts/ 61 | 62 | # StyleCop 63 | StyleCopReport.xml 64 | 65 | # Files built by Visual Studio 66 | *_i.c 67 | *_p.c 68 | *_h.h 69 | *.ilk 70 | *.meta 71 | *.obj 72 | *.iobj 73 | *.pch 74 | *.pdb 75 | *.ipdb 76 | *.pgc 77 | *.pgd 78 | *.rsp 79 | *.sbr 80 | *.tlb 81 | *.tli 82 | *.tlh 83 | *.tmp 84 | *.tmp_proj 85 | *_wpftmp.csproj 86 | *.log 87 | *.vspscc 88 | *.vssscc 89 | .builds 90 | *.pidb 91 | *.svclog 92 | *.scc 93 | 94 | # Chutzpah Test files 95 | _Chutzpah* 96 | 97 | # Visual C++ cache files 98 | ipch/ 99 | *.aps 100 | *.ncb 101 | *.opendb 102 | *.opensdf 103 | *.sdf 104 | *.cachefile 105 | *.VC.db 106 | *.VC.VC.opendb 107 | 108 | # Visual Studio profiler 109 | *.psess 110 | *.vsp 111 | *.vspx 112 | *.sap 113 | 114 | # Visual Studio Trace Files 115 | *.e2e 116 | 117 | # TFS 2012 Local Workspace 118 | $tf/ 119 | 120 | # Guidance Automation Toolkit 121 | *.gpState 122 | 123 | # ReSharper is a .NET coding add-in 124 | _ReSharper*/ 125 | *.[Rr]e[Ss]harper 126 | *.DotSettings.user 127 | 128 | # JustCode is a .NET coding add-in 129 | .JustCode 130 | 131 | # TeamCity is a build add-in 132 | _TeamCity* 133 | 134 | # DotCover is a Code Coverage Tool 135 | *.dotCover 136 | 137 | # AxoCover is a Code Coverage Tool 138 | .axoCover/* 139 | !.axoCover/settings.json 140 | 141 | # Visual Studio code coverage results 142 | *.coverage 143 | *.coveragexml 144 | 145 | # NCrunch 146 | _NCrunch_* 147 | .*crunch*.local.xml 148 | nCrunchTemp_* 149 | 150 | # MightyMoose 151 | *.mm.* 152 | AutoTest.Net/ 153 | 154 | # Web workbench (sass) 155 | .sass-cache/ 156 | 157 | # Installshield output folder 158 | [Ee]xpress/ 159 | 160 | # DocProject is a documentation generator add-in 161 | DocProject/buildhelp/ 162 | DocProject/Help/*.HxT 163 | DocProject/Help/*.HxC 164 | DocProject/Help/*.hhc 165 | DocProject/Help/*.hhk 166 | DocProject/Help/*.hhp 167 | DocProject/Help/Html2 168 | DocProject/Help/html 169 | 170 | # Click-Once directory 171 | publish/ 172 | 173 | # Publish Web Output 174 | *.[Pp]ublish.xml 175 | *.azurePubxml 176 | # Note: Comment the next line if you want to checkin your web deploy settings, 177 | # but database connection strings (with potential passwords) will be unencrypted 178 | *.pubxml 179 | *.publishproj 180 | 181 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 182 | # checkin your Azure Web App publish settings, but sensitive information contained 183 | # in these scripts will be unencrypted 184 | PublishScripts/ 185 | 186 | # NuGet Packages 187 | *.nupkg 188 | # The packages folder can be ignored because of Package Restore 189 | **/[Pp]ackages/* 190 | # except build/, which is used as an MSBuild target. 191 | !**/[Pp]ackages/build/ 192 | # Uncomment if necessary however generally it will be regenerated when needed 193 | #!**/[Pp]ackages/repositories.config 194 | # NuGet v3's project.json files produces more ignorable files 195 | *.nuget.props 196 | *.nuget.targets 197 | 198 | # Microsoft Azure Build Output 199 | csx/ 200 | *.build.csdef 201 | 202 | # Microsoft Azure Emulator 203 | ecf/ 204 | rcf/ 205 | 206 | # Windows Store app package directories and files 207 | AppPackages/ 208 | BundleArtifacts/ 209 | Package.StoreAssociation.xml 210 | _pkginfo.txt 211 | *.appx 212 | *.appxbundle 213 | *.appxupload 214 | 215 | # Visual Studio cache files 216 | # files ending in .cache can be ignored 217 | *.[Cc]ache 218 | # but keep track of directories ending in .cache 219 | !?*.[Cc]ache/ 220 | 221 | # Others 222 | ClientBin/ 223 | ~$* 224 | *~ 225 | *.dbmdl 226 | *.dbproj.schemaview 227 | *.jfm 228 | *.pfx 229 | *.publishsettings 230 | orleans.codegen.cs 231 | 232 | # Including strong name files can present a security risk 233 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 234 | #*.snk 235 | 236 | # Since there are multiple workflows, uncomment next line to ignore bower_components 237 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 238 | #bower_components/ 239 | 240 | # RIA/Silverlight projects 241 | Generated_Code/ 242 | 243 | # Backup & report files from converting an old project file 244 | # to a newer Visual Studio version. Backup files are not needed, 245 | # because we have git ;-) 246 | _UpgradeReport_Files/ 247 | Backup*/ 248 | UpgradeLog*.XML 249 | UpgradeLog*.htm 250 | ServiceFabricBackup/ 251 | *.rptproj.bak 252 | 253 | # SQL Server files 254 | *.mdf 255 | *.ldf 256 | *.ndf 257 | 258 | # Business Intelligence projects 259 | *.rdl.data 260 | *.bim.layout 261 | *.bim_*.settings 262 | *.rptproj.rsuser 263 | *- Backup*.rdl 264 | 265 | # Microsoft Fakes 266 | FakesAssemblies/ 267 | 268 | # GhostDoc plugin setting file 269 | *.GhostDoc.xml 270 | 271 | # Node.js Tools for Visual Studio 272 | .ntvs_analysis.dat 273 | node_modules/ 274 | 275 | # Visual Studio 6 build log 276 | *.plg 277 | 278 | # Visual Studio 6 workspace options file 279 | *.opt 280 | 281 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 282 | *.vbw 283 | 284 | # Visual Studio LightSwitch build output 285 | **/*.HTMLClient/GeneratedArtifacts 286 | **/*.DesktopClient/GeneratedArtifacts 287 | **/*.DesktopClient/ModelManifest.xml 288 | **/*.Server/GeneratedArtifacts 289 | **/*.Server/ModelManifest.xml 290 | _Pvt_Extensions 291 | 292 | # Paket dependency manager 293 | .paket/paket.exe 294 | paket-files/ 295 | 296 | # FAKE - F# Make 297 | .fake/ 298 | 299 | # CodeRush personal settings 300 | .cr/personal 301 | 302 | # Python Tools for Visual Studio (PTVS) 303 | __pycache__/ 304 | *.pyc 305 | 306 | # Cake - Uncomment if you are using it 307 | # tools/** 308 | # !tools/packages.config 309 | 310 | # Tabs Studio 311 | *.tss 312 | 313 | # Telerik's JustMock configuration file 314 | *.jmconfig 315 | 316 | # BizTalk build output 317 | *.btp.cs 318 | *.btm.cs 319 | *.odx.cs 320 | *.xsd.cs 321 | 322 | # OpenCover UI analysis results 323 | OpenCover/ 324 | 325 | # Azure Stream Analytics local run output 326 | ASALocalRun/ 327 | 328 | # MSBuild Binary and Structured Log 329 | *.binlog 330 | 331 | # NVidia Nsight GPU debugger configuration file 332 | *.nvuser 333 | 334 | # MFractors (Xamarin productivity tool) working folder 335 | .mfractor/ 336 | 337 | # Local History for Visual Studio 338 | .localhistory/ 339 | 340 | # BeatPulse healthcheck temp database 341 | healthchecksdb 342 | 343 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 344 | MigrationBackup/ --------------------------------------------------------------------------------