Пример #1
0
        public static void Run()
        {
            var client = new CustomDecisionServiceAPI();

            // 1. Sign in on https://ds.microsoft.com
            // 2. Create new app
            //   a.	Select "Custom App"
            //   b. Supply Azure storage account
            //   c. Set Vowpal Wabbit arguments to --cb_explore_adf --epsilon 0.2 -q DT -q LT

            var appId = "<<CHANGE ME";

            Console.WriteLine($"Requesting decision for app '{appId}'...\n");
            var decisions = client.Rank(appId: appId,
                                        decisionRequests: new DecisionRequestCollection(new List <DecisionRequest>
            {
                new DecisionRequest
                {
                    Shared = new DecisionRequestShared
                    {
                        Features = new List <object>
                        {
                            new SharedContext
                            {
                                Demographics = new DemographicNamespace
                                {
                                    Gender = "female",
                                },
                                Location = new LocationNamespace
                                {
                                    Country = "USA",
                                    City    = "New York"
                                }
                            }
                        }
                    },
                    Actions = new List <DecisionFeatures>
                    {
                        new DecisionFeatures
                        {
                            Ids = new List <DecisionReferenceId>
                            {
                                new DecisionReferenceId(id: "1")
                            },
                            Features = new List <object>
                            {
                                new ActionDependentFeatures
                                {
                                    Topic = new TopicNamespace
                                    {
                                        Category = "Fashion"
                                    }
                                }
                            }
                        },
                        new DecisionFeatures
                        {
                            Ids = new List <DecisionReferenceId>
                            {
                                new DecisionReferenceId(id: "2")
                            },
                            Features = new List <object>
                            {
                                new ActionDependentFeatures
                                {
                                    Topic = new TopicNamespace
                                    {
                                        Category = "Technology"
                                    }
                                }
                            }
                        }
                    }
                }
            }));

            var decision = decisions[0];

            Console.WriteLine("EventId {0} Ranking: {1} RewardAction: {2}\n",
                              decision.EventId,
                              string.Join(",", decision.Ranking.Select(r => r.Id)),
                              decision.RewardAction);

            client.Reward(decision.AppId, decision.EventId, 5f);

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
Пример #2
0
        public static void Run()
        {
            var client = new CustomDecisionServiceAPI();

            // 1. Sign in on https://ds.microsoft.com
            // 2. Create new app
            //   a.	Select "Custom App"
            //   b. Supply Azure storage account
            //   c. Set Vowpal Wabbit arguments to --cb_explore_adf --epsilon 0.2 -q DT -q LT

            var appId = "<<CHANGE ME";

            Console.WriteLine($"Requesting decision for app '{appId}'...\n");

            var random = new Random(42);

            // simulation parameterization
            var personas = new[] {
                new { gender = "male", probabilityOfChoosingAction1 = 1.0 },
                new { gender = "female", probabilityOfChoosingAction1 = 0.0 }
            };

            // gender (male/female) -> action (1/2) -> count (0)
            var distributionOverActionsPerContext = personas.ToDictionary(
                p => p.gender,
                p => CreateDecisionRequestCollection(p.gender).Decisions[0].Actions.ToDictionary(a => a.Ids.First().Id, _ => 0));

            var requests = 0;

            for (int i = 0; true; i++)
            {
                // alternate between personas
                var persona = personas[i % personas.Length];

                // generate context
                var decisionRequests = CreateDecisionRequestCollection(persona.gender);

                // get decision
                Console.Write(".");
                // automatically retries on failure
                var decision = client.Rank(appId, decisionRequests)[0];

                // simulate stochastic behavior
                var probabilityOfRewardingAction =
                    decision.RewardAction == "1" ?
                    persona.probabilityOfChoosingAction1 : 1 - persona.probabilityOfChoosingAction1;

                if (random.NextDouble() < probabilityOfRewardingAction)
                {
                    client.Reward(appId, decision.EventId, 1f);
                }

                // build up stats
                distributionOverActionsPerContext[persona.gender][decision.RewardAction]++;
                requests++;

                if (requests % 50 == 0)
                {
                    distributionOverActionsPerContext.PrintDecisionServiceBehavior();
                }

                // 10 req / second
                Thread.Sleep(TimeSpan.FromMilliseconds(100));
            }
        }