public void TestBatchingByTime() { var serviceConfig = new DecisionServiceConfiguration<TestContext>("", new EpsilonGreedyExplorer<TestContext>(new TestPolicy(), epsilon: 0.2f, numActions: 10)) { BatchConfig = new BatchingConfiguration() { MaxDuration = TimeSpan.FromMilliseconds(100), MaxEventCount = 2, MaxBufferSizeInBytes = 10 * 1024 * 1024 } }; var service = new DecisionService<TestContext>(serviceConfig); string uniqueKey = "eventid"; service.ChooseAction(uniqueKey, new TestContext()); System.Threading.Thread.Sleep(200); service.ChooseAction(uniqueKey, new TestContext()); var batch = JsonConvert.DeserializeObject<EventBatch>(File.ReadAllText(batchOutputFile), new EventJsonConverter()); Assert.AreEqual(1, batch.Events.Count); }
public void IndexAsync() { AsyncManager.OutstandingOperations.Increment(); var explorer = new EpsilonGreedyExplorer <string>(new MartPolicy(), epsilon: .2f, numActions: 10); var serviceConfig = new DecisionServiceConfiguration <string>( authorizationToken: "c01ff675-5710-4814-a961-d03d2d6bce65", explorer: explorer); var service = new DecisionService <string>(serviceConfig); var rand = new Random(); for (int i = 0; i < 10; i++) { int context = rand.Next(100); string uniqueKey = i.ToString(); service.ChooseAction(uniqueKey, context.ToString()); service.ReportReward((float)(context % 2), uniqueKey); } service.Flush(); AsyncManager.OutstandingOperations.Decrement(); }
public static void SampleCodeUsingASAJoinServer() { // Create configuration for the decision service var serviceConfig = new DecisionServiceConfiguration<ExpandedContext, ExpandedActionDependentFeatures>( authorizationToken: "sample-code", explorer: new EpsilonGreedyExplorer<ExpandedContext>(new ExpandedPolicy(), epsilon: 0.8f)) { EventHubConnectionString = "", EventHubInputName = "", GetContextFeaturesFunc = ExpandedContext.GetFeaturesFromContext }; var service = new DecisionService<ExpandedContext, ExpandedActionDependentFeatures>(serviceConfig); //string uniqueKey = "sample-asa-client-"; string uniqueKey = "scratch-key-"; var rg = new Random(uniqueKey.GetHashCode()); for (int i = 1; i < 20; i++) { int numActions = rg.Next(5, 10); DateTime timeStamp = DateTime.UtcNow; string key = uniqueKey + Guid.NewGuid().ToString(); uint[] action = service.ChooseAction(new UniqueEventID { Key = key, TimeStamp = timeStamp }, ExpandedContext.CreateRandom(numActions, rg), (uint)numActions); service.ReportReward(i / 100f, new UniqueEventID { Key = key, TimeStamp = timeStamp }); System.Threading.Thread.Sleep(1); } service.Flush(); }
public void TestADFExplorationResult() { joinServer.Reset(); var dsConfig = new DecisionServiceConfiguration<TestADFContext, DummyADFType>( authorizationToken: MockCommandCenter.AuthorizationToken, explorer: new EpsilonGreedyExplorer<TestADFContext>(new TestADFPolicy(), epsilon: 0.5f)) { PollingForModelPeriod = TimeSpan.MinValue, PollingForSettingsPeriod = TimeSpan.MinValue, JoinServerType = JoinServerType.CustomSolution, LoggingServiceAddress = MockJoinServer.MockJoinServerAddress }; var ds = new DecisionService<TestADFContext, DummyADFType>(dsConfig); string uniqueKey = "eventid"; for (int i = 1; i <= 100; i++) { var adfContext = new TestADFContext(i); uint[] action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, adfContext, (uint)adfContext.ActionDependentFeatures.Count); Assert.AreEqual(i, action.Length); // verify all unique actions in the list Assert.AreEqual(action.Length, action.Distinct().Count()); // verify the actions are in the expected range Assert.AreEqual((i * (i + 1)) / 2, action.Sum(a => a)); ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey }); } ds.Flush(); Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count)); }
public void TestBatchingByCount() { var serviceConfig = new DecisionServiceConfiguration<TestContext>("", new EpsilonGreedyExplorer<TestContext>(new TestPolicy(), epsilon: 0.2f, numActions: 10)) { BatchConfig = new BatchingConfiguration() { MaxDuration = TimeSpan.FromDays(30), MaxEventCount = 2, MaxBufferSizeInBytes = 10 * 1024 * 1024 } }; var service = new DecisionService<TestContext>(serviceConfig); string uniqueKey = "eventid"; service.ChooseAction(uniqueKey, new TestContext()); service.ReportOutcome("my json outcome", uniqueKey); service.ReportReward(0.5f, uniqueKey); var batch = JsonConvert.DeserializeObject<EventBatch>(File.ReadAllText(batchOutputFile), new EventJsonConverter()); Assert.AreEqual(2, batch.Events.Count); }
public void TestADFModelUpdateFromFile() { joinServer.Reset(); var dsConfig = new DecisionServiceConfiguration<TestADFContextWithFeatures, TestADFFeatures>( authorizationToken: MockCommandCenter.AuthorizationToken, explorer: new EpsilonGreedyExplorer<TestADFContextWithFeatures>(new TestADFWithFeaturesPolicy(), epsilon: 0.5f)) { JoinServerType = JoinServerType.CustomSolution, LoggingServiceAddress = MockJoinServer.MockJoinServerAddress, PollingForModelPeriod = TimeSpan.MinValue, PollingForSettingsPeriod = TimeSpan.MinValue }; var ds = new DecisionService<TestADFContextWithFeatures, TestADFFeatures>(dsConfig); string uniqueKey = "eventid"; string modelFile = "test_vw_adf{0}.model"; var actualModelFiles = new List<string>(); for (int i = 1; i <= 100; i++) { Random rg = new Random(i); if (i % 50 == 1) { int modelIndex = i / 50; string currentModelFile = string.Format(modelFile, modelIndex); byte[] modelContent = commandCenter.GetCBADFModelBlobContent(numExamples: 3 + modelIndex, numFeatureVectors: 4 + modelIndex); System.IO.File.WriteAllBytes(currentModelFile, modelContent); ds.UpdatePolicy(new VWPolicy<TestADFContextWithFeatures, TestADFFeatures>(GetFeaturesFromContext, currentModelFile)); actualModelFiles.Add(currentModelFile); } int numActions = rg.Next(5, 20); var context = TestADFContextWithFeatures.CreateRandom(numActions, rg); uint[] action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, context, (uint)context.ActionDependentFeatures.Count); Assert.AreEqual(numActions, action.Length); // verify all unique actions in the list Assert.AreEqual(action.Length, action.Distinct().Count()); // verify the actions are in the expected range Assert.AreEqual((numActions * (numActions + 1)) / 2, action.Sum(a => a)); ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey }); } ds.Flush(); Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count)); foreach (string actualModelFile in actualModelFiles) { System.IO.File.Delete(actualModelFile); } }
public void TestRcv1ModelUpdateFromStream() { joinServer.Reset(); int numActions = 10; int numFeatures = 1024; var dsConfig = new DecisionServiceConfiguration<TestRcv1Context>( authorizationToken: MockCommandCenter.AuthorizationToken, explorer: new EpsilonGreedyExplorer<TestRcv1Context>(new TestRcv1ContextPolicy(), epsilon: 0.5f, numActions: (uint)numActions)) { JoinServerType = JoinServerType.CustomSolution, LoggingServiceAddress = MockJoinServer.MockJoinServerAddress, PollingForModelPeriod = TimeSpan.MinValue, PollingForSettingsPeriod = TimeSpan.MinValue }; var ds = new DecisionService<TestRcv1Context>(dsConfig); string uniqueKey = "eventid"; string modelFile = "test_vw_adf{0}.model"; for (int i = 1; i <= 100; i++) { Random rg = new Random(i); if (i % 50 == 1) { int modelIndex = i / 50; string currentModelFile = string.Format(modelFile, modelIndex); byte[] modelContent = commandCenter.GetCBModelBlobContent(numExamples: 3 + modelIndex, numFeatures: numFeatures, numActions: numActions); var modelStream = new MemoryStream(modelContent); ds.UpdatePolicy(new VWPolicy<TestRcv1Context>(modelStream)); } var context = TestRcv1Context.CreateRandom(numActions, numFeatures, rg); DateTime timeStamp = DateTime.UtcNow; uint action = ds.ChooseAction(new UniqueEventID { Key = uniqueKey }, context); // verify the actions are in the expected range Assert.IsTrue(action >= 1 && action <= numActions); ds.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey }); } ds.Flush(); Assert.AreEqual(200, joinServer.EventBatchList.Sum(b => b.ExperimentalUnitFragments.Count)); }
public static void SampleCodeUsingActionDependentFeatures() { // Create configuration for the decision service var serviceConfig = new DecisionServiceConfiguration<ADFContext, ADFFeatures>( authorizationToken: "", explorer: new EpsilonGreedyExplorer<ADFContext>(new ADFPolicy(), epsilon: 0.8f)) { PollingForModelPeriod = TimeSpan.MinValue, PollingForSettingsPeriod = TimeSpan.MinValue }; var service = new DecisionService<ADFContext, ADFFeatures>(serviceConfig); string uniqueKey = "eventid"; var rg = new Random(uniqueKey.GetHashCode()); var vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext); for (int i = 1; i < 100; i++) { if (i == 30) { string vwModelFile = TrainNewVWModelWithRandomData(numExamples: 5, numActions: 10); vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext, vwModelFile); // Alternatively, VWPolicy can also be loaded from an IO stream: // var vwModelStream = new MemoryStream(File.ReadAllBytes(vwModelFile)); // vwPolicy = new VWPolicy<ADFContext, ADFFeatures>(GetFeaturesFromContext, vwModelStream); // Manually updates decision service with a new policy for consuming VW models. service.UpdatePolicy(vwPolicy); } if (i == 60) { string vwModelFile = TrainNewVWModelWithRandomData(numExamples: 6, numActions: 8); // Evolves the existing VWPolicy with a new model vwPolicy.ModelUpdate(vwModelFile); } int numActions = rg.Next(5, 10); uint[] action = service.ChooseAction(new UniqueEventID { Key = uniqueKey }, ADFContext.CreateRandom(numActions, rg), (uint)numActions); service.ReportReward(i / 100f, new UniqueEventID { Key = uniqueKey }); } service.Flush(); }