public void Reinsert(RoutingPlan plan, List <Visit> toInsert, int discrep)//List<Route> routes, List<Visit> toInsert, int discrep) { iterations++; if (toInsert.Count == 0) { double newCost = plan.CalculateCost(); if (newCost < bestPlanCost) { bestPlan = plan; bestPlanCost = newCost; } } else { Visit visit = plan.ChooseFarthest(toInsert); toInsert.Remove(visit); int i = 0; foreach ((int, int)position in plan.RankedPositions(visit)) { if (i <= discrep) { // Copy bestPlan RoutingPlan newPlan = new RoutingPlan(plan.CopyRoutes(), plan.GetProblem()); //Console.WriteLine("derdst" + toInsert.Count); // Make new plan and call reinsert newPlan.PlaceVisit(visit, position); //Console.WriteLine("viertst" + toInsert.Count); Reinsert(newPlan, toInsert, discrep - i); i++; } else { break; } } } }
public List <Visit> RemoveVisits(RoutingPlan plan) { List <Visit> allVisits = plan.GetVisits(); int randomIndex = rand.Next(allVisits.Count); //plan.RemoveVisit(allVisits[randomIndex]); // Remove inplan, RemoveVisit(plan) and add to removed set // Create list with only the removed visit List <Visit> removedVisits = new List <Visit> { allVisits[randomIndex] }; while (removedVisits.Count < toRemove) { // Get random removed visit id //List<Visit> removed = plan.GetRemoved(); randomIndex = rand.Next(removedVisits.Count); Visit randomVisit = removedVisits[randomIndex]; // Rank non-removed visits with relatedness List <Visit> ranked = plan.RankRelatedness(randomVisit, removedVisits); if (ranked.Count > 0) { // Get random double between 0 and 1 double random = rand.NextDouble(); // Get slightly random element from ranked list and remove it Visit newRemoved = ranked[Convert.ToInt32((ranked.Count - 1) * Math.Pow(random, determinism))]; //plan.RemoveVisit(newRemoved); removedVisits.Add(newRemoved); } } return(removedVisits); }
public void Tester(Visit visit, (int, int) position)
public void Draw(RoutingPlan plan) { // FIND MAX DISTANCE (int, int, int, int) extrema = plan.GetExtrema(); int maxX = extrema.Item3; //- extrema.Item1; int maxY = extrema.Item4; //- extrema.Item2; Random rand = new Random(); using (var bmp = new Bitmap(maxX * 5, maxY * 5)) using (var gfx = Graphics.FromImage(bmp)) using (var pen = new Pen(Color.Black)) { pen.Width = 3.0F; gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; gfx.Clear(Color.White); Visit lastVisit = plan.GetProblem().GetDepot(); int depotX = lastVisit.GetX() * 5; int depotY = lastVisit.GetY() * 5; // Draw depot Rectangle rect = new Rectangle(depotX - 3, depotY - 3, 6, 6); gfx.DrawEllipse(pen, rect); PointF point1; PointF point2; foreach (Route route in plan.GetRoutes()) { pen.Color = Color.FromArgb(rand.Next()); // Get visits and depot List<Visit> visits = route.GetVisits(); // Start with depot int lastX = depotX; int lastY = depotY; foreach (Visit visit in visits) { // Draw visit int xVisit = visit.GetX() * 5; int yVisit = visit.GetY() * 5; rect = new Rectangle(xVisit - 3, yVisit - 3, 6, 6); gfx.DrawEllipse(pen, rect); // Draw line to visit point1 = new PointF(lastX, lastY); point2 = new PointF(xVisit, yVisit); gfx.DrawLine(pen, point1, point2); // Change last visit lastX = xVisit; lastY = yVisit; } // Draw line back to depot point1 = new PointF(lastX, lastY); point2 = new PointF(depotX, depotY); gfx.DrawLine(pen, point1, point2); } bmp.Save("RouteVisualisation.png"); } }
public double GetDistanceFromDepot(Visit visit) { return(distancesFromDepot.GetDistance(visit)); }