//List<string> text = new List<string>();

        public LargeNeighbourhoodSearch(RoutingPlan plan, int maxIterations, int discrepancies,
                                        int attempts, int determinism)
        {
            this.bestPlan      = plan;
            this.bestPlanCost  = bestPlan.CalculateCost();
            this.maxIterations = maxIterations;
            this.discrepancies = discrepancies;
            this.attempts      = attempts;
            this.determinism   = determinism;



            for (int i = 0; i < 100; i++)
            {
                List <int> row = new List <int>();
                for (int j = 0; j < 100; j++)
                {
                    row.Add(0);
                }
                wereInRouteTogether.Add(row);
            }
        }
        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 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");
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            List <List <int> > costEachIteration = new List <List <int> >();

            PrintPlan printer = new PrintPlan();

            #region files variable

            /*
             * List<string> files = new List<string> { "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt", "R101.txt",
             *  "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt", "R102.txt",
             *  "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt", "R103.txt",
             *  "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt", "R104.txt",
             *  "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt", "R105.txt",
             *  "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt", "R106.txt",
             *  "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt", "R107.txt",
             *  "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt", "R108.txt",
             *  "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt", "R109.txt",
             *  "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt", "R110.txt",
             *  "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt", "R111.txt",
             *  "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt", "R112.txt",
             *  "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt", "C101.txt",
             *  "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt", "C102.txt",
             *  "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt", "C103.txt",
             *  "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt", "C104.txt",
             *  "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt", "C105.txt",
             *  "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt", "C106.txt",
             *  "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt", "C107.txt",
             *  "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt", "C108.txt",
             *  "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt", "C109.txt",
             *  "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt", "RC101.txt",
             *  "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt", "RC102.txt",
             *  "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt", "RC103.txt",
             *  "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt", "RC104.txt",
             *  "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt", "RC105.txt",
             *  "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt", "RC106.txt",
             *  "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt", "RC107.txt",
             *  "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt", "RC108.txt",
             *  "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt", "R201.txt",
             *  "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt", "R202.txt",
             *  "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt", "R203.txt",
             *  "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt", "R204.txt",
             *  "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt", "R205.txt",
             *  "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt", "R206.txt",
             *  "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt", "R207.txt",
             *  "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt", "R208.txt",
             *  "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt", "R209.txt",
             *  "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt", "R210.txt",
             *  "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt", "R211.txt",
             *  "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt", "C201.txt",
             *  "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt", "C202.txt",
             *  "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt", "C203.txt",
             *  "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt", "C204.txt",
             *  "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt", "C205.txt",
             *  "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt", "C206.txt",
             *  "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt", "C207.txt",
             *  "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt", "C208.txt",
             *  "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt", "RC201.txt",
             *  "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt", "RC202.txt",
             *  "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt", "RC203.txt",
             *  "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt", "RC204.txt",
             *  "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt", "RC205.txt",
             *  "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt", "RC206.txt",
             *  "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt", "RC207.txt",
             *  "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt", "RC208.txt" };
             */
            #endregion
            List <string> files = new List <string> {
                "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt", "RC101Split2.txt"
            };

            List <List <List <int> > > allDinges       = new List <List <List <int> > >();
            List <List <int> >         allTotalRemoved = new List <List <int> >();

            int counter = 1;
            foreach (string file in files)
            {
                Console.WriteLine(file + "   " + counter + "/" + files.Count);
                counter++;

                // Get problem from text file
                ReadProblem reader  = new ReadProblem(file);
                Problem     problem = reader.GetProblem();

                // Initial routing plan with one route per visit
                List <Route> initialRoutes = new List <Route>();
                foreach (Visit visit in problem.GetVisits())
                {
                    initialRoutes.Add(new Route(new List <Visit> {
                        visit
                    }, problem.GetCapacity()));
                }
                RoutingPlan initialPlan = new RoutingPlan(initialRoutes, problem);

                int maxIterations = 2000;
                int discrepancies = 5;   // discrep
                int attempts      = 250; // a
                int determinism   = 10;  // D

                LargeNeighbourhoodSearch LNS = new LargeNeighbourhoodSearch(initialPlan,
                                                                            maxIterations, discrepancies, attempts, determinism);
                RoutingPlan plan = LNS.Search();

                costEachIteration.Add(LNS.GetCostEachIteration());

                allDinges.Add(LNS.GetRoutesTogether());
                allTotalRemoved.Add(new List <int> {
                    LNS.GetTotalRemoved()
                });


                List <Route>       routes   = plan.GetRoutes();
                List <List <int> > solution = new List <List <int> >();
                for (int i = 0; i < 100; i++)
                {
                    List <int> row = new List <int>();
                    for (int j = 0; j < 100; j++)
                    {
                        row.Add(0);
                    }
                    solution.Add(row);
                }

                foreach (Route route in routes)
                {
                    List <int> ids = route.GetIds();

                    foreach (int id1 in ids)
                    {
                        foreach (int id2 in ids)
                        {
                            if (id1 != id2)
                            {
                                solution[id1 - 1][id2 - 1] += 1;
                            }
                        }
                    }
                }
                allDinges.Add(solution);
            }

            Console.WriteLine("Done");

            //printer.printXLS("test1.xlsx", costEachIteration, files);

            printer.PrintDeleted("EerdereRoutesSamen RC101Split2.xlsx", allDinges, files);
            printer.printXLS("Cost RC101Split2.xlsx", costEachIteration, files);
        }
        public RoutingPlan Search()
        {
            currentAttempts   = 0;
            bestEachIteration = new List <int>();

            capacityEachIteration         = new List <int>();
            routesEachIteration           = new List <int>();
            maxDistanceEachIteration      = new List <int>();
            AverageDeviationEachIteration = new List <int>();
            meanEachIteration             = new List <int>();
            costEachIteration             = new List <int> {
                (int)(bestPlan.CalculateCost() * 100)
            };
            toRemoveEachIteration = new List <int>();
            totalRemoved          = 0;

            for (int i = 0; i < maxIterations; i++)
            {
                //while (bestPlan.GetAmountOfRoutes() > 10) {



                // Check if currentAttempts >= attempts
                if (currentAttempts >= attempts)
                {
                    if (toRemove < 100)
                    {
                        toRemove++;
                    }
                    currentAttempts = 0;
                }

                double currentCost = bestPlan.CalculateCost();
                iterations = 0;

                // Copy bestPlan
                RoutingPlan newPlan  = new RoutingPlan(bestPlan.CopyRoutes(), bestPlan.GetProblem());
                RoutingPlan copyPlan = new RoutingPlan(bestPlan.CopyRoutes(), bestPlan.GetProblem());

                List <Visit> removed = RemoveVisits(newPlan);
                totalRemoved += toRemove;

                // copy removed
                List <Visit> removedCopy = new List <Visit>();
                foreach (Visit visit1 in removed)
                {
                    removedCopy.Add(visit1);
                }

                Reinsert(newPlan, removed, discrepancies);

                if (bestPlan.CalculateCost() < currentCost)
                {
                    currentAttempts = 0;

                    // Add to weretogether
                    foreach (Route route in copyPlan.GetRoutes())
                    {
                        foreach (Visit visit in removedCopy)
                        {
                            if (route.GetIds().Contains(visit.GetId()))
                            {
                                foreach (Visit v in route.GetVisits())
                                {
                                    if (v.GetId() != visit.GetId())
                                    {
                                        wereInRouteTogether[visit.GetId() - 1][v.GetId() - 1]++;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    currentAttempts++;
                }

                bestEachIteration.Add(bestPlan.GetAmountOfRoutes());

                capacityEachIteration.Add(bestPlan.GetPercentageCapacity());
                routesEachIteration.Add(bestPlan.GetAmountOfRoutes());
                maxDistanceEachIteration.Add(bestPlan.GetLongestNormDistance());
                AverageDeviationEachIteration.Add(bestPlan.GetAverageDeviationRouteLength());
                meanEachIteration.Add((int)(bestPlan.GetAverageRouteLength() * 100));
                costEachIteration.Add((int)(bestPlan.CalculateCost() * 100));
                toRemoveEachIteration.Add(toRemove);
            }

            //Console.WriteLine(totalRemoved);

            return(bestPlan);
        }