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); } }
public override bool IsAllowedMovement(TwoOpt m) { return(TwoOptStrongOverload(m) <= epsilon); }
public override bool IsAllowedMovement(TwoOpt m) { return(AlphaWeakFeasibility(m.current) <= WeakThreshold); }
public override bool IsAllowedMovement(TwoOpt m) { return(ProblemData.IsFeasible(m.current)); }
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(); }