private void Step <TOutcome>(RunContext runContext, IOutcomeReporter <TOutcome> outcomeReporter, IStepContext <TOutcome> step)
        {
            string eventId = step.EventId;

            if (!liveModel.TryChooseRank(eventId, step.DecisionContext, runContext.ResponseContainer, runContext.ApiStatusContainer))
            {
                this.SafeRaiseError(runContext.ApiStatusContainer);
            }

            long actionIndex = -1;

            if (!runContext.ResponseContainer.TryGetChosenAction(out actionIndex, runContext.ApiStatusContainer))
            {
                this.SafeRaiseError(runContext.ApiStatusContainer);
            }

            TOutcome outcome = step.GetOutcome(actionIndex, runContext.ResponseContainer.AsEnumerable());

            if (outcome == null)
            {
                return;
            }

            if (!outcomeReporter.TryQueueOutcomeEvent(runContext, eventId, outcome))
            {
                this.SafeRaiseError(runContext.ApiStatusContainer);
            }
        }
        public static void PdfExample(string configPath)
        {
            const float  outcome     = 1.0f;
            const string eventId     = "event_id";
            const string contextJson = "{\"GUser\":{\"id\":\"a\",\"major\":\"eng\",\"hobby\":\"hiking\"},\"_multi\":[ { \"TAction\":{\"a1\":\"f1\"} },{\"TAction\":{\"a2\":\"f2\"}}],\"p\":[0.2, 0.8]}";

            LiveModel liveModel = Helpers.CreateLiveModelOrExit(configPath);

            ApiStatus apiStatus = new ApiStatus();

            RankingResponse rankingResponse = new RankingResponse();

            if (!liveModel.TryChooseRank(eventId, contextJson, rankingResponse, apiStatus))
            {
                Helpers.WriteStatusAndExit(apiStatus);
            }

            long actionId;

            if (!rankingResponse.TryGetChosenAction(out actionId, apiStatus))
            {
                Helpers.WriteStatusAndExit(apiStatus);
            }

            Console.WriteLine($"Chosen action id: {actionId}");

            if (!liveModel.TryQueueOutcomeEvent(eventId, outcome, apiStatus))
            {
                Helpers.WriteStatusAndExit(apiStatus);
            }
        }
Beispiel #3
0
        private void Step <TOutcome>(RunContext runContext, IOutcomeReporter <TOutcome> outcomeReporter, IStepContext <TOutcome> step)
        {
            string   eventId = step.EventId;
            TOutcome outcome = default(TOutcome);

            if (loopKind == LoopKind.Slates)
            {
                if (!liveModel.TryRequestMultiSlotDecision(eventId, step.SlatesContext, runContext.SlatesContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                int[]   actions = runContext.SlatesContainer.Select(slot => slot.ActionId).ToArray();
                float[] probs   = runContext.SlatesContainer.Select(slot => slot.Probability).ToArray();
                outcome = step.GetSlatesOutcome(actions, probs);
                if (outcome == null)
                {
                    return;
                }
            }
            else if (loopKind == LoopKind.CA)
            {
                if (!liveModel.TryRequestContinuousAction(eventId, step.ContinuousActionContext, runContext.ContinuousActionContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }
                float action   = runContext.ContinuousActionContainer.ChosenAction;
                float pdfValue = runContext.ContinuousActionContainer.ChosenActionPdfValue;
                outcome = step.GetContinuousActionOutcome(action, pdfValue);
                if (outcome == null)
                {
                    return;
                }
            }
            else
            {
                if (!liveModel.TryChooseRank(eventId, step.DecisionContext, runContext.ResponseContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                long actionIndex = -1;
                if (!runContext.ResponseContainer.TryGetChosenAction(out actionIndex, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                outcome = step.GetOutcome(actionIndex, runContext.ResponseContainer.AsEnumerable());
                if (outcome == null)
                {
                    return;
                }
            }

            if (!outcomeReporter.TryQueueOutcomeEvent(runContext, eventId, outcome))
            {
                this.SafeRaiseError(runContext.ApiStatusContainer);
            }
        }
Beispiel #4
0
        // TODO: Pull this out to a separate sample once we implement the simulator in this.
        public static void BasicUsageExample(string [] args)
        {
            const float  outcome     = 1.0f;
            const string eventId     = "event_id";
            const string contextJson = "{'GUser':{'id':'a','major':'eng','hobby':'hiking'},'_multi':[ { 'TAction':{'a1':'f1'} },{'TAction':{'a2':'f2'}}]}";

            if (args.Length != 1)
            {
                WriteErrorAndExit("Missing path to client configuration json");
            }

            if (!File.Exists(args[0]))
            {
                WriteErrorAndExit($"Could not find file with path '{args[0]}'.");
            }

            string json = File.ReadAllText(args[0]);

            ApiStatus apiStatus = new ApiStatus();

            Configuration config;

            if (!Configuration.TryLoadConfigurationFromJson(json, out config, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            LiveModel liveModel = new LiveModel(config);

            if (!liveModel.TryInit(apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            RankingResponse rankingResponse = new RankingResponse();

            if (!liveModel.TryChooseRank(eventId, contextJson, rankingResponse, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            long actionId;

            if (!rankingResponse.TryGetChosenAction(out actionId, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            Console.WriteLine($"Chosen action id: {actionId}");

            if (!liveModel.TryReportOutcome(eventId, outcome, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }
        }
        private void Run_TestCustomSender_SendFailure(FactoryContext factoryContext, string expectedString, bool expectPrefix = false)
        {
            ManualResetEventSlim backgroundMessageWaiter = new ManualResetEventSlim(initialState: false);

            int    backgroundErrorCount   = 0;
            int    backgroundErrorCode    = 0;
            string backgroundErrorMessage = null;

            void OnBackgroundError(object sender, ApiStatus args)
            {
                Assert.AreEqual(0, backgroundErrorCount++, "Do not duplicate background errors.");
                backgroundErrorCode    = args.ErrorCode;
                backgroundErrorMessage = args.ErrorMessage;

                backgroundMessageWaiter.Set();
            }

            LiveModel liveModel = CreateLiveModel(factoryContext);

            liveModel.BackgroundError += OnBackgroundError;

            liveModel.Init();

            ApiStatus       apiStatus = new ApiStatus();
            RankingResponse response;

            Assert.IsTrue(liveModel.TryChooseRank(EventId, ContextJsonWithPdf, out response, apiStatus));
            Assert.AreEqual(NativeMethods.SuccessStatus, apiStatus.ErrorCode, "Errors from ISender.Send should be background errors.");

            backgroundMessageWaiter.Wait(TimeSpan.FromSeconds(1));

            Assert.AreEqual(NativeMethods.OpaqueBindingError, backgroundErrorCode, "Error from ISender did not get raised.");

            if (!expectPrefix)
            {
                Assert.AreEqual(OpaqueErrorMessage, backgroundErrorMessage);
            }
            else
            {
                Assert.IsTrue(backgroundErrorMessage.StartsWith(OpaqueErrorMessage));
            }
        }
Beispiel #6
0
        // TODO: Pull this out to a separate sample.
        public static void BasicUsageExample(string [] args)
        {
            const float  outcome     = 1.0f;
            const string eventId     = "event_id";
            const string contextJson = "{\"GUser\":{\"id\":\"a\",\"major\":\"eng\",\"hobby\":\"hiking\"},\"_multi\":[ { \"TAction\":{\"a1\":\"f1\"} },{\"TAction\":{\"a2\":\"f2\"}}]}";

            if (args.Length != 1)
            {
                WriteErrorAndExit("Missing path to client configuration json");
            }

            LiveModel liveModel = CreateLiveModelOrExit(args[0]);

            ApiStatus apiStatus = new ApiStatus();

            RankingResponse rankingResponse = new RankingResponse();

            if (!liveModel.TryChooseRank(eventId, contextJson, rankingResponse, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            long actionId;

            if (!rankingResponse.TryGetChosenAction(out actionId, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }

            Console.WriteLine($"Chosen action id: {actionId}");

            if (!liveModel.TryReportOutcome(eventId, outcome, apiStatus))
            {
                WriteStatusAndExit(apiStatus);
            }
        }
        private void Step <TOutcome>(RunContext runContext, IOutcomeReporter <TOutcome> outcomeReporter, IStepContext <TOutcome> step)
        {
            string   eventId = step.EventId;
            TOutcome outcome = default(TOutcome);

            if (loopKind == LoopKind.Slates)
            {
                if (!liveModel.TryRequestMultiSlotDecision(eventId, step.SlatesContext, runContext.MultiSlotResponseContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                int[]   actions = runContext.MultiSlotResponseContainer.Select(slot => slot.ActionId).ToArray();
                float[] probs   = runContext.MultiSlotResponseContainer.Select(slot => slot.Probability).ToArray();
                outcome = step.GetSlatesOutcome(actions, probs);
                if (outcome == null)
                {
                    return;
                }
            }
            else if (loopKind == LoopKind.CA)
            {
                if (!liveModel.TryRequestContinuousAction(eventId, step.ContinuousActionContext, runContext.ContinuousActionContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }
                float action   = runContext.ContinuousActionContainer.ChosenAction;
                float pdfValue = runContext.ContinuousActionContainer.ChosenActionPdfValue;
                outcome = step.GetContinuousActionOutcome(action, pdfValue);
                if (outcome == null)
                {
                    return;
                }
            }
            else if (loopKind == LoopKind.CCB)
            {
                if (!liveModel.TryRequestDecision(step.DecisionContext, runContext.DecisionResponseContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }
                // TODO: Populate actionProbs. Currently GetOutcome() just returns a fixed outcome value, so the values of actionProbs don't matter.
                ActionProbability[] actionProbs = new ActionProbability[runContext.DecisionResponseContainer.Count];
                foreach (var slot in runContext.DecisionResponseContainer)
                {
                    outcome = step.GetOutcome(slot.ActionId, actionProbs);
                    if (!outcomeReporter.TryQueueOutcomeEvent(runContext, slot.SlotId, outcome))
                    {
                        this.SafeRaiseError(runContext.ApiStatusContainer);
                    }
                }
                return;
            }
            else if (loopKind == LoopKind.CCBv2)
            {
                if (!liveModel.TryRequestMultiSlotDecisionDetailed(eventId, step.DecisionContext, runContext.MultiSlotResponseDetailedContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }
                foreach (var slot in runContext.MultiSlotResponseDetailedContainer)
                {
                    outcome = step.GetOutcome(slot.ChosenAction, slot);
                    if (!outcomeReporter.TryQueueOutcomeEvent(runContext, eventId, slot.SlotId, outcome))
                    {
                        this.SafeRaiseError(runContext.ApiStatusContainer);
                    }
                }
                return;
            }
            else
            {
                if (!liveModel.TryChooseRank(eventId, step.DecisionContext, runContext.ResponseContainer, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                long actionIndex = -1;
                if (!runContext.ResponseContainer.TryGetChosenAction(out actionIndex, runContext.ApiStatusContainer))
                {
                    this.SafeRaiseError(runContext.ApiStatusContainer);
                }

                outcome = step.GetOutcome(actionIndex, runContext.ResponseContainer.AsEnumerable());
                if (outcome == null)
                {
                    return;
                }
            }

            if (!outcomeReporter.TryQueueOutcomeEvent(runContext, eventId, outcome))
            {
                this.SafeRaiseError(runContext.ApiStatusContainer);
            }
        }