//inserting swap move into swap tabu list public void InsertIntoSwapTabuList(SwapTabuMove swapTabuMove) { if (swapTabuList.Count >= 10) { swapTabuList.RemoveAt(swapTabuList.Count - 1); } swapTabuList.Insert(0, swapTabuMove); }
//removing swap move from swap tabu list public void RemoveFromSwapTabuList(SwapTabuMove move) { foreach (SwapTabuMove swapTabuMove in swapTabuList) { if ((swapTabuMove.customer1 == move.customer1 && swapTabuMove.customer2 == move.customer2) || swapTabuMove.customer1 == move.customer2 && swapTabuMove.customer2 == move.customer1) { swapTabuList.Remove(swapTabuMove); return; } } }
//swapping pair of every 2 customer and returning global best neighbour public List <List <Customer> > MutateSwap(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; SwapTabuMove swapTabuMove = new SwapTabuMove(); double dif = 0; bool swapTabuMoveSelected = false; for (int firstRouteIndex = 0; firstRouteIndex < routeList.Count; firstRouteIndex++) { List <Customer> firstRoute = routeList[firstRouteIndex]; for (int firstRouteCustomerIndex = 1; firstRouteCustomerIndex < firstRoute.Count - 1; firstRouteCustomerIndex++) { for (int secondRouteIndex = firstRouteIndex; secondRouteIndex < routeList.Count; secondRouteIndex++) { List <Customer> secondRoute = routeList[secondRouteIndex]; for (int secondRouteCustomerIndex = 1; secondRouteCustomerIndex < secondRoute.Count - 1; secondRouteCustomerIndex++) { //Console.WriteLine("Before Swap: "+ firstRoute[firstRouteCustomerIndex].id + " "+ secondRoute[secondRouteCustomerIndex].id); swap(firstRoute, firstRouteCustomerIndex, secondRoute, secondRouteCustomerIndex); //Console.WriteLine("After Swap: " + firstRoute[firstRouteCustomerIndex].id + " " + secondRoute[secondRouteCustomerIndex].id); 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 (IsSwapTabu(firstRouteCustomerIndex, secondRouteCustomerIndex)) { if (costOfRouteList < costOfBestRouteList && costOfRouteList < costOfNeighbourRouteList) { dif = costOfRouteList - costOfNeighbourRouteList; neighbourRouteList.Clear(); Utils.CopyListOfList(routeList, neighbourRouteList); costOfNeighbourRouteList = costOfRouteList; swapTabuMoveSelected = true; swapTabuMove.customer1 = firstRouteCustomerIndex; swapTabuMove.customer2 = secondRouteCustomerIndex; } } else { if (costOfRouteList < costOfNeighbourRouteList) { dif = costOfRouteList - costOfNeighbourRouteList; neighbourRouteList.Clear(); Utils.CopyListOfList(routeList, neighbourRouteList); costOfNeighbourRouteList = costOfRouteList; swapTabuMoveSelected = false; swapTabuMove.customer1 = firstRouteCustomerIndex; swapTabuMove.customer2 = secondRouteCustomerIndex; } else if (costOfRouteList - costOfNeighbourRouteList < dif) { dif = costOfRouteList - costOfNeighbourRouteList; neighbourRouteList.Clear(); Utils.CopyListOfList(routeList, neighbourRouteList); costOfNeighbourRouteList = costOfRouteList; swapTabuMoveSelected = false; swapTabuMove.customer1 = firstRouteCustomerIndex; swapTabuMove.customer2 = secondRouteCustomerIndex; } } } swap(firstRoute, firstRouteCustomerIndex, secondRoute, secondRouteCustomerIndex); } } } } if (swapTabuMoveSelected) { RemoveFromSwapTabuList(swapTabuMove); } else { InsertIntoSwapTabuList(swapTabuMove); } Console.WriteLine("mutateSwap: " + Utils.CalculateTotalDistanceOfAllRoute(routeList, cTocDistance) + " " + Utils.CalculateTotalDistanceOfAllRoute(neighbourRouteList, cTocDistance)); return(neighbourRouteList); }