예제 #1
0
 //insering insertion move into insertion tabu list
 public void InsertIntoInsertionTabuList(InsertionTabuMove insertionTabuMove)
 {
     if (insertionTabuList.Count >= 10)
     {
         insertionTabuList.RemoveAt(insertionTabuList.Count - 1);
     }
     insertionTabuList.Insert(0, insertionTabuMove);
 }
예제 #2
0
 //removing insertion move from insertion tabu list
 public void RemoveFromInsertionTabuList(InsertionTabuMove insertionTabuMove)
 {
     foreach (InsertionTabuMove tabuMove in insertionTabuList)
     {
         if (tabuMove.customer == insertionTabuMove.customer &&
             (tabuMove.routeList1 == insertionTabuMove.routeList1 && tabuMove.routeList2 == insertionTabuMove.routeList2) ||
             (tabuMove.routeList1 == insertionTabuMove.routeList2 && tabuMove.routeList2 == insertionTabuMove.routeList1))
         {
             insertionTabuList.Remove(tabuMove);
         }
     }
 }
예제 #3
0
        //inserting every customer at every possible place and returning global best neighbour
        public List <List <Customer> > MutateInsertion(List <List <Customer> > routeList, double costOfRouteList, double costOfBestRouteList)
        {
            List <List <Customer> > neighbourRouteList = new List <List <Customer> >();

            Utils.CopyListOfList(routeList, neighbourRouteList);
            double costOfNeighbourRouteList = costOfRouteList;
            bool   feasible = false;

            InsertionTabuMove insertionTabuMove = new InsertionTabuMove();
            double            dif          = 0;
            bool insertionTabuMoveSelected = false;

            for (int firstRouteIndex = 0; firstRouteIndex < routeList.Count; firstRouteIndex++)
            {
                List <Customer> firstRoute = routeList[firstRouteIndex];
                for (int firstRouteCustomerIndex = 1; firstRouteCustomerIndex < firstRoute.Count - 1; firstRouteCustomerIndex++)
                {
                    Customer firstRouteCustomer = firstRoute[firstRouteCustomerIndex];
                    firstRoute.RemoveAt(firstRouteCustomerIndex);

                    for (int secondRouteIndex = firstRouteIndex; secondRouteIndex < routeList.Count; secondRouteIndex++)
                    {
                        List <Customer> secondRoute = routeList[secondRouteIndex];
                        for (int secondRouteCustomerIndex = 1; secondRouteCustomerIndex < secondRoute.Count; secondRouteCustomerIndex++)
                        {
                            secondRoute.Insert(secondRouteCustomerIndex, firstRouteCustomer);
                            feasible = Utils.CheckTimeConstraint(firstRoute, cTocDistance) && Utils.CheckCapacityConstraint(firstRoute, vehicle.capacity) &&
                                       Utils.CheckTimeConstraint(secondRoute, cTocDistance) && Utils.CheckCapacityConstraint(secondRoute, vehicle.capacity);

                            if (feasible)
                            {
                                costOfRouteList = Utils.CalculateTotalDistanceOfAllRoute(routeList, cTocDistance);

                                if (IsInsertionTabu(firstRouteIndex, firstRouteCustomerIndex, secondRouteIndex))
                                {
                                    if (costOfRouteList < costOfBestRouteList && costOfRouteList < costOfNeighbourRouteList)
                                    {
                                        dif = costOfRouteList - costOfNeighbourRouteList;
                                        neighbourRouteList.Clear();
                                        Utils.CopyListOfList(routeList, neighbourRouteList);
                                        costOfNeighbourRouteList = costOfRouteList;

                                        insertionTabuMoveSelected    = true;
                                        insertionTabuMove.routeList1 = firstRouteIndex;
                                        insertionTabuMove.customer   = firstRouteCustomerIndex;
                                        insertionTabuMove.routeList2 = secondRouteIndex;
                                    }
                                }
                                else
                                {
                                    if (costOfRouteList < costOfNeighbourRouteList)
                                    {
                                        dif = costOfRouteList - costOfNeighbourRouteList;
                                        neighbourRouteList.Clear();
                                        Utils.CopyListOfList(routeList, neighbourRouteList);
                                        costOfNeighbourRouteList = costOfRouteList;

                                        insertionTabuMoveSelected    = true;
                                        insertionTabuMove.routeList1 = firstRouteIndex;
                                        insertionTabuMove.customer   = firstRouteCustomerIndex;
                                        insertionTabuMove.routeList2 = secondRouteIndex;
                                    }
                                    else if (costOfRouteList - costOfNeighbourRouteList < dif)
                                    {
                                        dif = costOfRouteList - costOfNeighbourRouteList;
                                        neighbourRouteList.Clear();
                                        Utils.CopyListOfList(routeList, neighbourRouteList);
                                        costOfNeighbourRouteList = costOfRouteList;

                                        insertionTabuMoveSelected    = true;
                                        insertionTabuMove.routeList1 = firstRouteIndex;
                                        insertionTabuMove.customer   = firstRouteCustomerIndex;
                                        insertionTabuMove.routeList2 = secondRouteIndex;
                                    }
                                }
                            }
                            secondRoute.RemoveAt(secondRouteCustomerIndex);
                        }
                    }

                    firstRoute.Insert(firstRouteCustomerIndex, firstRouteCustomer);
                }
            }

            if (insertionTabuMoveSelected)
            {
                RemoveFromInsertionTabuList(insertionTabuMove);
            }
            else
            {
                InsertIntoInsertionTabuList(insertionTabuMove);
            }
            Console.WriteLine("mutateInsertion: " + Utils.CalculateTotalDistanceOfAllRoute(routeList, cTocDistance) + " " + Utils.CalculateTotalDistanceOfAllRoute(neighbourRouteList, cTocDistance));
            return(neighbourRouteList);
        }