Optimal path for square map

This commit is contained in:
2020-12-14 18:24:10 -07:00
parent 80ea690330
commit 5bb82f7579
11 changed files with 209 additions and 79 deletions

View File

@@ -13,6 +13,7 @@ namespace ConsoleApp
container.Register<ISimRunner, SimRunner>(Reuse.Singleton);
container.Register<IVehicle, Vehicle>(Reuse.Singleton);
container.Register<IJsonDeserializor, JsonDeserializor>(Reuse.Singleton);
container.Register<IPathPlanner, PathPlanner>(Reuse.Singleton);
}

View File

@@ -1,4 +1,11 @@
$HEADER$namespace $NAMESPACE$
using System.Collections.Generic;
using ConsoleApp.Maps;
namespace ConsoleApp
{
public interface $INTERFACE$ {$END$}
public interface IPathPlanner
{
Queue<ICell> GenerateOptimalSquarePath(SquareMap map, IVehicle vehicle);
}
}

View File

@@ -1,7 +1,10 @@
namespace ConsoleApp.Maps
{
public enum Direction
public enum GlobalDirection
{
North,
South,
East,
West
}
}

View File

@@ -1,5 +1,3 @@
using System.Net.NetworkInformation;
namespace ConsoleApp.Maps
{
public class Oreientation

View File

@@ -6,8 +6,8 @@ namespace ConsoleApp.Maps
public class HexMap : IHexMap
{
public Cell[,] Map { get; }
public ICell StartingCell { get; }
public ICell LastCell { get; }
public Cell StartingCell { get; }
public Cell LastCell { get; }
/// <summary>
/// Generate Hex map with cells of 25cm X 25cm
@@ -23,9 +23,11 @@ namespace ConsoleApp.Maps
var xCellCount = (int)Math.Ceiling((decimal) (x) / 25);
var yCellCount = (int)Math.Ceiling((decimal) (y) / 25);
//Initialize Map
Map = new Cell[xCellCount, yCellCount];
//set last cell;
StartingCell = Map[0, 0];
Map = new Cell[xCellCount, yCellCount];
for (int r = 0; r < yCellCount; r++) {
int r_offset = Convert.ToInt32(Math.Floor(Convert.ToDouble(r)/2));
for (int q = r_offset; q < xCellCount - r_offset; q++) {
@@ -35,7 +37,7 @@ namespace ConsoleApp.Maps
}
}
public List<ICell> PossibleMoves(ICell currentCell)
public List<GlobalDirection> PossibleMoves(ICell currentCell)
{
throw new NotImplementedException();
}

View File

@@ -6,8 +6,8 @@ namespace ConsoleApp.Maps
public interface IMap
{
public Cell[,] Map { get; }
ICell StartingCell { get; }
ICell LastCell { get; }
public List<ICell> PossibleMoves(ICell currentCell);
Cell StartingCell { get; }
Cell LastCell { get; }
public List<GlobalDirection> PossibleMoves(ICell currentCell);
}
}

View File

@@ -6,9 +6,14 @@ namespace ConsoleApp.Maps
public class SquareMap : ISquareMap
{
public Cell[,] Map { get;}
public ICell StartingCell { get; }
public ICell LastCell { get; }
public Cell StartingCell { get; }
public Cell LastCell { get; }
public int Height { get; }
public int Width { get; }
private int _mapWidth;
private int _mapHeight;
public SquareMap(int x, int y)
{
//convert to cm
@@ -18,31 +23,43 @@ namespace ConsoleApp.Maps
var xCellCount = (int)Math.Ceiling((decimal) (x) / 25);
var yCellCount = (int)Math.Ceiling((decimal) (y) / 25);
//set Width and height fields
_mapWidth = xCellCount-1;
_mapHeight = yCellCount-1;
Height = _mapHeight;
Width = _mapWidth;
//Initialize Map
Map = new Cell[xCellCount, yCellCount];
//set last cell;
StartingCell = Map[0, 0];
Map = new Cell[xCellCount, yCellCount];
for (int i = 0; i < x; i++)
for (int i = 0; i < xCellCount; i++)
{
for (int j = 0; j < y; j++)
for (int j = 0; j < yCellCount; j++)
{
Map[i,j] = new Cell(i, j);
}
}
}
public List<ICell> PossibleMoves(ICell currentCell)
public List<GlobalDirection> PossibleMoves(ICell currentCell)
{
var forward = (currentCell.X , currentCell.Y + 1);
var backwards= (currentCell.X , currentCell.Y - 1);
var right = (currentCell.X + 1, currentCell.Y);
var left = (currentCell.X - 1, currentCell.Y);
var possibles = new List<ICell>();
possibles.Add(Map[forward.Item1, forward.Item2]);
possibles.Add(Map[backwards.Item1, backwards.Item2]);
possibles.Add(Map[right.Item1, right.Item2]);
possibles.Add(Map[left.Item1, left.Item2]);
var possibles = new List<GlobalDirection>();
if (currentCell.X != 0)
possibles.Add(GlobalDirection.West);
if (currentCell.X != _mapWidth)
possibles.Add(GlobalDirection.East);
if (currentCell.Y != _mapHeight)
possibles.Add(GlobalDirection.North);
if (currentCell.Y != 0)
possibles.Add(GlobalDirection.South);
return possibles;
}
public Cell this[in int x, in int y] => Map[x, y];
}
}

View File

@@ -1,4 +1,56 @@
$HEADER$namespace $NAMESPACE$
using System;
using System.Collections.Generic;
using ConsoleApp.Maps;
namespace ConsoleApp
{
public class $CLASS$ {$END$}
public class PathPlanner : IPathPlanner
{
public Queue<ICell> GenerateOptimalSquarePath(SquareMap map, IVehicle vehicle)
{
var path = new Queue<ICell>();
var myCell = map.StartingCell;
var finished = false;
var width_cm = (double)(vehicle.DetectorWidth) * 100;
var swathOffset = (int)Math.Floor((decimal) (width_cm) / 25) + 1;
var currentHeading = GlobalDirection.North;
while (!finished)
{
var availableMoves = map.PossibleMoves(myCell);
if (availableMoves.Contains(GlobalDirection.North) && currentHeading == GlobalDirection.North &&
myCell.Y != map.Height)
{
path.Enqueue(map[myCell.X, myCell.Y + 1]);
myCell = map[myCell.X, myCell.Y + 1];
}
else if (availableMoves.Contains(GlobalDirection.South) && currentHeading == GlobalDirection.South && myCell.Y != 0)
{
path.Enqueue(map[myCell.X, myCell.Y - 1]);
myCell = map[myCell.X, myCell.Y - 1];
}
else
{
if (myCell.X + swathOffset >= map.Width)
{
finished = true;
}
else
{
for (int i = myCell.X; i < myCell.X + swathOffset; i++)
{
path.Enqueue(map[i, myCell.Y]);
}
myCell = map[myCell.X+swathOffset, myCell.Y];
if (currentHeading == GlobalDirection.North)
currentHeading = GlobalDirection.South;
else if (currentHeading == GlobalDirection.South)
currentHeading = GlobalDirection.North;
}
}
}
return path;
}
}
}

View File

@@ -10,58 +10,42 @@ namespace ConsoleApp
{
private IMapFactory _mapFactory;
private IVehicle _vehicle;
private IMap _squareMap;
private IMap _hexMap;
private int _cellWidth;
private IPathPlanner _pathPlanner;
public SimRunner(IMapFactory mapFactory, IVehicle vehicle)
public SimRunner(IMapFactory mapFactory, IVehicle vehicle, IPathPlanner pathPlanner)
{
_squareMap = mapFactory.Maps["SquareMap"];
_hexMap = mapFactory.Maps["HexMap"];
_cellWidth = mapFactory.CellWidth;
_mapFactory = mapFactory;
_vehicle = vehicle;
_pathPlanner = pathPlanner;
}
public void Run()
{
_vehicle.CurrentHexCell = _hexMap.StartingCell;
_vehicle.CurrentSquareCell = _squareMap.StartingCell;
var squareTask = Task.Run(() => SquareSimulation());
var hexTask = Task.Run(() => HexSimulation());
while(!squareTask.IsCompleted && !hexTask.IsCompleted){Thread.Sleep(500);}
SquareSimulation();
// var hexTask = Task.Run(() => HexSimulation());
// while(!squareTask.IsCompleted && !hexTask.IsCompleted){Thread.Sleep(500);}
}
private void HexSimulation()
{
var optimalPath = GenerateHexPath();
var hexMap = (HexMap)_mapFactory.Maps["HexMap"];
_vehicle.CurrentHexCell = hexMap.StartingCell;
}
private void SquareSimulation()
{
var optimalPath = GenerateSquarePath();
var squareMap = (SquareMap)_mapFactory.Maps["SquareMap"];
_vehicle.CurrentSquareCell = squareMap.StartingCell;
var optimalPath = _pathPlanner.GenerateOptimalSquarePath(squareMap, _vehicle);
}
private object GenerateSquarePath()
{
throw new NotImplementedException();
}
private Queue<ICell> GenerateHexPath()
{
var path = new Queue<ICell>();
var currentCell = _vehicle.CurrentHexCell;
var possibles = _hexMap.PossibleMoves(currentCell);
while (currentCell != _hexMap.LastCell)
{
}
return path;
}
}
}