public Route Solve(List<Point> points) { this.startPoint = points.First(p => p.Type == PointType.Start); this.endPoint = points.First(p => p.Type == PointType.End); // init route Route route = new Route(); route.Points.Add(this.startPoint); // remaining points List<Point> unvisitedPoints = points.Where(p => p.Type == PointType.Waypoiny).ToList(); // loop nodes Point nextPoint = this.startPoint; while(nextPoint != null) { nextPoint = GetNearPoint(nextPoint, unvisitedPoints); if(nextPoint != null) { route.Points.Add(nextPoint); unvisitedPoints.Remove(nextPoint); } } route.Points.Add(this.endPoint); route.Points = route.Points; //route.Result = String.Join(" - ", route.Points.Select(p => p.Name)) + " " + route.Cost.ToString("#.0"); route.Result = "Cost: " + Convert.ToInt32(route.Cost).ToString(); return route; }
public Route Solve(List<Point> points) { BestRoute = null; ExplorerCounter = 0; ExplorerFinishedCounter = 0; this.startPoint = points.First(p => p.Type == PointType.Start); this.endPoint = points.First(p => p.Type == PointType.End); // remaining points List<Point> unvisitedPoints = points.Where(p => p.Type == PointType.Waypoiny).ToList(); // Start del primo explorer Explorer explorer = new Explorer(new List<Point>(), this.startPoint, unvisitedPoints, this.endPoint); BestRoute.Result = String.Format("Total: {0}, Finished: {1}, Cost: {2}", ExplorerCounter, ExplorerFinishedCounter, Convert.ToInt32(BestRoute.Cost)); return BestRoute; }
// Metodo statico chiamato dai singoli explorer al termine dell'esplorazione public static void OnFinished(Route route) { ExplorerFinishedCounter++; if(BestRoute == null || route.Cost < BestRoute.Cost) { BestRoute = route; } }