public void ReturnsEmptySetIfNothingFound() { var search = new DijkstraSearch <int>(EqualityComparer <int> .Default, _ => Enumerable.Empty <(int, float)>()); var result = search.FindAll(0, x => false); Assert.AreEqual(0, result.Count); }
public void FindsInitialNode() { var search = new DijkstraSearch <int>( EqualityComparer <int> .Default, _ => Enumerable.Empty <(int, float)>()); var result = search.FindAll(33, x => true); Assert.AreEqual(1, result.Count); Assert.AreEqual(0, result[0].Length); Assert.AreEqual(1, result[0].Steps.Length); Assert.AreEqual(33, result[0].Steps[0]); }
public void TestSearch2() { var graph = new WeightedDirectedGraph <string>(); var a = graph.AddVertex("A"); var b = graph.AddVertex("B"); var c = graph.AddVertex("C"); var d = graph.AddVertex("D"); var e = graph.AddVertex("E"); var f = graph.AddVertex("F"); var g = graph.AddVertex("G"); var h = graph.AddVertex("H"); graph.AddEdge(a, b, 2); graph.AddEdge(a, d, 4); graph.AddEdge(a, c, 1); graph.AddEdge(b, c, 5); graph.AddEdge(b, f, 2); graph.AddEdge(b, e, 10); graph.AddEdge(c, a, 9); graph.AddEdge(c, e, 11); graph.AddEdge(d, c, 2); graph.AddEdge(e, d, 7); graph.AddEdge(e, g, 1); graph.AddEdge(f, h, 3); graph.AddEdge(g, e, 3); graph.AddEdge(g, f, 2); graph.AddEdge(h, g, 1); var actual = DijkstraSearch <string> .Search(graph, d, e); var expected = new string[] { d, c, e }; Assert.AreEqual(expected, actual); }
private static RouteInfo GetRoute(Map map, InputOption inputOption) { ISearchAlgorithm algorithm = new DijkstraSearch(); var directionService = new DirectionService(algorithm, inputOption); return(directionService.PrepareRouteInfoFrom(map)); }
/// <summary> /// Returns mission randomly generated on current worldMap, from current location. Sets mission destination /// and price per unit depending of routes hardness. Quantity is not set cuz its should be available on /// graphic menu to get as much quantity as player wants. /// </summary> /// <param name="worldMap"></param> /// <param name="currentLocation"></param> /// <returns></returns> public static Mission GenerateMission(WorldMap worldMap, float basePricePerUnit) { Mission mission = new Mission(); mission.Cargo = new Cargo(); //Vertex destination = worldMap.WorldGraph.Vertices[UnityEngine.Random.Range(0, worldMap.WorldGraph.Vertices.Length)]; Vertex destination = worldMap.GetRandomVertex(2); mission.Cargo.Destination = destination.Location; Vertex[] locations = DijkstraSearch.Search(worldMap.WorldGraph, worldMap.CurrentLocation, destination); List <Route> routes = new List <Route>(); for (int i = 0; i < locations.Length - 1; i++) { UnityEngine.Debug.Log("xd"); routes.Add(worldMap.RouteBetween(locations[i].Location, locations[i + 1].Location)); } int hardnessModifier = 0; foreach (Route route in routes) { hardnessModifier += route.HowHard + route.HowLong; } mission.Cargo.PricePerUnit = (hardnessModifier * PERCENTAGE_OF_PRICE_PER_UNIT + 1.0f) * basePricePerUnit; UnityEngine.Debug.Log("MNOZNIK: " + (hardnessModifier * PERCENTAGE_OF_PRICE_PER_UNIT + 1.0f) * basePricePerUnit); return(mission); }
public void FindAllTargetsWithDistance() { var search = new DijkstraSearch <int>( EqualityComparer <int> .Default, FiniteExpander); var targets = new Dictionary <int, float> { { 6, 1.8f }, { 12, 2.8f }, { 24, 3.8f }, { 36, 3.6f }, { 72, 4.6f }, { 108, 4.6f }, { 192, 6.8f } }; var result = search.FindAll(1, node => targets.ContainsKey(node)); Assert.AreEqual(targets.Count, result.Count); foreach (var hit in result) { Assert.AreEqual(targets[hit.Target], hit.Cost, 1e-6f); } }
public void TestSearch3() { var graph = new WeightedDirectedGraph <string>(); var a = graph.AddVertex("A"); var b = graph.AddVertex("B"); var c = graph.AddVertex("C"); var d = graph.AddVertex("D"); var e = graph.AddVertex("E"); var f = graph.AddVertex("F"); var g = graph.AddVertex("G"); graph.AddEdge(a, d, 1); graph.AddEdge(a, c, 2); graph.AddEdge(b, a, 2); graph.AddEdge(c, d, 1); graph.AddEdge(c, f, 2); graph.AddEdge(d, b, 5); graph.AddEdge(d, e, 1); graph.AddEdge(d, g, 5); graph.AddEdge(d, f, 6); graph.AddEdge(e, b, 1); graph.AddEdge(f, g, 10); graph.AddEdge(g, e, 3); var actual = DijkstraSearch <string> .Search(graph, a, g); var expected = new string[] { a, d, g }; Assert.AreEqual(expected, actual); }
public void GetRoute_NightTime_Involving_DT_In_Middle() { var rawRecords = new List <RawStationData> { new RawStationData { StationCode = "NE1", StationName = "SengKang", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE2", StationName = "Kovan", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE3", StationName = "Serangoon", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE4", StationName = "BoonKeng", OpeningDate = string.Empty }, new RawStationData { StationCode = "DT1", StationName = "Lorang", OpeningDate = string.Empty }, new RawStationData { StationCode = "DT2", StationName = "Serangoon", OpeningDate = string.Empty }, new RawStationData { StationCode = "DT3", StationName = "Bishan", OpeningDate = string.Empty }, new RawStationData { StationCode = "EW1", StationName = "Bugis", OpeningDate = string.Empty }, new RawStationData { StationCode = "EW2", StationName = "Bishan", OpeningDate = string.Empty }, new RawStationData { StationCode = "EW3", StationName = "Katib", OpeningDate = string.Empty }, new RawStationData { StationCode = "EW4", StationName = "Lavender", OpeningDate = string.Empty }, }; var map = new Map(rawRecords).LinkStations(); var start = new Station("Lavender"); var end = new Station("SengKang"); ISearchAlgorithm algorithm = new DijkstraSearch(); var directionService = new DirectionService(algorithm, new InputOption { Start = start.StationName, End = end.StationName, StartTime = new DateTime(2021, 3, 5, 23, 40, 00) }); var routeInfo = directionService.PrepareRouteInfoFrom(map); Assert.IsTrue(routeInfo.JourneyTitle.Contains("\"Not\" possible"), $"Was : {routeInfo.JourneyTitle}"); Assert.IsEmpty(routeInfo.Route); Assert.IsEmpty(routeInfo.StationsTraveled); Assert.IsEmpty(routeInfo.Journey); }
private string Processor(char startValue, char endValue) { var start = _graph.Vertices.FirstOrDefault(v => v.Value == startValue); var end = _graph.Vertices.FirstOrDefault(v => v.Value == endValue); var path = DijkstraSearch.FindPath(_graph, start, end); return(string.Join("->", path?.Select(v => v.Value) ?? "")); }
public void FindsFirstTarget() { var search = new DijkstraSearch <int>(EqualityComparer <int> .Default, InfiniteExpander); var result = search.FindFirst(1, x => x == 192); Assert.AreEqual(1, result.Steps.First()); Assert.AreEqual(192, result.Steps.Last()); Assert.AreEqual(6, result.Length); Assert.AreEqual(5 + 1.8, result.Cost, 1e-6f); }
private static void FindShortestRoute(IDictionary <char, Intersection> intersections, IEnumerable <Street> streets, string requirements) { Regex pattern = new Regex(@"^(?<from>[A-Z]{1}) (?<to>[A-Z]{1}) (?<time>\d{4})$"); Match match = pattern.Match(requirements); if (!match.Success) { Console.Write("Bad requirements: {0}", requirements); return; } int timeIndex; int time = int.Parse(match.Groups["time"].Value); if (time >= 600 && time <= 1000) { timeIndex = 0; } else if (time >= 1000 && time <= 1500) { timeIndex = 1; } else if (time >= 1500 && time <= 1900) { timeIndex = 2; } else { timeIndex = 3; } DijkstraSearch search = new DijkstraSearch(); foreach (Street street in streets) { search.AddConnection(street.Intersection1, street.Intersection2, street.Times[timeIndex]); } Intersection from = intersections[match.Groups["from"].Value[0]]; Intersection to = intersections[match.Groups["to"].Value[0]]; DijkstraPathSearchResult result = search.FindShortestPath(from, to); Console.WriteLine("Path from {0} to {1} at {2}:", from, to, time); IDijkstraSearchNode[] path = result.Path.ToArray(); for (int i = 0; i < path.Length - 1; ++i) { Street street = streets.First(s => (s.Intersection1 == path[i] || s.Intersection2 == path[i]) && (s.Intersection1 == path[i + 1] || s.Intersection2 == path[i + 1])); Console.WriteLine(" ({0,2} minutes) {1}", street.Times[timeIndex], street.Name); } Console.WriteLine("Total time: {0} minutes", result.TotalDistance); }
public void FindTargetSetInInfiniteGraph() { var search = new DijkstraSearch <int>(EqualityComparer <int> .Default, InfiniteExpander); var result = search.FindAll(1, x => x < 30, null, 12); var expected = new[] { 1, 2, 4, 8, 16, 3, 6, 12, 24, 9, 18, 27 }; Assert.AreEqual(12, result.Count); foreach (var target in expected) { Assert.IsTrue(result.Any(p => p.Target == target)); } }
public void Given_3_Stations_We_Can_Trace_To_BeginingStation_FromEndStation() { _sengkangStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _harborStation, Cost = 1 }); _harborStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _stations = new List <Station> { _sengkangStation, _kovanStation, _harborStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _harborStation.StationName }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Harbor") { NearestToStart = _kovanStation, MinimumCost = 2 }, new Station("Kovan") { NearestToStart = _sengkangStation, MinimumCost = 1 }, new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, }; path.Should().NotBeEmpty() .And.HaveCount(3) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart)); }
private List <Coord> GetPath(Coord A, Coord B, MapGrid mapGrid, bool isVisualize) { switch (AppConfig.searchAlgorithm) { case Algorithm.DFS: return(DepthFirstSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.BFS: return(BreadthFirstSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.Dijkstra: return(DijkstraSearch.GetPath(A, B, mapGrid, isVisualize)); case Algorithm.AStar: return(AStarSearch.GetPath(A, B, mapGrid, isVisualize)); default: return(AStarSearch.GetPath(A, B, mapGrid, isVisualize)); } }
public void Search(int algrithm) { ClearPath(); SearchAlgrithm ag = (SearchAlgrithm)algrithm; PathFind pathFind; switch (ag) { case SearchAlgrithm.BFS: pathFind = new BreadthFirstSearch(); break; case SearchAlgrithm.DFS: pathFind = new DeepFirstSearch(); break; case SearchAlgrithm.Dijkstra: pathFind = new DijkstraSearch(); break; case SearchAlgrithm.Greedy: pathFind = new GreedyBestFirstSearch(); break; case SearchAlgrithm.AStar: pathFind = new AStarSearch(); break; default: pathFind = new AStarSearch(); break; } Node start = graph.GetNode((int)mapView.startPos.x, (int)mapView.startPos.y); Node end = graph.GetNode((int)mapView.endPos.x, (int)mapView.endPos.y); if (pathFind.Find(graph, start, end)) { path = pathFind.GetPath(); Queue <Node> searchSteps = pathFind.GetSearchSteps(); mainUI.labelSearchDesc.text = pathFind.Name() + " " + pathFind.SearchResultDesc(); mainUI.SearchConsoleTxt = ""; StartCoroutine(DrawSearchSteps(searchSteps, path)); } else { mainUI.labelConsole.text = "no path to the target point from the start point"; } }
public void GetRoute_WithOutTimeFactor() { var rawRecords = new List <RawStationData> { new RawStationData { StationCode = "NE1", StationName = "SengKang", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE2", StationName = "Kovan", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE3", StationName = "Serangoon", OpeningDate = string.Empty }, new RawStationData { StationCode = "NE4", StationName = "BoonKeng", OpeningDate = string.Empty }, new RawStationData { StationCode = "CC1", StationName = "Lorang", OpeningDate = string.Empty }, new RawStationData { StationCode = "CC2", StationName = "Serangoon", OpeningDate = string.Empty }, new RawStationData { StationCode = "CC3", StationName = "Bishan", OpeningDate = string.Empty }, }; var map = new Map(rawRecords).LinkStations(); var start = new Station("SengKang"); var end = new Station("Bishan"); ISearchAlgorithm algorithm = new DijkstraSearch(); var directionService = new DirectionService(algorithm, new InputOption { Start = start.StationName, End = end.StationName }); var routeInfo = directionService.PrepareRouteInfoFrom(map); Assert.IsTrue(routeInfo.JourneyTitle.Contains(start.StationName), $"Was : {start.StationName}"); Assert.IsNotEmpty(routeInfo.Route); Assert.IsTrue(routeInfo.StationsTraveled.Contains("4"), $"Was : {routeInfo.StationsTraveled}"); Assert.IsNotEmpty(routeInfo.Journey); }
static void Main() { var sw = new Stopwatch(); sw.Start(); _laby = Grid2D.FromFile("../../../input.txt"); var labels = ReadLabels().ToList(); Debug.Assert(labels.Count == 2 * labels.Select(x => x.name).Distinct().Count() - 2); _portals = new Dictionary <Point, Point>(); var openLabels = new Dictionary <string, Point>(); foreach (var labelGroup in labels.ToLookup(x => x.name)) { if (labelGroup.Count() == 2) { var pos = labelGroup.Select(x => x.pos).ToArray(); _portals.Add(pos[0], pos[1]); _portals.Add(pos[1], pos[0]); } else { openLabels.Add(labelGroup.Key, labelGroup.First().pos); } } var search = new DijkstraSearch <Point>(EqualityComparer <Point> .Default, Expander); var path = search.FindFirst(openLabels["AA"], p => p == openLabels["ZZ"]); Console.WriteLine($"Part 1: Path has {path.Length} steps."); _innerregion = _laby.Bounds.InflatedCopy(-3, -3); var search2 = new DijkstraSearch <(Point, int level)>(EqualityComparer <(Point, int)> .Default, Expander2); var path2 = search2.FindFirst((openLabels["AA"], 0), p => p.Item1 == openLabels["ZZ"] && p.level == 0); Console.WriteLine($"Part 2: Path has {path2.Length} steps."); sw.Stop(); Console.WriteLine($"Solving took {sw.ElapsedMilliseconds}ms."); _ = Console.ReadLine(); }
public void Scenario_User_Cant_Take_DT_CG_CE_Lines_At_Night(string line) { _sengkangStation.AddLine(line); _kovanStation.AddLine(line); _sengkangStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _stations = new List <Station> { _sengkangStation, _kovanStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _kovanStation.StationName, StartTime = new DateTime(2019, 01, 31, 22, 00, 00) }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, new Station("Kovan") { NearestToStart = null, MinimumCost = null }, }; path.Should().NotBeEmpty() .And.HaveCount(2) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart)); }
public void Scenario_User_Journey_Needs_Interchange_At_Other_Times_Costs_10_More() { _sengkangStation.AddLine("NE"); _kovanStation.AddLine("CC"); _sengkangStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _stations = new List <Station> { _sengkangStation, _kovanStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _kovanStation.StationName, StartTime = new DateTime(2019, 01, 31, 21, 30, 00) }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Kovan") { NearestToStart = _sengkangStation, MinimumCost = 11 }, new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, }; path.Should().NotBeEmpty() .And.HaveCount(2) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart)); }
public void Scenario_User_Journey_Falls_In_Line_During_PeakTime_WeekDays_Costs_More(string line, int cost) { _sengkangStation.AddLine(line); _kovanStation.AddLine(line); _sengkangStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _stations = new List <Station> { _sengkangStation, _kovanStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _kovanStation.StationName, StartTime = new DateTime(2019, 01, 31, 8, 00, 00) }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Kovan") { NearestToStart = _sengkangStation, MinimumCost = cost }, new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, }; path.Should().NotBeEmpty() .And.HaveCount(2) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart)); }
private async void SearchButton_ClickAsync(object sender, RoutedEventArgs e) { //Disable Control to prevent this method from running parallely multiple times SearchButton.IsEnabled = false; //Create Start and Goal Vertices Vertex start = new Vertex(StartVertexTextBox.Text); Vertex goal = new Vertex(GoalVertexTextBox.Text); //Give feedback to the user DijkstraPathLabel.Content = "Searching for path from " + start + " to " + goal + " ..."; //if the graph was not initialized yet, tell the user to create one if (graph == null) { DijkstraPathLabel.Content = "Create a Graph before doing a path search."; } //if the graph does not contain both the vertices tell the user to use other vertices else if (!(graph.Vertices.Contains(start) && graph.Vertices.Contains(goal))) { DijkstraPathLabel.Content = "The Graph does not contain that start and/or goal."; } else { //Search for the Path Vertex[] path = await Task.Run(() => { return(DijkstraSearch.Search(graph, start, goal)); }); //Convert path to a readable string string sPath = ConvertVerticesToString(path); //Set Label Text to found path DijkstraPathLabel.Content = sPath; } //Enable Control again SearchButton.IsEnabled = true; }
public void DijkstraTest() { var graph = new WeightedDirectedGraph(); var s = new Vertex("S"); var a = new Vertex("A"); var b = new Vertex("B"); var c = new Vertex("C"); var d = new Vertex("D"); var e = new Vertex("E"); graph.AddPair(s, a, 4); graph.AddPair(s, e, 2); graph.AddPair(a, c, 6); graph.AddPair(a, b, 5); graph.AddPair(a, d, 3); graph.AddPair(e, d, 1); graph.AddPair(d, a, 1); graph.AddPair(d, c, 3); graph.AddPair(c, b, 1); graph.AddPair(b, a, 3); foreach (var vertex in graph.Vertices) { Debug.WriteLine(vertex.ToString()); } var dijkstra = new DijkstraSearch(); dijkstra.PrintShortestPaths(graph, s); }
private BaseSearchAlgo GetAlgorithm() { BaseSearchAlgo algo = null; switch (m_searchAlgo) { case SearchAlgo.A_Star: algo = new AStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.Theta_Star: algo = new ThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.LazyTheta_Star: algo = new LazyThetaStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BestFirstSearch: algo = new BestFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BreadthFirstSearch: algo = new BreadthFirstSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.DijkstraSearch: algo = new DijkstraSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.JPS: algo = new JumpPointSearch(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.JPSPlus: algo = new JPSPlus(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; case SearchAlgo.BiA_Star: algo = new BiAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime); break; #region Incremental case SearchAlgo.D_Star: algo = new DStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.FocussedD_Star: algo = new FocussedDStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.LPA_Star: algo = new LPAStar(m_startNode, m_goalNode, m_nodes, m_showTime); //algo = new LPAStar_Optimized(m_startNode, m_endNode, m_nodes, m_showTime); break; case SearchAlgo.DstarLite: algo = new DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.Path_AA_Star: algo = new Path_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.Tree_AA_Star: algo = new Tree_AAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; #endregion #region Moving Target case SearchAlgo.GAA_Star: algo = new GAAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.GFRA_Star: algo = new GFRAStar(m_startNode, m_goalNode, m_nodes, m_showTime); break; case SearchAlgo.MT_DstarLite: algo = new MT_DStarLite(m_startNode, m_goalNode, m_nodes, m_showTime); break; #endregion case SearchAlgo.AnnotatedA_Star: algo = new AnnotatedAStar(m_startNode, m_goalNode, m_nodes, m_weight, m_showTime, m_unitSize); break; default: Debug.LogError($"No code for SearchAlgo={m_searchAlgo}"); break; } return(algo); }
public void Scenario_4_Stations_Where_Start_And_End_Is_Same_Then_LowestCostRouteReached_WillBe_Returned() { _sengkangStation.Connections.Add(new Edge { ConnectedStation = _bishanStation, Cost = 1 }); _bishanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _bishanStation.Connections.Add(new Edge { ConnectedStation = _harborStation, Cost = 1 }); _harborStation.Connections.Add(new Edge { ConnectedStation = _bishanStation, Cost = 1 }); _sengkangStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _harborStation, Cost = 0.5m }); _harborStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 0.5m }); _stations = new List <Station> { _sengkangStation, _kovanStation, _bishanStation, _harborStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _harborStation.StationName }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Harbor") { NearestToStart = _kovanStation, MinimumCost = 1.5m }, new Station("Kovan") { NearestToStart = _sengkangStation, MinimumCost = 1 }, new Station("Bishan") { NearestToStart = _sengkangStation, MinimumCost = 1 }, new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, }; path.Should().NotBeEmpty() .And.HaveCount(4) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart)); }
public void Scenario_User_Cant_Journey_When_DT_CG_CE_Comes_As_Interchanges_At_Night(string line) { _sengkangStation.AddLine("NE"); _bishanStation.AddLine("NE"); _bishanStation.AddLine(line); _kovanStation.AddLine(line); _tuasStation.AddLine(line); _tuasStation.AddLine("CC"); _ubiStation.AddLine("CC"); _harborStation.AddLine("CC"); _sengkangStation.Connections.Add(new Edge { ConnectedStation = _bishanStation, Cost = 1 }); _bishanStation.Connections.Add(new Edge { ConnectedStation = _sengkangStation, Cost = 1 }); _bishanStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _bishanStation, Cost = 1 }); _kovanStation.Connections.Add(new Edge { ConnectedStation = _tuasStation, Cost = 1 }); _tuasStation.Connections.Add(new Edge { ConnectedStation = _kovanStation, Cost = 1 }); _tuasStation.Connections.Add(new Edge { ConnectedStation = _ubiStation, Cost = 1 }); _ubiStation.Connections.Add(new Edge { ConnectedStation = _tuasStation, Cost = 1 }); _ubiStation.Connections.Add(new Edge { ConnectedStation = _harborStation, Cost = 1 }); _harborStation.Connections.Add(new Edge { ConnectedStation = _ubiStation, Cost = 1 }); _stations = new List <Station> { _sengkangStation, _bishanStation, _kovanStation, _tuasStation, _ubiStation, _harborStation }; var dijkstraSearch = new DijkstraSearch(); var option = new InputOption { Start = _sengkangStation.StationName, End = _harborStation.StationName, StartTime = new DateTime(2019, 01, 31, 22, 00, 00) }; var path = dijkstraSearch.FillShortestPath(_stations, option); var expected = new List <Station> { new Station("Sengkang") { NearestToStart = null, MinimumCost = 0 }, new Station("Bishan") { NearestToStart = _sengkangStation, MinimumCost = 11 }, new Station("Kovan") { NearestToStart = null, MinimumCost = null }, new Station("Tuas") { NearestToStart = null, MinimumCost = null }, new Station("Ubi") { NearestToStart = null, MinimumCost = null }, new Station("Harbor") { NearestToStart = null, MinimumCost = null }, }; path.Should().NotBeEmpty() .And.HaveCount(6) .And.BeEquivalentTo(expected, options => options .Including(o => o.StationName) .Including(o => o.MinimumCost) .Including(a => a.NearestToStart) ); }