상속: IMultiThreadable
예제 #1
0
        private void initPopulation(GeneticAlgorithm ga)
        {
            ICalculateGenomeScore score = new TSPScore(cities);
            ga.CalculateScore = score;
            IPopulation population = new BasicPopulation(POPULATION_SIZE);
            ga.Population = population;

            for (int i = 0; i < POPULATION_SIZE; i++)
            {

                TSPGenome genome = new TSPGenome(ga, cities);
                ga.Population.Genomes.Add(genome);
                ga.PerformCalculateScore(genome);
            }
            population.Sort();
        }
예제 #2
0
        public TSPGenome(GeneticAlgorithm owner, City[] cities)
        {

            int[] organism = new int[cities.Length];
            bool[] taken = new bool[cities.Length];

            for (int i = 0; i < organism.Length; i++)
            {
                taken[i] = false;
            }
            for (int i = 0; i < organism.Length - 1; i++)
            {
                int icandidate;
                do
                {
                    icandidate = (int)(ThreadSafeRandom.NextDouble() * organism.Length);
                } while (taken[icandidate]);
                organism[i] = icandidate;
                taken[icandidate] = true;
                if (i == organism.Length - 2)
                {
                    icandidate = 0;
                    while (taken[icandidate])
                    {
                        icandidate++;
                    }
                    organism[i + 1] = icandidate;
                }
            }

            this.pathChromosome = new Chromosome();
            this.Chromosomes.Add(this.pathChromosome);

            for (int i = 0; i < organism.Length; i++)
            {
                IntegerGene gene = new IntegerGene();
                gene.Value = organism[i];
                this.pathChromosome.Genes.Add(gene);
            }
            Organism = organism;

            Encode();

        }
예제 #3
0
        /// <summary>
        /// Setup and solve the TSP.
        /// </summary>
        public void Execute(IExampleInterface app)
        {
            this.app = app;

            var builder = new StringBuilder();

            initCities();

            genetic = new BasicGeneticAlgorithm();

            initPopulation(genetic);
            genetic.MutationPercent = MUTATION_PERCENT;
            genetic.PercentToMate = PERCENT_TO_MATE;
            genetic.MatingPopulation = MATING_POPULATION_PERCENT;
            genetic.Crossover = new SpliceNoRepeat(CITIES/3);
            genetic.Mutate = new MutateShuffle();

            int sameSolutionCount = 0;
            int iteration = 1;
            double lastSolution = Double.MaxValue;

            while (sameSolutionCount < MAX_SAME_SOLUTION)
            {
                genetic.Iteration();

                double thisSolution = genetic.Population.Best.Score;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Best Path Length = ");
                builder.Append(thisSolution);

                Console.WriteLine(builder.ToString());

                if (Math.Abs(lastSolution - thisSolution) < 1.0)
                {
                    sameSolutionCount++;
                }
                else
                {
                    sameSolutionCount = 0;
                }

                lastSolution = thisSolution;
            }

            Console.WriteLine(@"Good solution found:");
            displaySolution();
        }
예제 #4
0
 public void Claim(GeneticAlgorithm ga)
 {
     foreach (IGenome genome in this._genomes)
     {
         genome.GA = ga;
     }
 }