Esempio 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();
        }
Esempio n. 2
0
        private void bwTsp_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            algorithmStatus.Text = "Press ESC to cancel";

            TspProblem  problem       = new TspProblem(tspCities);
            TspSolution startSolution = 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 multistartParameters = (MultistartParameters)multistartOptions.Clone();

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

            SimulatedAnnealingParameters saParameters = new SimulatedAnnealingParameters()
            {
                Name               = "TSP SA",
                InitProbability    = 0.5,
                TemperatureCooling = 0.91,
                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.007;
                saParameters.MinCostDeviation              = 10E-2;
                multistartParameters.Parameters            = ssParameters;
                multistartParameters.OptimizationAlgorithm = typeof(StackedSearch);
            }
            break;

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

            tspOptimizer = new ParallelMultistart(multistartParameters);

            toRenderBackground = false;

            foreach (ISolution solution in tspOptimizer.Minimize(startSolution))
            {
                if (worker.CancellationPending)
                {
                    tspOptimizer.Stop();
                    e.Cancel = true;
                }
                if (e.Cancel)
                {
                    solution.IsFinal = false;
                }
                worker.ReportProgress(0);
            }
        }