Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Fix Dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander0542 committed Oct 28, 2021
1 parent 79b4b90 commit 9cdd7f6
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Avans.FlatGalaxy.Models/CelestialBodies/CelestialBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void Collide(CelestialBody other)
CollisionState.Collide(this, other);
}

public double DistanceTo(CelestialBody target) => Math.Pow(CenterX - target.CenterX, 2) + Math.Pow(CenterY - target.CenterY, 2);

public IDisposable Subscribe(IObserver<CelestialBody> observer)
{
_observers.Add(observer);
Expand Down
1 change: 0 additions & 1 deletion src/Avans.FlatGalaxy.Presentation/SimulationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Avans.FlatGalaxy.Presentation.Extensions;
using Avans.FlatGalaxy.Simulation;
using Avans.FlatGalaxy.Simulation.Data;
using Color=System.Drawing.Color;

namespace Avans.FlatGalaxy.Presentation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding
{
public class BreadthFirstPathFinder : PathFinder
{
protected override List<Planet> Find(Planet start, Planet end)
protected override List<Planet> Find(Planet start, Planet end, List<Planet> planets)
{
var previous = new Dictionary<Planet, Planet>();
var queue = new Queue<Planet>();
Expand Down
15 changes: 15 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraEdge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Avans.FlatGalaxy.Simulation.PathFinding.Data
{
public class DijkstraEdge
{
public DijkstraNode Node { get; set; }

public double Weight { get; set; }

public DijkstraEdge(DijkstraNode node, double weight)
{
Node = node;
Weight = weight;
}
}
}
26 changes: 26 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using Avans.FlatGalaxy.Models.CelestialBodies;

namespace Avans.FlatGalaxy.Simulation.PathFinding.Data
{
public class DijkstraGraph
{
public List<DijkstraNode> Nodes { get; }

public DijkstraGraph(List<Planet> planets)
{
Nodes = planets.Select(planet => new DijkstraNode(planet)).ToList();

foreach (var node in Nodes)
{
foreach (var neighbour in node.Planet.Neighbours)
{
var neighbourNode = Nodes.First(node1 => node1.Planet == neighbour);

node.Neighbours.Add(new(neighbourNode, node.Planet.DistanceTo(neighbour)));
}
}
}
}
}
24 changes: 24 additions & 0 deletions src/Avans.FlatGalaxy.Simulation/PathFinding/Data/DijkstraNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using Avans.FlatGalaxy.Models.CelestialBodies;

namespace Avans.FlatGalaxy.Simulation.PathFinding.Data
{
public class DijkstraNode
{
public Planet Planet { get; }

public KeyValuePair<Planet, double> Weight { get; set; }

public List<DijkstraEdge> Neighbours { get; }

public bool Visited { get; set; }

public DijkstraNode(Planet planet)
{
Neighbours = new();
Weight = new(null, double.PositiveInfinity);
Planet = planet;
}
}
}
44 changes: 38 additions & 6 deletions src/Avans.FlatGalaxy.Simulation/PathFinding/DijkstraPathFinder.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Avans.FlatGalaxy.Models.CelestialBodies;
using Avans.FlatGalaxy.Simulation.PathFinding.Data;

namespace Avans.FlatGalaxy.Simulation.PathFinding
{
public class DijkstraPathFinder : PathFinder
{
protected override List<Planet> Find(Planet start, Planet end)
protected override List<Planet> Find(Planet start, Planet end, List<Planet> planets)
{
return null;
}
var graph = new DijkstraGraph(planets);
var startNode = graph.Nodes.First(node => node.Planet == start);
startNode.Weight = new(null, 0);
var unvisited = new List<DijkstraNode> { startNode };

while (unvisited.Any())
{
var node = unvisited.OrderBy(node1 => node1.Weight.Value).First();

foreach (var neighbour in node.Neighbours)
{
if (neighbour.Weight + node.Weight.Value < neighbour.Node.Weight.Value)
{
neighbour.Node.Weight = new(node.Planet, neighbour.Weight + node.Weight.Value);

private static double GetDistance(Planet origin, Planet target) => Math.Pow(origin.CenterX - target.CenterX, 2) + Math.Pow(origin.CenterY - target.CenterY, 2);
if (!unvisited.Contains(neighbour.Node) && !neighbour.Node.Visited)
{
unvisited.Add(neighbour.Node);
}
}
}
node.Visited = true;
unvisited.Remove(node);
}

var endNode = graph.Nodes.First(node => node.Planet == end);
var path = new List<DijkstraNode> { endNode };
while (endNode.Weight.Key != null)
{
endNode = endNode.Neighbours.OrderBy(edge => edge.Node.Weight.Value).First().Node;
path.Add(endNode);
}

return path.Select(node => node.Planet).ToList();
}
}
}
7 changes: 4 additions & 3 deletions src/Avans.FlatGalaxy.Simulation/PathFinding/PathFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ namespace Avans.FlatGalaxy.Simulation.PathFinding
{
public abstract class PathFinder
{
protected abstract List<Planet> Find(Planet start, Planet end);
protected abstract List<Planet> Find(Planet start, Planet end, List<Planet> planets);

public List<Planet> Get(ISimulator simulator)
{
var stepPlanets = simulator.Galaxy.CelestialBodies.OfType<Planet>().OrderByDescending(planet => planet.Radius).ToList();
return stepPlanets.Count < 2 ? null : Find(stepPlanets[0], stepPlanets[1]);
var planets = simulator.Galaxy.CelestialBodies.OfType<Planet>().ToList();
var planets2 = planets.OrderByDescending(planet => planet.Radius).ToList();
return planets2.Count < 2 ? null : Find(planets2[0], planets2[1], planets);
}
}
}
4 changes: 2 additions & 2 deletions src/Avans.FlatGalaxy.Simulation/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private void Tick(CancellationToken token)

_collisionHandler.Detect(this);

PathSteps = new BreadthFirstPathFinder().Get(this);
// PathSteps = new CheapestPathFinder().Get(this);
// PathSteps = new BreadthFirstPathFinder().Get(this);
PathSteps = new DijkstraPathFinder().Get(this);

_lastTick = DateTime.UtcNow;
if ((DateTime.UtcNow - _lastBookmark).TotalSeconds >= ISimulator.BookmarkTime)
Expand Down

0 comments on commit 9cdd7f6

Please sign in to comment.