-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCommunicationTest.cs
247 lines (209 loc) · 9.56 KB
/
CommunicationTest.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
// Copyright 2024 MAES
//
// This file is part of MAES
//
// MAES is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MAES is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with MAES. If not, see http://www.gnu.org/licenses/.
//
// Contributors: Rasmus Borrisholt Schmidt, Andreas Sebastian Sørensen, Thor Beregaard, Malte Z. Andreasen, Philip I. Holler and Magnus K. Jensen,
//
// Original repository: https://github.com/Molitany/MAES
using System.Collections;
using System.Collections.Generic;
using Maes;
using Maes.Map.MapGen;
using Maes.Robot;
using NUnit.Framework;
using UnityEngine;
using Random = System.Random;
namespace PlayModeTests
{
public class CommunicationTest
{
private const int MapWidth = 50, MapHeight = 50;
private const int RandomSeed = 123;
private Simulator _maes;
private Simulation _simulation;
private List<TestingAlgorithm> _robotTestAlgorithms;
private Tile[,] GenerateMapWithHorizontalWallInMiddle(int wallThicknessInTiles)
{
Tile[,] bitmap = new Tile[MapWidth, MapHeight];
int firstWallRowY = MapHeight / 2;
int lastWallRowY = firstWallRowY + wallThicknessInTiles;
Tile.Rand = new Random(RandomSeed);
var wall = Tile.GetRandomWall();
for (int x = 0; x < MapWidth; x++)
{
for (int y = 0; y < MapHeight; y++)
{
var isSolid = y >= firstWallRowY && y <= lastWallRowY - 1;
bitmap[x, y] = isSolid ? wall : new Tile(TileType.Room);
}
}
return bitmap;
}
[TearDown]
public void ClearSimulator()
{
Simulator.Destroy();
}
private void InitSimulator(MapFactory mapFactory,
RobotConstraints.SignalTransmissionSuccessCalculator transmissionSuccessCalculatorFunc,
List<Vector2Int> robotSpawnPositions)
{
_robotTestAlgorithms = new List<TestingAlgorithm>();
var testingScenario = new SimulationScenario(RandomSeed,
mapSpawner: mapFactory,
hasFinishedSim: simulation => false,
robotConstraints: new RobotConstraints(materialCommunication: false, calculateSignalTransmissionProbability: transmissionSuccessCalculatorFunc),
robotSpawner: (map, spawner) => spawner.SpawnRobotsAtPositions(robotSpawnPositions, map, RandomSeed, 2,
robotSeed => {
var algorithm = new TestingAlgorithm();
_robotTestAlgorithms.Add(algorithm);
return algorithm;
}));
_maes = Simulator.GetInstance();
_maes.EnqueueScenario(testingScenario);
_simulation = _maes.GetSimulationManager().CurrentSimulation;
// The first robot will broadcast immediatealy
_robotTestAlgorithms[0].UpdateFunction = (tick, controller) => {
if (tick == 0) controller.Broadcast("Test Message");
};
// The second robot will continuously receive broadcasts
_robotTestAlgorithms[0].UpdateFunction = (tick, controller) => {
controller.ReceiveBroadcast();
};
}
[Test(ExpectedResult = null)]
public IEnumerator Broadcast_TransmissionSuccessTest()
{
InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(distance, wallDistance) => true,
new List<Vector2Int> { new Vector2Int(2, 2), new Vector2Int(6, 6) });
string receivedMessage = null;
string sentMessage = "message sent between robots 1 and 2";
var algorithm1 = _robotTestAlgorithms[0];
var algorithm2 = _robotTestAlgorithms[1];
algorithm1.UpdateFunction = (tick, controller) => {
if (tick == 0) controller.Broadcast(sentMessage);
};
algorithm2.UpdateFunction = (tick, controller) => {
var results = controller.ReceiveBroadcast();
if (results.Count != 0) receivedMessage = results[0] as string;
};
_maes.PressPlayButton();
// Wait until the message has been broadcast
while (_simulation.SimulatedLogicTicks < 2)
{
yield return null;
}
Assert.AreEqual(sentMessage, receivedMessage);
}
[Test(ExpectedResult = null)]
public IEnumerator Broadcast_TransmissionFailedTest()
{
InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(distance, wallDistance) => false, // Always fail communication
new List<Vector2Int> { new Vector2Int(2, 2), new Vector2Int(6, 6) });
string receivedMessage = null;
string sentMessage = "message sent between robots 1 and 2";
var algorithm1 = _robotTestAlgorithms[0];
var algorithm2 = _robotTestAlgorithms[1];
algorithm1.UpdateFunction = (tick, controller) => {
if (tick == 0) controller.Broadcast(sentMessage);
};
algorithm2.UpdateFunction = (tick, controller) => {
var results = controller.ReceiveBroadcast();
if (results.Count != 0) receivedMessage = results[0] as string;
};
_maes.PressPlayButton();
// Wait until the message has been broadcast
while (_simulation.SimulatedLogicTicks < 2)
{
yield return null;
}
Assert.IsNull(receivedMessage);
}
[Test(ExpectedResult = null)]
public IEnumerator Broadcast_NoWallsCommunicationTest()
{
float foundWallDistance = float.PositiveInfinity;
InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(distance, wallDistance) => {
foundWallDistance = wallDistance;
return true;
},
new List<Vector2Int> { new Vector2Int(-10, -10), new Vector2Int(10, 10) });
_maes.PressPlayButton();
// Wait until the message has been broadcast
while (_simulation.SimulatedLogicTicks < 2)
{
yield return null;
}
// Assert that the signal is said to travel through 1 meter/unit of wall
Assert.AreEqual(foundWallDistance, 0f, 0.001f);
}
[Test(ExpectedResult = null)]
[TestCase(20, 20, ExpectedResult = null)]
[TestCase(5, 5, ExpectedResult = null)]
[TestCase(5, -5, ExpectedResult = null)]
[TestCase(-5, 5, ExpectedResult = null)]
[TestCase(-5, -5, ExpectedResult = null)]
public IEnumerator Broadcast_CorrectDistanceCalculation(int secondRobotX, int secondRobotY)
{
float transmissionDistance = float.PositiveInfinity;
Vector2Int firstRobotPosition = new Vector2Int(0, 0);
Vector2Int secondRobotPosition = new Vector2Int(secondRobotX, secondRobotY);
float actualDistance = (firstRobotPosition - secondRobotPosition).magnitude;
InitSimulator(StandardTestingConfiguration.EmptyCaveMapSpawner(RandomSeed),
(distance, wallDistance) => {
transmissionDistance = distance;
return true;
},
new List<Vector2Int> { firstRobotPosition, secondRobotPosition });
_maes.PressPlayButton();
// Wait until the message has been broadcast
while (_simulation.SimulatedLogicTicks < 2)
{
yield return null;
}
// Assert that the signal is said to travel through 1 meter/unit of wall
Assert.AreEqual(actualDistance, transmissionDistance, 0.001f);
}
[Test(ExpectedResult = null)]
[TestCase(1, ExpectedResult = null)]
[TestCase(2, ExpectedResult = null)]
[TestCase(5, ExpectedResult = null)]
[TestCase(10, ExpectedResult = null)]
public IEnumerator Broadcast_WallDistanceIsApproximatelyCorrect(int wallThickness)
{
float foundWallDistance = float.PositiveInfinity;
InitSimulator(
generator => generator.GenerateMap(GenerateMapWithHorizontalWallInMiddle(wallThickness), RandomSeed, borderSize: 2),
transmissionSuccessCalculatorFunc:
(distance, wallDistance) => {
foundWallDistance = wallDistance;
return true;
},
new List<Vector2Int> { new Vector2Int(0, -2), new Vector2Int(0, 3 + wallThickness) });
_maes.PressPlayButton();
// Wait until the message has been broadcast
while (_simulation.SimulatedLogicTicks < 5)
{
yield return null;
}
// Assert that the signal is said to travel through 1 meter/unit of wall
Assert.AreEqual((float)wallThickness, foundWallDistance, 0.1f * wallThickness);
}
}
}