Пример #1
0
        public void SoftmaxScores()
        {
            int   numActions = 10;
            float lambda     = 0.5f;
            var   recorder   = new TestRecorder <RegularTestContext>();
            var   scorer     = new TestScorer <RegularTestContext>(1, numActions, uniform: false);

            //var mwtt = new MwtExplorer<RegularTestContext>("mwt", recorder);
            var explorer = new SoftmaxExplorer(lambda);

            var mwtt = MwtExplorer.Create("mwt", numActions, recorder, explorer, scorer);

            Random rand = new Random();

            mwtt.ChooseAction(rand.NextDouble().ToString(), new RegularTestContext()
            {
                Id = 100
            });
            mwtt.ChooseAction(rand.NextDouble().ToString(), new RegularTestContext()
            {
                Id = 101
            });
            mwtt.ChooseAction(rand.NextDouble().ToString(), new RegularTestContext()
            {
                Id = 102
            });

            var interactions = recorder.GetAllInteractions();

            Assert.AreEqual(3, interactions.Count);

            for (int i = 0; i < interactions.Count; i++)
            {
                // Scores are not equal therefore probabilities should not be uniform
                Assert.AreNotEqual(interactions[i].Probability, 1.0f / numActions);
                Assert.AreEqual(100 + i, interactions[i].Context.Id);
            }

            // Verify that policy action is chosen all the time
            RegularTestContext context = new RegularTestContext {
                Id = 100
            };
            List <float> scores             = scorer.MapContext(context).Value.ToList();
            float        maxScore           = 0;
            int          highestScoreAction = 0;

            for (int i = 0; i < scores.Count; i++)
            {
                if (maxScore < scores[i])
                {
                    maxScore           = scores[i];
                    highestScoreAction = i + 1;
                }
            }

            explorer.EnableExplore(false);
            for (int i = 0; i < 1000; i++)
            {
                int chosenAction = mwtt.ChooseAction(rand.NextDouble().ToString(), new RegularTestContext()
                {
                    Id = (int)i
                });
                Assert.AreEqual(highestScoreAction, chosenAction);
            }
        }