Ejemplo n.º 1
0
        /*
         *
         * Grasp():
         * M <- calculateCustomerDistance
         * BestSolution = 0
         * While(StopCondition)
         *      Solution <- ConstructGreedySolution()
         *      NewSolution <- LocalSearch(solution)
         *      if NewSolution isBetterThan BestSolution
         *          BestSolution = NewSolution
         * return BestSolution
         *
         */
        override public void Grasp()
        {
            // Set iterator
            int    iterator        = 0;
            int    totalIterations = parameters.Customer_GRASPIterations;
            double alpha           = parameters.Customer_Alpha;

            // Main cycle
            while (iterator < totalIterations)
            {
                // For random alpha
                if (parameters.Customer_Alpha == -1)
                {
                    Random rnd = new Random();
                    alpha = rnd.Next(0, 11) / 10.0;
                }

                // Calculate new initial solution
                CluVRPSolution newSolution = constructGreedyRandomizedSolution(alpha);

                // Local search
                var    totalLSWatch  = System.Diagnostics.Stopwatch.StartNew();
                double routeDistance = newSolution.totalCustomerRouteDistance;
                for (int i = 0; i < 1; i++)
                {
                    // Perform local searchs
                    this.localSearch(newSolution);

                    // For control of best iteration number
                    if (newSolution.totalCustomerRouteDistance < routeDistance)
                    {
                        newSolution.cluster_LSCycle_iterations = i;
                        routeDistance = newSolution.totalCustomerRouteDistance;
                    }
                }
                newSolution.customer_LSCycleTime += totalLSWatch.ElapsedMilliseconds;

                // Update Best solution
                if (newSolution.totalCustomerRouteDistance < solution.totalCustomerRouteDistance)
                {
                    solution.setCostumerSolution(newSolution.customersPaths, newSolution.vehiculeRouteDistance);
                    solution.bestCustomerLSOrder = localSearchsOrder;

                    solution.customerLevelIterations           = iterator;
                    solution.customer_LSCycle_iterations       = newSolution.customer_LSCycle_iterations;
                    solution.customer_twoOpt_iterations        = newSolution.customer_twoOpt_iterations;
                    solution.customer_relocate_iterations      = newSolution.customer_relocate_iterations;
                    solution.customer_exchange_iterations      = newSolution.customer_exchange_iterations;
                    solution.customer_swapCustomers_iterations = newSolution.customer_swapCustomers_iterations;

                    solution.customer_LSCycleTime        = newSolution.customer_LSCycleTime;
                    solution.customer_twoOpt_time        = newSolution.customer_twoOpt_time;
                    solution.customer_relocate_time      = newSolution.customer_relocate_time;
                    solution.customer_exchange_time      = newSolution.customer_exchange_time;
                    solution.customer_swapCustomers_time = newSolution.customer_swapCustomers_time;
                }

                // Increace iterator
                iterator++;
            }

            //End
            return;
        }
Ejemplo n.º 2
0
        /*
         *
         *  Create a complete Greedy Randomized Solution (with cluster and customers)
         *
         */
        private CluVRPSolution constructGreedyRandomizedSolution(double alpha)
        {
            // Init variables
            int[][]          originalClusters         = instance.clusters;
            List <int>[]     clusterRoute             = solution.clusterRouteForVehicule;
            List <int>[][]   customersCircuit         = new List <int> [clusterRoute.Length][];
            Tuple <int, int> customersConnectClusters = new Tuple <int, int>(0, 0);

            double[] vehiculeTotalDistance = new double[instance.vehicles];

            // Start from depot
            int startingCustomer = 1;

            int[][][] clusterRouteWithCustomers = this.clusterRouteWithCustomers(originalClusters, clusterRoute);

            // For each vehicule cluster-route
            for (int vehicle = 0; vehicle < clusterRoute.Length; vehicle++)
            {
                // For each cluster in the i-vehicle route
                int     numbersOfClusters  = clusterRoute[vehicle].Count;
                int[][] clustersOneVehicle = clusterRouteWithCustomers[vehicle];
                customersCircuit[vehicle] = new List <int> [numbersOfClusters];

                // If vehicle has not travel
                if (clusterRoute[vehicle].Count == 2)
                {
                    customersCircuit[vehicle]    = new List <int> [2];
                    customersCircuit[vehicle][0] = new List <int>();
                    customersCircuit[vehicle][1] = new List <int>();
                    customersCircuit[vehicle][0].Add(1);
                    customersCircuit[vehicle][1].Add(1);
                    vehiculeTotalDistance[vehicle] = 0;
                    continue;
                }

                // Visit all cluster for the i-vehicle
                for (int i = 0; i < numbersOfClusters; i++)
                {
                    // Add actual (initial of cluster) customer
                    customersCircuit[vehicle][i] = new List <int>();
                    customersCircuit[vehicle][i].Add(startingCustomer);

                    // For best customer to conect actual cluster to the next
                    // If the cluster only has 1 customer, you only have to know the
                    // customer for the next cluster
                    if (i + 1 < numbersOfClusters && clustersOneVehicle[i].Length > 1)
                    {
                        customersConnectClusters = bestCustomersBetween2Clusters(clustersOneVehicle[i], clustersOneVehicle[i + 1], startingCustomer);
                    }
                    else if (i + 1 < numbersOfClusters && clustersOneVehicle[i].Length == 1)
                    {
                        int nextCustomer = bestNextCustomer(startingCustomer, clustersOneVehicle[i + 1]);
                        customersConnectClusters = new Tuple <int, int>(startingCustomer, nextCustomer);
                    }

                    // Convert array to list
                    List <int> customersToVisit = clustersOneVehicle[i].OfType <int>().ToList();

                    // Remove initial and final customers
                    customersToVisit.Remove(startingCustomer);
                    customersToVisit.Remove(customersConnectClusters.Item1);

                    // While exists customers to visit
                    while (customersToVisit.Count > 0)
                    {
                        // Create RCL for customer
                        List <int> customerRCL = buildCustomerRCL(startingCustomer, customersToVisit, alpha);

                        // Select customer for RCL
                        int customerSelected = Functions.selectRandomElement(customerRCL);

                        // Add customer to the path
                        customersCircuit[vehicle][i].Add(customerSelected);

                        // Quit visited customer
                        customersToVisit.Remove(customerSelected);

                        // Set new actual customer
                        startingCustomer = customerSelected;
                    }

                    // Add final customer that connect to i+1 cluster
                    // In the final cluster the next customer is 0
                    // the it has not be added
                    if (clustersOneVehicle[i].Length > 1 && i + 1 < numbersOfClusters)
                    {
                        customersCircuit[vehicle][i].Add(customersConnectClusters.Item1);
                    }

                    // Next customer of next cluster
                    startingCustomer = customersConnectClusters.Item2;
                }

                // Calculte total inter-cluster distance
                vehiculeTotalDistance[vehicle] = Functions.calculateTotalTravelDistance(customersCircuit, instance.customersDistanceMatrix, vehicle);
            }

            // Set solution
            CluVRPSolution newSolution = new CluVRPSolution(instance);

            newSolution.setCostumerSolution(customersCircuit, vehiculeTotalDistance);

            // Return solution
            return(newSolution);
        }