Created Mine Map and ReactivePlanner

This commit is contained in:
2020-12-15 18:19:07 -07:00
parent 45eca3b572
commit 4edcef4664
24 changed files with 309 additions and 84 deletions

View File

@@ -19,7 +19,7 @@
</ItemGroup>
<ItemGroup>
<None Update="Vehicle\VehicleConfiguration.json">
<None Update="Sim\VehicleConfiguration.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@@ -1,7 +1,6 @@
using System.Text.Json;
using System.Threading;
using ConsoleApp.Maps;
using ConsoleApp.PathPlanners;
using ConsoleApp.Sim;
using DryIoc;
namespace ConsoleApp
@@ -15,6 +14,7 @@ namespace ConsoleApp
container.Register<IVehicle, Vehicle>(Reuse.Singleton);
container.Register<IJsonDeserializor, JsonDeserializor>(Reuse.Singleton);
container.Register<IPathPlanner, PathPlanner>(Reuse.Singleton);
container.Register<IReactivePathPlanner, ReactivePathPlanner>(Reuse.Singleton);
}

View File

@@ -2,16 +2,22 @@ using System;
namespace ConsoleApp.Maps
{
public enum Coverage
{
Uncoverd,
Covered
}
public struct Cell : ICell
{
public int X { get; }
public int Y { get; }
public Coverage Coverage { get; set; }
public Cell(int x, int y)
{
X = x;
Y = y;
Coverage = Coverage.Uncoverd;
}

View File

@@ -4,5 +4,6 @@ namespace ConsoleApp.Maps
{
int X { get; }
int Y { get; }
Coverage Coverage { get; set; }
}
}

View File

@@ -8,8 +8,9 @@ namespace ConsoleApp.Maps
int Height { get; }
int Width { get; }
int CellWidth { get; }
void GenerateMaps(int x, int y);
void GenerateMaps(int x, int y, double minePercentage);
IHexMap GetHexMap();
ISquareMap GetSquareMap();
IMineMap GetMineMap();
}
}

View File

@@ -0,0 +1,7 @@
namespace ConsoleApp.Maps
{
public interface IMineMap
{
bool[,] Map { get; }
}
}

View File

@@ -0,0 +1,20 @@
namespace ConsoleApp.Maps
{
public static class MapExtensions
{
public static void Fill2DArray<T>(this T[,] arr, T value)
{
int numRows = arr.GetLength(0);
int numCols = arr.GetLength(1);
for (int i = 0; i < numRows; ++i)
{
for (int j = 0; j < numCols; ++j)
{
arr[i, j] = value;
}
}
}
}
}

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using ConsoleApp.Vehicle;
using ConsoleApp.Sim;
namespace ConsoleApp.Maps
{
@@ -17,16 +17,20 @@ namespace ConsoleApp.Maps
private ISquareMap _squareMap;
private IHexMap _hexMap;
private IMineMap _mineMap;
public void GenerateMaps(int x, int y)
public void GenerateMaps(int x, int y, double minePercentage)
{
Width = x;
Height = y;
_squareMap = new SquareMap(x, y);
_hexMap = new HexMap(x, y);
_mineMap = new MineMap(x, y, minePercentage);
}
public IHexMap GetHexMap() => _hexMap ?? throw new NullReferenceException("hex map not initialized");
public ISquareMap GetSquareMap() => _squareMap ?? throw new NullReferenceException("square map not initialized");
public IMineMap GetMineMap() => _mineMap ?? throw new NullReferenceException("mine map not initialized");
public MapFactory(IVehicle vehicle)
{

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
namespace ConsoleApp.Maps
{
public class MineMap : IMineMap
{
private int _mineCount;
private int _x;
private int _y;
public bool[,] Map { get; }
public MineMap(int x, int y, double minePercentage)
{
_x = x;
_y = y;
_mineCount = x*y*((int)minePercentage/100);
Map = new bool[x, y];
Map.Fill2DArray(false);
PlaceMines();
}
private void PlaceMines()
{
var rand = new Random();
for (int i = 0; i < _mineCount; i++)
{
var x = rand.Next(_x);
var y = rand.Next(_y);
Map[x, y] = true;
}
}
}
}

View File

@@ -1,13 +1,14 @@
using System.Collections.Generic;
using ConsoleApp.Maps;
using ConsoleApp.Vehicle;
using ConsoleApp.Sim;
using HexCore;
namespace ConsoleApp.PathPlanners
{
public interface IPathPlanner
{
Queue<ICell> GenerateOptimalSquarePath(ISquareMap map, IVehicle vehicle);
Queue<ICell> GenerateOptimalHexPath(IHexMap hexMap, IVehicle vehicle);
Queue<Coordinate2D> GenerateOptimalHexPath(IHexMap hexMap, IVehicle vehicle);
}
}

View File

@@ -0,0 +1,10 @@
using System.Collections.Generic;
using HexCore;
namespace ConsoleApp.PathPlanners
{
public interface IReactivePathPlanner
{
Queue<Coordinate2D> ReactiveHexPath { get; }
}
}

View File

@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ConsoleApp.Maps;
using ConsoleApp.Vehicle;
using ConsoleApp.Sim;
using HexCore;
namespace ConsoleApp.PathPlanners
@@ -55,12 +56,76 @@ namespace ConsoleApp.PathPlanners
return path;
}
public Queue<ICell> GenerateOptimalHexPath(IHexMap hexMap, IVehicle vehicle)
public Queue<Coordinate2D> GenerateOptimalHexPath(IHexMap hexMap, IVehicle vehicle)
{
// var t = hexMap.Graph.GetMovementRange(
// new Coordinate2D(0, 0, hexMap.OffsetType), 2, hexMap.DefaultMovement);
// Console.WriteLine(t);
throw new NotImplementedException();
var currentPostion = vehicle.CurrentHexCell;
var currentHeading = GlobalDirection.East;
var finished = false;
var path = new List<Coordinate2D>();
while (!finished)
{
if (currentHeading == GlobalDirection.East)
{
path.AddRange(hexMap.Graph.GetShortestPath(
currentPostion,
new Coordinate2D(hexMap.Width, currentPostion.Y, hexMap.OffsetType),
hexMap.DefaultMovement));
currentPostion = new Coordinate2D(hexMap.Width, currentPostion.Y, hexMap.OffsetType);
}
else if(currentHeading == GlobalDirection.West)
{
path.AddRange(hexMap.Graph.GetShortestPath(
currentPostion,
new Coordinate2D(0, currentPostion.Y, hexMap.OffsetType),
hexMap.DefaultMovement));
currentPostion = new Coordinate2D(0, currentPostion.Y, hexMap.OffsetType);
}
if (currentPostion.Y + vehicle.DetectorRadius * 2 > hexMap.Height)
{
finished = true;
break;
}
Coordinate2D tmpPosition;
if(currentHeading == GlobalDirection.East)
tmpPosition = new Coordinate3D(
currentPostion.To3D().X-1,
currentPostion.To3D().Y,
currentPostion.To3D().Z+1).To2D(hexMap.OffsetType);
else
tmpPosition = new Coordinate3D(
currentPostion.To3D().X,
currentPostion.To3D().Y-1,
currentPostion.To3D().Z+1).To2D(hexMap.OffsetType);
while (hexMap.Graph.GetRange(currentPostion, vehicle.DetectorRadius*2).Contains(tmpPosition))
{
if(currentHeading == GlobalDirection.East)
tmpPosition = new Coordinate3D(
tmpPosition.To3D().X-1,
tmpPosition.To3D().Y,
tmpPosition.To3D().Z+1).To2D(hexMap.OffsetType);
else
tmpPosition = new Coordinate3D(
tmpPosition.To3D().X,
tmpPosition.To3D().Y-1,
tmpPosition.To3D().Z+1).To2D(hexMap.OffsetType);
}
path.AddRange(hexMap.Graph.GetShortestPath(
currentPostion,
tmpPosition,
hexMap.DefaultMovement));
currentPostion = tmpPosition;
if (currentHeading == GlobalDirection.East)
currentHeading = GlobalDirection.West;
else
currentHeading = GlobalDirection.East;
}
return new Queue<Coordinate2D>(path);
}
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using HexCore;
namespace ConsoleApp.PathPlanners
{
public class ReactivePathPlanner : IReactivePathPlanner
{
public Queue<Coordinate2D> ReactiveHexPath { get; }
public ReactivePathPlanner()
{
ReactiveHexPath = new Queue<Coordinate2D>();
}
public void GenerateReactiveHexPath(Graph graph, ref List<Coordinate3D> optimalPath, Coordinate2D minePosition)
{
}
}
}

View File

@@ -19,25 +19,26 @@ namespace ConsoleApp
private static void Initialization()
{
_userConsole.PrintStartMenu();
var input = _userConsole.GetUserInput();
var input = UserConsole.GetUserInput();
if (input == "1")
{
var (x,y) = _userConsole.GetMapDimensions();
RunSimulation(x, y);
var minePercentage = UserConsole.GetMinePercentage();
RunSimulation(x, y, minePercentage);
}
else
{
_userConsole.PrintInvalidInput();
UserConsole.PrintInvalidInput();
Initialization();
}
}
private static void RunSimulation(int x,int y)
private static void RunSimulation(int x, int y, double minePercentage)
{
var mapFactory = _bootstrapper.Resolve<IMapFactory>();
var simRunner = _bootstrapper.Resolve<ISimRunner>();
mapFactory.GenerateMaps(x, y);
mapFactory.GenerateMaps(x, y, minePercentage);
simRunner.Run();
}

View File

@@ -2,17 +2,17 @@ using System.Collections.Generic;
using ConsoleApp.Maps;
using HexCore;
namespace ConsoleApp.Vehicle
namespace ConsoleApp.Sim
{
public class DetectionHead
public static class DetectionHead
{
public List<Coordinate2D> GetCoveredCells(
public static List<Coordinate2D> GetCoveredCells(
Graph graph,
Coordinate2D centerCoordinate,
int detectorRadius,
GlobalDirection direction) => graph.GetRange(centerCoordinate, detectorRadius);
public List<Cell> GetCoveredCells(
public static List<Cell> GetCoveredCells(
ISquareMap squareMap,
Cell centerCell,
int detectorRadius) => squareMap.GetRange(centerCell, detectorRadius);

View File

@@ -1,6 +1,6 @@
using ConsoleApp.Maps;
namespace ConsoleApp.Vehicle
namespace ConsoleApp.Sim
{
public interface IVehicle
{

View File

@@ -1,6 +1,6 @@
using ConsoleApp.Maps;
namespace ConsoleApp.Vehicle
namespace ConsoleApp.Sim
{
public class Vehicle : IVehicle
{
@@ -13,20 +13,19 @@ namespace ConsoleApp.Vehicle
public Heading HexHeading { get; set; }
public Heading SquareHeading { get; set; }
public ICell CurrentSquareCell { get; set; }
public DetectionHead DetectionHead { get; }
public Vehicle(IJsonDeserializor jsonDeserializor)
{
var config = jsonDeserializor.DeserializeObject<VehicleConfiguration>("/Users/brady.bodily/Documents/Repositories/CS5890_Robot_Intelligence/RobotIntelFinal/ConsoleApp/Vehicle/VehicleConfiguration.json");
var config = jsonDeserializor.DeserializeObject<VehicleConfiguration>("/Users/brady.bodily/Documents/Repositories/CS5890_Robot_Intelligence/RobotIntelFinal/ConsoleApp/Sim/VehicleConfiguration.json");
Length = config.Length;
Width = config.Width;
DetectorRadius = config.DetectorRadius;
DetectorOffset = config.DetectorOffset;
CurrentHexCell = default;
CurrentSquareCell = default;
DetectionHead = new DetectionHead();
}
}
}

View File

@@ -1,4 +1,4 @@
namespace ConsoleApp.Vehicle
namespace ConsoleApp.Sim
{
public class VehicleConfiguration
{

View File

@@ -0,0 +1,6 @@
{
"Length": 3,
"Width": 2,
"DetectorRadius": 3,
"DetectorOffset": 2
}

View File

@@ -4,7 +4,7 @@ using System.Threading;
using System.Threading.Tasks;
using ConsoleApp.Maps;
using ConsoleApp.PathPlanners;
using ConsoleApp.Vehicle;
using ConsoleApp.Sim;
using HexCore;
namespace ConsoleApp
@@ -16,20 +16,23 @@ namespace ConsoleApp
private int _cellWidth;
private IPathPlanner _pathPlanner;
private IMineMap _mineMap;
private IReactivePathPlanner _reactivePathPlanner;
public SimRunner(IMapFactory mapFactory, IVehicle vehicle, IPathPlanner pathPlanner)
public SimRunner(IMapFactory mapFactory, IVehicle vehicle, IPathPlanner pathPlanner, IReactivePathPlanner reactivePathPlanner)
{
_cellWidth = mapFactory.CellWidth;
_mapFactory = mapFactory;
_vehicle = vehicle;
_pathPlanner = pathPlanner;
_reactivePathPlanner = reactivePathPlanner;
}
public void Run()
{
_mineMap = _mapFactory.GetMineMap();
SquareSimulation();
HexSimulation();
// while(!squareTask.IsCompleted && !hexTask.IsCompleted){Thread.Sleep(500);}
@@ -41,6 +44,7 @@ namespace ConsoleApp
var hexMap = _mapFactory.GetHexMap();
_vehicle.CurrentHexCell = new Coordinate2D(0, 0, OffsetTypes.OddRowsRight);
var optimalPath = _pathPlanner.GenerateOptimalHexPath(hexMap, _vehicle);
var reactivePath = _reactivePathPlanner.ReactiveHexPath;
}

View File

@@ -18,14 +18,11 @@ namespace ConsoleApp
$"\t 2: Load custom map. \n");
}
public string GetUserInput() => Console.ReadLine();
public static string GetUserInput() => Console.ReadLine();
public void PrintInvalidInput()
{
Console.WriteLine($"Invalid input try again \n");
}
public static void PrintInvalidInput() => Console.WriteLine($"Invalid input try again \n");
public (int width, int height) GetMapDimensions()
public (int width, int height) GetMapDimensions()
{
Console.WriteLine($"Enter map height: ");
var x = GetUserInput();
@@ -38,12 +35,23 @@ namespace ConsoleApp
Console.WriteLine($"Enter map height: ");
var y = GetUserInput();
if(!int.TryParse(y, out var height))
{
PrintInvalidInput();
GetMapDimensions();
}
if (int.TryParse(y, out var height)) return (width, height);
PrintInvalidInput();
GetMapDimensions();
return (width, height);
}
public static double GetMinePercentage()
{
Console.WriteLine($"Enter desired percentage of mines: ");
var x = GetUserInput();
if (double.TryParse(x, out var percent)) return percent;
PrintInvalidInput();
GetMinePercentage();
return percent;
}
}
}

View File

@@ -1,6 +0,0 @@
{
"Length": 3,
"Width": 2,
"DetectorWidth": 3,
"DetectorOffset": 1
}