Ejemplo n.º 1
0
        public void FromRandomMoves2Test()
        {
            Random random      = new Random(7777777);
            int    length      = 50;
            int    repetitions = 1000;

            double[] probabilities = Enumerable
                                     .Repeat(0d, Constants.NumMoves)
                                     .ToArray();

            probabilities[9] = 100000;
            probabilities[3] = 1;
            probabilities[4] = 1;
            probabilities[5] = 1;
            probabilities[6] = 3;

            Move[] possibleMoves = { Move.L1, Move.U1, Move.U2, Move.U3, Move.F1 };

            int[] sumOfMoves = Enumerable.
                               Repeat(0, Constants.NumMoves)
                               .ToArray();

            for (int repetition = 0; repetition < repetitions; repetition++)
            {
                int lastCount = sumOfMoves.Sum();

                Alg randomAlg = Alg.FromRandomMoves(length, random, probabilities);
                Assert.AreEqual(length, randomAlg.Length);

                for (int move = 0; move < Constants.NumMoves; move++)
                {
                    sumOfMoves[move] += randomAlg.Count(randomMove => randomMove == (Move)move);
                }

                Assert.AreEqual(length, sumOfMoves.Sum() - lastCount);
            }
            Assert.AreEqual(length * repetitions, sumOfMoves.Sum());

            double[] effectiveProbabilities = sumOfMoves
                                              .Select(count => count / (double)(length * repetitions))
                                              .ToArray();

            //make sure only move with probability > 0 occur
            double impossibleMoveProbability = effectiveProbabilities
                                               .Where((probability, index) => !possibleMoves.Contains((Move)index))
                                               .Sum();

            Assert.AreEqual(0d, impossibleMoveProbability);

            //compare the effective probabilities
            double delta = 0.02d;

            Assert.AreEqual(1 / 2d, effectiveProbabilities[9], delta);
            Assert.AreEqual(1 / 4d, effectiveProbabilities[6], delta);
            Assert.AreEqual(1 / 12d, effectiveProbabilities[3], delta);
            Assert.AreEqual(1 / 12d, effectiveProbabilities[4], delta);
            Assert.AreEqual(1 / 12d, effectiveProbabilities[5], delta);
        }