/// <summary>
        /// Load a population of genomes from an XmlReader and returns the genomes in a new list.
        /// The genome factory for the genomes can be obtained from any one of the genomes.
        /// </summary>
        public List <NeatGenome> LoadPopulation(XmlReader xr)
        {
            NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory();

            return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory));
        }
Ejemplo n.º 2
0
 public void SavePopulation(XmlWriter xw, IList <NeatGenome> genomeList)
 {
     NeatGenomeXmlIO.WriteComplete(xw, genomeList, false);
 }
 /// <summary>
 /// Save a population of genomes to an XmlWriter.
 /// </summary>
 public void SavePopulation(XmlWriter xw, IList<NeatGenome> genomeList)
 {
     // Writing node IDs is not necessary for NEAT.
     NeatGenomeXmlIO.WriteComplete(xw, genomeList, false);
 }
Ejemplo n.º 4
0
        private static List <NeatGenome> LoadPopulation_static(XmlReader xr, ExperimentInitArgs_Activation activation, HyperNEAT_Args hyperneatArgs)
        {
            NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(hyperneatArgs, activation);

            return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory));
        }
Ejemplo n.º 5
0
        private static List <NeatGenome> LoadPopulation_static(XmlReader xr, ExperimentInitArgs_Activation activation, int inputCount, int outputCount)
        {
            NeatGenomeFactory genomeFactory = (NeatGenomeFactory)CreateGenomeFactory(inputCount, outputCount, activation);

            return(NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory));
        }
Ejemplo n.º 6
0
        static void Generate(int songId)
        {
            Console.WriteLine("_________________________________________________________________________");
            Console.WriteLine("Now generating song " + songId);

            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            MusicExperiment experiment = new MusicExperiment(MODULES_COUNT);

            // Set the currentFitness
            MusicEnvironment.SetFitnessById(songId);

            // Set the currentSong
            MusicEnvironment.SetSongById(songId);

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load("config.xml");
            experiment.Initialize("NeatMusic", xmlConfig.DocumentElement);

            // Create evolution algorithm and attach update event.
            _ea = experiment.CreateEvolutionAlgorithms();
            _ea[0].UpdateEvent += new EventHandler(ea_UpdateEvent);

            // Start algorithm (it will run on a background thread).
            foreach (var neatEvolutionAlgorithm in _ea)
            {
                neatEvolutionAlgorithm.StartContinue();
            }

            // Continue until certain generations.
            while (_ea[0].CurrentGeneration < GENERATIONS_PER_SONG)
            {
            }

            foreach (var neatEvolutionAlgorithm in _ea)
            {
                neatEvolutionAlgorithm.Stop();
            }


            // Wait for threads to pause.
            while (_ea.All(ea => ea.RunState != RunState.Paused))
            {
                Thread.Sleep(100);
            }

            // Save the best genome to file
            //Console.Write("\nFinal Genomes: ");
            Monitor.Enter(_ea);
            List <NeatGenome> finalGenomes = new List <NeatGenome>();

            foreach (var a in _ea)
            {
                //Console.Write("\t" + a.CurrentChampGenome.EvaluationInfo.Fitness + ", id_" + a.CurrentChampGenome.Id);
                finalGenomes.Add(a.CurrentChampGenome);
            }
            var    doc          = NeatGenomeXmlIO.SaveComplete(finalGenomes, false);
            string championFile = @"..\..\..\NeatMusic\bin\Debug\Champions\" + songId + ".xml";

            Console.WriteLine("\nSaving champion file...");
            Console.WriteLine("_________________________________________________________________________");
            doc.Save(championFile);
            Monitor.Exit(_ea);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            bool train = true;

            GameExperiment experiment = new GameExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load("expconfig.xml");
            experiment.Initialize("TicTacToe", xmlConfig.DocumentElement);

            if (!train)
            {
                #region Play
                NeatGenome genome = null;

                // Try to load the genome from the XML document.
                try
                {
                    using (XmlReader xr = XmlReader.Create(FILE))
                        genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory())[0];
                }
                catch (Exception e1)
                {
                    return;
                }

                // Get a genome decoder that can convert genomes to phenomes.
                var genomeDecoder = experiment.CreateGenomeDecoder();

                // Decode the genome into a phenome (neural network).
                var phenome = genomeDecoder.Decode(genome);

                IPlayer p1 = new NeatBrain(phenome);
                IPlayer p2 = new RandomBrain();

                while (true)
                {
                    Game Game = new Game(p1, p2);
                    Game.PlayUntilFinished();

                    Draw(Game.Board);

                    Console.WriteLine(Game.GetGameState().ToString());
                    Console.ReadKey();
                    Console.WriteLine();
                }
                #endregion Play
            }
            else
            {
                #region Instructions
                Console.WriteLine("This is the training example. If you want to APPLY it, modify the boolean 'train' to false.");
                Console.WriteLine("Take into acocunt that training generates a file 'expconfig.xml' that you can load to play.");
                #endregion


                #region Train
                NeatEvolutionAlgorithm <NeatGenome> _ea;

                // Create evolution algorithm and attach update event.
                _ea              = experiment.CreateEvolutionAlgorithm();
                _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

                // Start algorithm (it will run on a background thread).
                _ea.StartContinue();

                Console.ReadLine();
                #endregion Train
            }
        }
        /// <summary>
        /// Evaluates a list of genomes. Here we decode each genome in using the contained IGenomeDecoder
        /// and evaluate the resulting TPhenome using the contained IPhenomeEvaluator.
        /// </summary>
        public void EvaluateCondor(IList <NeatGenome> genomeList)
        {
            int totalNumberGenomes = genomeList.Count;
            int numberGenomes      = 0;

            string[] finishedFiles = Directory.GetFiles(@"..\..\..\experiments\genomes\genome-finished\", "*.txt"); //finished files used as flags

            // Delete existing finished files that are used as flags
            foreach (string finishedFile in finishedFiles)
            {
                File.Delete(finishedFile);
            }


            // Write genome to file
            Parallel.For(0, genomeList.Count, a =>
            {
                NeatGenome genome = genomeList[a];

                var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>()
                {
                    genome
                }, true);
                doc.Save(@"..\..\..\experiments\genomes\genome-" + a + ".xml");
            });

            GenomeEvaluator.Evaluate(_genomeDecoder, _passwordCrackingEvaluator, experiment);

            /* launch condor */

            do
            {
                string[] flags = Directory.GetFiles(@"..\..\..\experiments\genomes\genome-finished\", "*.txt");
                numberGenomes = flags.Length;
                //System.Threading.Thread.Sleep(1000); //??
            } while (numberGenomes != totalNumberGenomes);


            for (int a = 0; a < genomeList.Count; a++)
            {
                NeatGenome genome = genomeList[a];
                try
                {
                    string       name   = "genome-" + a + "-finished.txt";
                    StreamReader sr     = new StreamReader(@"..\..\..\experiments\genomes\genome-results\genome-" + a + "-results.txt");
                    string       line   = sr.ReadLine();
                    string[]     values = line.Split(' ');
                    genome.EvaluationInfo.SetFitness(double.Parse(values[0]));
                    genome.EvaluationInfo.AlternativeFitness = double.Parse(values[1]);
                }
                catch (System.Exception e)
                {
                    System.Console.WriteLine("File not found.");
                }
            }



            foreach (string p in _passwordCrackingEvaluator.FoundPasswords)
            {
                double val = PasswordCrackingEvaluator.Passwords[p].Reward;
                PasswordCrackingEvaluator.Passwords[p].Reward = val * 0.75;
            }
        }