static void Main(string[] args) { // 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. XorExperiment experiment = new XorExperiment(); // Load config XML. XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load("xor.config.xml"); experiment.Initialize("XOR", xmlConfig.DocumentElement); // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes. _genomeFactory = experiment.CreateGenomeFactory(); // Create an initial population of randomly generated genomes. _genomeList = _genomeFactory.CreateGenomeList(150, 0); // Create evolution algorithm and attach update event. _ea = experiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList); _ea.UpdateEvent += new EventHandler(ea_UpdateEvent); // Start algorithm (it will run on a background thread). _ea.StartContinue(); // Hit return to quit. Console.ReadLine(); }
static void Main(string[] args) { // 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. XorExperiment experiment = new XorExperiment(); // Load config XML. XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load("xor.config.xml"); experiment.Initialize("XOR", xmlConfig.DocumentElement); WriteHelp(); // Read key commands from the console. for(;;) { // Read command. Console.Write(">"); string cmdstring = Console.ReadLine(); // Parse command. string[] cmdArgs = cmdstring.Split(' '); try { // Process command. switch(cmdArgs[0]) { // Init commands. case "randpop": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot create population while algorithm is running."); break; } // Attempt to parse population size arg. if(cmdArgs.Length <= 1) { Console.WriteLine("Error. Missing {size} argument."); break; } int populationSize; if(!int.TryParse(cmdArgs[1], out populationSize)) { Console.WriteLine(string.Format("Error. Invalid {size} argument [{0}].", cmdArgs[1])); break; } // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes. _genomeFactory = experiment.CreateGenomeFactory(); // Create an initial population of randomly generated genomes. _genomeList = _genomeFactory.CreateGenomeList(populationSize, 0); Console.WriteLine(string.Format("Created [{0}] random genomes.", populationSize)); break; } case "loadpop": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot load population while algorithm is running."); break; } // Attempt to get population filename arg. if(cmdArgs.Length <= 1) { Console.WriteLine("Error. Missing {filename} argument."); break; } // Open and load population XML file. using(XmlReader xr = XmlReader.Create(cmdArgs[1])) { _genomeList = experiment.LoadPopulation(xr); } _genomeFactory = _genomeList[0].GenomeFactory; Console.WriteLine(string.Format("Loaded [{0}] genomes.", _genomeList.Count)); break; } case "loadseed": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot load population while algorithm is running."); break; } // Attempt to get genome filename arg. if(cmdArgs.Length <= 1) { Console.WriteLine("Error. Missing {filename} argument."); break; } // Attempt to parse population size arg. if(cmdArgs.Length <= 2) { Console.WriteLine("Error. Missing {size} argument."); break; } int populationSize; if(!int.TryParse(cmdArgs[1], out populationSize)) { Console.WriteLine(string.Format("Error. Invalid {size} argument [{0}].", cmdArgs[1])); break; } // Open and load genome XML file. using(XmlReader xr = XmlReader.Create(cmdArgs[1])) { _genomeList = experiment.LoadPopulation(xr); } if(_genomeList.Count == 0) { Console.WriteLine(string.Format("No genome loaded from file [{0}]", cmdArgs[1])); _genomeList = null; break;; } // Create genome list from seed. _genomeFactory = _genomeList[0].GenomeFactory; _genomeList = _genomeFactory.CreateGenomeList(populationSize, 0u, _genomeList[0]); Console.WriteLine(string.Format("Created [{0}] genomes from loaded seed genome.", _genomeList.Count)); break; } // Execution control commands. case "start": { if(null == _ea) { // Create new evolution algorithm. if(null == _genomeList) { Console.WriteLine("Error. No loaded genomes"); break; } _ea = experiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList); _ea.UpdateEvent += new EventHandler(ea_UpdateEvent); } Console.WriteLine("Starting..."); _ea.StartContinue(); break; } case "stop": { Console.WriteLine("Stopping. Please wait..."); _ea.RequestPauseAndWait(); Console.WriteLine("Stopped."); break; } case "reset": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot reset while algorithm is running."); break; } _ea = null; _genomeFactory = null; _genomeList = null; Console.WriteLine("Reset completed."); break; } // Genome saving commands. case "savepop": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot save population while algorithm is running."); break; } if(null == _genomeList) { Console.WriteLine("Error. No population to save."); break; } // Attempt to get population filename arg. if(cmdArgs.Length <= 1) { Console.WriteLine("Error. Missing {filename} argument."); break; } // Save genomes to xml file. XmlWriterSettings xwSettings = new XmlWriterSettings(); xwSettings.Indent = true; using(XmlWriter xw = XmlWriter.Create(cmdArgs[1], xwSettings)) { experiment.SavePopulation(xw, _genomeList); } Console.WriteLine(string.Format("[{0}] genomes saved to file [{1}]", _genomeList.Count, cmdArgs[1])); break; } case "savebest": { if(null != _ea && _ea.RunState == RunState.Running) { Console.WriteLine("Error. Cannot save population while algorithm is running."); break; } if(null == _ea || null == _ea.CurrentChampGenome) { Console.WriteLine("Error. No best genome to save."); break; } // Attempt to get genome filename arg. if(cmdArgs.Length <= 1) { Console.WriteLine("Error. Missing {filename} argument."); break; } // Save genome to xml file. XmlWriterSettings xwSettings = new XmlWriterSettings(); xwSettings.Indent = true; using(XmlWriter xw = XmlWriter.Create(cmdArgs[1], xwSettings)) { experiment.SavePopulation(xw, new NeatGenome[] {_ea.CurrentChampGenome}); } Console.WriteLine(string.Format("Best genome saved to file [{1}]", _genomeList.Count, cmdArgs[1])); break; } // Misc commands case "help": { WriteHelp(); break; } case "quit": case "exit": { Console.WriteLine("Stopping. Please wait..."); _ea.RequestPauseAndWait(); Console.WriteLine("Stopped."); goto quit; } case "": { // Do nothing. break; } default: { Console.WriteLine(string.Format("Unknown command [{0}]", cmdArgs[0])); break; } } } catch(Exception ex) { Console.WriteLine(string.Format("Exception [{0}]", ex.Message)); } } quit: Console.WriteLine("bye!"); }