Example #1
0
        private IPopulation initPopulation()
        {
            		IPopulation result = new BasicPopulation(POPULATION_SIZE, null);

		BasicSpecies defaultSpecies = new BasicSpecies();
		defaultSpecies.Population = result;
		for (int i = 0; i < POPULATION_SIZE; i++) {
			IntegerArrayGenome genome = RandomGenome();
			defaultSpecies.Members.Add(genome);
		}
		result.GenomeFactory = new IntegerArrayGenomeFactory(cities.Length);
		result.Species.Add(defaultSpecies);
		
		return result;
        }
        /// <summary>
        /// Construct a method genetic algorithm. 
        /// </summary>
        /// <param name="phenotypeFactory">The phenotype factory.</param>
        /// <param name="calculateScore">The score calculation object.</param>
        /// <param name="populationSize">The population size.</param>
        public MLMethodGeneticAlgorithm(MLMethodGenomeFactory.CreateMethod phenotypeFactory,
                ICalculateScore calculateScore, int populationSize)
            : base(TrainingImplementationType.Iterative)
        {
            // create the population
            IPopulation population = new BasicPopulation(populationSize, null);
            population.GenomeFactory = new MLMethodGenomeFactory(phenotypeFactory,
                    population);

            ISpecies defaultSpecies = population.CreateSpecies();

            for (int i = 0; i < population.PopulationSize; i++)
            {
                IMLEncodable chromosomeNetwork = (IMLEncodable)phenotypeFactory();
                MLMethodGenome genome = new MLMethodGenome(chromosomeNetwork);
                defaultSpecies.Add(genome);
            }
            defaultSpecies.Leader = defaultSpecies.Members[0];

            // create the trainer
            this.genetic = new MLMethodGeneticAlgorithmHelper(population,
                    calculateScore);
            this.genetic.CODEC = new MLEncodableCODEC();

            IGenomeComparer comp = null;
            if (calculateScore.ShouldMinimize)
            {
                comp = new MinimizeScoreComp();
            }
            else
            {
                comp = new MaximizeScoreComp();
            }
            this.genetic.BestComparer = comp;
            this.genetic.SelectionComparer = comp;

            // create the operators
            int s = Math
                    .Max(defaultSpecies.Members[0].Size / 5, 1);
            Genetic.Population = population;

            this.genetic.AddOperation(0.9, new Splice(s));
            this.genetic.AddOperation(0.1, new MutatePerturb(1.0));
        }