-
Notifications
You must be signed in to change notification settings - Fork 27
/
MazeAgent.cs
289 lines (254 loc) · 9.86 KB
/
MazeAgent.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
using System;
using System.Collections.Generic;
using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using MBaske.Sensors.Grid;
namespace MBaske.MazeExplorer
{
/// <summary>
/// Agent that navigates a <see cref="Maze"/>.
/// It has to cover as much ground as possible and find food items.
/// </summary>
public class MazeAgent : Agent
{
/// <summary>
/// Invoked on <see cref="OnEpisodeBegin"/>.
/// Triggers <see cref="Maze"/> randomization.
/// </summary>
public event Action EpisodeBeginEvent;
/// <summary>
/// Invoked on food item found. Will remove item from <see cref="Maze"/>.
/// </summary>
public event Action<Vector2Int> FoundFoodEvent;
[SerializeField]
[Tooltip("The number of grid cells the agent can observe in any cardinal direction. " +
"The resulting grid observation will always have odd dimensions, as the agent " +
"is located at its center position, e.g. radius = 10 results in grid size 21 x 21.")]
private int m_LookDistance = 10;
[SerializeField]
[Tooltip("Amount by which rewards diminish for staying on, or repeat visits to grid " +
"positions. Initial reward is 0.5 for every move onto a position the agent hasn't" +
"visited before. Episodes end when rewards drop to -0.5 on any position.")]
[Range(0, 1)]
private float m_RewardDecrement = 0.25f;
[SerializeField]
[Tooltip("The animation duration for every agent step at inference.")]
[Range(0, 0.5f)]
private float m_StepDuration = 0.1f;
private float m_StepTime;
[SerializeField]
[Tooltip("Select to enable action masking. Note that a model trained with action " +
"masking turned on may not behave optimally when action masking is turned off.")]
private bool m_MaskActions;
private const int c_Stay = 0;
private const int c_Up = 1;
private const int c_Down = 2;
private const int c_Left = 3;
private const int c_Right = 4;
private GridBuffer m_SensorBuffer;
private GridBuffer m_MazeBuffer;
// Current agent position on grid.
private Vector2Int m_GridPosition;
private Vector3 m_LocalPosNext;
private Vector3 m_LocalPosPrev;
private List<int> m_ValidActions;
private Vector2Int[] m_Directions;
private bool m_IsTraining;
// Whether the agent is currently requesting decisions.
// Agent is inactive during animation at inference.
private bool m_IsActive;
/// <inheritdoc/>
public override void Initialize()
{
m_IsTraining = Academy.Instance.IsCommunicatorOn;
m_ValidActions = new List<int>(5);
m_Directions = new Vector2Int[]
{
Vector2Int.zero,
Vector2Int.up,
Vector2Int.down,
Vector2Int.left,
Vector2Int.right
};
int length = m_LookDistance * 2 + 1;
// The ColorGridBuffer supports PNG compression.
m_SensorBuffer = new ColorGridBuffer(Maze.NumChannels, length, length);
var sensorComp = GetComponent<GridSensorComponent>();
sensorComp.GridBuffer = m_SensorBuffer;
// Labels for sensor debugging.
sensorComp.ChannelLabels = new List<ChannelLabel>()
{
new ChannelLabel("Wall", new Color32(0, 128, 255, 255)),
new ChannelLabel("Food", new Color32(64, 255, 64, 255)),
new ChannelLabel("Visited", new Color32(255, 64, 64, 255)),
};
}
/// <inheritdoc/>
public override void OnEpisodeBegin()
{
EpisodeBeginEvent.Invoke();
}
/// <summary>
/// Invoked by <see cref="Controller"/> after it randomized the <see cref="Maze"/>.
/// </summary>
/// <param name="buffer">The <see cref="Maze"/>'s <see cref="GridBuffer"/></param>
/// <param name="spawnPos">The agents spawn position on the grid</param>
public void StartEpisode(GridBuffer buffer, Vector2Int spawnPos)
{
m_MazeBuffer ??= buffer;
m_GridPosition = spawnPos;
m_LocalPosNext = new Vector3(spawnPos.x, 0, spawnPos.y);
transform.localPosition = m_LocalPosNext;
}
/// <inheritdoc/>
/// <summary>
/// Stores valid actions, so that the agent can be penalized for
/// invalid ones, in case <see cref="m_MaskActions"/> is set to false.
/// </summary>
public override void WriteDiscreteActionMask(IDiscreteActionMask actionMask)
{
m_ValidActions.Clear();
m_ValidActions.Add(c_Stay);
for (int action = 1; action < 5; action++)
{
bool isValid = m_MazeBuffer.TryRead(Maze.Wall,
m_GridPosition + m_Directions[action],
out float value) && value == 0; // no wall
if (isValid)
{
m_ValidActions.Add(action);
}
else if (m_MaskActions)
{
actionMask.SetActionEnabled(0, action, false);
}
}
}
/// <inheritdoc/>
public override void OnActionReceived(ActionBuffers actionBuffers)
{
var action = actionBuffers.DiscreteActions[0];
m_LocalPosPrev = m_LocalPosNext;
bool isDone;
if (m_ValidActions.Contains(action))
{
m_GridPosition += m_Directions[action];
m_LocalPosNext = new Vector3(m_GridPosition.x, 0, m_GridPosition.y);
// Reward/penalize depending on visit value.
isDone = ValidatePosition(true);
}
else
{
// Penalize invalid action, m_MaskActions = false.
AddReward(-1);
// Don't reward/penalize, but update visit value.
isDone = ValidatePosition(false);
}
if (isDone)
{
// Visit value for m_GridPosition reached maximum.
m_IsActive = false;
EndEpisode();
}
else if (m_IsTraining || action == c_Stay || m_StepDuration == 0)
{
// Immediate update.
transform.localPosition = m_LocalPosNext;
}
else
{
// Animate to next position.
m_IsActive = false;
m_StepTime = 0;
}
}
/// <inheritdoc/>
public override void Heuristic(in ActionBuffers actionsOut)
{
var discreteActionsOut = actionsOut.DiscreteActions;
discreteActionsOut[0] = c_Stay;
if (Input.GetKey(KeyCode.D))
{
discreteActionsOut[0] = c_Right;
}
if (Input.GetKey(KeyCode.W))
{
discreteActionsOut[0] = c_Up;
}
if (Input.GetKey(KeyCode.A))
{
discreteActionsOut[0] = c_Left;
}
if (Input.GetKey(KeyCode.S))
{
discreteActionsOut[0] = c_Down;
}
}
private void FixedUpdate()
{
if (m_IsActive)
{
UpdateSensorBuffer();
RequestDecision();
}
else if (m_StepDuration > 0)
{
m_StepTime += Time.fixedDeltaTime;
m_IsActive = m_StepTime >= m_StepDuration;
// Animate to next position.
transform.localPosition = Vector3.Lerp(m_LocalPosPrev,
m_LocalPosNext, m_StepTime / m_StepDuration);
}
else
{
// Wait one step before activating.
m_IsActive = true;
}
}
private bool ValidatePosition(bool rewardAgent)
{
// From 0 to +1.
float visitValue = m_MazeBuffer.Read(Maze.Visit, m_GridPosition);
m_MazeBuffer.Write(Maze.Visit, m_GridPosition,
Mathf.Min(1, visitValue + m_RewardDecrement));
if (rewardAgent)
{
// From +0.5 to -0.5.
AddReward(0.5f - visitValue);
if (m_MazeBuffer.Read(Maze.Food, m_GridPosition) == 1)
{
// Reward for finding food.
AddReward(1);
FoundFoodEvent.Invoke(m_GridPosition);
}
}
return visitValue == 1;
}
private void UpdateSensorBuffer()
{
m_SensorBuffer.Clear();
// Current FOV.
int xMin = m_GridPosition.x - m_LookDistance;
int xMax = m_GridPosition.x + m_LookDistance;
int yMin = m_GridPosition.y - m_LookDistance;
int yMax = m_GridPosition.y + m_LookDistance;
for (int mx = xMin; mx <= xMax; mx++)
{
int sx = mx - xMin;
for (int my = yMin; my <= yMax; my++)
{
int sy = my - yMin;
// TryRead -> FOV might extend beyond maze bounds.
if (m_MazeBuffer.TryRead(Maze.Wall, mx, my, out float wall))
{
// Copy maze -> sensor.
m_SensorBuffer.Write(Maze.Wall, sx, sy, wall);
m_SensorBuffer.Write(Maze.Food, sx, sy, m_MazeBuffer.Read(Maze.Food, mx, my));
m_SensorBuffer.Write(Maze.Visit, sx, sy, m_MazeBuffer.Read(Maze.Visit, mx, my));
}
}
}
}
}
}