Created
October 31, 2022 12:13
-
-
Save arafattehsin/3127941a02fd6e1d4e4b95c9fffa9549 to your computer and use it in GitHub Desktop.
This is a sample for the Simulator used for Azure Personalizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Simulator | |
{ | |
public class Simulator | |
{ | |
string[] occassionContextValues = new string[] { "birthday", "meal", "catchup" }; | |
string[] varietyContextValues = new string[] { "cake", "dessert", "cupcake", "shake" }; | |
private Random rand = new Random(DateTime.Now.Millisecond); | |
private int rewardCounter = 0; | |
public void SimulateEvents(int count) | |
{ | |
float sumRewards = 0; | |
for (int i = 0; i < count; i++) | |
{ | |
Console.Write(i.ToString() + ":"); | |
sumRewards = sumRewards + SimulateEvent(); | |
Console.Write("[{0}]", sumRewards / (i + 2)); | |
} | |
} | |
public float SimulateEvent() | |
{ | |
UserSimulator userSimulator = new UserSimulator(rand); | |
var currentContext = GetSimulatedContext(); | |
float reward = 0f; | |
try | |
{ | |
string simulationResponse = userSimulator.ReturnSimulatedAction(currentContext); | |
Personalizer.PersonalizerLibrary personalizerLibrary = new Personalizer.PersonalizerLibrary(); | |
var personalizerResponse = personalizerLibrary.GetRankResults(currentContext); | |
if (personalizerResponse.RewardActionId == simulationResponse) | |
{ | |
reward = 1f; | |
rewardCounter++; | |
} | |
Console.WriteLine($"For Context {userSimulator.GetKey((Dictionary<string,string>)currentContext[0])}: Personalizer suggested {personalizerResponse.RewardActionId}, simulation chose {simulationResponse} - reward: {reward} - count {rewardCounter}"); | |
personalizerLibrary.SubmitReward(personalizerResponse.EventId, reward); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
return reward; | |
} | |
public IList<object> GetSimulatedContext() | |
{ | |
IList<object> currentContext = new List<object>() { | |
new Dictionary<string, string>() | |
{ | |
{ "occassion", occassionContextValues[rand.Next(3)] }, | |
{ "variety", varietyContextValues[rand.Next(4)] }, | |
} | |
}; | |
return currentContext; | |
} | |
} | |
public class FoodContext | |
{ | |
public string occassion; | |
public string variety; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment