/
RNNMazeTraveler.cs
368 lines (299 loc) · 13.5 KB
/
RNNMazeTraveler.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RNN;
using RNN.Enums;
using uPLibrary.Networking.M2Mqtt;
using Newtonsoft.Json;
using uPLibrary.Networking.M2Mqtt.Messages;
using useAIble.Core.Models.GameData;
using System.Threading;
namespace useAIble.GameLibrary.Maze
{
public delegate void MazeCycleCompleteDelegate(int x, int y, bool bumpedIntoWall);
public delegate void MazeCycleErrorDelegate(string networkName);
public delegate void SessionStartedDelegate(double randomnessLeft);
public delegate void SetRandomnessLeftDelegate(double val);
public class RNNMazeTraveler : Traveler, IDisposable
{
private const string NETWORK_NAME = "AIMazeTraveler01";
private const double CORRECT_SCORE = 100;
private const double WRONG_SCORE = 0;
private rnn_network rnn_net;
private Int64 currentSessionID;
private Int64 currentCycleID;
private SetRandomnessLeftDelegate SetRandomnessLeft;
public event MazeCycleCompleteDelegate MazeCycleComplete;
public event MazeCycleErrorDelegate MazeCycleError;
public event SessionStartedDelegate SessionStarted;
private MqttClient OUTPUT_FROM_SERVER_MQTT_CLIENT;
public MazeInfo Maze { get; set; }
public int Temp_num_sessions { get; set; }
public int StartRandomness { get; set; }
public int EndRandomness { get; set; }
public bool Learn { get; set; }
public string USER_TOKEN { get; set; }
public rnn_network Rnetwork
{
get
{
return rnn_net;
}
}
public RNNMazeTraveler(MazeInfo maze, string token)
{
Maze = maze;
USER_TOKEN = token;
OUTPUT_FROM_SERVER_MQTT_CLIENT = new MqttClient("dev.useaible.com");
OUTPUT_FROM_SERVER_MQTT_CLIENT.Connect(Guid.NewGuid().ToString("N"));
OUTPUT_FROM_SERVER_MQTT_CLIENT.MqttMsgPublishReceived += (sender, e) => {
DisconnectMQTT();
};
OUTPUT_FROM_SERVER_MQTT_CLIENT.Subscribe(new string[] { token + "/disconnect" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
// restore db from file or template
string dbName = "RNN_maze_" + USER_TOKEN;
rnn_network.RestoreDB(dbName, true);
rnn_net = CreateOrLoadNetwork(dbName, Maze);
rnn_net.Temp_Num_Sessions = Temp_num_sessions;
rnn_net.StartRandomness = StartRandomness;
rnn_net.EndRandomness = EndRandomness;
}
public rnn_network CreateOrLoadNetwork(string databaseName, MazeInfo maze)
{
var rnn_net = new rnn_network(databaseName);
if (!rnn_net.LoadNetwork(maze.Name))
{
var inputs = new List<rnn_io>()
{
new rnn_io(rnn_net, "X", typeof(Int32).ToString(), 0, maze.Width - 1, RnnInputType.Distinct),
new rnn_io(rnn_net, "Y", typeof(Int32).ToString(), 0, maze.Height - 1, RnnInputType.Distinct),
//new rnn_io(rnn_net, "BumpedIntoWall", typeof(Boolean).ToString(), 0, 1)
};
var outputs = new List<rnn_io>()
{
new rnn_io(rnn_net, "Direction", typeof(Int16).ToString(), 0, 3)
};
rnn_net.NewNetwork(maze.Name, inputs, outputs);
}
return rnn_net;
}
public double RandomnessLeft
{
get
{
return rnn_net.RandomnessCurrentValue;
}
}
public double BumpedIntoWallPercentage
{
get
{
return rnn_net.GetBumpedIntoWallsCount(rnn_net.CurrentNetworkID, rnn_net.LastSessionID);
}
}
public void ResestStaticSessionData()
{
//rnn_network.SessionCountInitial = null;
if (rnn_net != null)
{
rnn_net.SessionCountInitial = 0;// rnn_net.SessionCount;
}
}
public void DisconnectMQTT()
{
if (OUTPUT_FROM_SERVER_MQTT_CLIENT != null && OUTPUT_FROM_SERVER_MQTT_CLIENT.IsConnected)
{
OUTPUT_FROM_SERVER_MQTT_CLIENT.Disconnect();
}
}
public void SendErrorViaMQTT(Exception e)
{
GameErrorMetadata gameError = new GameErrorMetadata()
{
Id = USER_TOKEN,
Exception = JsonConvert.SerializeObject(e)
};
if (OUTPUT_FROM_SERVER_MQTT_CLIENT != null && OUTPUT_FROM_SERVER_MQTT_CLIENT.IsConnected)
{
OUTPUT_FROM_SERVER_MQTT_CLIENT.Publish(USER_TOKEN + "/mazeMove", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(gameError)), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
}
}
/// <summary>
/// This method will let the AI play the maze for one game (windowless)
/// </summary>
///
int sessionCounter = 0;
public MazeCycleOutcome Travel(int currentSession = 1, CancellationToken? cancelToken = null)
{
sessionCounter++;
MazeGame game = new MazeGame();
game.InitGame(Maze);
game.traveler = this;
game.traveler.location.X = Maze.StartingPosition.X;
game.traveler.location.Y = Maze.StartingPosition.Y;
MazeCycleOutcome outcome = new MazeCycleOutcome();
bool isFirst = true;
//Start AI Training Cycle
currentSessionID = rnn_net.SessionStart();
//hack, should not have to do this anymore but the way the maz app was built I have to do it this way
rnn_net.SessionCount = currentSession - 1;
if (SessionStarted != null) SessionStarted(rnn_net.RandomnessCurrentValue);
var cycleCounter = 0;
var cycleOutputs = new List<int>();
dynamic outObj = null;
var sessionDone = false;
while (!outcome.GameOver)
{
if (cancelToken.HasValue && cancelToken.Value.IsCancellationRequested)
{
return outcome;
}
// start next AI's move
var inputs = new List<rnn_io_with_value>()
{
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "X"), game.traveler.location.X.ToString()),
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "Y"), game.traveler.location.Y.ToString()),
//new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "BumpedIntoWall"), ((isFirst) ? false.ToString() : outcome.BumpedIntoWall.ToString()))
};
isFirst = false;
// get AI output
rnn_cycle cycle = new rnn_cycle();
var aiResult = cycle.RunCycle(rnn_net, currentSessionID, inputs, Learn);
var direction = Convert.ToInt16(aiResult.CycleOutput.Outputs.First().Value);
// make the move
outcome = game.CycleMaze(direction, true);
// score AI
double score = ScoreAI(outcome, game.traveler);
rnn_net.ScoreCycle(aiResult.CycleOutput.CycleID, score);
if (MazeCycleComplete != null) MazeCycleComplete(game.traveler.location.X, game.traveler.location.Y, outcome.BumpedIntoWall);
cycleCounter++;
cycleOutputs.Add(direction);
if (cycleCounter == 100)
{
outObj = new { Moves = cycleOutputs, Session = sessionCounter, Done = false, SessionScore = 0 };
OUTPUT_FROM_SERVER_MQTT_CLIENT.Publish(USER_TOKEN + "/mazeMove", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(outObj)), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
cycleCounter = 0;
cycleOutputs = new List<int>();
}
else
{
sessionDone = true;
}
}
// compute final score
outcome.FinalScore = game.CalculateFinalScore(game.Moves);
if (sessionDone)
{
outObj = new { Moves = cycleOutputs, Session = sessionCounter, Done = true, SessionScore = outcome.FinalScore };
OUTPUT_FROM_SERVER_MQTT_CLIENT.Publish(USER_TOKEN + "/mazeMove", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(outObj)), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
}
System.Diagnostics.Debug.WriteLine($"Randomness left: {rnn_net.RandomnessCurrentValue}");
// End AI Training Cycle
rnn_net.SessionEnd(outcome.FinalScore);
return outcome;
}
private void Rnn_net_CycleComplete(rnn_cyclecomplete_args e)
{
if (e.RnnType != RnnNetworkType.Supervised) // Unsupervised & Predict only
{
currentCycleID = e.CycleOutput.CycleID;
var aiOutput = Convert.ToInt16(e.CycleOutput.Outputs.First().Value);
// queue AI's output - the Direction the AI will go
GameRef.DirectionsStack.Enqueue(aiOutput);
}
}
void GameRef_GameStartEvent(Traveler traveler, int currentIteration = 1)
{
try
{
//Start AI Training Cycle
currentSessionID = rnn_net.SessionStart();
//hack, should not have to do this anymore but the way the maz app was built I have to do it this way
rnn_net.SessionCount = currentIteration - 1;
if (SetRandomnessLeft != null)
{
SetRandomnessLeft(rnn_net.RandomnessCurrentValue);
}
//Make your first move
var initial_inputs = new List<rnn_io_with_value>()
{
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "X"), traveler.location.X.ToString()),
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "Y"), traveler.location.Y.ToString()),
//new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "BumpedIntoWall"), false.ToString())
};
rnn_cycle cycle = new rnn_cycle();
cycle.RunCycle(rnn_net, currentSessionID, initial_inputs, Learn);
}
catch (Exception)
{
if (MazeCycleError != null)
MazeCycleError(rnn_net.CurrentNetworkName);
}
}
public void GameRef_GameOverEvent(MazeGameFinalOutcome final)
{
System.Diagnostics.Debug.WriteLine($"Randomness left: {rnn_net.RandomnessCurrentValue}");
//End AI Training Cycle
rnn_net.SessionEnd(final.FinalScore);
}
void GameRef_GameCycleEvent(MazeCycleOutcome mazeCycle, Traveler traveler)
{
try
{
// A Cycle Ended, Mark the Score, then make your next move
// determine score
double score = ScoreAI(mazeCycle, traveler);
rnn_net.ScoreCycle(currentCycleID, score);
if (!mazeCycle.GameOver)
{
// start next AI's move
var inputs = new List<rnn_io_with_value>()
{
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "X"), traveler.location.X.ToString()),
new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "Y"), traveler.location.Y.ToString()),
//new rnn_io_with_value(rnn_net.Inputs.First(a => a.Name == "BumpedIntoWall"), mazeCycle.BumpedIntoWall.ToString())
};
rnn_cycle cycle = new rnn_cycle();
cycle.RunCycle(rnn_net, currentSessionID, inputs, Learn);
}
}
catch (Exception)
{
if (MazeCycleError != null)
MazeCycleError(rnn_net.CurrentNetworkName);
}
}
private double ScoreAI(MazeCycleOutcome mazeCycle, Traveler traveler)
{
double retVal = WRONG_SCORE;
if (mazeCycle.GameOver)
{
retVal = CORRECT_SCORE * 2;
}
else if (!mazeCycle.BumpedIntoWall)
{
retVal = CORRECT_SCORE;
}
return retVal;
}
override public void AcceptUIKeyDown(short direction)
{
//Ignore the human no input on AI driven games.
}
public void Dispose()
{
if (rnn_net != null)
{
rnn_net.CycleComplete -= Rnn_net_CycleComplete;
}
if (GameRef != null)
{
GameRef.GameCycleEvent -= GameRef_GameCycleEvent;
GameRef.GameStartEvent -= GameRef_GameStartEvent;
// GameRef.GameOverEvent -= GameRef_GameOverEvent;
}
}
}
}