Exemplo n.º 1
0
        private void bwFloorplan_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            algorithmStatus.Text = "Press ESC to cancel";

            FloorplanProblem  problem       = new FloorplanProblem(floorplanRectangles);
            FloorplanSolution startSolution = 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);

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

            LocalDescentParameters ldParameters = new LocalDescentParameters()
            {
                Name           = "VLSI LD",
                Seed           = seed,
                DetailedOutput = true,
                Operators      = new List <Operator> {
                    swap, fLeaf
                }
            };

            SimulatedAnnealingParameters saParameters = new SimulatedAnnealingParameters()
            {
                Name               = "VLSI SA",
                InitProbability    = 0.5,
                TemperatureCooling = 0.96,
                MinCostDeviation   = 0,
                Seed               = seed,
                DetailedOutput     = true,
                Operators          = new List <Operator> {
                    swap, fNode
                }
            };

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

            switch (optimizerType)
            {
            case 0:
            {
                multistartParameters.Parameters            = ldParameters;
                multistartParameters.OptimizationAlgorithm = typeof(LocalDescent);
            }
            break;

            case 1:
            {
                multistartParameters.Parameters            = saParameters;
                multistartParameters.OptimizationAlgorithm = typeof(SimulatedAnnealing);
            }
            break;

            case 2:
            {
                saParameters.InitProbability               = 0.005;
                saParameters.TemperatureCooling            = 0.95;
                multistartParameters.Parameters            = ssParameters;
                multistartParameters.OptimizationAlgorithm = typeof(StackedSearch);
            }
            break;

            case 3:
            {
                saParameters.InitProbability               = 0.005;
                saParameters.TemperatureCooling            = 0.95;
                multistartParameters.InstancesNumber       = 3;
                multistartParameters.Parameters            = ssParameters;
                multistartParameters.OptimizationAlgorithm = typeof(StackedSearch);
            }
            break;
            }

            floorplanOptimizer = new ParallelMultistart(multistartParameters);

            toRenderBackground = false;

            foreach (ISolution solution in floorplanOptimizer.Minimize(startSolution))
            {
                if (worker.CancellationPending)
                {
                    floorplanOptimizer.Stop();
                    e.Cancel = true;
                }
                worker.ReportProgress(0);
            }
        }
Exemplo n.º 2
0
        private void bwVehicleRouting_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            algorithmStatus.Text = "Press ESC to cancel";

            VehicleRoutingProblem  problem       = new VehicleRoutingProblem(routingCustomers, routingVehicles);
            VehicleRoutingSolution startSolution = 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 multistartParameters = (MultistartParameters)multistartOptions.Clone();

            LocalDescentParameters ldParameters = new LocalDescentParameters()
            {
                Name           = "VEHICLE LD",
                Seed           = seed,
                DetailedOutput = true,
                Operators      = operations
            };

            SimulatedAnnealingParameters saParameters = new SimulatedAnnealingParameters()
            {
                Name               = "VEHICLE SA",
                InitProbability    = 0.4,
                TemperatureCooling = 0.95,
                MinCostDeviation   = 10E-3,
                Seed               = seed,
                DetailedOutput     = true,
                Operators          = operations
            };

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

            switch (optimizerType)
            {
            case 0:
            {
                multistartParameters.Parameters            = ldParameters;
                multistartParameters.OptimizationAlgorithm = typeof(LocalDescent);
            }
            break;

            case 1:
            {
                multistartParameters.Parameters            = saParameters;
                multistartParameters.OptimizationAlgorithm = typeof(SimulatedAnnealing);
            }
            break;

            case 2:
            {
                saParameters.InitProbability               = 0.002;
                saParameters.MinCostDeviation              = 10E-2;
                multistartParameters.Parameters            = ssParameters;
                multistartParameters.OptimizationAlgorithm = typeof(StackedSearch);
            }
            break;

            case 3:
            {
                saParameters.InitProbability               = 0.01;
                saParameters.MinCostDeviation              = 10E-2;
                multistartParameters.InstancesNumber       = 3;
                multistartParameters.Parameters            = ssParameters;
                multistartParameters.OptimizationAlgorithm = typeof(StackedSearch);
            }
            break;
            }

            vehicleRoutingOptimizer = new ParallelMultistart(multistartParameters);

            toRenderBackground = false;

            foreach (ISolution solution in vehicleRoutingOptimizer.Minimize(startSolution))
            {
                if (worker.CancellationPending)
                {
                    vehicleRoutingOptimizer.Stop();
                    e.Cancel = true;
                }
                if (e.Cancel)
                {
                    solution.IsFinal = false;
                }
                worker.ReportProgress(0);
            }
        }
        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();
        }