Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations and Cleanup #191

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Assets/EditTests/CoverageCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ namespace EditTests
public class CoverageCalculatorTest
{

private CoverageCalculator<ExplorationCell> _coverageCalculator;
private CoverageCalculator _coverageCalculator;
private SimulationMap<Tile> _collisionMap;
private SimulationMap<ExplorationCell> _explorationMap;
private SimulationMap<Cell> _explorationMap;
private const int RandomSeed = 123;
private const int Width = 50, Height = 50;

[SetUp]
public void InitializeCalculatorAndMaps()
{
_collisionMap = GenerateCollisionMap();
_explorationMap = _collisionMap.FMap(tile => new ExplorationCell(!Tile.IsWall(tile.Type)));
_coverageCalculator = new CoverageCalculator<ExplorationCell>(_explorationMap, _collisionMap);
_explorationMap = _collisionMap.FMap(tile => new Cell(!Tile.IsWall(tile.Type)));
_coverageCalculator = new CoverageCalculator(_explorationMap, _collisionMap);
}

// Generates a collision map where only the edge tiles are solid
Expand Down Expand Up @@ -114,7 +114,7 @@ public void AdjacentTilesAreCoveredTest(float robotX, float robotY)
var robotWorldPos = new Vector2(robotX, robotY);

// Find all cells that are immediate neighbours of tile currently occupied by the robot
var cells = new List<ExplorationCell>();
var cells = new List<Cell>();
for (var x = -1; x < 1; x++)
{
for (var y = -1; y < 1; y++)
Expand Down Expand Up @@ -177,7 +177,7 @@ public void CoverageTimeUpdateTest()
public void TilesAreMarkedCoverableCorrectly()
{
// Copy the existing exploration map and to get a new map where all CanBeCovered flags are true
var freshExplorationMap = _explorationMap.FMap((cell) => new ExplorationCell(cell.IsExplorable));
var freshExplorationMap = _explorationMap.FMap((cell) => new Cell(cell.IsExplorable));
for (var x = 0; x < Width; x++)
{
for (var y = 0; y < Height; y++)
Expand All @@ -186,7 +186,7 @@ public void TilesAreMarkedCoverableCorrectly()
}
}

new CoverageCalculator<ExplorationCell>(freshExplorationMap, _collisionMap);
new CoverageCalculator(freshExplorationMap, _collisionMap);
// Pass the new exploration map to the coverage calculator which should cause the solid tiles to be
// marked as non-coverable
for (var x = 0; x < Width; x++)
Expand Down
10 changes: 5 additions & 5 deletions Assets/PlayModeTests/EstimateTickTest/EstimateTestTurnsPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ namespace PlayModeTests.EstimateTickTest
using MySimulationScenario = ExplorationSimulationScenario;
using MySimulator = ExplorationSimulator;

[TestFixture(0.5f, 5, 5, -42)]
[TestFixture(0.5f, 10, 10, -39)]
[TestFixture(0.5f, 5, 5, -41)]
[TestFixture(0.5f, 10, 10, -42)]
[TestFixture(0.5f, 20, 20, -31)]
[TestFixture(0.5f, 15, 15, -31)]

[TestFixture(1.0f, 5, 5, -17)]
[TestFixture(1.0f, 10, 10, -27)]
[TestFixture(1.0f, 10, 10, -33)]
[TestFixture(1.0f, 20, 20, -29)]
[TestFixture(1.0f, 15, 15, -23)]

[TestFixture(1.5f, 5, 5, -36)]
[TestFixture(1.5f, 10, 10, -43)]
[TestFixture(1.5f, 5, 5, -46)]
[TestFixture(1.5f, 10, 10, -53)]
[TestFixture(1.5f, 15, 15, -39)]

public class EstimateTestTurnsPath
Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/CustomScriptsAssembly.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"rootNamespace": "Maes",
"references": [
"GUID:acf5d101701ab1987b7babf0fae71e0a",
"GUID:b3f49edfedc855a48aa1a9e5d3cba438",
"GUID:625bfc588fb96c74696858f2c467e978",
"GUID:1da8e23352f14494396eac5033ba9894",
"GUID:ba58c43074cf72549aa2ac5937dae44b",
"GUID:5db6cbfd5d0e86a46862a70f4b4067f2",
"GUID:1da8e23352f14494396eac5033ba9894",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:6a9ab44e5e0403d4ebaf2dee2f9f1172",
"GUID:dd8043639e4014317a7246f064330196"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// - Proposed fix: When finding the nearest unexplored tile, the robot could exclude tiles which are close to other robots.
// - The anti-wall collision 'CollisionCorrector()' is primitive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -66,7 +67,7 @@ private void ShareSlamMap()
{
Debug.Log("Received slam");
var combinedMessage = receivedHeartbeats[0];
foreach (var message in receivedHeartbeats[1..])
foreach (var message in receivedHeartbeats.AsSpan(1))
{
combinedMessage = combinedMessage.Combine(message, _logicTicks);
}
Expand Down
65 changes: 34 additions & 31 deletions Assets/Scripts/ExplorationAlgorithm/Minotaur/EdgeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,12 @@ namespace Maes.ExplorationAlgorithm.Minotaur
{
internal class EdgeDetector
{
public EdgeState State => UpdateState();
public bool isStuck => GetTilesAroundPoint(_edgeSize, _defaultLimitors).Where(tile => _coarseMap.GetTileStatus(tile) != SlamTileStatus.Open).Any();

private readonly SlamMap _slamMap;
private readonly CoarseGrainedMap _coarseMap;
private readonly int _edgeSize;
private readonly int _visionRange;
private readonly List<SlamTileStatus> _defaultLimitors = new List<SlamTileStatus> { SlamTileStatus.Solid };
private Vector2Int _robotPosition => _coarseMap.GetCurrentPosition();

public enum EdgeState
{
Forward,
ForwardLeft,
ForwardRight,
}
private static readonly SlamTileStatus[] _defaultLimiters = { SlamTileStatus.Solid };
private Vector2Int RobotPosition => _coarseMap.GetCurrentPosition();

public EdgeDetector(SlamMap map, float visionRange)
{
Expand All @@ -59,11 +49,6 @@ public EdgeDetector(SlamMap map, float visionRange)
_visionRange = (int)visionRange;
}

private EdgeState UpdateState()
{
return EdgeState.ForwardRight;
}

public Vector2Int? GetNearestUnseenTile()
{
var angles = Enumerable.Range(0, 360).ToList();
Expand All @@ -74,7 +59,7 @@ private EdgeState UpdateState()
var removedAngles = new List<int>();
foreach (var angle in angles)
{
var tile = GetFurthestTileAroundPoint(_coarseMap.GetApproximateGlobalDegrees() + angle, range, _defaultLimitors);
var tile = GetFurthestTileAroundPoint(_coarseMap.GetApproximateGlobalDegrees() + angle, range, _defaultLimiters);

if (_coarseMap.GetTileStatus(tile) == SlamTileStatus.Solid)
{
Expand All @@ -92,7 +77,7 @@ private EdgeState UpdateState()
}
if (unseenTiles.Any())
{
return unseenTiles.OrderBy(tile => Vector2.Distance(_robotPosition, tile)).First();
return unseenTiles.OrderBy(tile => Vector2.Distance(RobotPosition, tile)).First();
}
range++;
}
Expand All @@ -102,15 +87,15 @@ private EdgeState UpdateState()
/// <summary>
/// Gets the tiles around the robot by casting 360-<paramref name="startAngle"/> rays. These rays expand from the robot and out being stopped by the <paramref name="limiters"/>.
/// <para></para>
/// If only one ray is desired, consider <seealso cref="GetFurthestTileAroundPoint(float, int, List{SlamTileStatus}, Vector2Int?, bool, bool)"/>
/// If only one ray is desired, consider <seealso cref="GetFurthestTileAroundPoint(float, int, SlamTileStatus[], Vector2Int?, bool, bool)"/>
/// </summary>
/// <param name="range">The distance of the ray</param>
/// <param name="limiters">What tiles should stop the rays</param>
/// <param name="point"></param>
/// <param name="slamPrecision">Target slam tiles instead of coarse tiles</param>
/// <param name="startAngle">If set above 0 then this will create arcs instead of circles around the robot, based on <see cref="Vector2.right"/> counter-clockwise</param>
/// <returns>The unique tiles that were hit</returns>
public HashSet<Vector2Int> GetTilesAroundPoint(int range, List<SlamTileStatus> limiters, Vector2Int? point = null, bool slamPrecision = false, int startAngle = 0)
public HashSet<Vector2Int> GetTilesAroundPoint(int range, SlamTileStatus[] limiters, Vector2Int? point = null, bool slamPrecision = false, int startAngle = 0)
{
IPathFindingMap map = slamPrecision ? _slamMap : _coarseMap;
var tiles = new HashSet<Vector2Int>();
Expand All @@ -126,7 +111,7 @@ public HashSet<Vector2Int> GetTilesAroundPoint(int range, List<SlamTileStatus> l
return tiles;
}

public Vector2Int GetFurthestTileAroundPoint(float angle, int range, List<SlamTileStatus> limiters, Vector2Int? point = null, bool snapToGrid = false, bool slamPrecision = false)
public Vector2Int GetFurthestTileAroundPoint(float angle, int range, SlamTileStatus[] limiters, Vector2Int? point = null, bool snapToGrid = false, bool slamPrecision = false)
{
Vector2Int position;
if (point.HasValue)
Expand All @@ -135,7 +120,7 @@ public Vector2Int GetFurthestTileAroundPoint(float angle, int range, List<SlamTi
}
else
{
position = slamPrecision ? _slamMap.GetCurrentPosition() : _robotPosition;
position = slamPrecision ? _slamMap.GetCurrentPosition() : RobotPosition;
}
IPathFindingMap map = slamPrecision ? _slamMap : _coarseMap;
var tile = position;
Expand All @@ -160,20 +145,38 @@ public Vector2Int GetFurthestTileAroundPoint(float angle, int range, List<SlamTi

public IEnumerable<Vector2Int> GetBoxAroundRobot()
{
var boxTileList = new List<Vector2Int>();
for (var x = -_edgeSize; x <= _edgeSize; x++)
{
for (var y = _edgeSize; y <= _edgeSize + 1; y++)
{
boxTileList.Add(_robotPosition + new Vector2Int(x, y));
boxTileList.Add(_robotPosition + new Vector2Int(x, -y));
boxTileList.Add(_robotPosition + new Vector2Int(y, x));
boxTileList.Add(_robotPosition + new Vector2Int(-y, x));
var rx = RobotPosition.x;
var ry = RobotPosition.y;

var xy = new Vector2Int(rx + x, ry + y);
if (_coarseMap.IsWithinBounds(xy))
{
yield return xy;
}

var xmy = new Vector2Int(rx + x, ry - y);
if (_coarseMap.IsWithinBounds(xmy))
{
yield return xmy;
}

var yx = new Vector2Int(rx + y, ry + x);
if (_coarseMap.IsWithinBounds(yx))
{
yield return yx;
}

var myx = new Vector2Int(rx - y, ry + x);
if (_coarseMap.IsWithinBounds(myx))
{
yield return myx;
}
}
}
return boxTileList.Where(tile => _coarseMap.IsWithinBounds(tile));
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public BiddingMessage Combine(BiddingMessage biddingMessage, MinotaurAlgorithm m
{
if (biddingMessage._doorway == _doorway && biddingMessage._requestorRobotID == _requestorRobotID)
{
foreach ((var key, var value) in biddingMessage._allBids)
foreach (var (key, value) in biddingMessage._allBids)
{
_allBids[key] = value; //This would overwrite bid values for the requestor and doorway, if it somehow changes, through it shouldnt
}
Expand All @@ -73,7 +73,7 @@ public IMinotaurMessage Process(MinotaurAlgorithm minotaur)
{
var orderedBids = _allBids.OrderBy(key => key.Value);
var winnerbids = orderedBids.Take((_allBids.Count + 1) / 2);
foreach ((var key, var value) in winnerbids)
foreach (var (key, value) in winnerbids)
{
winnerIDList.Add(key);
}
Expand Down
Loading
Loading