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();
        }
Exemplo n.º 2
0
        protected bool Step3_OnNodeBegin(TreeNode tree, TreeNode node, List <Context> stack, int depth)
        {
            Context context = stack[depth];

            SyncPlayerTreeWithGameTree(stack, depth, _step3CurPos);

            TreeNode playerNode = context.PlayerTreeNode;

            // Correct and validate strategy of the opponent.
            if (depth == 0)
            {
                playerNode.StrategicProbab = 1;
            }
            else if (!node.State.IsGameOver)
            {
                if (node.State.IsPlayerActing(_step3CurPos))
                {
                    // Sum of strategic probability of children must be equal to the str. prob. of the parent.
                    double StrategicProbabOfChildren = 0;
                    for (int c = 0; c < playerNode.Children.Count; ++c)
                    {
                        StrategicProbabOfChildren += playerNode.Children[c].StrategicProbab;
                    }

                    if (
                        !FloatingPoint.AreEqualRel(playerNode.StrategicProbab, StrategicProbabOfChildren,
                                                   0.000001))
                    {
                        throw new ApplicationException(
                                  string.Format(
                                      "Player {0}, node id {1} : strategic probab. {2} != sum of strategic probab. of children {3}",
                                      _step3CurPos, playerNode.Id,
                                      playerNode.StrategicProbab,
                                      StrategicProbabOfChildren));
                    }
                }
                else
                {
                    // Strategic probability of each child must be the same as of the parent.
                    for (int c = 0; c < playerNode.Children.Count; ++c)
                    {
                        playerNode.Children[c].StrategicProbab = playerNode.StrategicProbab;
                    }
                }
            }
            node.StrategicProbab *= playerNode.StrategicProbab;
            return(true);
        }