public override Solution executeStrategy(Solution toStartFrom)
        {
            Planning = toStartFrom.GetRandomPlanning();
            if (Planning.Item3.Count == 0)
                return toStartFrom;

            OriginalRoute = Planning.Item3[random.Next(Planning.Item3.Count)];
            if (OriginalRoute == null || OriginalRoute.Orders.Count < 2)
                return toStartFrom;

            int orderIndex = random.Next(OriginalRoute.Orders.Count - 1);
            OrderRemoved = OriginalRoute.Orders[orderIndex];
            OrderBefore = orderIndex == 0 ? null : OriginalRoute.Orders[orderIndex - 1];
            OrderRemoved.AddAvailableOrderBackToCluster();
            OriginalRoute.RemoveOrder(OrderRemoved);

            if (OriginalRoute.Orders.Count == 1)
            { // Basically delete the route (remove it from planning)
                toStartFrom.RemoveRouteFromPlanning(Planning.Item1, Planning.Item2, OriginalRoute);
                toStartFrom.RemoveRoute(OriginalRoute);
            }

            strategyHasExecuted = true;
            return toStartFrom;
        }
        public override Solution undoStrategy(Solution toStartFrom)
        {
            if (!strategyHasExecuted)
                return toStartFrom;

                toStartFrom.AddRoute(old_route);
                toStartFrom.RemoveRouteFromPlanning(Planning.Item1, Planning.Item2, new_route);
                toStartFrom.AddRouteToPlanning(Planning.Item1, Planning.Item2, old_route);
                toStartFrom.RemoveRoute(new_route);

            return toStartFrom;
        }
Пример #3
0
        public override Solution undoStrategy(Solution toStartFrom)
        {
            if (!strategyHasExecuted)
                return toStartFrom;

            foreach (Order order in routeCreated.Orders)
                order.AddAvailableOrderBackToCluster();

            toStartFrom.RemoveRoute(routeCreated);
            routeCreated.Destroy();

            return toStartFrom;
        }
        public override Solution executeStrategy(Solution toStartFrom)
        {
            if (toStartFrom.AvailableRoutes.Count == 0)
                return toStartFrom;

            int routeIndex = random.Next(toStartFrom.AvailableRoutes.Count);
            Route routeToDestroy = toStartFrom.AvailableRoutes[routeIndex];
            toStartFrom.RemoveRoute(routeToDestroy);
            ordersDestroyed = routeToDestroy.Orders;
            dayDestroyed = routeToDestroy.Day;
            routeToDestroy.Destroy();

            strategyHasExecuted = true;
            return toStartFrom;
        }
        public override Solution executeStrategy(Solution toStartFrom)
        {
            Planning = toStartFrom.GetRandomPlanning();
            if (Planning.Item3.Count == 0)
                return toStartFrom;

            Route routeToDestroy = Planning.Item3[random.Next(Planning.Item3.Count)];
            ordersDestroyed = routeToDestroy.Orders;
            dayDestroyed = routeToDestroy.Day;

            toStartFrom.RemoveRouteFromPlanning(Planning.Item1, Planning.Item2, routeToDestroy);
            toStartFrom.RemoveRoute(routeToDestroy);
            routeToDestroy.Destroy();

            strategyHasExecuted = true;
            return toStartFrom;
        }
        public override Solution executeStrategy(Solution toStartFrom)
        {
            Plans[0] = toStartFrom.GetRandomPlanning();
            Plans[1] = toStartFrom.GetRandomPlanning();

            if (Plans[0].Item3.Count == 0 || Plans[1].Item3.Count == 0 || (Plans[0].Equals(Plans[1]) && Plans[0].Item3.Count < 2))
                return toStartFrom;

            for (int i = 0; i < 2; i++)
            {
                originalRoutes[i] = Plans[i].Item3[random.Next(Plans[i].Item3.Count)];
                if (originalRoutes[i].Orders.Count < 2 || (i == 1 && originalRoutes[0].Equals(originalRoutes[1])))
                    return toStartFrom;

                int shiftOrderIndex = random.Next(originalRoutes[i].Orders.Count - 1);
                ordersShifted[i] = originalRoutes[i].Orders[shiftOrderIndex];

                if (i == 0)
                    orderInFrontOfTheShiftedOrder = shiftOrderIndex == 0 ? null : originalRoutes[0].Orders[shiftOrderIndex - 1];
            }

            double timeLimit = 0.0d; // Check if route can be swapped traveltime-wise
            foreach (Route route in Plans[1].Item3)
                if (route != originalRoutes[1])
                    timeLimit += route.TravelTime;

            timeLimit = 43200.0d - timeLimit;

            if (originalRoutes[1].CanAddOrderAfter(ordersShifted[0], ordersShifted[1], timeLimit)) // Check if can be shifted
            {
                originalRoutes[0].RemoveOrder(ordersShifted[0]);
                originalRoutes[1].AddOrderAt(ordersShifted[0], ordersShifted[1]);
                strategyHasExecuted = true;

                if (originalRoutes[0].Orders.Count == 1) // if route has only 0order (because other order has been shifted away...)
                {
                    toStartFrom.RemoveRouteFromPlanning(Plans[0].Item1, Plans[0].Item2, originalRoutes[0]);
                    toStartFrom.RemoveRoute(originalRoutes[0]);
                }
            }

            return toStartFrom;
        }
        public override Solution executeStrategy(Solution toStartFrom)
        {
            Planning = toStartFrom.GetRandomPlanning();
            if (Planning.Item3.Count == 0)
                return toStartFrom;

            old_route = Planning.Item3[random.Next(Planning.Item3.Count)];
            if (old_route == null || old_route.Orders.Count < 4)
                return toStartFrom; // Could not find a valid route to shuffle

            new_route = new Route(Planning.Item1);
            foreach (Order order in old_route.Orders)
                new_route.AddOrder(order);

            int firstIndex = random.Next(old_route.Orders.Count - 1);
            int secondIndex = random.Next(old_route.Orders.Count - 1);
            int thirdIndex = random.Next(old_route.Orders.Count - 1);

            while (firstIndex == secondIndex || firstIndex == thirdIndex || secondIndex == thirdIndex)
                return toStartFrom;

            double timeLimit = 0.0d; // Check if route can be swapped traveltime-wise
            foreach (Route route in Planning.Item3)
                if (route != old_route)
                    timeLimit += route.TravelTime;

            timeLimit = 43200.0d - timeLimit;

            if (new_route.CanHalfSwapOrder(old_route.Orders[firstIndex], old_route.Orders[thirdIndex], old_route.Orders[thirdIndex], timeLimit))
            {
                new_route.HalfSwapOrders(old_route.Orders[firstIndex], old_route.Orders[thirdIndex], old_route.Orders[thirdIndex]);
                toStartFrom.AddRoute(new_route);
                toStartFrom.RemoveRouteFromPlanning(Planning.Item1, Planning.Item2, old_route);
                toStartFrom.AddRouteToPlanning(Planning.Item1, Planning.Item2, new_route);
                toStartFrom.RemoveRoute(old_route);

                strategyHasExecuted = true;
            }

            return toStartFrom;
        }