Exemplo n.º 1
0
        public static void Main()
        {
            // define cost matrix

            Double INF = Double.PositiveInfinity;
               /* Double[,] costMatrix = new Double[,]
            {
                { INF,   2.0,    4.0,   22.0,    2.0,    INF},
                { 2.0,   INF,    8.0,   15.0,   13.0,   10.0},
                { 4.0,   8.0,    INF,    5.0,    2.0,    INF},
                {22.0,  15.0,    5.0,    INF,   11.0,   12.0},
                { 2.0,  13.0,    2.0,   11.0,    INF,   14.0},
                { INF,  10.0,    INF,   12.0,   14.0,    INF},
            };
            */
            Double[,] costMatrix = new Double[,]
            {
                { INF,   2.0,    INF,   INF,    7.0},
                { 2.0,   INF,    1.0,   3.0,   INF},
                { INF,   1.0,    INF,    1.0,    5.0},
                { INF,  3.0,    1.0,    INF,   INF},
                { 7.0,  INF,    5.0,   INF,    INF},
            };
            costMatrix = new Double[,]
            {
                { INF,   20.0,  INF,  70.0},
                { 20.0,   INF,    40.0,  INF},
                { INF,   40.0,    INF,50.0},
                { 70.0, INF,    50.0,INF},
            };
            // create prototype chromosome
            PermutationChromosome prototype = new PermutationChromosome(0, costMatrix.GetLength(0) - 1);

            // set prototype's parameters
            prototype.CrossOverStrategy = new PermutationChromosome.CycleCrossOverStrategy();
            prototype.RandomGenerator = new ThreadSafeRandomGenerator();
            prototype.MutationStrategy = new PermutationChromosome.InsertMutationStrategy();
            prototype.Fitness = new TSPFitness(costMatrix);

            // stop condition, keep reference for selecting the leader
            NoChangeStopCondion stopCondition = new NoChangeStopCondion(10);

            // create population
            DefaultPopulation population = new DefaultPopulation(prototype, 40);

            // set population's parameters
            population.SelectionStrategy
                = new StochasticUniversalSamplingStrategy(new FixedSizeStrategy(40), new ThreadSafeRandomGenerator());
            population.RandomGenerator = new ThreadSafeRandomGenerator();
            population.StopCondition = stopCondition;

            // perform util the stop condition returns false
            while (population.NextGeneration())
                ;

            // print the result
            System.Console.WriteLine("Best fitness: " + (1.0 / stopCondition.Leader.Evaluate()));
            System.Console.WriteLine(stopCondition.Leader.ToString());
            System.Console.ReadKey();
        }
Exemplo n.º 2
0
        private void startProcessingButton_Click(object sender, EventArgs e)
        {
            if (worker.IsBusy)
            {
                worker.CancelAsync();
                return;
            }
            if (costMatrix == null)
            {
                MessageBox.Show("Macierz jest pusta. Uzupełnij graf.");
                return;
            }

            /*
             * Prototyp chromosomu.
             */
            PermutationChromosome chromosomePrototype = new PermutationChromosome(0, costMatrix.GetLength(0) - 1);
            chromosomePrototype.MutationStrategy = (PermutationChromosome.IMutationStrategy)mutationAlgorithmComboBox.SelectedItem;
            chromosomePrototype.CrossOverStrategy = (PermutationChromosome.ICrossOverStrategy)crossoverAlgorithmComboBox.SelectedItem;
            chromosomePrototype.RandomGenerator = new ThreadSafeRandomGenerator();
            chromosomePrototype.Fitness = new TSPFitness(costMatrix);

            /*
             * Populacja
             */
            population = new DefaultPopulation(chromosomePrototype, (UInt32)populationSpinner.Value);
            population.RandomGenerator = new ThreadSafeRandomGenerator();
            population.StopCondition = stopCondition;

            /*
             * Selekcja
             */
            ISelectionSizeStrategy selectionSizeStrategy = selectionSizeCheckBox.Checked ? (ISelectionSizeStrategy)new ProportionalSizeStrategy((Double)selectionSizeSpinner.Value) : (ISelectionSizeStrategy)new FixedSizeStrategy((UInt32)selectionSizeSpinner.Value);
            switch ((SelectionStrategies)selectionAlgorithmComboBox.SelectedValue)
            {
                case SelectionStrategies.NoSelectionStrategy:
                    population.SelectionStrategy = new NoSelectionStrategy();
                    break;
                case SelectionStrategies.RouletteWheelSelectionStrategy:
                    population.SelectionStrategy = new RouletteWheelSelectionStrategy(selectionSizeStrategy, new ThreadSafeRandomGenerator());
                    break;
                case SelectionStrategies.StochasticUniversalSamplingStrategy:
                    population.SelectionStrategy = new StochasticUniversalSamplingStrategy(selectionSizeStrategy, new ThreadSafeRandomGenerator());
                    break;
                case SelectionStrategies.TournamentSelectionStrategy:
                    ISelectionSizeStrategy tournamentGroupSizeStrategy = tournamentGroupSizeCheckBox.Checked ? (ISelectionSizeStrategy)new ProportionalSizeStrategy((Double)tournamentGroupSizeSpinner.Value) : (ISelectionSizeStrategy)new FixedSizeStrategy((UInt32)tournamentGroupSizeSpinner.Value);
                    population.SelectionStrategy = new TournamentSelectionStrategy(selectionSizeStrategy, tournamentGroupSizeStrategy, new ThreadSafeRandomGenerator());
                    break;
                default:
                    return;
            }

            ISelectionSizeStrategy eliteSizeStrategy = eliteSizeCheckBox.Checked ? (ISelectionSizeStrategy)new ProportionalSizeStrategy((Double)eliteSizeSpinner.Value) : (ISelectionSizeStrategy)new FixedSizeStrategy((UInt32)eliteSizeSpinner.Value);
            ISelectionSizeStrategy survivorSizeStrategy = eliteSizeCheckBox.Checked ? (ISelectionSizeStrategy)new ProportionalSizeStrategy((Double)survivorSizeSpinner.Value) : (ISelectionSizeStrategy)new FixedSizeStrategy((UInt32)survivorSizeSpinner.Value);
            switch ((ModifierCombination)selecitonModifierComboBox.SelectedValue)
            {
                case ModifierCombination.None:
                    break;
                case ModifierCombination.Elite:
                    population.SelectionStrategy = new EliteSelectionStrategyAdapter(population.SelectionStrategy, eliteSizeStrategy);
                    break;
                case ModifierCombination.Survivor:
                    population.SelectionStrategy = new SurvivorSelectionStrategyAdapter(population.SelectionStrategy, survivorSizeStrategy);
                    break;
                case ModifierCombination.EliteSurvivor:
                    population.SelectionStrategy = new EliteSelectionStrategyAdapter(population.SelectionStrategy, eliteSizeStrategy);
                    population.SelectionStrategy = new SurvivorSelectionStrategyAdapter(population.SelectionStrategy, survivorSizeStrategy);
                    break;
                case ModifierCombination.SurvivorElite:
                    population.SelectionStrategy = new SurvivorSelectionStrategyAdapter(population.SelectionStrategy, survivorSizeStrategy);
                    population.SelectionStrategy = new EliteSelectionStrategyAdapter(population.SelectionStrategy, eliteSizeStrategy);
                    break;
            }

            /*
             * Warunek stopu. Zachować referencję dla wskazania lidera.
             */
            stopCondition = new NoChangeStopCondion((UInt32)stopSpinner.Value);
            population.StopCondition = stopCondition;

            /*
             * Rozpocznij przetwarzanie.
             */
            startProcessingButton.Text = "Stop";
            TimeStart = DateTime.Now;
            worker.RunWorkerAsync(stopCondition);
        }