/
RLSimulator.cs
141 lines (112 loc) · 4.44 KB
/
RLSimulator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Rl.Net;
namespace Rl.Net.Cli {
public enum Topic : long
{
HerbGarden,
MachineLearning
}
internal static class RankingResponseExtensions
{
public static string ToDistributionString(this RankingResponse response)
{
StringBuilder stringBuilder = new StringBuilder("(");
foreach (ActionProbability actionProbability in response)
{
stringBuilder.Append($"[{actionProbability.ActionIndex}, {actionProbability.Probability}]");
}
stringBuilder.Append(')');
return stringBuilder.ToString();
}
}
internal class RLSimulator
{
public static readonly Random RandomSource = new Random();
private LiveModel liveModel;
private RankingResponse responseContainer;
private ApiStatus apiStatusContainer;
private StatisticsCalculator stats;
public RLSimulator(LiveModel liveModel)
{
this.liveModel = liveModel;
}
public TimeSpan StepInterval
{
get;
set;
} = TimeSpan.FromSeconds(2);
public void Run(int steps = -1)
{
this.responseContainer = new RankingResponse();
this.apiStatusContainer = new ApiStatus();
this.stats = new StatisticsCalculator();
int stepsSoFar = 0;
while (steps < 0 || (stepsSoFar++ < steps))
{
this.Step();
}
}
public event EventHandler<ApiStatus> OnError;
private void Step()
{
Person person = GetRandomPerson();
string decisionContext = CreateDecisionContext(person);
Guid eventId = Guid.NewGuid();
if (!liveModel.TryChooseRank(eventId.ToString(), decisionContext, this.responseContainer, this.apiStatusContainer))
{
this.SafeRaiseError(this.apiStatusContainer);
}
long actionId = -1;
if (!responseContainer.TryGetChosenAction(out actionId, this.apiStatusContainer))
{
this.SafeRaiseError(this.apiStatusContainer);
}
Topic actionTopic = (Topic)actionId;
float outcome = person.GenerateOutcome(actionTopic);
if (!liveModel.TryReportOutcome(eventId.ToString(), outcome, this.apiStatusContainer))
{
this.SafeRaiseError(this.apiStatusContainer);
}
// TODO: Record stats
this.stats.Record(person, actionTopic, outcome);
Console.WriteLine($" {this.stats.TotalActions}, ctxt, {person.Id}, action, {actionTopic}, outcome, {outcome}, dist, {responseContainer.ToDistributionString()}, {this.stats.GetStats(person, actionTopic)}");
}
private void SafeRaiseError(ApiStatus errorStatus)
{
EventHandler<ApiStatus> localHandler = this.OnError;
if (localHandler != null)
{
localHandler(this, errorStatus);
}
}
private static Func<Topic, float> GenerateRewardDistribution(float herbGardenProbability, float machineLearningProbability)
{
Dictionary<Topic, float> topicProbabilities = new Dictionary<Topic, float>
{
{ Topic.HerbGarden, herbGardenProbability },
{ Topic.MachineLearning, machineLearningProbability }
};
return (topic) => topicProbabilities[topic];
}
internal static Person[] People = new []
{
new Person("rnc", "engineering", "hiking", "spock", GenerateRewardDistribution(0.03f, 0.1f)),
new Person("mk", "psychology", "kids", "7of9", GenerateRewardDistribution(0.3f, 0.1f))
};
internal static readonly Topic[] ActionSet = new [] { Topic.HerbGarden, Topic.MachineLearning };
private static readonly string ActionsJson = String.Join(",", ActionSet.Select(topic => $"{{ \"TAction\": {{ \"topic\": \"{topic}\" }} }}"));
private string CreateDecisionContext(Person p, params Topic[] topics)
{
return $"{{ {p.FeaturesJson}, \"_multi\": [{ ActionsJson }] }}";
}
private Person GetRandomPerson()
{
int index = RandomSource.Next(People.Length);
return People[index];
}
}
}