Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //FloorplanProblem problem = new FloorplanProblem(50);
            //FloorplanSolution solution = new FloorplanSolution(problem);
            //Swap swap = new Swap(problem.Dimension);
            //Shift shift = new Shift(problem.Dimension);
            //FullLeafMove leaf = new FullLeafMove(problem.Dimension);
            //List<Operator> operations = new List<Operator> { swap, shift, leaf };



            TspProblem      problem    = new TspProblem(100);
            TspSolution     solution   = new TspSolution(problem);
            Swap            swap       = new Swap(problem.Dimension, 1);
            Shift           shift      = new Shift(problem.Dimension, 2);
            TwoOpt          twoOpt     = new TwoOpt(problem.Dimension, 3);
            List <Operator> operations = new List <Operator> {
                swap, shift, twoOpt
            };



            MultistartParameters multistartOptions = new MultistartParameters()
            {
                InstancesNumber = 1,
                OutputFrequency = 500,
            };

            LocalDescentParameters ldParameters = new LocalDescentParameters()
            {
                DetailedOutput    = true,
                Seed              = 0,
                Operators         = operations,
                IsSteepestDescent = false
            };

            SimulatedAnnealingParameters saParameters = new SimulatedAnnealingParameters()
            {
                InitProbability         = 0.3,
                TemperatureCooling      = 0.97,
                UseWeightedNeighborhood = true,
                DetailedOutput          = false,
                Seed      = 0,
                Operators = operations,
            };

            MultistartParameters ldMultistartParameters = (MultistartParameters)multistartOptions.Clone();

            ldMultistartParameters.Parameters = ldParameters;

            MultistartParameters saMultistartParameters = (MultistartParameters)multistartOptions.Clone();

            saMultistartParameters.Parameters = saParameters;

            LocalDescent       ld  = new LocalDescent(ldParameters);
            SimulatedAnnealing sa  = new SimulatedAnnealing(saParameters);
            ParallelMultistart pld = new ParallelMultistart(ldMultistartParameters);
            ParallelMultistart psa = new ParallelMultistart(saMultistartParameters);

            List <string> operators = new List <string>();

            IPermutation sol = solution;

            foreach (IPermutation s in ld.Minimize(solution))
            {
                Console.WriteLine("{0}, {1:f}s, {2}, {3}, {4}, {5}", s.CostValue, s.TimeInSeconds, s.IterationNumber, s.IsCurrentBest, s.IsFinal, sol.CostValue - s.CostValue);
                sol = s;
                operators.Add(s.OperatorTag);
            }


            //var groups = operators.GroupBy(s => s).Select(s => new { Operator = s.Key, Count = s.Count() });
            //var dictionary = groups.ToDictionary(g => g.Operator, g => g.Count);

            //foreach (var o in groups)
            //{
            //    Console.WriteLine("{0} = {1}", o.Operator, o.Count);
            //}

            Console.WriteLine("Done");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            //VehicleRoutingProblem vrProblem = new VehicleRoutingProblem(4, 2);
            //VehicleRoutingSolution vrSolution = new VehicleRoutingSolution(vrProblem);

            //Console.WriteLine(vrSolution.ToString());
            //Console.WriteLine(vrSolution.CostValue);
            //Console.ReadLine();

            //return;


            //FloorplanProblem problem = new FloorplanProblem(100);
            //FloorplanProblem problem = FloorplanProblem.Load("500.vlsi");
            //FloorplanSolution solution = new FloorplanSolution(problem);
            //Swap swap = new Swap(problem.Dimension, 10);
            //Shift shift = new Shift(problem.Dimension, 1);
            //EmptyLeafMove eLeaf = new EmptyLeafMove(problem.Dimension, 5);
            //FullLeafMove fLeaf = new FullLeafMove(problem.Dimension, 5);
            //FullNodeMove fnode = new FullNodeMove(problem.Dimension, 5);
            //List<Operator> operations = new List<Operator> { swap, fLeaf };



            //TspProblem problem = new TspProblem(200);
            //TspSolution solution = new TspSolution(problem);

            VehicleRoutingProblem  problem  = new VehicleRoutingProblem(225, 10);
            VehicleRoutingSolution solution = new VehicleRoutingSolution(problem);

            Swap            swap       = new Swap(problem.Dimension, 1);
            Shift           shift      = new Shift(problem.Dimension, 2);
            TwoOpt          twoOpt     = new TwoOpt(problem.Dimension, 3);
            List <Operator> operations = new List <Operator> {
                swap, shift, twoOpt
            };


            MultistartParameters multistartOptions = new MultistartParameters()
            {
                Name            = "P",
                InstancesNumber = 5,
                RandomizeStart  = false,
                DetailedOutput  = true,
                OutputFrequency = 100,
            };

            LocalDescentParameters ldParameters = new LocalDescentParameters()
            {
                Name              = "LD",
                DetailedOutput    = true,
                Seed              = 0,
                Operators         = operations,
                IsSteepestDescent = false
            };

            SimulatedAnnealingParameters saParameters = new SimulatedAnnealingParameters()
            {
                Name                    = "SA",
                InitProbability         = 0.5,
                TemperatureCooling      = 0.98,
                MinCostDeviation        = 0,
                UseWeightedNeighborhood = false,
                DetailedOutput          = false,
                Seed                    = 0,
                Operators               = operations,
            };

            MultistartParameters pldParameters = (MultistartParameters)multistartOptions.Clone();

            pldParameters.OptimizationAlgorithm = typeof(LocalDescent);
            pldParameters.Parameters            = ldParameters;

            MultistartParameters psaParameters = (MultistartParameters)multistartOptions.Clone();

            psaParameters.OptimizationAlgorithm = typeof(SimulatedAnnealing);
            psaParameters.Parameters            = saParameters;

            StackedParameters ssParameters = new StackedParameters()
            {
                Name                   = "B",
                DetailedOutput         = true,
                OptimizationAlgorithms = new Type[] { typeof(LocalDescent), typeof(SimulatedAnnealing), typeof(LocalDescent) },
                Parameters             = new OptimizationParameters[] { ldParameters, saParameters, ldParameters }
            };

            StackedParameters sspParameters = new StackedParameters()
            {
                Name                   = "B",
                DetailedOutput         = false,
                OptimizationAlgorithms = new Type[] { typeof(LocalDescent), typeof(ParallelMultistart), typeof(LocalDescent) },
                Parameters             = new OptimizationParameters[] { ldParameters, psaParameters, ldParameters }
            };



            LocalDescent       ld  = new LocalDescent(ldParameters);
            SimulatedAnnealing sa  = new SimulatedAnnealing(saParameters);
            ParallelMultistart pld = new ParallelMultistart(pldParameters);
            ParallelMultistart psa = new ParallelMultistart(psaParameters);

            StackedSearch ss  = new StackedSearch(ssParameters);
            StackedSearch ssp = new StackedSearch(sspParameters);


            MultistartParameters pssParameters = (MultistartParameters)multistartOptions.Clone();

            pssParameters.DetailedOutput        = false;
            pssParameters.RandomizeStart        = true;
            pssParameters.OptimizationAlgorithm = typeof(StackedSearch);
            pssParameters.Parameters            = ssParameters;

            ParallelMultistart pss = new ParallelMultistart(pssParameters);

            IOptimizationAlgorithm optimizer = sa;
            ISolution bestSolution           = solution;

            foreach (ISolution s in optimizer.Minimize(solution.Shuffle(0)))
            {
                //if (s.IsCurrentBest)
                Console.WriteLine("\t{0}, {1:f}s, {2}, {3}, {4}, {5}, {6}", s.CostValue, s.TimeInSeconds, s.IterationNumber, s.IsCurrentBest, s.IsFinal, bestSolution.CostValue - s.CostValue, s.InstanceTag);
                bestSolution = s;
            }

            Console.WriteLine(bestSolution.TimeInSeconds + "s");
            Console.WriteLine(bestSolution.IterationNumber + " iterations");

            //var groups = optimizer.SearchHistory.GroupBy(s => s.OperatorTag).Select(s => new { Operator = s.Key, Count = s.Count() });
            //var dictionary = groups.ToDictionary(g => g.Operator, g => g.Count);

            //foreach (var o in groups)
            //{
            //    Console.WriteLine("{0} = {1}", o.Operator, o.Count);
            //}

            //Console.WriteLine("Done");


            foreach (var b in DrawSolution(bestSolution, optimizer))
            {
                b?.Save("solution" + DateTime.Now.Millisecond + ".jpg");
            }

            Console.ReadLine();
        }