Exemplo n.º 1
0
        public void Start()
        {
            // 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);

            // 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();
            }

            // Hit return to stop.
            Console.ReadLine();

            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);
                //Console.Write("\t" + a.BestChampion.EvaluationInfo.Fitness + ", id_" + a.BestChampion.Id);
                //finalGenomes.Add(a.BestChampion);
            }
            Console.WriteLine("\n\n(Hit ENTER to finish");
            var doc = NeatGenomeXmlIO.SaveComplete(finalGenomes, false);

            doc.Save(CHAMPION_FILE);
            Monitor.Exit(_ea);
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public void doWork()
        {
            // 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();

            // 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).
            _ea[0].StartContinue();
            _ea[1].StartContinue();
            _ea[2].StartContinue();
            _ea[3].StartContinue();

            // Hit return to quit.
            //Console.ReadLine();

            // continue execution until RequestStop is called
            while (!_shouldStop)
            {
            }

            // stop the ga threads
            _ea[0].UpdateEvent -= new EventHandler(ea_UpdateEvent);
            _ea[0].RequestPause();
            _ea[1].RequestPause();
            _ea[2].RequestPause();
            _ea[3].RequestPause();
        }
Exemplo n.º 3
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);
        }