Skip to content

Commit

Permalink
Added stopping criteria to patrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Puvikaran2001 committed Nov 11, 2024
1 parent d732ef4 commit b267bc1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ private void Start()
foreach (var (algorithmName, algorithm) in algorithms)
{
simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 4,
stopAfterDiff: false,
mapSpawner: generator => generator.GenerateMap(mapConfig),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsTogether(
buildingConfig,
Expand All @@ -155,6 +157,8 @@ private void Start()
}

simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 4,
stopAfterDiff: false,
mapSpawner: generator => generator.GenerateMap(mapConfig),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsAtPositions(
collisionMap: buildingConfig,
Expand All @@ -173,6 +177,8 @@ private void Start()
//Just code to make sure we don't get too many maps of the last one in the experiment
var dumpMap = new BuildingMapConfig(-1, widthInTiles: 50, heightInTiles: 50);
simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 4,
stopAfterDiff: false,
mapSpawner: generator => generator.GenerateMap(dumpMap),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsTogether(
buildingConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ private void Start()
foreach (var (algorithmName, algorithm) in algorithms)
{
simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 3,
stopAfterDiff: true,
mapSpawner: generator => generator.GenerateMap(mapConfig),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsTogether(
buildingConfig,
Expand All @@ -149,6 +151,8 @@ private void Start()
}

simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 3,
stopAfterDiff: true,
mapSpawner: generator => generator.GenerateMap(mapConfig),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsAtPositions(
collisionMap: buildingConfig,
Expand All @@ -167,6 +171,8 @@ private void Start()
//Just code to make sure we don't get too many maps of the last one in the experiment
var dumpMap = new BuildingMapConfig(-1, widthInTiles: 50, heightInTiles: 50);
simulator.EnqueueScenario(new MySimulationScenario(seed: 123,
totalCycles: 3,
stopAfterDiff: true,
mapSpawner: generator => generator.GenerateMap(dumpMap),
robotSpawner: (buildingConfig, spawner) => spawner.SpawnRobotsTogether(
buildingConfig,
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Simulation/ExplorationSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void SetScenario(ExplorationSimulationScenario scenario)

public override bool HasFinishedSim()
{
return ExplorationTracker.ExploredProportion > 0.99f;
return ExplorationTracker.ExploredProportion > 0.99f || SimulatedLogicTicks > 3600 * 10;
}

public override void OnSimulationFinished()
Expand Down
15 changes: 12 additions & 3 deletions Assets/Scripts/Simulation/PatrollingSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected override void AfterCollisionMapGenerated(PatrollingSimulationScenario
{
var patrollingMap = scenario.PatrollingMapFactory(new PatrollingMapSpawner(), _collisionMap);

PatrollingTracker = new PatrollingTracker(_collisionMap, patrollingVisualizer, this, scenario.RobotConstraints, patrollingMap);
PatrollingTracker = new PatrollingTracker(_collisionMap, patrollingVisualizer, this, scenario, patrollingMap);

patrollingVisualizer.SetPatrollingMap(patrollingMap);

Expand All @@ -32,8 +32,17 @@ protected override void AfterCollisionMapGenerated(PatrollingSimulationScenario

public override bool HasFinishedSim()
{
// TODO: Implement
return false;
if (_scenario.TotalCycles != PatrollingTracker.CompletedCycles)
{
return false;
}

if (!PatrollingTracker.StopAfterDiff)
{
return true;
}

return PatrollingTracker.AverageGraphDiffLastTwoCyclesProportion <= 0.025;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ namespace Maes.Simulation.SimulationScenarios
public sealed class PatrollingSimulationScenario : SimulationScenario<PatrollingSimulation, IPatrollingAlgorithm>
{
public PatrollingMapFactory PatrollingMapFactory { get; }
public int TotalCycles { get; }
public bool StopAfterDiff { get; }

public PatrollingSimulationScenario(
int seed,
SimulationEndCriteriaDelegate<PatrollingSimulation>? hasFinishedSim = null,
int totalCycles,
bool stopAfterDiff,
MapFactory? mapSpawner = null,
RobotFactory<IPatrollingAlgorithm>? robotSpawner = null,
RobotConstraints? robotConstraints = null,
Expand All @@ -26,11 +29,13 @@ public PatrollingSimulationScenario(
)
: base(seed,
robotSpawner ?? ((map, spawner) => spawner.SpawnRobotsTogether(map, seed, 1, Vector2Int.zero, _ => new ConscientiousReactiveAlgorithm())),
hasFinishedSim,
null,
mapSpawner,
robotConstraints,
statisticsFileName)
{
TotalCycles = totalCycles;
StopAfterDiff = stopAfterDiff;
PatrollingMapFactory = patrollingMapFactory ?? ((generator, map) => generator.GeneratePatrollingMapRectangleBased(map));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected SimulationScenario(
)
{
Seed = seed;
HasFinishedSim = hasFinishedSim ?? (simulation => simulation.HasFinishedSim() || simulation.SimulatedLogicTicks > 3600 * 10);
HasFinishedSim = hasFinishedSim ?? (simulation => simulation.HasFinishedSim());
// Default to generating a cave map when no map generator is specified
MapSpawner = mapSpawner ?? (generator => generator.GenerateMap(new CaveMapConfig(seed)));
RobotSpawner = robotSpawner;
Expand Down
10 changes: 7 additions & 3 deletions Assets/Scripts/Trackers/PatrollingTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Maes.Robot;
using Maes.Simulation;
using Maes.Simulation.SimulationScenarios;
using Maes.Statistics;

using UnityEngine;
Expand All @@ -31,14 +32,17 @@ public class PatrollingTracker : Tracker<PatrollingCell, PatrollingVisualizer, I

private List<float> GraphIdlenessList { get; } = new();
//TODO: TotalCycles is not set any where in the code
public int TotalCycles { get; set; } = 10;
public int TotalCycles { get; }
public bool StopAfterDiff { get; set; }

public PatrollingTracker(SimulationMap<Tile> collisionMap, PatrollingVisualizer visualizer, PatrollingSimulation patrollingSimulation, RobotConstraints constraints,
PatrollingMap map) : base(collisionMap, visualizer, constraints, tile => new PatrollingCell(isExplorable: !Tile.IsWall(tile.Type)))
public PatrollingTracker(SimulationMap<Tile> collisionMap, PatrollingVisualizer visualizer, PatrollingSimulation patrollingSimulation, PatrollingSimulationScenario scenario,
PatrollingMap map) : base(collisionMap, visualizer, scenario.RobotConstraints, tile => new PatrollingCell(isExplorable: !Tile.IsWall(tile.Type)))
{
PatrollingSimulation = patrollingSimulation;
Map = map;
Vertices = map.Vertices.ToDictionary(vertex => vertex.Position, vertex => new VertexDetails(vertex));
TotalCycles = scenario.TotalCycles;
StopAfterDiff = scenario.StopAfterDiff;

_visualizer.meshRenderer.enabled = false;
_currentVisualizationMode = new WaypointHeatMapVisualizationMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ protected override void AfterStart()
};
SelectVisualizationButton(WaypointHeatMapButton);

if (Simulation != null)
{
StoppingCriteriaToggle.isOn = Simulation.PatrollingTracker.StopAfterDiff;
}

StoppingCriteriaToggle.onValueChanged.AddListener(delegate
StoppingCriteriaToggle.onValueChanged.AddListener(toggleValue =>
{
//TODO: when the stopping criteria is toggled
if (Simulation != null)
{
Simulation.PatrollingTracker.StopAfterDiff = toggleValue;
}
});

WaypointHeatMapButton.onClick.AddListener(() =>
Expand Down

0 comments on commit b267bc1

Please sign in to comment.