Esempio n. 1
0
        private static void Lab5Test(int?seed)
        {
            var instance = EBinaryKnapsackInstance.knapPI_1_100_1000_1;

            CBinaryKnapsackEvaluation evaluation    = new CheckingBinaryKnapsackEvaluation(instance);
            IterationsStopCondition   stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);

            BinaryRandomGenerator   generator = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            OnePointCrossover       crossover = new OnePointCrossover(0.5, seed);
            BinaryBitFlipMutation   mutation  = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            TournamentSelection     selection = new TournamentSelection(2, seed);
            GeneticAlgorithm <bool> ga        = new GeneticAlgorithm <bool>(evaluation, stopCondition, generator, selection, crossover, mutation, 50);

            ga.Run();
            ReportOptimizationResult(ga.Result);

            evaluation    = new CheckingBinaryKnapsackEvaluation(instance);
            stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);
            generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            crossover     = new OnePointCrossover(0.5, seed);
            mutation      = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            selection     = new TournamentSelection(2, seed);
            ga            = new LamarckGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, 50);
            ga.Run();
            ReportOptimizationResult(ga.Result);

            evaluation    = new CheckingBinaryKnapsackEvaluation(instance);
            stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);
            generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            crossover     = new OnePointCrossover(0.5, seed);
            mutation      = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            selection     = new TournamentSelection(2, seed);
            ga            = new BaldwinGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, 50);
            ga.Run();
            ReportOptimizationResult(ga.Result);

            evaluation    = new PenalizingBinaryKnapsackEvaluation(instance);
            stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);
            generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            crossover     = new OnePointCrossover(0.5, seed);
            mutation      = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            selection     = new TournamentSelection(2, seed);
            ga            = new GeneticAlgorithm <bool>(evaluation, stopCondition, generator, selection, crossover, mutation, 50);
            ga.Run();
            ReportOptimizationResult(ga.Result);

            evaluation    = new PenalizingBinaryKnapsackEvaluation(instance);
            stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);
            generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            crossover     = new OnePointCrossover(0.5, seed);
            mutation      = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            selection     = new TournamentSelection(2, seed);
            ga            = new LamarckGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, 50);
            ga.Run();
            ReportOptimizationResult(ga.Result);

            evaluation    = new PenalizingBinaryKnapsackEvaluation(instance);
            stopCondition = new IterationsStopCondition(evaluation.dMaxValue, 100);
            generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            crossover     = new OnePointCrossover(0.5, seed);
            mutation      = new BinaryBitFlipMutation(1.0 / evaluation.iSize, evaluation, seed);
            selection     = new TournamentSelection(2, seed);
            ga            = new BaldwinGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, 50);
            ga.Run();
            ReportOptimizationResult(ga.Result);
        }
Esempio n. 2
0
        private static Experiment <bool> PrepareExperiment5(int evaluationType, int instance, int algorithm, int selectionMethod, int crossoverMethod, double crossoverProb, double mutationProb, int populationSize, int?seed = null)
        {
            CBinaryKnapsackEvaluation   evaluation;
            Dictionary <string, string> info = new Dictionary <string, string>();
            List <string> instances          = new List <string>
            {
                "f1_l_d_kp_10_269",
                "f2_l_d_kp_20_878",
                "f3_l_d_kp_4_20",
                "f4_l_d_kp_4_11",
                "f5_l_d_kp_15_375",
                "f6_l_d_kp_10_60",
                "f7_l_d_kp_7_50",
                "f8_l_d_kp_23_10000",
                "f9_l_d_kp_5_80",
                "f10_l_d_kp_20_879",
                "knapPI_1_100_1000_1",
                "knapPI_1_200_1000_1",
                "knapPI_1_500_1000_1",
                "knapPI_1_1000_1000_1",
                "knapPI_1_2000_1000_1",
                "knapPI_1_5000_1000_1",
                "knapPI_1_10000_1000_1",
                "knapPI_2_100_1000_1",
                "knapPI_2_200_1000_1",
                "knapPI_2_500_1000_1",
                "knapPI_2_1000_1000_1",
                "knapPI_2_2000_1000_1",
                "knapPI_2_5000_1000_1",
                "knapPI_2_10000_1000_1",
                "knapPI_3_100_1000_1",
                "knapPI_3_200_1000_1",
                "knapPI_3_500_1000_1",
                "knapPI_3_1000_1000_1",
                "knapPI_3_2000_1000_1",
                "knapPI_3_5000_1000_1",
                "knapPI_3_10000_1000_1"
            };

            info["instance"] = instances[instance];
            switch (evaluationType)
            {
            case 0:
                evaluation         = new CBinaryKnapsackEvaluation((EBinaryKnapsackInstance)instance);
                info["evaluation"] = "base";
                break;

            case 1:
                evaluation         = new CheckingBinaryKnapsackEvaluation((EBinaryKnapsackInstance)instance);
                info["evaluation"] = "checking";
                break;

            case 2:
                evaluation         = new NegativeBinaryKnapsackEvaluation((EBinaryKnapsackInstance)instance);
                info["evaluation"] = "negative";
                break;

            case 3:
                evaluation         = new PenalizingBinaryKnapsackEvaluation((EBinaryKnapsackInstance)instance);
                info["evaluation"] = "penalizing";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            RunningTimeStopCondition stopCondition = new RunningTimeStopCondition(evaluation.dMaxValue, 30);
            BinaryRandomGenerator    generator     = new BinaryRandomGenerator(evaluation.pcConstraint, seed);
            ASelection selection;

            switch (selectionMethod)
            {
            case 0:
                selection = new RouletteWheelSelection(seed);
                info["selection_method"] = "roulette";
                break;

            case 1:
                selection = new TournamentSelection(2, seed);
                info["selection_method"] = "tournament";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            ACrossover crossover;

            info["crossover_prob"] = crossoverProb.ToString();
            switch (crossoverMethod)
            {
            case 0:
                crossover = new OnePointCrossover(crossoverProb, seed);
                info["crossover_method"] = "OnePoint";
                break;

            case 1:
                crossover = new UniformCrossover(crossoverProb, seed);
                info["crossover_method"] = "uniform";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            mutationProb          = mutationProb / evaluation.iSize;
            info["mutation_prob"] = mutationProb.ToString();
            BinaryBitFlipMutation mutation = new BinaryBitFlipMutation(mutationProb, evaluation, seed);

            info["population"] = populationSize.ToString();
            GeneticAlgorithm <bool> ga;

            switch (algorithm)
            {
            case 0:
                ga = new GeneticAlgorithm <bool>(evaluation, stopCondition, generator, selection, crossover, mutation, populationSize);
                info["algorithm"] = "GA";
                break;

            case 1:
                ga = new KnapsackGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, populationSize);
                info["algorithm"] = "Checking";
                break;

            case 2:
                ga = new LamarckGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, populationSize);
                info["algorithm"] = "Lamarck";
                break;

            case 3:
                ga = new BaldwinGeneticAlgorithm(evaluation, stopCondition, generator, selection, crossover, mutation, populationSize);
                info["algorithm"] = "Baldwin";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            Experiment <bool> experiment = new Experiment <bool>(ga, info, ExperimentFinished);

            return(experiment);
        }