internal override void OnTerminated() { CityMapClone = null; Task.WaitAll(Solver.AlgorithmTasks.Values.ToArray()); Console.ForegroundColor = ConsoleColor.Green; SendMessage(MessageCode.GreedyStop); SendMessage(MessageCode.SolverUpdatedSolution, Solver.BestSolution.Id, Solver.BestSolution.Cost); Console.ForegroundColor = ConsoleColor.Gray; base.OnTerminated(); if (!_canDoImprovements) { return; } Task improvementTask = RunImprovementAlgorithm(); try { improvementTask.Wait(); } catch (AggregateException ae) { OnError(ae.InnerException); } }
/// <summary> /// This implementation is the classic GetClosestNeighborByScore. /// It returns the best candidate node near to point of interest passed as argument. /// </summary> /// <param name="interestPoint">Point of interest</param> /// <returns></returns> protected virtual InterestPointWorker GetBestNeighbor(InterestPointWorker interestPoint) { int bestScore = default; InterestPointWorker candidateNode = default; var adjPoiIds = CityMapClone.GetAdjacentNodes(interestPoint.Entity.Id); adjPoiIds.ToList().ForEach(adjPoiId => { var adjNode = CityMapClone[adjPoiId]; if (adjNode.IsVisited) { return; } var adjNodeScore = adjNode.Entity.Score.Value; if (adjNodeScore > bestScore) { bestScore = adjNodeScore; candidateNode = adjNode; } else if (adjNodeScore == bestScore) { CityMapGraph.SetRandomCandidateId(candidateNode, adjNode, out int pointId); candidateNode = CityMapClone[pointId]; } }); return(candidateNode); }
/// <summary> /// Initialize the graph of the city using CityScoverRepository. /// </summary> private void InitializeTour() { CityScoverRepository.LoadPoints(WorkingConfiguration.PointsFilename); Points = (WorkingConfiguration.TourCategory == TourCategoryType.None) ? CityScoverRepository.Points : GetPointsByCategory(WorkingConfiguration.TourCategory); RoutesGenerator.GenerateRoutes(Points, Points.Count()); CityScoverRepository.LoadRoutes(Points); // Creation of the Graph. CityMapGraph cityGraph = new CityMapGraph(); foreach (var point in Points) { cityGraph.AddNode(point.Id, new InterestPointWorker(point)); } var routes = CityScoverRepository.Routes; foreach (var route in routes) { cityGraph.AddEdge(route.PointFrom.Id, route.PointTo.Id, new RouteWorker(route)); } if (cityGraph.NodeCount == 0) { string message = MessagesRepository.GetMessage( MessageCode.SolverGraphCreationError, nameof(InitializeTour)); throw new Exception(message); } CityMapGraph = cityGraph; }
internal override void OnInitializing() { base.OnInitializing(); Console.ForegroundColor = ConsoleColor.Green; SendMessage(MessageCode.GreedyStart, Type); Console.ForegroundColor = ConsoleColor.Gray; Tour = new CityMapGraph(); NodesQueue = new Queue <int>(); SolutionsHistory = new Collection <ToSolution>(); CityMapClone = Solver.CityMapGraph.DeepCopy(); _canDoImprovements = default; int maxNodesToAdd = default; if (Parameters.ContainsKey(ParameterCodes.CanDoImprovements)) { _canDoImprovements = Parameters[ParameterCodes.CanDoImprovements]; } if (Parameters.ContainsKey(ParameterCodes.GreedyMaxNodesToAdd)) { maxNodesToAdd = Parameters[ParameterCodes.GreedyMaxNodesToAdd]; } InitializeNodesQueue(maxNodesToAdd); StartingPoint = CityMapClone.GetStartPoint(); if (StartingPoint is null) { throw new OperationCanceledException( $"{nameof(StartingPoint)} in {nameof(NearestNeighbor)}"); } StartingPoint.IsVisited = true; _timeSpent = DateTime.Now; }
private bool IsTimeWindowsConstraintSatisfied(ToSolution solution) { bool satisfied = true; CityMapGraph solutionGraph = solution.Tour; using (var processingNodes = solutionGraph.TourPoints.GetEnumerator()) { while (satisfied && processingNodes.MoveNext()) { var node = processingNodes.Current; TimeSpan visitTime = default; if (node?.Entity.TimeVisit != null) { visitTime = node.Entity.TimeVisit.Value; } if (node == null) { continue; } DateTime nodeTime = node.ArrivalTime + node.WaitOpeningTime; foreach (var time in node.Entity.OpeningTimes) { if (time.ClosingTime.HasValue && nodeTime > (time.ClosingTime.Value - visitTime)) { satisfied = false; //continue; } } } } return(satisfied); }
public void CreateGraph2() { CityMapGraph graph = new CityMapGraph(); Assert.IsTrue(graph.NodeCount == 0); Assert.IsTrue(graph.EdgeCount == 0); InterestPoint p1 = new InterestPoint("P1"); graph.AddNode(1, p1); graph.AddNode(2, new InterestPoint("P2")); graph.AddNode(3, new InterestPoint("P3")); graph.AddNode(4, new InterestPoint("P4")); graph.AddNode(5, new InterestPoint("P5")); Assert.IsTrue(graph.NodeCount == 5); graph.AddUndirectedEdge(1, 2, new Route(100.0)); graph.AddUndirectedEdge(1, 5, new Route(200.0)); graph.AddUndirectedEdge(2, 5, new Route(300.0)); graph.AddUndirectedEdge(2, 4, new Route(400.0)); graph.AddUndirectedEdge(2, 3, new Route(500.0)); graph.AddUndirectedEdge(3, 4, new Route(600.0)); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(17, 2, new Route(250.0))); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(1, 17, new Route(250.0))); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(1, 2, new Route(250.0))); Assert.ThrowsException <InvalidOperationException>(() => graph.AddUndirectedEdge(2, 2)); Assert.IsTrue(graph.EdgeCount == 12); }
public void CreateGraph3() { CityMapGraph graph = new CityMapGraph(); Assert.IsTrue(graph.NodeCount == 0); Assert.IsTrue(graph.EdgeCount == 0); graph.AddNode(1, new InterestPoint("P1")); graph.AddNode(2, new InterestPoint("P2")); graph.AddNode(3, new InterestPoint("P3")); graph.AddNode(4, new InterestPoint("P4")); graph.AddNode(5, new InterestPoint("P5")); Assert.IsTrue(graph.NodeCount == 5); //graph.AddUndirectedEdge(1, 2); //graph.AddUndirectedEdge(1, 5); //graph.AddUndirectedEdge(2, 4); //graph.AddUndirectedEdge(4, 3); //graph.AddUndirectedEdge(5, 3); //Assert.IsTrue(graph.EdgeCount == 10); graph.AddEdge(1, 2); graph.AddEdge(2, 5); graph.AddEdge(5, 4); graph.AddEdge(4, 3); graph.AddEdge(3, 1); Assert.IsTrue(graph.EdgeCount == 5); var P1Neighbors = graph.GetAdjacentNodes(1); Assert.IsTrue(P1Neighbors.Count() == 1); }
public void CreateGraph4() { CityMapGraph graph = new CityMapGraph(); graph.AddNode(1, new InterestPoint("P1")); graph.AddNode(2, new InterestPoint("P2")); graph.AddNode(3, new InterestPoint("P3")); graph.AddNode(4, new InterestPoint("P4")); graph.AddEdge(1, 2, new Route(100.0)); graph.AddEdge(2, 3, new Route(200.0)); graph.AddEdge(3, 4, new Route(300.0)); graph.AddEdge(4, 1, new Route(400.0)); Assert.IsTrue(graph.AreAdjacentEdges(1, 2, 2, 3)); Assert.IsFalse(graph.AreAdjacentEdges(1, 2, 3, 4)); Assert.IsFalse(graph.AreAdjacentEdges(1, 2, 3, 2)); Assert.IsTrue(graph.AreAdjacentEdges(1, 2, 4, 1)); IEnumerable <Route> edges = graph.Edges; IEnumerable <Route> nodeEdges = graph.GetEdges(1); CityMapGraph graph2 = new CityMapGraph(); graph2.AddNode(1, new InterestPoint("P1")); graph2.AddNode(2, new InterestPoint("P2")); graph2.AddNode(3, new InterestPoint("P3")); graph2.AddNode(4, new InterestPoint("P4")); graph2.AddNode(5, new InterestPoint("P5")); graph2.AddNode(6, new InterestPoint("P6")); graph2.AddNode(7, new InterestPoint("P7")); graph2.AddNode(8, new InterestPoint("P8")); graph2.AddEdge(1, 2, new Route(10.0)); graph2.AddEdge(2, 4, new Route(10.0)); graph2.AddEdge(4, 3, new Route(10.0)); graph2.AddEdge(3, 5, new Route(10.0)); graph2.AddEdge(5, 6, new Route(10.0)); graph2.AddEdge(6, 7, new Route(10.0)); graph2.AddEdge(7, 8, new Route(10.0)); string prova = $"PROVA: "; IEnumerable <InterestPoint> visit = graph2.BreadthFirstSearch(1, (node, isVisited) => node.IsVisited = isVisited, (node) => { return(node.IsVisited); }, node => prova += node.Name, edge => prova += " -- "); }
public void CreateGraph1() { CityMapGraph graph = new CityMapGraph(); Assert.IsTrue(graph.NodeCount == 0); Assert.IsTrue(graph.EdgeCount == 0); InterestPoint p1 = new InterestPoint("P1"); graph.AddNode(1, p1); graph.AddNode(2, new InterestPoint("P2")); graph.AddNode(3, new InterestPoint("P3")); graph.AddNode(4, new InterestPoint("P4")); graph.AddNode(5, new InterestPoint("P5")); graph.AddNode(6, new InterestPoint("P6")); Assert.IsTrue(graph.NodeCount == 6); graph.AddEdge(1, 2, new Route(100.0)); graph.AddEdge(1, 4, new Route(200.0)); graph.AddEdge(4, 2, new Route(300.0)); graph.AddEdge(2, 5, new Route(400.0)); graph.AddEdge(5, 4, new Route(500.0)); graph.AddEdge(3, 5, new Route(600.0)); graph.AddEdge(3, 6, new Route(700.0)); graph.AddEdge(6, 6, new Route(0.0)); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(17, 2, new Route(250.0))); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(1, 17, new Route(250.0))); Assert.ThrowsException <InvalidOperationException>(() => graph.AddEdge(1, 2, new Route(250.0))); Assert.IsTrue(graph.EdgeCount == 8); Assert.IsTrue(graph.ContainsNode(4)); Assert.IsFalse(graph.ContainsNode(17)); List <int> plist = new List <int>(graph.GetPredecessorNodes(5)); Assert.IsTrue(plist.Count == 2); Assert.IsTrue(plist[0] == 2); Assert.IsTrue(plist[1] == 3); List <int> slist = new List <int>(graph.GetSuccessorNodes(5)); Assert.IsTrue(slist.Count == 1); Assert.IsTrue(slist[0] == 4); }