コード例 #1
0
        public static void ReportRewardForCachedProducts(ICacheManager cacheManager, int explorationJoinKeyIndex = -1)
        {
            if (cacheManager.IsSet(ProductController.JoinKeyCacheKey) && DecisionServiceWrapper <object> .Service != null)
            {
                var explorationKeys = cacheManager.Get <List <string> >(ProductController.JoinKeyCacheKey);
                for (int i = 0; i < explorationKeys.Count; i++)
                {
                    if (i != explorationJoinKeyIndex)
                    {
                        DecisionServiceWrapper <object> .Service.ReportReward(-1f, explorationKeys[i]);
                    }
                    else
                    {
                        DecisionServiceWrapper <object> .Service.ReportReward(1f, explorationKeys[i]);
                    }
                }

                var imageHtmlBuilder = new StringBuilder();

                if (cacheManager.IsSet(ProductController.CacheKey))
                {
                    var model = cacheManager.Get <IList <Nop.Web.Models.Catalog.ProductOverviewModel> >(ProductController.CacheKey);
                    for (int i = 0; i < model.Count; i++)
                    {
                        if (model[i].ExplorationJoinKeyIndex != -1)
                        {
                            string imageClass = string.Empty;
                            if (model[i].ExplorationJoinKeyIndex == explorationJoinKeyIndex)
                            {
                                imageClass = "mwt-rewarded";
                            }
                            imageHtmlBuilder.Append(string.Format("<img class=\"{0}\" src=\"{1}\" />",
                                                                  imageClass,
                                                                  model[i].DefaultPictureModel.ImageUrl));
                        }
                    }
                }

                string imageHtml = imageHtmlBuilder.Length > 0 ? " <br /> <br /> " + imageHtmlBuilder.ToString() : imageHtmlBuilder.ToString();

                Trace.WriteLine(TraceMessage.GetHeader(TraceMessage.TraceComponentType.Client) + "Reported rewards for presented products" + imageHtml);

                // Clears cache once rewards have been determined.
                cacheManager.Remove(ProductController.JoinKeyCacheKey);

                CurrentTraceType.Value = TraceType.ClientToServerReward;
            }
        }
コード例 #2
0
        static void AutoRetrainModel(int numberOfActions)
        {
            if (!LoadSettings().AutoRetrainModel)
            {
                return;
            }
            using (var client = new System.Net.Http.HttpClient())
            {
                var values = new Dictionary <string, string>
                {
                    { "token", appToken },
                    { "numberOfActions", numberOfActions.ToString() },
                    { "useAfx", LoadSettings().UseAfxForModelRetrain.ToString() }
                };

                var content = new System.Net.Http.FormUrlEncodedContent(values);

                var responseTask = client.PostAsync(
                    commandCenterAddress + "/Application/RetrainModel",
                    content
                    );
                responseTask.Wait();

                var response = responseTask.Result;

                if (!response.IsSuccessStatusCode)
                {
                    var t2 = response.Content.ReadAsStringAsync();
                    t2.Wait();

                    Trace.TraceError(TraceMessage.GetHeader(TraceMessage.TraceComponentType.AzureML) + "Failed to request model retraining, Result: {0}, Reason: {1}, Headers: {2}.",
                                     t2.Result, response.ReasonPhrase, response.Headers.ToString());
                }
                else
                {
                    Trace.WriteLine(TraceMessage.GetHeader(TraceMessage.TraceComponentType.AzureML) + "Requested model retraining.");
                }
            }
        }
コード例 #3
0
        public static void ObserveStorageAndRetrain(CancellationToken cancelToken, int numberOfActions)
        {
            bool retrainOnUpdate = true;

            CloudStorageAccount storageAccount = null;
            CloudBlobClient     blobClient     = null;

            try
            {
                using (var wc = new WebClient())
                {
                    string jsonMetadata = wc.DownloadString(commandCenterAddress + "/Application/GetMetadata?token=" + appToken);
                    ApplicationTransferMetadata appMetadata = JsonConvert.DeserializeObject <ApplicationTransferMetadata>(jsonMetadata);

                    storageAccount = CloudStorageAccount.Parse(appMetadata.ConnectionString);
                    blobClient     = storageAccount.CreateCloudBlobClient();
                }
            }
            catch { }

            DecisionServiceSettings settings = LoadSettings();
            int serverObserveDelay           = settings.ServerObserveDelay;
            int modelRetrainPeriodicDelay    = settings.ModelRetrainPeriodicDelay;

            if (storageAccount == null || blobClient == null)
            {
                retrainOnUpdate = false;
                Trace.TraceWarning("Could not connect to Azure Storage for observation, Model Retraining will run automatically every {0} ms.", modelRetrainPeriodicDelay);
            }

            int waitCount = 0;

            if (LastBlobModifiedDate == null)
            {
                LastBlobModifiedDate = new DateTimeOffset();
            }

            while (!cancelToken.IsCancellationRequested)
            {
                cancelToken.WaitHandle.WaitOne(serverObserveDelay);
                waitCount++;

                if (retrainOnUpdate)
                {
                    IEnumerable <CloudBlobContainer> containers = blobClient.ListContainers("complete");

                    var lastContainerDate            = new DateTimeOffset();
                    CloudBlobContainer lastContainer = null;
                    foreach (var container in containers)
                    {
                        if (container.Properties.LastModified.Value >= lastContainerDate)
                        {
                            lastContainerDate = container.Properties.LastModified.Value;
                            lastContainer     = container;
                        }
                    }
                    if (lastContainer != null)
                    {
                        var lastBlobDate = new DateTimeOffset();
                        IEnumerable <IListBlobItem> blobs = lastContainer.ListBlobs();
                        foreach (var blob in blobs)
                        {
                            if (blob is CloudBlockBlob)
                            {
                                DateTimeOffset blobDate = ((CloudBlockBlob)blob).Properties.LastModified.Value;
                                if (blobDate >= lastBlobDate)
                                {
                                    lastBlobDate = blobDate;
                                }
                            }
                        }
                        if (lastBlobDate > LastBlobModifiedDate)
                        {
                            LastBlobModifiedDate = lastBlobDate;
                            Trace.WriteLine(TraceMessage.GetHeader(TraceMessage.TraceComponentType.Server) + "new data created.");

                            AutoRetrainModel(numberOfActions);
                        }
                    }
                }
                else
                {
                    if (waitCount >= ((float)modelRetrainPeriodicDelay / serverObserveDelay))
                    {
                        AutoRetrainModel(numberOfActions);
                        waitCount = 0;

                        // Reload new settings here for consistency
                        settings                  = LoadSettings();
                        serverObserveDelay        = settings.ServerObserveDelay;
                        modelRetrainPeriodicDelay = settings.ModelRetrainPeriodicDelay;
                    }
                }
            }
        }