Exemplo n.º 1
0
        protected RouteSet HeuristicCGSolve <TProcedure, TParameters>(List <RouteSet> pool, Exploration expCondition, double overloadFactor, Random rdObj,
                                                                      List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods,
                                                                      List <Func <RouteSet, Random, RouteSet> > shaking,
                                                                      TProcedure procedure, TParameters parameters,
                                                                      string folderPath, int maxIterations,
                                                                      bool includedDualInfo, bool useCompletePool,
                                                                      bool parallelexe) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            RouteSet bestSolution = (RouteSet)pool[0].Clone();
            double   bestCost     = GetCost(bestSolution);

            for (int i = 0; i < pool.Count; i++)
            {
                DirectoryInfo iterationDir = Directory.CreateDirectory(folderPath).CreateSubdirectory(string.Format("mcg{0}", i));
                RouteSet      current      = HeuristicCGSolve(pool[i], expCondition, overloadFactor, rdObj, neighborhoods, shaking, procedure, parameters, iterationDir.FullName, maxIterations, includedDualInfo, useCompletePool, parallelexe);
                double        currentCost  = GetCost(current);
                if (currentCost < bestCost)
                {
                    bestCost     = currentCost;
                    bestSolution = (RouteSet)current.Clone();
                    OnIteration(new IterationEventArgs <RouteSet>(bestSolution));
                }
            }
            return(bestSolution);
        }
        public RouteSet OrderByLoadDifference(RouteSet solution)
        {
            RouteSet newSolution = (RouteSet)solution.Clone();

            for (int i = 0; i < newSolution.Count; i++)
            {
                newSolution[i] = OrderByLoadDifference(newSolution[i]);
            }
            return(newSolution);
        }
        private void SimpleShaking <TProcedure, TParameters>(Exploration expCondition, Random rdObj, List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods, List <Func <RouteSet, Random, RouteSet> > shaking, TProcedure procedure, TParameters parameters, ref RouteSet bestSolution, ref double bestCost, ref RouteSet current) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            current = shaking[rdObj.Next(shaking.Count)](current, rdObj);
            current = procedure.Solve(parameters, current, neighborhoods, expCondition, GetCost, rdObj);
            double currentCost = GetCost(current);

            if (currentCost < bestCost)
            {
                bestCost     = currentCost;
                bestSolution = (RouteSet)current.Clone();
            }
        }
        private void ParallelShaking <TProcedure, TParameters>(Exploration expCondition, Random rdObj, List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods, List <Func <RouteSet, Random, RouteSet> > shaking, TProcedure procedure, TParameters parameters, ref RouteSet bestSolution, ref double bestCost, ref RouteSet current, int iteration, Stopwatch timer, bool parallelExe) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            List <Tuple <RouteSet, double> > parallelSolutions = (parallelExe)? ParallelExeParallelShaking(expCondition, rdObj, neighborhoods, shaking, procedure, parameters, bestSolution, current, iteration, timer) : ParallelShaking(expCondition, rdObj, neighborhoods, shaking, procedure, parameters, bestSolution, current, iteration, timer);
            var ccurrent = parallelSolutions.Aggregate((min, x) => (min.Item2 < x.Item2) ? min : x);

            current = ccurrent.Item1;
            if (ccurrent.Item2 < bestCost)
            {
                bestCost     = ccurrent.Item2;
                bestSolution = (RouteSet)current.Clone();
            }
            OnIteration(new IterationEventArgs <RouteSet>(bestSolution, current, (iteration + 1) * (shaking.Count), timer.Elapsed.TotalSeconds));/*iteration+1 pq las iteraciones comienzan en cero*/
        }
        protected RouteSet ShakingPenalization <TProcedure, TParameters>(RouteSet initial, int reStarts, Exploration expCondition, Random rdObj, double overloadFactor,
                                                                         List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods,
                                                                         List <Func <RouteSet, Random, RouteSet> > shaking,
                                                                         TProcedure procedure, TParameters parameters) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            OverloadFactor = overloadFactor;
            Stopwatch timer        = new Stopwatch();
            RouteSet  bestSolution = procedure.Solve(parameters, initial, neighborhoods, expCondition, GetCost, rdObj);
            double    bestCost     = GetCost(bestSolution);
            RouteSet  current      = (RouteSet)bestSolution.Clone();

            timer.Start();
            for (int i = 0; i < reStarts; i++)
            {
                SimpleShaking(expCondition, rdObj, neighborhoods, shaking, procedure, parameters, ref bestSolution, ref bestCost, ref current);
                OnIteration(new IterationEventArgs <RouteSet>(bestSolution, current, i, timer.Elapsed.TotalSeconds));
            }
            timer.Stop();
            return(bestSolution);
        }
        private List <RouteSet> BuildSolutionPool <TProcedure, TParameters>(double overloadFactor, int shakings, Exploration expCondition, Random rdObj, List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods, List <Func <RouteSet, Random, RouteSet> > shakingProcedures, TProcedure procedure, TParameters parameters, List <RouteSet> initialPool) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            List <RouteSet> solutionPool = new List <RouteSet>();

            foreach (var solution in initialPool)
            {
                overloadFactor = 0;
                RouteSet current = solution;
                for (int i = 0; i < shakings; i++)
                {
                    current = shakingProcedures[rdObj.Next(shakingProcedures.Count)](current, rdObj);
                    current = procedure.Solve(parameters, current, neighborhoods, expCondition, GetCost, rdObj);
                    var clone = (RouteSet)current.Clone();
                    clone.CleanUnusedRoutes();
                    solutionPool.Add(clone);
                    if (!ProblemData.IsStrongFeasible(current))
                    {
                        OverloadFactor += overloadFactor;
                    }
                }
            }
            return(solutionPool);
        }
        protected RouteSet MultiStartPenalization <TProcedure, TParameters>(Exploration expCondition, Random rdObj, double overloadFactor, List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods, TProcedure procedure, TParameters parameters, List <RouteSet> initialPool) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            OverloadFactor = overloadFactor;
            Stopwatch timer        = new Stopwatch();
            RouteSet  bestSolution = initialPool[0];
            double    bestCost     = GetCost(bestSolution);
            int       i            = 0;

            timer.Start();
            foreach (var item in initialPool)
            {
                RouteSet current     = procedure.Solve(parameters, item, neighborhoods, expCondition, GetCost, rdObj);
                double   currentCost = GetCost(current);
                if (currentCost < bestCost)
                {
                    bestCost     = currentCost;
                    bestSolution = (RouteSet)current.Clone();
                }
                OnIteration(new IterationEventArgs <RouteSet>(bestSolution, current, i, timer.Elapsed.TotalSeconds));
                i++;
            }
            timer.Stop();
            return(bestSolution);
        }
Exemplo n.º 8
0
        protected RouteSet HeuristicCGSolve <TProcedure, TParameters>(RouteSet initial, Exploration expCondition, double overloadFactor, Random rdObj,
                                                                      List <Func <RouteSet, Exploration, Random, RouteSet> > neighborhoods,
                                                                      List <Func <RouteSet, Random, RouteSet> > shaking,
                                                                      TProcedure procedure, TParameters parameters,
                                                                      string folderPath, int maxIterations,
                                                                      bool includedDualInfo, bool useCompletePool,
                                                                      bool parallelexe) where TProcedure : LocalSearchProcedure <RouteSet, TParameters>
        {
            Stopwatch timer     = new Stopwatch();
            int       iteration = 0;

            OverloadFactor = overloadFactor;

            RouteSet bestSolution  = procedure.Solve(parameters, initial, neighborhoods, expCondition, GetCost, rdObj);
            double   bestCost      = GetCost(bestSolution);
            RouteSet current       = (RouteSet)initial.Clone();
            double   currentCost   = GetCost(initial);
            double   currentMPCost = int.MaxValue;
            double   refCost       = int.MaxValue;
            List <Tuple <Route, double> > coveringSelection = new List <Tuple <Route, double> >();
            List <ExtRouteInfo>           completePool      = new List <ExtRouteInfo>(); //GetInitialPool(current);

            double[] previousDual = GetDualVariables(bestSolution);
            double[] currentDual  = GetDualVariables(bestSolution);


            timer.Start();
            do
            {
                refCost      = currentMPCost;
                previousDual = currentDual;
                List <ExtRouteInfo> shkPool = null;

                if (includedDualInfo)
                {
                    DirectoryInfo gamsDir     = Directory.CreateDirectory(Path.Combine(folderPath, string.Format("gams{0}", iteration)));
                    double[]      dualExtreme = GetExtremeDualVariables(gamsDir.FullName, completePool, ProblemData.Clients.Count + 1);
                    currentDual = GetDualLinearCombination(dualExtreme, previousDual, rdObj);
                    shkPool     = BuildDualRestrictedPool(current, expCondition, rdObj, neighborhoods, shaking, iteration, timer, procedure, parameters, currentDual, parallelexe);
                }
                else
                {
                    shkPool = BuildRestrictedPool(current, expCondition, rdObj, neighborhoods, shaking, iteration, timer, procedure, parameters, parallelexe);
                }

                if (useCompletePool)
                {
                    completePool.AddRange(shkPool);
                    completePool = RemovePoolRedundancy(completePool);
                }
                else
                {
                    completePool = new List <ExtRouteInfo>(shkPool);
                }

                coveringSelection = SolveRestrictedMasterProblem(folderPath, string.Format("coveringPool{0}", iteration), completePool);
                currentMPCost     = coveringSelection.Sum(r => GetCost(r.Item1) * r.Item2);

                current     = procedure.Solve(parameters, BuildSolution(coveringSelection, rdObj), neighborhoods, expCondition, GetCost, rdObj);
                currentCost = GetCost(current);

                if (currentCost < bestCost)
                {
                    bestSolution = (RouteSet)current.Clone();
                    bestCost     = currentCost;
                    OnIteration(new IterationEventArgs <RouteSet>(bestSolution, iteration, timer.Elapsed.TotalSeconds));
                }

                iteration++;
            } while (currentMPCost < refCost && iteration < maxIterations);
            timer.Stop();
            return(bestSolution);
        }