Exemple #1
0
        public void Add(int setSize)
        {
            var matchesBuffer = new SetMatchesBuffer(setSize, 10);
            var addBuffer     = new KeyValuePair <int, float> [setSize];

            Assert.AreEqual(0, matchesBuffer.Count);

            const int addCount = 2;

            for (var n = 0; n < addCount; n++)
            {
                for (var i = 0; i < addBuffer.Length; i++)
                {
                    addBuffer[i] = new KeyValuePair <int, float>(i + 1, 0.5f);
                }

                matchesBuffer.Add(addBuffer, 1f / n + 1);
            }

            Assert.AreEqual(addCount, matchesBuffer.Count);
            for (var i = 0; i < addCount; i++)
            {
                Assert.AreNotEqual(0f, matchesBuffer.Ratings[i]);
            }
            for (var i = 0; i < addCount * setSize; i++)
            {
                Assert.AreNotEqual(0, matchesBuffer.DataIds[i]);
            }
        }
Exemple #2
0
 static void AddMultipleHypotheses(SetMatchesBuffer buffer,
                                   List <KeyValuePair <int, float>[]> hypotheses, List <float> ratings)
 {
     for (var i = 0; i < hypotheses.Count; i++)
     {
         buffer.Add(hypotheses[i], ratings[i]);
     }
 }
Exemple #3
0
        public void Constructor(int capacity, int setSize)
        {
            var buffer = new SetMatchesBuffer(setSize, capacity);

            Assert.AreEqual(0, buffer.Count);
            Assert.AreEqual(capacity, buffer.Capacity);
            Assert.GreaterOrEqual(buffer.Ratings.Length, capacity);
            Assert.GreaterOrEqual(buffer.DataIds.Length, capacity * setSize);
        }
Exemple #4
0
        public void ChooseHighestRated_WithFirstBehavior(List <KeyValuePair <int, float>[]> hypotheses, List <float> ratings,
                                                         KeyValuePair <int, float> expectedIndexToRating)
        {
            var tieBuffer     = new int[4];
            var matchesBuffer = new SetMatchesBuffer(hypotheses[0].Length, 10);

            AddMultipleHypotheses(matchesBuffer, hypotheses, ratings);

            var chosenIndexToRating = matchesBuffer.ChooseHighestRated(tieBuffer, TieChoiceBehavior.First);

            Assert.AreEqual(expectedIndexToRating, chosenIndexToRating);
        }
Exemple #5
0
        public void FindHighestRated_NoTies(List <KeyValuePair <int, float>[]> hypotheses, List <float> ratings,
                                            KeyValuePair <float, int> expectedRatingToIndex)
        {
            var tieBuffer     = new int[4];
            var matchesBuffer = new SetMatchesBuffer(hypotheses[0].Length, 10);

            AddMultipleHypotheses(matchesBuffer, hypotheses, ratings);

            var highestRatingToCount = matchesBuffer.FindHighestRated(ref tieBuffer);

            Assert.AreEqual(expectedRatingToIndex.Key, highestRatingToCount.Key);
            // only one entry in the tie buffer ?
            Assert.AreEqual(expectedRatingToIndex.Value, tieBuffer[0]);
            Assert.AreEqual(tieBuffer[1], default(int));
        }
Exemple #6
0
        public void FindHighestRated_HasTies(List <KeyValuePair <int, float>[]> hypotheses, List <float> ratings,
                                             KeyValuePair <float, int[]> expectedRatingToIndices)
        {
            var tieBuffer     = new int[4];
            var matchesBuffer = new SetMatchesBuffer(hypotheses[0].Length, 10);

            AddMultipleHypotheses(matchesBuffer, hypotheses, ratings);

            var highestRatingToCount = matchesBuffer.FindHighestRated(ref tieBuffer);

            Assert.AreEqual(expectedRatingToIndices.Key, highestRatingToCount.Key);
            // did we get exactly the indices we expected in the tie buffer ?
            Assert.AreEqual(expectedRatingToIndices.Value.Length, highestRatingToCount.Value);
            for (var i = 0; i < expectedRatingToIndices.Value.Length; i++)
            {
                Assert.AreEqual(expectedRatingToIndices.Value[i], tieBuffer[i]);
            }
        }
Exemple #7
0
        public void ChooseHighestRated_WithRandomBehavior(List <KeyValuePair <int, float>[]> hypotheses, List <float> ratings,
                                                          KeyValuePair <int, float> expectedIndexToRating)
        {
            var tieBuffer     = new int[4];
            var matchesBuffer = new SetMatchesBuffer(hypotheses[0].Length, 10);

            AddMultipleHypotheses(matchesBuffer, hypotheses, ratings);

            var anyNonFirstChoices = false;

            for (var i = 0; i < 10; i++)
            {
                UnityEngine.Random.InitState((i + 1) * 1000);
                var chosenIndexToRating = matchesBuffer.ChooseHighestRated(tieBuffer, TieChoiceBehavior.Random);

                Assert.Contains(chosenIndexToRating.Key, tieBuffer);
                anyNonFirstChoices |= chosenIndexToRating.Key != expectedIndexToRating.Key;
                // all random choices should be sure to have the same value
                Assert.AreEqual(expectedIndexToRating.Value, chosenIndexToRating.Value);
            }

            // make sure that a random choice resulted in something different than a first choice would have
            Assert.True(anyNonFirstChoices);
        }