/// <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);
        }
        private void EvolutionaryThread()
        {
            m_exp = CreateExperiment();
            var idgen = new IdGenerator();

            m_evoAlg = new EvolutionAlgorithm(
                new Population(idgen,
                               GenomeFactory.CreateGenomeList(m_exp.DefaultNeatParameters, idgen,
                                                              m_exp.InputNeuronCount, m_exp.OutputNeuronCount,
                                                              m_exp.DefaultNeatParameters.pInitialPopulationInterconnections,
                                                              NeatExpParams.PopulationSize)),
                m_exp.PopulationEvaluator, m_exp.DefaultNeatParameters);

            while (!m_shouldQuit)
            {
                Console.WriteLine("::::: Performing one generation");
                Console.WriteLine();

                m_evoAlg.PerformOneGeneration();

                if (NeatExpParams.SaveFitnessGrowth)
                {
                    m_eaLogger.WriteLine(String.Format("{0,-10} {1,-20} {2,-20} {3,-20}",
                                                       m_evoAlg.Generation,
                                                       m_evoAlg.BestGenome.Fitness,
                                                       m_evoAlg.Population.MeanFitness, m_evoAlg.Population.AvgComplexity));
                }

                m_curBestGenome = m_evoAlg.BestGenome as NeatGenome;
                if (m_evoAlg.BestGenome.Fitness > m_overalBestFitness)
                {
                    m_overalBestFitness = m_evoAlg.BestGenome.Fitness;
                    m_overalBestGenome  = m_curBestGenome;

                    if (NeatExpParams.SaveEachGenerationChampionCPPN)
                    {
                        try
                        {
                            var doc = new XmlDocument();
                            XmlGenomeWriterStatic.Write(doc, (NeatGenome)m_evoAlg.BestGenome);
                            var oFileInfo = new FileInfo(Path.Combine(
                                                             NeatExpParams.EALogDir, String.Format("BestIndividual-{0}-{1}.xml", MyUnum, m_evoAlg.Generation.ToString())));
                            doc.Save(oFileInfo.FullName);
                        }
                        catch
                        {
                        }
                    }
                }

                if (EAUpdate != null)
                {
                    EAUpdate.Invoke(this, EventArgs.Empty);
                }
            }
        }
        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);
            }
        }
Exemple #4
0
        public void addPending(bool suppressThresholdUpdate = false)
        {
            int length = pending_addition.Count;


            if (!suppressThresholdUpdate)
            {
                if (length == 0)
                {
                    archive_threshold *= 0.95;
                }
                if (length > 5)
                {
                    archive_threshold *= 1.3;
                }
            }

            for (int i = 0; i < length; i++)
            {
                if (measureAgainstArchive((NeatGenome.NeatGenome)pending_addition[i], false))
                {
                    archive.Add(pending_addition[i]);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter("NoveltyLog.txt", true))
                        file.WriteLine(pending_addition[i].Fitness);

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(Chromaria.Modes.NoveltySearchRun.noveltyLogsFolder + "archive.txt", true))
                    {
                        // Write the genome's info to the novelty log
                        file.WriteLine("ID: " + pending_addition[i].GenomeId);
                        file.WriteLine("Fitness: " + pending_addition[i].Fitness);
                        file.Write("Behavior: ");
                        foreach (double behavior in pending_addition[i].Behavior.behaviorList)
                        {
                            file.Write(behavior.ToString() + ", ");
                        }
                        file.WriteLine();
                        file.WriteLine();
                    }

                    // Also print out the genome itself
                    XmlDocument doc = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome.NeatGenome)pending_addition[i]);
                    FileInfo oFileInfo = new FileInfo(Chromaria.Modes.NoveltySearchRun.noveltyLogsFolder + "genome" + pending_addition[i].GenomeId + ".xml");
                    doc.Save(oFileInfo.FullName);
                }
            }
            pending_addition.Clear();
        }
Exemple #5
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());
            }
        }
Exemple #6
0
        //private static Random random;

        public static void Main(string[] args)
        {
            Util.Initialize(args[0]);
            var         idgen      = new IdGenerator();
            IExperiment experiment = new LimitExperiment();

            XmlSerializer ser = new XmlSerializer(typeof(Settings));
            //Settings settings = new Settings()
            //{
            //    SmallBlind = 1,
            //    BigBlind = 2,
            //    GamesPerIndividual = 100,
            //    LogFile = "mutlithreaded_log.txt",
            //    MaxHandsPerTourney = 200,
            //    PlayersPerGame = 6,
            //    StackSize = 124,
            //    Threads = 4
            //};
            //ser.Serialize(new StreamWriter("settings.xml"), settings);
            Settings settings = (Settings)ser.Deserialize(new StreamReader("settings.xml"));
            var      eval     = new PokerPopulationEvaluator <SimpleLimitNeuralNetPlayer2, RingGamePlayerEvaluator>(settings);

            var ea = new EvolutionAlgorithm(
                new Population(idgen,
                               GenomeFactory.CreateGenomeList(experiment.DefaultNeatParameters,
                                                              idgen, experiment.InputNeuronCount,
                                                              experiment.OutputNeuronCount,
                                                              experiment.DefaultNeatParameters.pInitialPopulationInterconnections,
                                                              experiment.DefaultNeatParameters.populationSize)),
                eval,
                experiment.DefaultNeatParameters);

            Console.WriteLine("Starting real evolution");
            for (int i = 0; true; i++)
            {
                Console.WriteLine("Generation {0}", i + 1);
                ea.PerformOneGeneration();
                Console.WriteLine("Champion Fitness={0}", ea.BestGenome.Fitness);
                var doc = new XmlDocument();
                XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                FileInfo oFileInfo = new FileInfo("genomes_simple\\" + "bestGenome" + i.ToString() + ".xml");
                doc.Save(oFileInfo.FullName);
            }
        }
Exemple #7
0
        public void saveGenome(IGenome genomeToSave)
        {
            try{
                string genomeAttempt = genomeFileName(genomeToSave);

                string genomePath = ExperimentDirectory + "/" + genomeAttempt;

                if (!File.Exists(genomePath))
                {
                    XmlDocument doc = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome.NeatGenome)genomeToSave);
                    doc.Save(genomePath);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to write genome file: " + e.Message + " Genome: " + genomeToSave.GenomeId);
            }
        }
Exemple #8
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);
            }
        }
Exemple #9
0
        //Todo REFINE... adding highest fitness might
        //not correspond with most novel?
        public void add_most_novel(Population p)
        {
            double  max_novelty = 0;
            IGenome best        = null;

            for (int i = 0; i < p.GenomeList.Count; i++)
            {
                if (p.GenomeList[i].Fitness > max_novelty)
                {
                    best        = p.GenomeList[i];
                    max_novelty = p.GenomeList[i].Fitness;
                }
            }
            archive.Add(best);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(Chromaria.Modes.NoveltySearchRun.noveltyLogsFolder + "archive.txt", true))
            {
                // Write the genome's info to the novelty log
                file.WriteLine("*BEST*");
                file.WriteLine("ID: " + best.GenomeId);
                file.WriteLine("Fitness: " + best.Fitness);
                file.Write("Behavior: ");
                foreach (double behavior in best.Behavior.behaviorList)
                {
                    file.Write(behavior.ToString() + ", ");
                }
                file.WriteLine();
                file.WriteLine();
            }

            // Also print out the genome itself
            XmlDocument doc = new XmlDocument();

            XmlGenomeWriterStatic.Write(doc, (NeatGenome.NeatGenome)best);
            FileInfo oFileInfo = new FileInfo(Chromaria.Modes.NoveltySearchRun.noveltyLogsFolder + "genome" + best.GenomeId + ".xml");

            doc.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();
            }
        }
Exemple #11
0
        static void Main(string[] args)
        {
            NeatGenome seedGenome = null;
            string     filename   = @"seedGenome.xml";

            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                seedGenome = XmlNeatGenomeReaderStatic.Read(document);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Problem loading genome. \n" + e.Message);
            }

            string experimentName = "_2Pref";

            for (int k = 3; k < 10; k++)
            {
                double maxFitness     = 0;
                int    maxGenerations = 400; //150;
                int    populationSize = 300; //100;//150;

                Thread extraWindowThread;
                extraWindowThread = new System.Threading.Thread(delegate()
                {
                    var myForm = new SharpNeatExperiments.Pacman.MyForm1();
                    System.Windows.Forms.Application.Run(myForm);
                });
                extraWindowThread.Start();

                IExperiment exp = null;
                //exp = new SUPGONLYExperiment(4,12,12,5,2);
                //exp = new PacmanExperimentSUPG(4, 12, 12, 5, 2);
                //exp = new SPMMExperiment(4, 12, 12, 5, 2);
                //exp = new SPCExperiment(4, 12, 12, 5, 2);
                exp = new SPSUPGExperiment(4, 12, 12, 5, 6);

                StreamWriter SW;
                SW = File.CreateText("logfile.txt");
                //Change this line for different experiments
                XmlDocument        doc;
                FileInfo           oFileInfo;
                IdGenerator        idgen;
                EvolutionAlgorithm ea;
                if (seedGenome == null)
                {
                    idgen = new IdGenerator();
                    ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
                }
                else
                {
                    idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome);
                    ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, exp.DefaultNeatParameters.populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
                }
                bool isNSGAiiEnabled = false;
                for (int j = 0; j < maxGenerations; j++)
                {
                    DateTime dt = DateTime.Now;
                    if (isNSGAiiEnabled)
                    {
                        ea.PerformOneGenerationNSGAii();
                    }
                    else
                    {
                        ea.PerformOneGeneration();
                    }
                    if (ea.BestGenome.Fitness > maxFitness)
                    {
                        maxFitness = ea.BestGenome.Fitness;
                        Console.WriteLine(maxFitness + "maxFitness");
                        Console.WriteLine("objectiveFitness" + ea.BestGenome.MultiObjectiveFitness[0] + " " + ea.BestGenome.MultiObjectiveFitness[1]);
                        doc = new XmlDocument();
                        XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                        oFileInfo = new FileInfo("bestGenome" + j.ToString() + ".xml");
                        doc.Save(oFileInfo.FullName);

                        //MadsXMLWriter(j, maxFitness);
                    }
                    MadsXMLWriter(j, maxFitness, k, experimentName);
                    Console.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString() + " " + (DateTime.Now.Subtract(dt)));
                    //Do any post-hoc stuff here


                    SW.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString());
                }
                SW.Close();
                //----- Write the genome to an XmlDocument.
                doc = new XmlDocument();
                XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
                oFileInfo = new FileInfo("bestGenome" + experimentName + "_" + k + ".xml");
                doc.Save(oFileInfo.FullName);
            }
        }
Exemple #12
0
        /// <summary>
        /// This method is called when one individual finishes its run in the world (either because it planted itself
        /// or because it timed out). It should ONLY be called when the creature is being controlled by a neural network (novelty search or evolution).
        /// </summary>
        ///
        private void ResetToFirstTrial()
        {
            // Reset numUpdates
            numUpdates = 0;

            // Remove the creature from the old region lists
            // Update the region lists
            foreach (List <int> region in regions)
            {
                region.RemoveAt(indexOfCurrentCreature);
            }

            // If the creature has already completed both trials, we need to _actually_ reset the simulator.
            #region Planter XML writing
            if (GenomeIndexOfCurrentCreature != -1)
            {
                // First, check to see if the individual was a valid planter
                if (freezeAfterPlanting)
                {
                    // Success on both trials
                    if (plantedInColoredSpace1 && plantedInColoredSpace2)
                    {
                        numBidirectionalPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_BidirectionalPlanterGenome" + numBidirectionalPlanters.ToString() + ".xml");
                    }

                    // Success on the first trial
                    else if (plantedInColoredSpace1)
                    {
                        numFirstTrialPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_FirstTrialPlanterGenome" + numFirstTrialPlanters.ToString() + ".xml");
                    }

                    // Sucess on the second trial
                    else if (plantedInColoredSpace2)
                    {
                        numSecondTrialPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_SecondTrialPlanterGenome" + numSecondTrialPlanters.ToString() + ".xml");
                    }
                }

                else
                {
                    // Success on both trials, no misplanting
                    if (plantedInColoredSpace1 && plantedInColoredSpace2 && !plantedInWhiteSpace1 && !plantedInWhiteSpace2)
                    {
                        numBidirectionalPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_BidirectionalPlanterGenome" + numBidirectionalPlanters.ToString() + ".xml");
                    }

                    // Planted only in white
                    // Success on the first trial, no misplanting
                    else if (!plantedInColoredSpace1 && plantedInWhiteSpace1)
                    {
                        numFirstTrialPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_FirstTrialPlanterGenome_White" + numFirstTrialPlanters.ToString() + ".xml");
                    }

                    // Success on the first trial, no misplanting
                    else if (plantedInColoredSpace1 && !plantedInWhiteSpace1)
                    {
                        numFirstTrialPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_FirstTrialPlanterGenome" + numFirstTrialPlanters.ToString() + ".xml");
                    }

                    // Sucess on the second trial, no misplanting
                    else if (plantedInColoredSpace2 && !plantedInWhiteSpace2)
                    {
                        numSecondTrialPlanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_SecondTrialPlanterGenome" + numSecondTrialPlanters.ToString() + ".xml");
                    }

                    // Success on both trials, but with misplanting
                    if ((plantedInColoredSpace1 && plantedInWhiteSpace1) || (plantedInColoredSpace2 && plantedInWhiteSpace2))
                    {
                        numBidirectionalMisplanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_BidirectionalPlanterGenome_Misplanted" + numBidirectionalMisplanters.ToString() + ".xml");
                    }

                    // Success on the first trial, but with misplanting
                    else if (plantedInColoredSpace1 && plantedInWhiteSpace1)
                    {
                        numFirstTrialMisplanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_FirstTrialPlanterGenome_Misplanted" + numFirstTrialMisplanters.ToString() + ".xml");
                    }

                    // Sucess on the second trial, but with misplanting
                    else if (plantedInColoredSpace2 && plantedInWhiteSpace2)
                    {
                        numSecondTrialMisplanters++;
                        XmlDocument    results     = new XmlDocument();
                        XmlDeclaration declaration = results.CreateXmlDeclaration("1.0", null, null);
                        results.AppendChild(declaration);
                        XmlGenomeWriterStatic.Write(results, (NeatGenome)ea.Population.GenomeList[GenomeIndexOfCurrentCreature]);
                        results.Save(plantersFolder + "NoveltySearch_SecondTrialPlanterGenome_Misplanted" + numSecondTrialMisplanters.ToString() + ".xml");
                    }
                }
            }
            #endregion

            // Remove the individual from the global image list
            Components.Remove(currentCreature);

            // Reset the success flags
            plantedInColoredSpace1 = false;
            plantedInWhiteSpace1   = false;
            plantedInColoredSpace2 = false;
            plantedInWhiteSpace2   = false;

            // Also reset the trial flag
            firstTrial = true;

            // Get the next creature for this generation, decode its CPPN, and replace the creature's controller
            GenomeIndexOfCurrentCreature++;

            // Update the folders for storing the archive and planters if necessary
            if (GenomeIndexOfCurrentCreature % numCreaturesPerFolder == 0)
            {
                int newFolderNumber = GenomeIndexOfCurrentCreature / numCreaturesPerFolder;
                noveltyLogsFolder = Directory.GetCurrentDirectory() + "\\archive\\" + newFolderNumber + "\\";
                if (!Directory.Exists(noveltyLogsFolder))
                {
                    Directory.CreateDirectory(noveltyLogsFolder);
                }
                plantersFolder = Directory.GetCurrentDirectory() + "\\planters\\" + newFolderNumber + "\\";
                if (!Directory.Exists(plantersFolder))
                {
                    Directory.CreateDirectory(plantersFolder);
                }
            }

            // Initialize the genome's behavior characterization structure if this genome has not previously been evaluated
            if (ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Behavior == null)
            {
                ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Behavior = new BehaviorType();
                ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Behavior.behaviorList = new List <double>();
            }
            INetwork newController = controllerSubstrate.generateGenome(ea.Population.GenomeList[GenomeIndexOfCurrentCreature].Decode(ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"))).Decode(ActivationFunctionFactory.GetActivationFunction("BipolarSigmoid"));
            if (bidirectionalTrials)
            {
                currentCreature = new NNControlledCreature(morphology, initialBoardWidth / 2, initialBoardHeight / 2, initialHeading + (float)(Math.PI / 2.0), newController, this, drawSensorField, trackPlanting, defaultNumSensors, freezeAfterPlanting);
            }
            else
            {
                currentCreature = new NNControlledCreature(morphology, initialBoardWidth / 2, initialBoardHeight / 2, initialHeading, newController, this, drawSensorField, trackPlanting, defaultNumSensors, freezeAfterPlanting);
            }

            // Add the creature back into the appropriate region lists
            RotationPacket rp = currentCreature.getRotationPacket();
            regions[rp.NWCoord.Y / regionHeight, rp.NWCoord.X / regionWidth].Add(indexOfCurrentCreature);
            if ((rp.NWCoord.X % regionWidth > rp.SECoord.X % regionWidth) && (rp.NWCoord.Y % regionHeight > rp.SECoord.Y % regionHeight))
            {
                regions[rp.SECoord.Y / regionHeight, rp.SECoord.X / regionWidth].Add(indexOfCurrentCreature);
            }
            if (rp.NWCoord.X % regionWidth > rp.SECoord.X % regionWidth)
            {
                regions[(rp.NWCoord.Y / regionHeight), rp.SECoord.X / regionWidth].Add(indexOfCurrentCreature);
            }
            if (rp.NWCoord.Y % regionHeight > rp.SECoord.Y % regionHeight)
            {
                regions[rp.SECoord.Y / regionHeight, rp.SECoord.X / regionWidth].Add(indexOfCurrentCreature);
            }
        }
        private void startEvolutionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NeatGenome seedGenome = null;

            if (EvoSeedFileName != null)
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(EvoSeedFileName);
                    seedGenome = XmlNeatGenomeReaderStatic.Read(document);
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("Problem loading genome. \n" + ex.Message);
                }
            }

            double      maxFitness     = 0;
            int         maxGenerations = 1000;
            int         populationSize = 150;
            int         inputs         = 4;
            IExperiment exp            = new SkirmishExperiment(inputs, 1, EvoIsMulti, EvoShape);

            w     = SkirmishNetworkEvaluator.lWorldVar(network, 125);
            timer = 0;

            StreamWriter SW;

            SW = File.CreateText(EvoOutputLogFolder + "logfile.txt");
            XmlDocument        doc;
            FileInfo           oFileInfo;
            IdGenerator        idgen;
            EvolutionAlgorithm ea;

            if (seedGenome == null)
            {
                idgen = new IdGenerator();
                ea    = new EvolutionAlgorithm(
                    new Population(idgen,
                                   GenomeFactory.CreateGenomeList(
                                       exp.DefaultNeatParameters, idgen,
                                       exp.InputNeuronCount, exp.OutputNeuronCount,
                                       exp.DefaultNeatParameters.pInitialPopulationInterconnections,
                                       populationSize)),
                    exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            else
            {
                idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome);
                ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            for (int j = 0; j < maxGenerations; j++)
            {
                DateTime dt = DateTime.Now;
                ea.PerformOneGeneration();
                if (ea.BestGenome.Fitness > maxFitness)
                {
                    maxFitness = ea.BestGenome.Fitness;
                    doc        = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                    oFileInfo = new FileInfo(EvoOutputLogFolder + "bestGenome" + j.ToString() + ".xml");
                    doc.Save(oFileInfo.FullName);

                    // This will output the substrate, uncomment if you want that

                    /* doc = new XmlDocument();
                     * XmlGenomeWriterStatic.Write(doc, (NeatGenome) SkirmishNetworkEvaluator.substrate.generateMultiGenomeModulus(ea.BestGenome.Decode(null),5));
                     * oFileInfo = new FileInfo(folder + "bestNetwork" + j.ToString() + ".xml");
                     * doc.Save(oFileInfo.FullName);
                     */
                }
                Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.Fitness + " " + (DateTime.Now.Subtract(dt)));
                //Do any post-hoc stuff here

                SW.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString());
            }
            SW.Close();

            doc = new XmlDocument();
            XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            oFileInfo = new FileInfo(EvoOutputLogFolder + "bestGenome.xml");
            doc.Save(oFileInfo.FullName);
        }
Exemple #14
0
        static void Main(string[] args)
        {
            string     folder     = "";
            NeatGenome seedGenome = null;
            string     filename   = null;
            string     shape      = "triangle";
            bool       isMulti    = false;

            for (int j = 0; j < args.Length; j++)
            {
                if (j <= args.Length - 2)
                {
                    switch (args[j])
                    {
                    case "-seed": filename = args[++j];
                        Console.WriteLine("Attempting to use seed from file " + filename);
                        break;

                    case "-folder": folder = args[++j];
                        Console.WriteLine("Attempting to output to folder " + folder);
                        break;

                    case "-shape": shape = args[++j];
                        Console.WriteLine("Attempting to do experiment with shape " + shape);
                        break;

                    case "-multi": isMulti = Boolean.Parse(args[++j]);
                        Console.WriteLine("Experiment is heterogeneous? " + isMulti);
                        break;
                    }
                }
            }

            if (filename != null)
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(filename);
                    seedGenome = XmlNeatGenomeReaderStatic.Read(document);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Problem loading genome. \n" + e.Message);
                }
            }

            double       maxFitness     = 0;
            int          maxGenerations = 1000;
            int          populationSize = 150;
            int          inputs         = 4;
            IExperiment  exp            = new SkirmishExperiment(inputs, 1, isMulti, shape);
            StreamWriter SW;

            SW = File.CreateText(folder + "logfile.txt");
            XmlDocument        doc;
            FileInfo           oFileInfo;
            IdGenerator        idgen;
            EvolutionAlgorithm ea;

            if (seedGenome == null)
            {
                idgen = new IdGenerator();
                ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            else
            {
                idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome);
                ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            for (int j = 0; j < maxGenerations; j++)
            {
                DateTime dt = DateTime.Now;
                ea.PerformOneGeneration();
                if (ea.BestGenome.Fitness > maxFitness)
                {
                    maxFitness = ea.BestGenome.Fitness;
                    doc        = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                    oFileInfo = new FileInfo(folder + "bestGenome" + j.ToString() + ".xml");
                    doc.Save(oFileInfo.FullName);

                    // This will output the substrate, uncomment if you want that

                    /* doc = new XmlDocument();
                     * XmlGenomeWriterStatic.Write(doc, (NeatGenome) SkirmishNetworkEvaluator.substrate.generateMultiGenomeModulus(ea.BestGenome.Decode(null),5));
                     * oFileInfo = new FileInfo(folder + "bestNetwork" + j.ToString() + ".xml");
                     * doc.Save(oFileInfo.FullName);
                     */
                }
                Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.Fitness + " " + (DateTime.Now.Subtract(dt)));
                //Do any post-hoc stuff here

                SW.WriteLine(ea.Generation.ToString() + " " + (maxFitness).ToString());
            }
            SW.Close();

            doc = new XmlDocument();
            XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            oFileInfo = new FileInfo(folder + "bestGenome.xml");
            doc.Save(oFileInfo.FullName);
        }
        public void run(int type, int evaluationMethod, System.Threading.CancellationToken token)
        {
            double maxFitness     = 0;
            int    maxGenerations = 800;
            int    populationSize = 300;

            IExperiment exp;

            if (evaluationMethod == 1)
            {
                novelty = true;
            }

            if (type == 0)
            {
                exp = new CTRNNExperiment(4, 12, 8, 8, 4);
            }
            else if (type == 1)
            {
                exp = new SUPGExperiment(4, 12, 12, 3, 2);
            }
            else
            {
                doClune = true;
                exp     = new CluneExperiment(20, 20, 20, 6, 1);
            }

            XmlDocument        doc;
            FileInfo           oFileInfo;
            IdGenerator        idgen;
            EvolutionAlgorithm ea;
            NeatGenome         seedGenome = null;

            if (seedGenome == null)
            {
                idgen = new IdGenerator();
                ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            else
            {
                idgen = new IdGeneratorFactory().CreateIdGenerator(seedGenome);
                ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(seedGenome, populationSize, exp.DefaultNeatParameters, idgen)), exp.PopulationEvaluator, exp.DefaultNeatParameters);
            }
            Directory.CreateDirectory(folder);
            using (var logWriter = File.CreateText(folder + "Log " + DateTime.Now.ToString("u").Replace(':', '.') + ".txt"))
                for (int j = 0; j < maxGenerations; j++)
                {
                    if (token.IsCancellationRequested)
                    {
                        logWriter.WriteLine("Cancelled");
                        break;
                    }

                    DateTime dt = DateTime.Now;
                    ea.PerformOneGeneration();

                    if (ea.BestGenome.ObjectiveFitness > maxFitness)
                    {
                        maxFitness = ea.BestGenome.ObjectiveFitness;
                        doc        = new XmlDocument();
                        XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                        oFileInfo = new FileInfo(folder + "bestGenome" + j + ".xml");
                        doc.Save(oFileInfo.FullName);

                        /*/ This will output the substrate
                         * doc = new XmlDocument();
                         * XmlGenomeWriterStatic.Write(doc, SUPGNetworkEvaluator.substrate.generateGenome(ea.BestGenome.Decode(null)));
                         * oFileInfo = new FileInfo(folder + "bestNetwork" + j + ".xml");
                         * doc.Save(oFileInfo.FullName);*/
                    }
                    var msg = DateTime.Now.ToLongTimeString()
                              + "; Duration=" + DateTime.Now.Subtract(dt).ToString("mm\\:ss")
                              + "; Gen=" + ea.Generation.ToString("000") + "; Neurons=" + (ea.Population.TotalNeuronCount / (float)ea.Population.GenomeList.Count).ToString("00.00") + "; Connections=" + (ea.Population.TotalConnectionCount / (float)ea.Population.GenomeList.Count).ToString("00.00")
                              + "; BestFit=" + ea.BestGenome.ObjectiveFitness.ToString("0.000") + "; MaxFit=" + maxFitness.ToString("0.000");
                    Console.WriteLine(msg);
                    logWriter.WriteLine(msg);
                    logWriter.Flush();
                    //Do any post-hoc stuff here
                }

            doc = new XmlDocument();
            XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            oFileInfo = new FileInfo(folder + "bestGenome.xml");
            doc.Save(oFileInfo.FullName);
        }
Exemple #16
0
        public static void ThreeDHyperNEAT()
        {
            if (!cmdParser.IsArgumentProvided("-shape", out Program.Shape))
            {
                Program.Shape = "triangle";
            }

            if (!cmdParser.IsArgumentProvided("-folder", out Program.LogFolder))
            {
                Program.LogFolder = "TestLogs";
            }

            if (!Directory.Exists(Program.LogFolder))
            {
                Directory.CreateDirectory(Program.LogFolder);
            }

            if (!cmdParser.IsIntArgumentProvided("-gens", out Program.MaxGenerations))
            {
                Program.MaxGenerations = 1000;
            }



            double       maxFitness     = 0;
            int          maxGenerations = Program.MaxGenerations;
            int          populationSize = Program.PopulationSize;
            IExperiment  exp            = new Skirmish3DExperiment(5, Program.Shape, Program.PopulationSize);
            StreamWriter sw             = File.CreateText(Path.Combine(Program.LogFolder,
                                                                       String.Format("{0}-logfile.log", Program.LogFolder)));

            sw.AutoFlush = true;

            XmlDocument        doc;
            FileInfo           oFileInfo;
            IdGenerator        idgen;
            EvolutionAlgorithm ea;

            idgen = new IdGenerator();
            ea    = new EvolutionAlgorithm(new Population(idgen, GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters, idgen, exp.InputNeuronCount, exp.OutputNeuronCount, exp.DefaultNeatParameters.pInitialPopulationInterconnections, populationSize)), exp.PopulationEvaluator, exp.DefaultNeatParameters);

            for (int j = 0; j < maxGenerations; j++)
            {
                DateTime dt = DateTime.Now;
                ea.PerformOneGeneration();
                if (ea.BestGenome.Fitness > maxFitness)
                {
                    maxFitness = ea.BestGenome.Fitness;
                    doc        = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                    oFileInfo = new FileInfo(Path.Combine(Program.LogFolder, "BestGenome-" + j.ToString() + ".xml"));
                    doc.Save(oFileInfo.FullName);

                    // This will output the substrate, uncomment if you want that

                    /* doc = new XmlDocument();
                     * XmlGenomeWriterStatic.Write(doc, (NeatGenome) SkirmishNetworkEvaluator.substrate.generateMultiGenomeModulus(ea.BestGenome.Decode(null),5));
                     * oFileInfo = new FileInfo(folder + "bestNetwork" + j.ToString() + ".xml");
                     * doc.Save(oFileInfo.FullName);
                     */
                }
                Console.WriteLine(ea.Generation.ToString() + " " + ea.BestGenome.Fitness + " " + (DateTime.Now.Subtract(dt)));
                //Do any post-hoc stuff here

                sw.WriteLine("{0} {1} {2}", ea.Generation, maxFitness, ea.Population.MeanFitness);
                sw.Flush();
            }
            sw.Close();

            doc = new XmlDocument();
            XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            oFileInfo = new FileInfo(Path.Combine(Program.LogFolder, "BestGenome.xml"));
            doc.Save(oFileInfo.FullName);
        }
Exemple #17
0
        private static void PredatorCCEAHNMainThread(object oAgentId)
        {
            int         agentId        = (int)oAgentId;
            double      maxFitness     = 0;
            int         maxGenerations = Program.MaxGenerations;
            int         populationSize = Program.PopulationSize;
            IExperiment exp            = new SkirmishCCEAExperiment(agentId, Program.PopulationSize);

            StreamWriter sw = File.CreateText(Path.Combine(Program.LogFolder,
                                                           String.Format("{0}-logfile-{1}.log", Program.LogFolder, agentId)));

            sw.AutoFlush = true;

            XmlDocument doc;
            FileInfo    oFileInfo;

            IdGenerator        idgen = new IdGenerator();
            EvolutionAlgorithm ea    = new EvolutionAlgorithm(
                new Population(idgen,
                               GenomeFactory.CreateGenomeList(exp.DefaultNeatParameters,
                                                              idgen, exp.InputNeuronCount, exp.OutputNeuronCount,
                                                              exp.DefaultNeatParameters.pInitialPopulationInterconnections,
                                                              populationSize)),
                exp.PopulationEvaluator, exp.DefaultNeatParameters);

            for (int j = 0; j < maxGenerations; j++)
            {
                DateTime dt = DateTime.Now;
                ea.PerformOneGeneration();

                if (ea.BestGenome.Fitness > maxFitness)
                {
                    maxFitness = ea.BestGenome.Fitness;
                    doc        = new XmlDocument();
                    XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome);
                    oFileInfo = new FileInfo(Path.Combine(LogFolder,
                                                          String.Format("BestGenome-{0}-{1}.xml", agentId, j)));
                    doc.Save(oFileInfo.FullName);

                    // This will output the substrate, uncomment if you want that

                    /* doc = new XmlDocument();
                     * XmlGenomeWriterStatic.Write(doc, (NeatGenome) SkirmishNetworkEvaluator.substrate.generateMultiGenomeModulus(ea.BestGenome.Decode(null),5));
                     * oFileInfo = new FileInfo(folder + "bestNetwork" + j.ToString() + ".xml");
                     * doc.Save(oFileInfo.FullName);
                     */
                }

                Console.WriteLine("[{0}] {1} {2} {3}", agentId, ea.Generation, ea.BestGenome.Fitness, (DateTime.Now.Subtract(dt)));
                //Do any post-hoc stuff here

                sw.WriteLine("{0} {1} {2}", ea.Generation, maxFitness, ea.Population.MeanFitness);
                sw.Flush();
            }
            sw.Close();

            //doc = new XmlDocument();
            //XmlGenomeWriterStatic.Write(doc, (NeatGenome)ea.BestGenome, ActivationFunctionFactory.GetActivationFunction("NullFn"));
            //oFileInfo = new FileInfo(Path.Combine(LogFolder, "BestGenome.xml"));
            //doc.Save(oFileInfo.FullName);

            Environment.Exit(0);
        }