public void CheckWeights(double[] weights, Random underlyingRng) { int[] counts = new int[weights.Length]; DiscreteProbabilityRng rng = new DiscreteProbabilityRng(underlyingRng); rng.SetWeights(weights); for (int i = 0; i < 200000; ++i) { int r = rng.Next(); counts[r]++; } double[] resultWeights = new double[weights.Length]; int sum = 0; double sumWeights = 0; for (int i = 0; i < counts.Length; ++i) { Console.Write("{0} ", counts[i]); sum += counts[i]; sumWeights += weights[i]; } Console.WriteLine(); for (int i = 0; i < counts.Length; ++i) { resultWeights[i] = (double)counts[i] / sum * sumWeights; Console.Write("{0:0.000} ", resultWeights[i]); Assert.IsTrue(FloatingPoint.AreEqualRel(weights[i], resultWeights[i], 0.05)); if (weights[i] == 0) { Assert.AreEqual(0, counts[i], "0-weigths must not occur"); } } Console.WriteLine(); }
public void Test_SameSeed() { double[] weights = (new double[256]).Fill(i => i); int seed = 123; DiscreteProbabilityRng rng1; DiscreteProbabilityRng rng2; // Case 1 rng1 = new DiscreteProbabilityRng(seed); rng2 = new DiscreteProbabilityRng(seed); rng1.SetWeights(weights); rng2.SetWeights(weights); for (int i = 0; i < 100; ++i) { Assert.AreEqual(rng1.Next(), rng2.Next()); } // Case 2 rng1 = new DiscreteProbabilityRng(new Random(seed)); rng2 = new DiscreteProbabilityRng(new Random(seed)); rng1.SetWeights(weights); rng2.SetWeights(weights); for (int i = 0; i < 100; ++i) { Assert.AreEqual(rng1.Next(), rng2.Next()); } }
public void Benchmark(Random underlyingRng) { double[] weights = new double[1000]; for (int i = 0; i < weights.Length; ++i) { weights[i] = i; } DiscreteProbabilityRng rng = new DiscreteProbabilityRng(underlyingRng); rng.SetWeights(weights); int REPETITIONS = 50000000; DateTime startTime = DateTime.Now; for (int i = 0; i < REPETITIONS; ++i) { rng.Next(); } TimeSpan time = DateTime.Now - startTime; Console.Write("{0} Repetitions done in {1:0.0} s, {2:0,0} numbers/s", REPETITIONS, time.TotalSeconds, REPETITIONS / time.TotalSeconds); }
void Initialize() { _playerInfoProps = new Props(); string strDir = _creationParams.Get("StrategyDir"); _playerInfoProps.Set("StrategyDir", strDir); string propsFile = Path.Combine(strDir, "props.xml"); Props props = XmlSerializerExt.Deserialize <Props>(propsFile); int rngSeed = int.Parse(_creationParams.GetDefault("RngSeed", "0")); if (rngSeed == 0) { rngSeed = (int)DateTime.Now.Ticks; } _checkBlinds = bool.Parse(_creationParams.GetDefault("CheckBlinds", "true")); string amountCompareMethodText = _creationParams.GetDefault("AmountSearchMethod", "Equal"); if (amountCompareMethodText == "Equal") { _amountSearchMethod = AmountSearchMethod.Equal; } else if (amountCompareMethodText == "Closest") { _amountSearchMethod = AmountSearchMethod.Closest; } else { throw new ApplicationException(string.Format("Unknown amount compare method {0}", amountCompareMethodText)); } _relProbabIgnoreLevel = double.Parse(_creationParams.GetDefault("RelProbabIgnoreLevel", "0.0"), CultureInfo.InvariantCulture); _playerInfoProps.Set("RelProbabIgnoreLevel", _relProbabIgnoreLevel.ToString()); // Use MersenneTwister because it is under our control on all platforms and // is probably better than System.Random. Random underlyingRng = new MersenneTwister(rngSeed); _moveSelector = new DiscreteProbabilityRng(underlyingRng); _playerInfoProps.Set("RngSeed", rngSeed.ToString()); _deckDescr = XmlSerializerExt.Deserialize <DeckDescriptor>(props.Get("DeckDescriptor")); _chanceAbsrtractions = new IChanceAbstraction[0]; // Create chance abstractions for (int pos = 0; ; pos++) { string caPropName = String.Format("ChanceAbstraction-{0}", pos); string relFileName = props.Get(caPropName); if (string.IsNullOrEmpty(relFileName)) { break; } Array.Resize(ref _chanceAbsrtractions, _chanceAbsrtractions.Length + 1); string absFileName = Path.Combine(strDir, relFileName); _chanceAbsrtractions[pos] = ChanceAbstractionHelper.CreateFromPropsFile(absFileName); _playerInfoProps.Set(caPropName + ".Name", _chanceAbsrtractions[pos].Name); } Dictionary <string, int> fileToPos = new Dictionary <string, int>(); _strategies = new StrategyTree[0]; _strIndexes = new UFTreeChildrenIndex[0]; // Load strategies, reuse if file is the same for (int pos = 0; ; pos++) { string strPropName = String.Format("Strategy-{0}", pos); string relFileName = props.Get(strPropName); if (string.IsNullOrEmpty(relFileName)) { break; } Array.Resize(ref _strategies, _strategies.Length + 1); Array.Resize(ref _strIndexes, _strIndexes.Length + 1); string absFileName = Path.Combine(strDir, relFileName); int existingPos; if (fileToPos.TryGetValue(absFileName, out existingPos)) { _strategies[pos] = _strategies[existingPos]; _strIndexes[pos] = _strIndexes[existingPos]; } else { fileToPos.Add(absFileName, pos); _strategies[pos] = StrategyTree.ReadFDA <StrategyTree>(absFileName); _strIndexes[pos] = new UFTreeChildrenIndex(_strategies[pos], Path.Combine(strDir, "strategy-idx.dat"), false); } _playerInfoProps.Set(strPropName + ".Version", _strategies[pos].Version.ToString()); } // Read blinds for (int strPos = 0; strPos < _strategies.Length; ++strPos) { StringBuilder sb = new StringBuilder(); for (int playerPos = 0; playerPos < _strategies.Length; ++playerPos) { StrategyTreeNode stNode = new StrategyTreeNode(); _strategies[strPos].GetNode(playerPos + 1, &stNode); sb.AppendFormat("{0:0.000000 }", stNode.Amount); } _playerInfoProps.Set("Blinds." + strPos.ToString(), sb.ToString()); } }