/// <summary>
        /// Create an initial random population.
        /// </summary>
        public void Reset()
        {
            // create the genome factory
            if (IsHyperNEAT)
            {
                CODEC = new HyperNEATCODEC();
                GenomeFactory = new FactorHyperNEATGenome();
            }
            else
            {
                CODEC = new NEATCODEC();
                GenomeFactory = new FactorNEATGenome();
            }

            // create the new genomes
            Species.Clear();

            // reset counters
            GeneIdGenerate.CurrentID = 1;
            InnovationIDGenerate.CurrentID = 1;

            EncogRandom rnd = RandomNumberFactory.Factor();

            // create one default species
            BasicSpecies defaultSpecies = new BasicSpecies();
            defaultSpecies.Population = this;

            // create the initial population
            for (int i = 0; i < PopulationSize; i++)
            {
                NEATGenome genome = GenomeFactory.Factor(rnd, this ,
                        InputCount, OutputCount,
                        InitialConnectionDensity);
                defaultSpecies.Add(genome);
            }
            defaultSpecies.Leader = defaultSpecies.Members[0];
            Species.Add(defaultSpecies);

            // create initial innovations
            Innovations = new NEATInnovationList(this);
        }
        /// <summary>
        ///     Determine the species.
        /// </summary>
        /// <param name="genomes">The genomes to speciate.</param>
        private void SpeciateAndCalculateSpawnLevels(IList <IGenome> genomes)
        {
            double maxScore = 0;

            if (genomes.Count == 0)
            {
                throw new EncogError("Can't speciate, the population is empty.");
            }

            IList <ISpecies> speciesCollection = _population.Species;

            if (speciesCollection.Count == 0)
            {
                throw new EncogError("Can't speciate, there are no species.1");
            }

            // calculate compatibility between genomes and species
            AdjustCompatibilityThreshold();

            // assign genomes to species (if any exist)
            foreach (IGenome g in genomes)
            {
                ISpecies currentSpecies = null;
                IGenome  genome         = g;

                if (!Double.IsNaN(genome.AdjustedScore) &&
                    !double.IsInfinity(genome.AdjustedScore))
                {
                    maxScore = Math.Max(genome.AdjustedScore, maxScore);
                }

                foreach (ISpecies s in speciesCollection)
                {
                    double compatibility = GetCompatibilityScore(genome,
                                                                 s.Leader);

                    if (compatibility <= _compatibilityThreshold)
                    {
                        currentSpecies = s;
                        AddSpeciesMember(s, genome);
                        genome.Species = s;
                        break;
                    }
                }

                // if this genome did not fall into any existing species, create a
                // new species
                if (currentSpecies == null)
                {
                    currentSpecies = new BasicSpecies(_population, genome);
                    _population.Species.Add(currentSpecies);
                }
            }

            //
            double totalSpeciesScore = speciesCollection.Sum(species => species.CalculateShare(_owner.ScoreFunction.ShouldMinimize, maxScore));

            if (speciesCollection.Count == 0)
            {
                throw new EncogError("Can't speciate, there are no species.2");
            }
            if (totalSpeciesScore < EncogFramework.DefaultDoubleEqual)
            {
                // This should not happen much, or if it does, only in the
                // beginning.
                // All species scored zero. So they are all equally bad. Just divide
                // up the right to produce offspring evenly.
                DivideEven(speciesCollection);
            }
            else
            {
                // Divide up the number of offspring produced to the most fit
                // species.
                DivideByFittestSpecies(speciesCollection, totalSpeciesScore);
            }

            LevelOff();
        }
        /// <inheritdoc/>
        public Object Read(Stream istream)
        {
            long nextInnovationId = 0;
            long nextGeneId = 0;

            var result = new NEATPopulation();
            var innovationList = new NEATInnovationList {Population = result};
            result.Innovations = innovationList;
            var reader = new EncogReadHelper(istream);
            EncogFileSection section;

            while ((section = reader.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("NEAT-POPULATION")
                        && section.SubSectionName.Equals("INNOVATIONS"))
                {
                    foreach (String line in section.Lines)
                    {
                        IList<String> cols = EncogFileSection
                                .SplitColumns(line);
                        var innovation = new NEATInnovation();
                        var innovationId = int.Parse(cols[1]);
                        innovation.InnovationId = innovationId;
                        innovation.NeuronId = int.Parse(cols[2]);
                        result.Innovations.Innovations[cols[0]] = innovation;
                        nextInnovationId = Math.Max(nextInnovationId, innovationId + 1);
                    }
                }
                else if (section.SectionName.Equals("NEAT-POPULATION")
                      && section.SubSectionName.Equals("SPECIES"))
                {
                    NEATGenome lastGenome = null;
                    BasicSpecies lastSpecies = null;

                    foreach (String line in section.Lines)
                    {
                        IList<String> cols = EncogFileSection.SplitColumns(line);

                        if (String.Compare(cols[0], "s", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            lastSpecies = new BasicSpecies
                                {
                                    Population = result,
                                    Age = int.Parse(cols[1]),
                                    BestScore = CSVFormat.EgFormat.Parse(cols[2]),
                                    GensNoImprovement = int.Parse(cols[3])
                                };
                            result.Species.Add(lastSpecies);
                        }
                        else if (String.Compare(cols[0], "g", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            bool isLeader = lastGenome == null;
                            lastGenome = new NEATGenome
                                {
                                    InputCount = result.InputCount,
                                    OutputCount = result.OutputCount,
                                    Species = lastSpecies,
                                    AdjustedScore = CSVFormat.EgFormat.Parse(cols[1]),
                                    Score = CSVFormat.EgFormat.Parse(cols[2]),
                                    BirthGeneration = int.Parse(cols[3])
                                };
                            lastSpecies.Add(lastGenome);
                            if (isLeader)
                            {
                                lastSpecies.Leader = lastGenome;
                            }
                        }
                        else if (String.Compare(cols[0], "n", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            var neuronGene = new NEATNeuronGene();
                            int geneId = int.Parse(cols[1]);
                            neuronGene.Id = geneId;

                            IActivationFunction af = EncogFileSection.ParseActivationFunction(cols[2]);
                            neuronGene.ActivationFunction = af;

                            neuronGene.NeuronType = PersistNEATPopulation.StringToNeuronType(cols[3]);
                            neuronGene.InnovationId = int.Parse(cols[4]);
                            lastGenome.NeuronsChromosome.Add(neuronGene);
                            nextGeneId = Math.Max(geneId + 1, nextGeneId);
                        }
                        else if (String.Compare(cols[0], "l", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            var linkGene = new NEATLinkGene
                                {
                                    Id = int.Parse(cols[1]),
                                    Enabled = (int.Parse(cols[2]) > 0),
                                    FromNeuronId = int.Parse(cols[3]),
                                    ToNeuronId = int.Parse(cols[4]),
                                    Weight = CSVFormat.EgFormat.Parse(cols[5]),
                                    InnovationId = int.Parse(cols[6])
                                };
                            lastGenome.LinksChromosome.Add(linkGene);
                        }
                    }

                }
                else if (section.SectionName.Equals("NEAT-POPULATION")
                      && section.SubSectionName.Equals("CONFIG"))
                {
                    IDictionary<string, string> prm = section.ParseParams();

                    string afStr = prm[NEATPopulation.PropertyNEATActivation];

                    if (String.Compare(afStr, TypeCppn, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        HyperNEATGenome.BuildCPPNActivationFunctions(result.ActivationFunctions);
                    }
                    else
                    {
                        result.NEATActivationFunction = EncogFileSection.ParseActivationFunction(prm,
                                        NEATPopulation.PropertyNEATActivation);
                    }

                    result.ActivationCycles = EncogFileSection.ParseInt(prm,
                            PersistConst.ActivationCycles);
                    result.InputCount = EncogFileSection.ParseInt(prm,
                            PersistConst.InputCount);
                    result.OutputCount = EncogFileSection.ParseInt(prm,
                            PersistConst.OutputCount);
                    result.PopulationSize = EncogFileSection.ParseInt(prm,
                            NEATPopulation.PropertyPopulationSize);
                    result.SurvivalRate = EncogFileSection.ParseDouble(prm,
                            NEATPopulation.PropertySurvivalRate);
                    result.ActivationCycles = EncogFileSection.ParseInt(prm,
                            NEATPopulation.PropertyCycles);
                }
            }

            // set factories
            if (result.IsHyperNEAT)
            {
                result.GenomeFactory = new FactorHyperNEATGenome();
                result.CODEC = new HyperNEATCODEC();
            }
            else
            {
                result.GenomeFactory = new FactorNEATGenome();
                result.CODEC = new NEATCODEC();
            }

            // set the next ID's
            result.InnovationIDGenerate.CurrentID = nextInnovationId;
            result.GeneIdGenerate.CurrentID = nextGeneId;

            // find first genome, which should be the best genome
            if (result.Species.Count > 0)
            {
                ISpecies species = result.Species[0];
                if (species.Members.Count > 0)
                {
                    result.BestGenome = species.Members[0];
                }
            }

            return result;
        }
 /// <inheritdoc />
 public ISpecies CreateSpecies()
 {
     ISpecies species = new BasicSpecies();
     species.Population = this;
     _species.Add(species);
     return species;
 }
        /// <inheritdoc />
        public Object Read(Stream istream)
        {
            var context = new EncogProgramContext();

            var result = new PrgPopulation(context, 0);

            var reader = new EncogReadHelper(istream);
            EncogFileSection section;

            int count = 0;
            ISpecies lastSpecies = null;
            while ((section = reader.ReadNextSection()) != null)
            {
                if (section.SectionName.Equals("BASIC")
                    && section.SubSectionName.Equals("PARAMS"))
                {
                    IDictionary<string, string> prms = section.ParseParams();
                    EngineArray.PutAll(prms, result.Properties);
                }
                else if (section.SectionName.Equals("BASIC")
                         && section.SubSectionName.Equals("EPL-POPULATION"))
                {
                    foreach (string line in section.Lines)
                    {
                        IList<String> cols = EncogFileSection.SplitColumns(line);

                        if (String.Compare(cols[0], "s", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            lastSpecies = new BasicSpecies
                                {
                                    Age = int.Parse(cols[1]),
                                    BestScore = CSVFormat.EgFormat.Parse(cols[2]),
                                    Population = result,
                                    GensNoImprovement = int.Parse(cols[3])
                                };
                            result.Species.Add(lastSpecies);
                        }
                        else if (cols[0].Equals("p"))
                        {
                            double score;
                            double adjustedScore;

                            if (String.Compare(cols[1], "nan", StringComparison.OrdinalIgnoreCase) == 0
                                || String.Compare(cols[2], "nan", StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                score = Double.NaN;
                                adjustedScore = Double.NaN;
                            }
                            else
                            {
                                score = CSVFormat.EgFormat.Parse(cols[1]);
                                adjustedScore = CSVFormat.EgFormat.Parse(cols[2]);
                            }

                            String code = cols[3];
                            var prg = new EncogProgram(context);
                            prg.CompileEPL(code);
                            prg.Score = score;
                            prg.Species = lastSpecies;
                            prg.AdjustedScore = adjustedScore;
                            if (lastSpecies == null)
                            {
                                throw new EncogError(
                                    "Have not defined a species yet");
                            }
                            lastSpecies.Add(prg);
                            count++;
                        }
                    }
                }
                else if (section.SectionName.Equals("BASIC")
                         && section.SubSectionName.Equals("EPL-OPCODES"))
                {
                    foreach (String line in section.Lines)
                    {
                        IList<string> cols = EncogFileSection.SplitColumns(line);
                        String name = cols[0];
                        int args = int.Parse(cols[1]);
                        result.Context.Functions.AddExtension(name, args);
                    }
                }
                else if (section.SectionName.Equals("BASIC")
                         && section.SubSectionName.Equals("EPL-SYMBOLIC"))
                {
                    bool first = true;
                    foreach (string line in section.Lines)
                    {
                        if (!first)
                        {
                            IList<String> cols = EncogFileSection.SplitColumns(line);
                            String name = cols[0];
                            String t = cols[1];
                            var vt = EPLValueType.Unknown;

                            if (string.Compare(t, "f", true) == 0)
                            {
                                vt = EPLValueType.FloatingType;
                            }
                            else if (string.Compare(t, "b", true) == 0)
                            {
                                vt = EPLValueType.BooleanType;
                            }
                            else if (string.Compare(t, "i", true) == 0)
                            {
                                vt = EPLValueType.IntType;
                            }
                            else if (string.Compare(t, "s", true) == 0)
                            {
                                vt = EPLValueType.StringType;
                            }
                            else if (string.Compare(t, "e", true) == 0)
                            {
                                vt = EPLValueType.EnumType;
                            }

                            int enumType = int.Parse(cols[2]);
                            int enumCount = int.Parse(cols[3]);
                            var mapping = new VariableMapping(
                                name, vt, enumType, enumCount);
                            if (mapping.Name.Length > 0)
                            {
                                result.Context.DefineVariable(mapping);
                            }
                            else
                            {
                                result.Context.Result = mapping;
                            }
                        }
                        else
                        {
                            first = false;
                        }
                    }
                }
            }
            result.PopulationSize = count;

            // set the best genome, should be the first genome in the first species
            if (result.Species.Count > 0)
            {
                ISpecies species = result.Species[0];
                if (species.Members.Count > 0)
                {
                    result.BestGenome = species.Members[0];
                }

                // set the leaders
                foreach (ISpecies sp in result.Species)
                {
                    if (sp.Members.Count > 0)
                    {
                        sp.Leader = sp.Members[0];
                    }
                }
            }
            return result;
        }
        /// <summary>
        ///     Determine the species.
        /// </summary>
        /// <param name="genomes">The genomes to speciate.</param>
        private void SpeciateAndCalculateSpawnLevels(IList<IGenome> genomes)
        {
            double maxScore = 0;

            if (genomes.Count == 0)
            {
                throw new EncogError("Can't speciate, the population is empty.");
            }

            IList<ISpecies> speciesCollection = _population.Species;

            if (speciesCollection.Count == 0)
            {
                throw new EncogError("Can't speciate, there are no species.1");
            }

            // calculate compatibility between genomes and species
            AdjustCompatibilityThreshold();

            // assign genomes to species (if any exist)
            foreach (IGenome g in genomes)
            {
                ISpecies currentSpecies = null;
                IGenome genome = g;

                if (!Double.IsNaN(genome.AdjustedScore)
                    && !double.IsInfinity(genome.AdjustedScore))
                {
                    maxScore = Math.Max(genome.AdjustedScore, maxScore);
                }

                foreach (ISpecies s in speciesCollection)
                {
                    double compatibility = GetCompatibilityScore(genome,
                                                                 s.Leader);

                    if (compatibility <= _compatibilityThreshold)
                    {
                        currentSpecies = s;
                        AddSpeciesMember(s, genome);
                        genome.Species = s;
                        break;
                    }
                }

                // if this genome did not fall into any existing species, create a
                // new species
                if (currentSpecies == null)
                {
                    currentSpecies = new BasicSpecies(_population, genome);
                    _population.Species.Add(currentSpecies);
                }
            }

            //
            double totalSpeciesScore = speciesCollection.Sum(species => species.CalculateShare(_owner.ScoreFunction.ShouldMinimize, maxScore));

            if (speciesCollection.Count == 0)
            {
                throw new EncogError("Can't speciate, there are no species.2");
            }
            if (totalSpeciesScore < EncogFramework.DefaultDoubleEqual)
            {
                // This should not happen much, or if it does, only in the
                // beginning.
                // All species scored zero. So they are all equally bad. Just divide
                // up the right to produce offspring evenly.
                DivideEven(speciesCollection);
            }
            else
            {
                // Divide up the number of offspring produced to the most fit
                // species.
                DivideByFittestSpecies(speciesCollection, totalSpeciesScore);
            }

            LevelOff();
        }
예제 #7
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;
        }