示例#1
0
        /// <summary>
        /// Read the object.
        /// </summary>
        /// <param name="mask0">The stream to read the object from.</param>
        /// <returns>The object that was loaded.</returns>
        public virtual Object Read(Stream mask0)
        {
            var result = new NEATPopulation();
            var innovationList = new NEATInnovationList {Population = result};
            result.Innovations = innovationList;
            var ins0 = new EncogReadHelper(mask0);
            IDictionary<Int32, ISpecies> speciesMap = new Dictionary<Int32, ISpecies>();
            IDictionary<ISpecies, Int32> leaderMap = new Dictionary<ISpecies, Int32>();
            IDictionary<Int32, IGenome> genomeMap = new Dictionary<Int32, IGenome>();
            EncogFileSection section;

            while ((section = ins0.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
                                             {
                                                 InnovationID = Int32.Parse(cols[0]),
                                                 InnovationType = StringToInnovationType(cols[1]),
                                                 NeuronType = StringToNeuronType(cols[2]),
                                                 SplitX = CSVFormat.EgFormat.Parse(cols[3]),
                                                 SplitY = CSVFormat.EgFormat.Parse(cols[4]),
                                                 NeuronID = Int32.Parse(cols[5]),
                                                 FromNeuronID = Int32.Parse(cols[6]),
                                                 ToNeuronID = Int32.Parse(cols[7])
                                             };
                        result.Innovations.Add(innovation);
                    }
                }
                else if (section.SectionName.Equals("NEAT-POPULATION")
                         && section.SubSectionName.Equals("SPECIES"))
                {
                    foreach (String line  in  section.Lines)
                    {
                        String[] cols = line.Split(',');
                        var species = new BasicSpecies
                                          {
                                              SpeciesID = Int32.Parse(cols[0]),
                                              Age = Int32.Parse(cols[1]),
                                              BestScore = CSVFormat.EgFormat.Parse(cols[2]),
                                              GensNoImprovement = Int32.Parse(cols[3]),
                                              SpawnsRequired = CSVFormat.EgFormat
                                                  .Parse(cols[4])
                                          };

                        species.SpawnsRequired = CSVFormat.EgFormat
                            .Parse(cols[5]);
                        leaderMap[(species)] = (Int32.Parse(cols[6]));
                        result.Species.Add(species);
                        speciesMap[((int) species.SpeciesID)] = (species);
                    }
                }
                else if (section.SectionName.Equals("NEAT-POPULATION")
                         && section.SubSectionName.Equals("GENOMES"))
                {
                    NEATGenome lastGenome = null;

                    foreach (String line  in  section.Lines)
                    {
                        IList<String> cols = EncogFileSection.SplitColumns(line);
                        if (cols[0].Equals("g", StringComparison.InvariantCultureIgnoreCase))
                        {
                            lastGenome = new NEATGenome
                                             {
                                                 NeuronsChromosome = new Chromosome(),
                                                 LinksChromosome = new Chromosome()
                                             };
                            lastGenome.Chromosomes.Add(lastGenome.NeuronsChromosome);
                            lastGenome.Chromosomes.Add(lastGenome.LinksChromosome);
                            lastGenome.GenomeID = Int32.Parse(cols[1]);
                            lastGenome.SpeciesID = Int32.Parse(cols[2]);
                            lastGenome.AdjustedScore = CSVFormat.EgFormat
                                .Parse(cols[3]);
                            lastGenome.AmountToSpawn = CSVFormat.EgFormat
                                .Parse(cols[4]);
                            lastGenome.NetworkDepth = Int32.Parse(cols[5]);
                            lastGenome.Score = CSVFormat.EgFormat.Parse(cols[6]);
                            result.Add(lastGenome);
                            genomeMap[(int) lastGenome.GenomeID] = lastGenome;
                        }
                        else if (cols[0].Equals("n", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var neuronGene = new NEATNeuronGene
                                                 {
                                                     Id = Int32.Parse(cols[1]),
                                                     NeuronType = StringToNeuronType(cols[2]),
                                                     Enabled = Int32.Parse(cols[3]) > 0,
                                                     InnovationId = Int32.Parse(cols[4]),
                                                     ActivationResponse = CSVFormat.EgFormat
                                                         .Parse(cols[5]),
                                                     SplitX = CSVFormat.EgFormat.Parse(cols[6]),
                                                     SplitY = CSVFormat.EgFormat.Parse(cols[7])
                                                 };
                            lastGenome.Neurons.Add(neuronGene);
                        }
                        else if (cols[0].Equals("l", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var linkGene = new NEATLinkGene();
                            linkGene.Id = Int32.Parse(cols[1]);
                            linkGene.Enabled = Int32.Parse(cols[2]) > 0;
                            linkGene.Recurrent = Int32.Parse(cols[3]) > 0;
                            linkGene.FromNeuronID = Int32.Parse(cols[4]);
                            linkGene.ToNeuronID = Int32.Parse(cols[5]);
                            linkGene.Weight = CSVFormat.EgFormat.Parse(cols[6]);
                            linkGene.InnovationId = Int32.Parse(cols[7]);
                            lastGenome.Links.Add(linkGene);
                        }
                    }
                }
                else if (section.SectionName.Equals("NEAT-POPULATION")
                         && section.SubSectionName.Equals("CONFIG"))
                {
                    IDictionary<String, String> paras = section.ParseParams();

                    result.NeatActivationFunction = EncogFileSection
                        .ParseActivationFunction(paras,
                                                 NEATPopulation.PropertyNEATActivation);
                    result.OutputActivationFunction = EncogFileSection
                        .ParseActivationFunction(paras,
                                                 NEATPopulation.PropertyOutputActivation);
                    result.Snapshot = EncogFileSection.ParseBoolean(paras,
                                                                    PersistConst.Snapshot);
                    result.InputCount = EncogFileSection.ParseInt(paras,
                                                                  PersistConst.InputCount);
                    result.OutputCount = EncogFileSection.ParseInt(paras,
                                                                   PersistConst.OutputCount);
                    result.OldAgePenalty = EncogFileSection.ParseDouble(paras,
                                                                        PopulationConst.PropertyOldAgePenalty);
                    result.OldAgeThreshold = EncogFileSection.ParseInt(paras,
                                                                       PopulationConst.PropertyOldAgeThreshold);
                    result.PopulationSize = EncogFileSection.ParseInt(paras,
                                                                      PopulationConst.PropertyPopulationSize);
                    result.SurvivalRate = EncogFileSection.ParseDouble(paras,
                                                                       PopulationConst.PropertySurvivalRate);
                    result.YoungBonusAgeThreshhold = EncogFileSection.ParseInt(
                        paras, PopulationConst.PropertyYoungAgeThreshold);
                    result.YoungScoreBonus = EncogFileSection.ParseDouble(paras,
                                                                          PopulationConst.PropertyYoungAgeBonus);
                    result.GenomeIDGenerate.CurrentID = EncogFileSection.ParseInt(paras,
                                                                                  PopulationConst.
                                                                                      PropertyNextGenomeID);
                    result.InnovationIDGenerate.CurrentID = EncogFileSection.ParseInt(paras,
                                                                                      PopulationConst.
                                                                                          PropertyNextInnovationID);
                    result.GeneIDGenerate.CurrentID = EncogFileSection.ParseInt(paras,
                                                                                PopulationConst.
                                                                                    PropertyNextGeneID);
                    result.SpeciesIDGenerate.CurrentID = EncogFileSection.ParseInt(paras,
                                                                                   PopulationConst.
                                                                                       PropertyNextSpeciesID);
                }
            }

            // now link everything up

            // first put all the genomes into correct species
            foreach (IGenome genome  in  result.Genomes)
            {
                var neatGenome = (NEATGenome) genome;
                var speciesId = (int) neatGenome.SpeciesID;
                if( speciesMap.ContainsKey(speciesId))
                {
                    ISpecies s = speciesMap[speciesId];
                    s.Members.Add(neatGenome);
                }

                neatGenome.InputCount = result.InputCount;
                neatGenome.OutputCount = result.OutputCount;
            }

            // set the species leader links
            foreach (ISpecies species  in  leaderMap.Keys)
            {
                int leaderID = leaderMap[species];
                IGenome leader = genomeMap[leaderID];
                species.Leader = leader;
                ((BasicSpecies) species).Population = result;
            }

            return result;
        }
示例#2
0
 public virtual object Read(Stream mask0)
 {
     IDictionary<ISpecies, int> dictionary2;
     IDictionary<int, IGenome> dictionary3;
     EncogFileSection section;
     IDictionary<string, string> dictionary4;
     int num;
     int num2;
     NEATPopulation population = new NEATPopulation();
     NEATInnovationList list = new NEATInnovationList {
         Population = population
     };
     population.Innovations = list;
     EncogReadHelper helper = new EncogReadHelper(mask0);
     IDictionary<int, ISpecies> dictionary = new Dictionary<int, ISpecies>();
     goto Label_0BD6;
     Label_0023:
     if ((section = helper.ReadNextSection()) != null)
     {
         if (!section.SectionName.Equals("NEAT-POPULATION"))
         {
             goto Label_085C;
         }
         if (section.SubSectionName.Equals("INNOVATIONS"))
         {
             using (IEnumerator<string> enumerator = section.Lines.GetEnumerator())
             {
                 string str;
                 IList<string> list2;
                 NEATInnovation innovation;
                 NEATInnovation innovation2;
                 goto Label_0A6C;
             Label_0A43:
                 innovation = innovation2;
                 if ((((uint) num2) - ((uint) num)) <= uint.MaxValue)
                 {
                 }
                 population.Innovations.Add(innovation);
             Label_0A6C:
                 if (enumerator.MoveNext())
                 {
                     goto Label_0B54;
                 }
                 goto Label_0AD7;
             Label_0A7A:
                 innovation2.SplitY = CSVFormat.EgFormat.Parse(list2[4]);
                 innovation2.NeuronID = int.Parse(list2[5]);
                 innovation2.FromNeuronID = int.Parse(list2[6]);
                 innovation2.ToNeuronID = int.Parse(list2[7]);
                 goto Label_0A43;
             Label_0AD7:
                 if ((((uint) num2) - ((uint) num)) >= 0)
                 {
                     goto Label_0023;
                 }
             Label_0AEF:
                 innovation2 = new NEATInnovation();
                 innovation2.InnovationID = int.Parse(list2[0]);
                 innovation2.InnovationType = StringToInnovationType(list2[1]);
                 innovation2.NeuronType = StringToNeuronType(list2[2]);
                 innovation2.SplitX = CSVFormat.EgFormat.Parse(list2[3]);
                 if (0 == 0)
                 {
                     goto Label_0A7A;
                 }
                 goto Label_0023;
             Label_0B54:
                 str = enumerator.Current;
                 do
                 {
                     list2 = EncogFileSection.SplitColumns(str);
                 }
                 while (0 != 0);
                 goto Label_0AEF;
             }
         }
         if (((uint) num) <= uint.MaxValue)
         {
             goto Label_085C;
         }
         goto Label_030B;
     }
     using (IEnumerator<IGenome> enumerator4 = population.Genomes.GetEnumerator())
     {
         IGenome genome3;
         NEATGenome genome4;
         ISpecies species3;
     Label_0040:
         if (enumerator4.MoveNext())
         {
             goto Label_00D6;
         }
         goto Label_0102;
     Label_0051:
         genome4.OutputCount = population.OutputCount;
         if ((((uint) num) - ((uint) num2)) >= 0)
         {
             goto Label_0040;
         }
     Label_007D:
         if (dictionary.ContainsKey(num))
         {
             goto Label_00BA;
         }
     Label_0087:
         genome4.InputCount = population.InputCount;
         goto Label_0051;
     Label_0096:
         num = (int) genome4.SpeciesID;
         if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue)
         {
         }
         goto Label_007D;
     Label_00BA:
         species3 = dictionary[num];
         species3.Members.Add(genome4);
         goto Label_0087;
     Label_00D6:
         genome3 = enumerator4.Current;
         genome4 = (NEATGenome) genome3;
         goto Label_0096;
     }
     Label_0102:
     using (IEnumerator<ISpecies> enumerator5 = dictionary2.Keys.GetEnumerator())
     {
         ISpecies current;
         goto Label_011F;
     Label_0112:
         ((BasicSpecies) current).Population = population;
     Label_011F:
         if (enumerator5.MoveNext())
         {
             current = enumerator5.Current;
             num2 = dictionary2[current];
             do
             {
                 IGenome genome5 = dictionary3[num2];
                 current.Leader = genome5;
             }
             while (-1 == 0);
             goto Label_0112;
         }
         return population;
     }
     Label_016E:
     population.YoungScoreBonus = EncogFileSection.ParseDouble(dictionary4, "youngAgeBonus");
     population.GenomeIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextGenomeID");
     population.InnovationIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextInnovationID");
     population.GeneIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextGeneID");
     population.SpeciesIDGenerate.CurrentID = EncogFileSection.ParseInt(dictionary4, "nextSpeciesID");
     goto Label_0023;
     Label_0201:
     population.SurvivalRate = EncogFileSection.ParseDouble(dictionary4, "survivalRate");
     if (0 != 0)
     {
         goto Label_03AA;
     }
     goto Label_02E9;
     Label_0242:
     population.OldAgePenalty = EncogFileSection.ParseDouble(dictionary4, "oldAgePenalty");
     population.OldAgeThreshold = EncogFileSection.ParseInt(dictionary4, "oldAgeThreshold");
     Label_0266:
     population.PopulationSize = EncogFileSection.ParseInt(dictionary4, "populationSize");
     if (((uint) num2) <= uint.MaxValue)
     {
         if ((((uint) num2) - ((uint) num2)) >= 0)
         {
             if ((((uint) num) + ((uint) num2)) > uint.MaxValue)
             {
                 goto Label_0242;
             }
             goto Label_0201;
         }
         goto Label_02E9;
     }
     Label_028A:
     population.OutputActivationFunction = EncogFileSection.ParseActivationFunction(dictionary4, "outAct");
     if ((((uint) num2) + ((uint) num2)) < 0)
     {
         goto Label_0BD6;
     }
     population.Snapshot = EncogFileSection.ParseBoolean(dictionary4, "snapshot");
     population.InputCount = EncogFileSection.ParseInt(dictionary4, "inputCount");
     population.OutputCount = EncogFileSection.ParseInt(dictionary4, "outputCount");
     goto Label_0242;
     Label_02E9:
     if ((((uint) num2) - ((uint) num)) >= 0)
     {
         population.YoungBonusAgeThreshhold = EncogFileSection.ParseInt(dictionary4, "youngAgeThreshold");
     }
     if (0xff != 0)
     {
         goto Label_016E;
     }
     Label_030B:
     population.NeatActivationFunction = EncogFileSection.ParseActivationFunction(dictionary4, "neatAct");
     goto Label_028A;
     Label_03AA:
     if (!section.SectionName.Equals("NEAT-POPULATION") || !section.SubSectionName.Equals("CONFIG"))
     {
         goto Label_0023;
     }
     if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue)
     {
         if ((((uint) num2) + ((uint) num2)) <= uint.MaxValue)
         {
             dictionary4 = section.ParseParams();
         }
         goto Label_030B;
     }
     Label_0821:
     if (section.SectionName.Equals("NEAT-POPULATION"))
     {
         if (section.SubSectionName.Equals("GENOMES"))
         {
             NEATGenome genome = null;
             using (IEnumerator<string> enumerator3 = section.Lines.GetEnumerator())
             {
                 string str3;
                 IList<string> list3;
                 NEATGenome genome2;
                 NEATNeuronGene gene;
                 NEATNeuronGene gene2;
                 NEATLinkGene gene3;
                 goto Label_0402;
             Label_03DA:
                 genome.Links.Add(gene3);
                 goto Label_0402;
             Label_03EA:
                 if (list3[0].Equals("l", StringComparison.InvariantCultureIgnoreCase))
                 {
                     goto Label_04EA;
                 }
             Label_0402:
                 if (enumerator3.MoveNext())
                 {
                     goto Label_0770;
                 }
                 if ((((uint) num2) | 0x80000000) != 0)
                 {
                     goto Label_0023;
                 }
             Label_0429:
                 gene3.Enabled = int.Parse(list3[2]) > 0;
             Label_0440:
                 gene3.Recurrent = int.Parse(list3[3]) > 0;
                 gene3.FromNeuronID = int.Parse(list3[4]);
                 gene3.ToNeuronID = int.Parse(list3[5]);
                 gene3.Weight = CSVFormat.EgFormat.Parse(list3[6]);
                 gene3.InnovationId = int.Parse(list3[7]);
                 goto Label_03DA;
             Label_04B4:
                 if ((((uint) num) & 0) != 0)
                 {
                     goto Label_03DA;
                 }
                 gene3.Id = int.Parse(list3[1]);
                 goto Label_0429;
             Label_04EA:
                 gene3 = new NEATLinkGene();
                 goto Label_04B4;
             Label_04F6:
                 gene2.SplitY = CSVFormat.EgFormat.Parse(list3[7]);
             Label_050F:
                 gene = gene2;
                 genome.Neurons.Add(gene);
                 if ((((uint) num2) & 0) != 0)
                 {
                     goto Label_0782;
                 }
                 goto Label_0402;
             Label_053D:
                 gene2.Enabled = int.Parse(list3[3]) > 0;
                 gene2.InnovationId = int.Parse(list3[4]);
                 if (0xff == 0)
                 {
                     goto Label_050F;
                 }
                 gene2.ActivationResponse = CSVFormat.EgFormat.Parse(list3[5]);
                 gene2.SplitX = CSVFormat.EgFormat.Parse(list3[6]);
                 goto Label_04F6;
             Label_05A7:
                 gene2.Id = int.Parse(list3[1]);
                 gene2.NeuronType = StringToNeuronType(list3[2]);
                 goto Label_053D;
             Label_05DA:
                 if (!list3[0].Equals("n", StringComparison.InvariantCultureIgnoreCase))
                 {
                     goto Label_03EA;
                 }
                 if (3 == 0)
                 {
                     goto Label_0440;
                 }
                 gene2 = new NEATNeuronGene();
                 goto Label_07C8;
             Label_0608:
                 population.Add(genome);
                 dictionary3[(int) genome.GenomeID] = genome;
                 if (((uint) num) >= 0)
                 {
                     goto Label_0402;
                 }
                 goto Label_06B0;
             Label_0638:
                 genome.AmountToSpawn = CSVFormat.EgFormat.Parse(list3[4]);
                 genome.NetworkDepth = int.Parse(list3[5]);
                 if ((((uint) num2) | 0x80000000) == 0)
                 {
                     goto Label_07AD;
                 }
                 if (((uint) num2) < 0)
                 {
                     goto Label_0023;
                 }
                 genome.Score = CSVFormat.EgFormat.Parse(list3[6]);
                 goto Label_06F8;
             Label_06B0:
                 genome.GenomeID = int.Parse(list3[1]);
                 genome.SpeciesID = int.Parse(list3[2]);
                 genome.AdjustedScore = CSVFormat.EgFormat.Parse(list3[3]);
                 goto Label_0638;
             Label_06F8:
                 if (3 != 0)
                 {
                     goto Label_0608;
                 }
                 goto Label_0402;
             Label_0704:
                 genome.Chromosomes.Add(genome.LinksChromosome);
                 if ((((uint) num2) + ((uint) num2)) >= 0)
                 {
                     goto Label_06B0;
                 }
                 goto Label_0402;
             Label_0737:
                 if (8 == 0)
                 {
                     goto Label_050F;
                 }
                 genome = genome2;
                 genome.Chromosomes.Add(genome.NeuronsChromosome);
                 if ((((uint) num) - ((uint) num2)) <= uint.MaxValue)
                 {
                     goto Label_0704;
                 }
             Label_0770:
                 str3 = enumerator3.Current;
                 list3 = EncogFileSection.SplitColumns(str3);
             Label_0782:
                 if (!list3[0].Equals("g", StringComparison.InvariantCultureIgnoreCase))
                 {
                     goto Label_05DA;
                 }
                 genome2 = new NEATGenome {
                     NeuronsChromosome = new Chromosome()
                 };
             Label_07AD:
                 genome2.LinksChromosome = new Chromosome();
                 goto Label_0737;
             Label_07C8:
                 if ((((uint) num2) - ((uint) num)) <= uint.MaxValue)
                 {
                     goto Label_05A7;
                 }
                 if (0 == 0)
                 {
                     goto Label_04EA;
                 }
                 goto Label_04B4;
             }
         }
         if ((((uint) num2) < 0) || ((((uint) num2) - ((uint) num2)) > uint.MaxValue))
         {
             goto Label_030B;
         }
     }
     goto Label_03AA;
     Label_085C:
     if (section.SectionName.Equals("NEAT-POPULATION"))
     {
         if ((((uint) num) | 1) == 0)
         {
             goto Label_0266;
         }
         if (section.SubSectionName.Equals("SPECIES"))
         {
             if ((((uint) num) | 8) == 0)
             {
                 goto Label_0201;
             }
             using (IEnumerator<string> enumerator2 = section.Lines.GetEnumerator())
             {
                 string str2;
                 string[] strArray;
                 BasicSpecies species;
                 BasicSpecies species2;
                 goto Label_0913;
             Label_08CB:
                 species = species2;
                 species.SpawnsRequired = CSVFormat.EgFormat.Parse(strArray[5]);
                 dictionary2[species] = int.Parse(strArray[6]);
                 population.Species.Add(species);
                 dictionary[(int) species.SpeciesID] = species;
             Label_0913:
                 if (enumerator2.MoveNext())
                 {
                     goto Label_09D6;
                 }
                 goto Label_0023;
             Label_0924:
                 if ((((uint) num2) & 0) != 0)
                 {
                     goto Label_0969;
                 }
                 if (2 == 0)
                 {
                     goto Label_0924;
                 }
                 goto Label_09BE;
             Label_0941:
                 species2 = new BasicSpecies();
                 species2.SpeciesID = int.Parse(strArray[0]);
                 species2.Age = int.Parse(strArray[1]);
             Label_0969:
                 species2.BestScore = CSVFormat.EgFormat.Parse(strArray[2]);
                 species2.GensNoImprovement = int.Parse(strArray[3]);
                 species2.SpawnsRequired = CSVFormat.EgFormat.Parse(strArray[4]);
                 if ((((uint) num) + ((uint) num2)) >= 0)
                 {
                     goto Label_0924;
                 }
             Label_09BE:
                 if ((((uint) num2) + ((uint) num)) <= uint.MaxValue)
                 {
                     goto Label_09FD;
                 }
             Label_09D6:
                 str2 = enumerator2.Current;
                 strArray = str2.Split(new char[] { ',' });
                 goto Label_0941;
             Label_09FD:
                 if ((((uint) num) - ((uint) num)) <= uint.MaxValue)
                 {
                     goto Label_08CB;
                 }
                 goto Label_0023;
             }
         }
     }
     goto Label_0821;
     Label_0BD6:
     dictionary2 = new Dictionary<ISpecies, int>();
     dictionary3 = new Dictionary<int, IGenome>();
     goto Label_0023;
 }