コード例 #1
0
        public NotGettingBetterTest()
        {
            _notGettingBetterDur = 300u;
            _cooler = new Mock <Cooler>();
            _cooler.SetupProperty(c => c.Temperature, 1000);
            _stop = new NotGettingBetter <Solution>(_cooler.Object, OptimizationType.Maximization, 200000, _notGettingBetterDur);

            _mockedSolution = new Mock <Solution>();
            _mockedSolution.SetupProperty(s => s.CalculatedValue, 0);
        }
コード例 #2
0
        private void ShouldStop_IfValueIsNotBetter()
        {
            _stop = new NotGettingBetter <Solution>(_cooler.Object, OptimizationType.Maximization, 5000, _notGettingBetterDur);
            var step = 0u;

            while (!_stop.ShouldStop(_mockedSolution.Object))
            {
                step++;
            }

            Assert.Equal(_notGettingBetterDur + 1, step);
        }
コード例 #3
0
        private void ShouldStop_Max_steps(uint maxSteps)
        {
            _stop = new NotGettingBetter <Solution>(_cooler.Object, OptimizationType.Maximization, maxSteps, 250000);
            var step = 0u;

            while (!_stop.ShouldStop(_mockedSolution.Object))
            {
                step++;
            }

            Assert.Equal(maxSteps, step);
        }
コード例 #4
0
        private void ShouldNotStop_IfValueIsBetter(uint threshold)
        {
            _stop = new NotGettingBetter <Solution>(_cooler.Object, OptimizationType.Maximization, 20000, _notGettingBetterDur);
            var step = 0u;

            while (!_stop.ShouldStop(_mockedSolution.Object))
            {
                step++;
                if (step == 10)
                {
                    _mockedSolution.Object.CalculatedValue += threshold + 1;
                }
            }

            Assert.NotEqual(_notGettingBetterDur + 1, step);
        }
コード例 #5
0
        public static void KnapsackTest()
        {
            Console.WriteLine("============");
            Console.WriteLine("SA Knapsack problem example");
            Console.WriteLine("Knapsack instances are generated according to this paper:");
            Console.WriteLine("http://www.dcs.gla.ac.uk/~pat/cpM/jchoco/knapsack/papers/hardInstances.pdf");
            Console.WriteLine("============");
            var logger = new Logger <SimulatedAnnealing <KnapsackSolution> >(new LoggerFactory());
            var cooler = new QuadraticCooler(0.998, 800);
            var mover  = new KnapsackMover();

            var instances = new KnapsackInstancesFactory(50, 5000, 1)
                            .GenDistribution(DistributionVariant.Strong, 3)
                            .GenDistribution(DistributionVariant.Uncorrelated, 3)
                            .Collect();

            var stop = new NotGettingBetter <KnapsackSolution>(cooler, OptimizationType.Maximization, 500000, 300000);

            Console.WriteLine("COMMON ELEMENTS");
            Console.WriteLine($"    Stop criteria: {stop}");
            Console.WriteLine($"    Cooler: {cooler}");
            Console.WriteLine("INSTANCES");
            for (int i = 0; i < instances.Count; i++)
            {
                var instance = instances[i];
                Console.WriteLine($"Instance name: {instance.Name}");

                var sa         = new SimulatedAnnealing <KnapsackSolution>(cooler, mover, stop, logger);
                var obj        = new KnapsackObj(instance);
                var constraint = new KnapsackConstraint(instance);

                var objAggr        = new WeightedObjectiveAggregator <KnapsackSolution>(new[] { obj }, new double[] { 1 });
                var constraintAggr = new WeightedConstraintAggregator <KnapsackSolution>(new[] { constraint }, new double[] { 1 });
                var criterion      = new Criterion <KnapsackSolution>(OptimizationType.Maximization, objAggr, constraintAggr);

                var solution = new KnapsackSolution(instance);

                var bestSol = sa.Solve(solution, criterion);

                Console.WriteLine(instance);
                Console.WriteLine(bestSol);
                Console.WriteLine("----");
            }
        }