/// <summary>
        /// Performs multiple generations of the evolutionary algorithm.
        /// </summary>
        public void evolve(int generations)
        {
            for (int j = 0; j < generations; j++)
            {
                oneGeneration(j);
            }
            LogOutput.Close();

            XmlDoc = new XmlDocument();
            XmlGenomeWriterStatic.Write(XmlDoc, (NeatGenome)EA.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            OutputFileInfo = new FileInfo(Path.Combine(OutputFolder, "bestGenome.xml"));
            XmlDoc.Save(OutputFileInfo.FullName);


            //if doing novelty search, write out archive
            if (experiment.DefaultNeatParameters.noveltySearch)
            {
                XmlDocument archiveout = new XmlDocument();

                XmlPopulationWriter.WriteGenomeList(archiveout, EA.noveltyFixed.archive);
                OutputFileInfo = new FileInfo(Path.Combine(OutputFolder, "archive.xml"));
                archiveout.Save(OutputFileInfo.FullName);
            }

            // dump the MapElites grid contents (empty if we aren'type doing ME and we aren'type tracking ME-style-grid for some other algorithm)
            XmlDocument gridout = new XmlDocument();

            XmlPopulationWriter.WriteGenomeList(gridout, EA.meDumpGrid());
            OutputFileInfo = new FileInfo(Path.Combine(OutputFolder, "finalgrid.xml"));
            gridout.Save(OutputFileInfo.FullName);
        }
        public void oneGeneration(int currentGeneration)
        {
            DateTime dt = DateTime.Now;

            ea.PerformOneGeneration();
            if (ea.BestGenome.RealFitness > maxFitness)
            {
                simExperiment.bestGenomeSoFar = (NeatGenome)ea.BestGenome;
                maxFitness = ea.BestGenome.RealFitness;
                doc        = new XmlDocument();
                XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                oFileInfo = new FileInfo(outputFolder + "bestGenome" + currentGeneration.ToString() + ".xml");
                doc.Save(oFileInfo.FullName);
            }
            //Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.RealFitness + " "  + ea.Population.GenomeList.Count + " " + (DateTime.Now.Subtract(dt)));
            // Schrum: Changed this to include fitness values from each environment: Mainly for FourTasks
            Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.RealFitness + " " + ea.Population.GenomeList.Count + " (" + string.Join(",", ea.BestGenome.Behavior.objectives) + ") " + (DateTime.Now.Subtract(dt)) + " " + ea.BestGenome.Behavior.modules + " " + ea.BestGenome.Behavior.cppnLinks + " " + ea.BestGenome.Behavior.substrateLinks);
            int gen_mult = 200;

            if (logging)
            {
                if (experiment.DefaultNeatParameters.noveltySearch && currentGeneration % gen_mult == 0)
                {
                    XmlDocument archiveout = new XmlDocument();

                    XmlPopulationWriter.WriteGenomeList(archiveout, ea.noveltyFixed.archive);
                    oFileInfo = new FileInfo(outputFolder + "archive.xml");
                    archiveout.Save(oFileInfo.FullName);
                }

                if ((experiment.DefaultNeatParameters.noveltySearch || experiment.DefaultNeatParameters.multiobjective) && currentGeneration % gen_mult == 0)
                {
                    XmlDocument popout = new XmlDocument();
                    if (!experiment.DefaultNeatParameters.multiobjective)
                    {
                        XmlPopulationWriter.Write(popout, ea.Population, ActivationFunctionFactory.GetActivationFunction("NullFn"));
                    }
                    else
                    {
                        XmlPopulationWriter.WriteGenomeList(popout, ea.multiobjective.population);
                    }

                    oFileInfo = new FileInfo(outputFolder + "population" + currentGeneration.ToString() + ".xml");
                    popout.Save(oFileInfo.FullName);
                }
                // Schrum: Added contents of objective array to log so individual environment scores can be seen in FourTasks domain
                // Also always print modules, cppn links, and substrate links
                logOutput.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString() + " " + string.Join(" ", ea.BestGenome.Behavior.objectives) + " " + ea.BestGenome.Behavior.modules + " " + ea.BestGenome.Behavior.cppnLinks + " " + ea.BestGenome.Behavior.substrateLinks);
            }
        }
Beispiel #3
0
        public void oneGeneration(int currentGeneration)
        {
            DateTime dt = DateTime.Now;

            ea.PerformOneGeneration();
            if (ea.BestGenome.RealFitness > maxFitness)
            {
                //simExperiment.bestGenomeSoFar = (NeatGenome)ea.BestGenome;
                maxFitness = ea.BestGenome.RealFitness;
                doc        = new XmlDocument();
                XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                oFileInfo = new FileInfo(outputFolder + "bestGenome" + currentGeneration.ToString() + ".xml");
                doc.Save(oFileInfo.FullName);
            }
            Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.RealFitness + " " + ea.Population.GenomeList.Count + " " + (DateTime.Now.Subtract(dt)));
            int gen_mult = 200;

            if (logging)
            {
                if (neatParams.noveltySearch && currentGeneration % gen_mult == 0)
                {
                    XmlDocument archiveout = new XmlDocument();

                    XmlPopulationWriter.WriteGenomeList(archiveout, ea.noveltyFixed.archive);
                    oFileInfo = new FileInfo(outputFolder + "archive.xml");
                    archiveout.Save(oFileInfo.FullName);
                }

                if ((neatParams.noveltySearch || neatParams.multiobjective) && currentGeneration % gen_mult == 0)
                {
                    XmlDocument popout = new XmlDocument();
                    if (!neatParams.multiobjective)
                    {
                        XmlPopulationWriter.Write(popout, ea.Population, ActivationFunctionFactory.GetActivationFunction("NullFn"));
                    }
                    else
                    {
                        XmlPopulationWriter.WriteGenomeList(popout, ea.multiobjective.population);
                    }

                    oFileInfo = new FileInfo(outputFolder + "population" + currentGeneration.ToString() + ".xml");
                    popout.Save(oFileInfo.FullName);
                }

                logOutput.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString());
            }
        }
        public void printDistribution()
        {
            String filename = "dist" + generation + ".txt";
            string content  = "";

            XmlDocument archiveout = new XmlDocument();

            XmlPopulationWriter.WriteGenomeList(archiveout, population);
            archiveout.Save(filename);

            /*
             * for(int i=0;i<population.Count;i++) {
             *            for(int j=0;j<population[i].objectives.Length;j++)
             *      content+=population[i].objectives[j]+" ";
             *            content+=population[i].Fitness+"\n";
             * }
             * System.IO.File.WriteAllText(filename,content);
             */
        }
Beispiel #5
0
        public void evolve(int generations)
        {
            for (int j = 0; j < generations; j++)
            {
                oneGeneration(j);
            }
            logOutput.Close();

            doc = new XmlDocument();
            XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            oFileInfo = new FileInfo(outputFolder + "bestGenome.xml");
            doc.Save(oFileInfo.FullName);

            //if doing novelty search, write out archive
            if (neatParams.noveltySearch)
            {
                XmlDocument archiveout = new XmlDocument();

                XmlPopulationWriter.WriteGenomeList(archiveout, ea.noveltyFixed.archive);
                oFileInfo = new FileInfo(outputFolder + "archive.xml");
                archiveout.Save(oFileInfo.FullName);
            }
        }
        /// <summary>
        /// Runs one generation of the evolutionary algorithm.
        /// </summary>
        public void oneGeneration(int currentGeneration)
        {
            DateTime dt = DateTime.Now;

            EA.PerformOneGeneration();
            if (EA.BestGenome.RealFitness > MaxFitness)
            {
                SimExperiment.bestGenomeSoFar = (NeatGenome)EA.BestGenome;
                MaxFitness = EA.BestGenome.RealFitness;
                XmlDoc     = new XmlDocument();
                XmlGenomeWriterStatic.Write(XmlDoc, (NeatGenome)EA.BestGenome);
                OutputFileInfo = new FileInfo(Path.Combine(OutputFolder, "bestGenome" + currentGeneration.ToString() + "_" + ((int)MaxFitness).ToString() + ".xml"));
                XmlDoc.Save(OutputFileInfo.FullName);
            }
            if (experiment.DefaultNeatParameters.NS2 || experiment.DefaultNeatParameters.NSLC)
            {
                // The only reason NS2 has special output is so we can profile the novelty calc time versus the fitness calc time
                Console.Write(EA.Generation.ToString() + " " + EA.BestGenome.RealFitness);
                if (experiment.DefaultNeatParameters.NS1)
                {
                    Console.Write(" nov: " + EA.ns1archiveAverageNovelty + " " + EA.Population.GenomeList.Count + "[" + EA.archiveSize + "]");
                }
                else
                {
                    Console.Write(" nov: " + EA.averageNovelty + " " + EA.Population.GenomeList.Count + "[" + EA.archiveSize + "]");
                }

                if (experiment.DefaultNeatParameters.track_me_grid)
                {
                    Console.Write("[" + EA.numFilledBins + "] " + EA.gridAverageFitness);
                }
                Console.WriteLine(" T-sim/nov/spe: " + EA.timeSpentInFitnessEval + " " + EA.timeSpentOutsideFitnessEval + " " + EA.timeSpentInSpeciation);
            }
            else if (experiment.DefaultNeatParameters.mapelites)
            {
                Console.WriteLine(EA.Generation.ToString() + " " + EA.BestGenome.RealFitness + " " + EA.Population.GenomeList.Count + "[" + EA.numFilledBins + "] " + EA.gridAverageFitness + " " + (DateTime.Now.Subtract(dt)));
            }
            else
            {
                Console.Write(EA.Generation.ToString() + " " + EA.BestGenome.RealFitness + " " + EA.Population.GenomeList.Count); // TODO: Add in average novelty
                if (experiment.DefaultNeatParameters.track_me_grid)
                {
                    Console.Write("[" + EA.numFilledBins + "] " + EA.gridAverageFitness);
                }
                Console.WriteLine(" " + (DateTime.Now.Subtract(dt)));
            }

            if (FinalPositionLogging)
            {
                foreach (IGenome g in EA.Population.GenomeList)
                {
                    FinalPositionOutput.WriteLine(g.GenomeId + "," + g.Behavior.finalLocation[0] + "," + g.Behavior.finalLocation[1]);
                }
            }

            if (TrajectoryLogging)
            {
                // If this is the first generation, create the new output folder
                if (currentGeneration == 0)
                {
                    TrajectoryFolderPath = Path.Combine(OutputFolder, "trajectories-" + FolderNum.ToString());
                    if (!Directory.Exists(TrajectoryFolderPath))
                    {
                        System.IO.Directory.CreateDirectory(TrajectoryFolderPath);
                    }
                }

                foreach (IGenome g in EA.Population.GenomeList)
                {
                    // Check to see if we need to create a new folder
                    if (NumTrajectoriesRecorded == NumTrajectoriesPerFolder)
                    {
                        NumTrajectoriesRecorded = 0;
                        FolderNum++;
                        TrajectoryFolderPath = Path.Combine(OutputFolder, "trajectories-" + FolderNum.ToString());
                        if (!Directory.Exists(TrajectoryFolderPath))
                        {
                            System.IO.Directory.CreateDirectory(TrajectoryFolderPath);
                        }
                    }

                    // Print the Trajectory
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Path.Combine(TrajectoryFolderPath, NumTrajectoriesRecorded.ToString() + ".txt"), true))
                    {
                        file.WriteLine("x,y");
                        foreach (double component in g.Behavior.trajectory)
                        {
                            file.Write(component.ToString() + ",");
                        }
                    }

                    // Increment the Trajectory counter
                    NumTrajectoriesRecorded++;
                }
            }

            int gen_mult = 200;

            if (Logging)
            {
                if (experiment.DefaultNeatParameters.noveltySearch && currentGeneration % gen_mult == 0)
                {
                    XmlDocument archiveout = new XmlDocument();

                    XmlPopulationWriter.WriteGenomeList(archiveout, EA.noveltyFixed.archive);
                    OutputFileInfo = new FileInfo(Path.Combine(OutputFolder, "archive.xml"));
                    archiveout.Save(OutputFileInfo.FullName);
                }
                // If doing MapElites: print base500 generation instead of regular, and also print averageFitness and number of filled bins (instead of not)
                // regular: generation bestfitness
                // mapelites: generation500 bestfitness [numfilledgrids] averagefitness
                if (experiment.DefaultNeatParameters.mapelites)
                {
                    LogOutput.WriteLine(EA.generationBase500.ToString() + " " + (MaxFitness).ToString() + " " + EA.numFilledBins.ToString() + " " + EA.averageFitness.ToString());
                    LogOutput.Flush();

                    if (FinalPositionLogging)
                    {
                        foreach (IGenome g in EA.addedToArchive)
                        {
                            ArchiveModificationOutput.WriteLine(g.GenomeId + ",+," + EA.numEvaluations + "," + g.Behavior.finalLocation[0] + "," + g.Behavior.finalLocation[1]);
                        }
                        EA.addedToArchive.Clear();
                        foreach (IGenome g in EA.removedFromArchive)
                        {
                            ArchiveModificationOutput.WriteLine(g.GenomeId + ",-," + EA.numEvaluations + "," + g.Behavior.finalLocation[0] + "," + g.Behavior.finalLocation[1]);
                        }
                        EA.removedFromArchive.Clear();
                    }
                }
                else if (experiment.DefaultNeatParameters.NS2 || experiment.DefaultNeatParameters.NSLC)
                {
                    LogOutput.Write(EA.generationBase500.ToString() + " " + (MaxFitness).ToString() + " " + EA.archiveSize + " " + EA.averageFitness.ToString() + " " + (EA.ns1 ? EA.ns1archiveAverageNovelty.ToString() : EA.averageNovelty.ToString()));
                    if (experiment.DefaultNeatParameters.track_me_grid)
                    {
                        LogOutput.Write(" " + EA.numFilledBins.ToString() + " " + EA.gridAverageFitness.ToString());
                    }
                    LogOutput.WriteLine();
                    LogOutput.Flush();

                    if (FinalPositionLogging)
                    {
                        foreach (IGenome g in EA.addedToArchive)
                        {
                            ArchiveModificationOutput.WriteLine(g.GenomeId + ",+," + EA.numEvaluations + "," + g.Behavior.finalLocation[0] + "," + g.Behavior.finalLocation[1]);
                        }
                        EA.addedToArchive.Clear();
                        foreach (IGenome g in EA.removedFromArchive)
                        {
                            ArchiveModificationOutput.WriteLine(g.GenomeId + ",-," + EA.numEvaluations + "," + g.Behavior.finalLocation[0] + "," + g.Behavior.finalLocation[1]);
                        }
                        EA.removedFromArchive.Clear();
                    }
                }
                else
                {
                    LogOutput.Write(EA.generationBase500.ToString() + " " + (MaxFitness).ToString());
                    if (experiment.DefaultNeatParameters.track_me_grid)
                    {
                        LogOutput.Write(" " + EA.numFilledBins.ToString() + " " + EA.gridAverageFitness.ToString());
                    }
                    LogOutput.WriteLine();
                    LogOutput.Flush();
                }

                // Output complexity statistics
                ComplexityOutput.WriteLine(EA.avgComplexity + ", " + EA.stdevComplexity + ", " + EA.minComplexity + ", " + EA.maxComplexity);
                ComplexityOutput.Flush();
            }
        }