static void ea_UpdateEvent(object sender, EventArgs e) { Console.WriteLine(string.Format("gen={0:N0} bestFitness={1:N6}", _ea.CurrentGeneration, _ea.Statistics._maxFitness)); // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete(new List<NeatGenome>() { _ea.CurrentChampGenome }, false); doc.Save(CHAMPION_FILE); }
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(); }
private static void CreateXmlNetworkFile(NeatEvolutionAlgorithm <NeatGenome> _ea) { var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { _ea.CurrentChampGenome }, false); doc.Save($"{NeatConsts.experimentName}/best.xml"); }
public static void SaveCurrentGenome(string username, string foldername, NeatGenome genome) { List <NeatGenome> list = new List <NeatGenome>(); list.Add(genome); string path = resultsPath + "/" + username + "/" + foldername + "/genome.xml"; var doc = NeatGenomeXmlIO.SaveComplete(list, false); doc.Save(path); }
private void saveChampion(IList <NeatGenome> genomeList) { var champ = genomeList.ArgMax(g => g.EvaluationInfo.Fitness); var champDoc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { champ }, true); champDoc.Save(string.Format(CHAMPIONS_FORMAT, _generations)); }
private void writeGenomes(IList <NeatGenome> genomeList) { for (int i = 0; i < genomeList.Count; i++) { var genome = genomeList[i]; var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { genome }, true); doc.Save(string.Format(GENOMES_FORMAT, i)); } }
static void SaveChampion(string folderPath, string userName) { // We use this seemingly unnecessary method (TryGetChampion) because // simultaneous users have triggered exceptions here! List <NeatGenome> onlyTheChamp = TryGetChampion(userName); if (onlyTheChamp != null) { var doc = NeatGenomeXmlIO.SaveComplete(onlyTheChamp, false); doc.Save(ChampionDefaultPath(folderPath)); } }
/// <summary> /// This method is called at the end of every generation and logs the progress of the EA. /// </summary> static void logEvolutionProgress(object sender, EventArgs e) { //Console.WriteLine("Number of genomes at the start of logEvol: " + _ea.GenomeList.Count); var maxFitness = _ea.GenomeList.Max(g => g.EvaluationInfo.Fitness); var maxAltFitness = _ea.GenomeList.Max(g => g.EvaluationInfo.AlternativeFitness); var genChampion = _ea.GenomeList.First(g => g.EvaluationInfo.Fitness == maxFitness); // Write the results to file. using (TextWriter writer = new StreamWriter(_generationalResultsFile, true)) { Console.WriteLine("Gen {0}: {1} ({2}) Total: {3}", _ea.CurrentGeneration, maxFitness, maxAltFitness, _experiment.Evaluator.FoundPasswords.Count); lock (_experiment.Evaluator.FoundPasswords) { Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}", _ea.CurrentGeneration, maxFitness, maxAltFitness, _ea.GenomeList.Average(g => g.EvaluationInfo.Fitness), _ea.GenomeList.Average(g => g.EvaluationInfo.AlternativeFitness), _experiment.Evaluator.FoundPasswords.Sum(s => PasswordCrackingEvaluator.Passwords[s].Accounts), _experiment.Evaluator.FoundPasswords.Count); writer.WriteLine("{0},{1},{2},{3},{4},{5},{6}", _ea.CurrentGeneration, maxFitness, maxAltFitness, _ea.GenomeList.Average(g => g.EvaluationInfo.Fitness), _ea.GenomeList.Average(g => g.EvaluationInfo.AlternativeFitness), _experiment.Evaluator.FoundPasswords.Sum(s => PasswordCrackingEvaluator.Passwords[s].Accounts), _experiment.Evaluator.FoundPasswords.Count); } Console.WriteLine("Done."); } if (_gens <= MAX_GENERATIONS) { // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { genChampion }, true); doc.Save(CHAMPION_FILE_ROOT + "_gen_" + _gens + ".xml"); } // If we've reached the maximum number of generations, // tell the algorithm to stop. if (_gens >= MAX_GENERATIONS) { _ea.Stop(); } _gens++; }
static void ea_UpdateEvent(object sender, EventArgs e) { Console.WriteLine(string.Format("gen={0:N0} bestFitness={1:N6}", _ea.CurrentGeneration, _ea.Statistics._maxFitness)); // Save the best genome to file // Note: Unlike the other experiments, you must save // function IDs of HyperNEAT genomes. var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { _ea.CurrentChampGenome }, true); doc.Save(CHAMPION_FILE); }
void ea_UpdateEvent(object sender, EventArgs e) { Console.WriteLine(string.Format("gen={0:N0} bestFitness={1:N6}", _ea.CurrentGeneration, _ea.Statistics._maxFitness)); this.BeginInvoke(new incDel(incrementGens)); //MessageBox.Show("New Gen! Best individual: " + _ea.CurrentChampGenome.EvaluationInfo.AlternativeFitness); // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { _ea.CurrentChampGenome }, false); doc.Save(CHAMPION_FILE); }
static void ea_UpdateEvent(object sender, EventArgs e) { /* * using (System.IO.StreamWriter file = new System.IO.StreamWriter(Current_Genomes, true)) //@"C:\Users\Sabre\Desktop\SHARPNEAT\RoboCupSharp\Sources\KeepAway\bin\Debug\Current_Genomes.csv", true)) * { * file.WriteLine(string.Format("{0} , {1:f6}", _ea.CurrentGeneration, _ea.CurrentChampGenome.EvaluationInfo.Fitness)); * } */ List <NeatGenome> pop = new List <NeatGenome>(_ea.GenomeList); var pops = NeatGenomeXmlIO.SaveComplete(pop, false); pops.Save(Pop_File + _ea.CurrentGeneration + ".xml"); // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { _ea.CurrentChampGenome }, false); doc.Save(CHAMPION_FILE + _ea.CurrentGeneration + ".xml"); }
static void SavePopulation(string folderPath, string userName) { // T try block is an extra precaution. Problems always appear in SaveChampion, // but the same principle could work here... NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = null; try { evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); } catch (NullReferenceException ex) { string errorLine = "User " + userName + " encountered an exception during SavePopulation in PopulationReadWrite: " + ex.Message; WriteErrorForDebug(errorLine); return; } if (evolutionAlgorithm != null) { var doc = NeatGenomeXmlIO.SaveComplete(evolutionAlgorithm.GenomeList, false); doc.Save(PopulationDefaultPath(folderPath)); } }
static void ea_UpdateEvent(object sender, EventArgs e) { // set the isWriting flag _isWriting = true; //Console.WriteLine(string.Format("gen={0:N0} bestFitnessX={1:N6} bestFitnessO={2:N6}\n\t bestFitnessX={3:N6} bestFitnessO={4:N6}", // _ea[0].CurrentGeneration, _ea[0].Statistics._maxFitness, _ea[1].Statistics._maxFitness, _ea[2].Statistics._maxFitness, _ea[3].Statistics._maxFitness)); // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete( new List <NeatGenome>() { _ea[0].CurrentChampGenome, _ea[1].CurrentChampGenome, _ea[2].CurrentChampGenome, _ea[3].CurrentChampGenome }, false); doc.Save(CHAMPION_FILE); // finished writing, unset flag _isWriting = false; }
/// <summary> /// Method for performing evolutions /// </summary> /// <param name="id">Indicates if its a new evolution or not. Id of chosen individual by CIEC</param> /// <param name="fitness">Fitness given by user input for the chosen individual</param> /// <param name="novelty">Indicates whether or not to perform novelty search</param> public ActionResult Evolve(int id = -1, int fitness = 0) { // Get username for this evolution string username = HttpContext.Session["userId"].ToString().ToString();//Request.UserHostAddress; // Prepares next evolution view List <Evolution> evolutions = new List <Evolution>(); // Get client pool used for running the evaluations MalmoClientPool clientPool = Global.GloabalVariables.MalmoClientPool; // Initialize the experiment and the evaluator object (MinecraftSimpleEvaluator) MinecraftBuilderExperiment experiment; if (fitness < 0) { experiment = new MinecraftBuilderExperiment(clientPool, "Novelty", username); } else { experiment = new MinecraftBuilderExperiment(clientPool, "Simple", username); } XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(System.AppDomain.CurrentDomain.BaseDirectory + "minecraft.config.xml"); experiment.Initialize("Minecraft", xmlConfig.DocumentElement); // The evolutionary algorithm object NeatEvolutionAlgorithm <NeatGenome> algorithm; if (id != -1) { if (fitness < 0) { //Novelty //Initialize experiment experiment.Initialize("Novelty", xmlConfig.DocumentElement); //load population, choose selected as parent and create offsprings var reader = XmlReader.Create(FileUtility.GetUserResultPath(username) + "Population.xml"); var list = experiment.LoadPopulation(reader); var parent = list[id]; List <NeatGenome> offSprings = new List <NeatGenome>(); offSprings.Add(parent); while (offSprings.Count != GloabalVariables.POPULATION_SIZE) { offSprings.Add(parent.CreateOffspring(parent.BirthGeneration)); } //save fitness of current generation double maxFitness = -1; for (int i = 0; i < GloabalVariables.POPULATION_SIZE; i++) { string folderName = i.ToString(); double currentStructureFitness = FileUtility.GetStructureFitness(username, folderName); if (currentStructureFitness > maxFitness) { maxFitness = currentStructureFitness; } } FileUtility.SaveMaxFitness(username, maxFitness); reader.Close(); // add novel structure to archive FileUtility.SaveNovelStructure(username, id.ToString()); //save chosen to parent FileUtility.CopyCanditateToParentFolder(username, id.ToString()); // Initialize algorithm object using the current generation algorithm = experiment.CreateEvolutionAlgorithm(offSprings[0].GenomeFactory, offSprings); // Continue evolution after first generation if stop condition hasnt been met if (!algorithm.StopConditionSatisfied) { algorithm.StartContinue(); } while (algorithm.RunState != RunState.Paused && algorithm.RunState != RunState.Ready) { Thread.Sleep(100); } // save separate genomes to population.xml var tempList = new List <NeatGenome>(); for (int i = 0; i < algorithm.GenomeList.Count; i++) { reader = XmlReader.Create(FileUtility.GetUserResultPath(username) + i.ToString() + "/" + "genome.xml"); list = experiment.LoadPopulation(reader); tempList.Add(list[0]); reader.Close(); } var doc = NeatGenomeXmlIO.SaveComplete(tempList, false); doc.Save(FileUtility.GetUserResultPath(username) + "Population.xml"); // Save population after evaluating the generation string action = "2"; string sequence = HttpContext.Session["sequence"].ToString(); HttpContext.Session.Add("sequence", sequence + action); for (int i = 0; i < algorithm.GenomeList.Count; i++) { string folderName = i.ToString(); string videoPath = ""; if (i == 0) { videoPath = FileUtility.GetVideoPathWithoutDecoding(username, folderName); } else { videoPath = FileUtility.DecodeArchiveAndGetVideoPath(username, folderName); } Evolution evolution = new Evolution() { ID = i, DirectoryPath = FileUtility.GetUserResultVideoPath(username, folderName), BranchID = i, Username = HttpContext.Session["userId"].ToString() }; evolutions.Add(evolution); } } else { //IEC if (fitness == 1) { experiment.Initialize("Small mutation", xmlConfig.DocumentElement); } else if (fitness == 2) { experiment.Initialize("Big mutation", xmlConfig.DocumentElement); } // read current population and set fitness of the chosen genome var reader = XmlReader.Create(FileUtility.GetUserResultPath(username) + "Population.xml"); var list = experiment.LoadPopulation(reader); foreach (var genome in list) { genome.EvaluationInfo.SetFitness(0); } list[id].EvaluationInfo.SetFitness(fitness); reader.Close(); // add novel structure to archive FileUtility.SaveNovelStructure(username, id.ToString()); // Initialize algorithm object using the current generation algorithm = experiment.CreateEvolutionAlgorithm(list[0].GenomeFactory, list); //save fitness of current generation double maxFitness = -1; for (int i = 0; i < algorithm.GenomeList.Count; i++) { string folderName = i.ToString(); double currentStructureFitness = FileUtility.GetStructureFitness(username, folderName); if (currentStructureFitness > maxFitness) { maxFitness = currentStructureFitness; } } FileUtility.SaveMaxFitness(username, maxFitness); // Copy video files of the generation champion into the parent folder and delete the other // folders to allow for new candidate videos int indexOfChamp = 0; foreach (var genome in list) { if (genome.Id == algorithm.CurrentChampGenome.Id && algorithm.CurrentChampGenome.Id != 0) { FileUtility.CopyCanditateToParentFolder(username, indexOfChamp.ToString()); } indexOfChamp++; } // Perform evaluation of a generation. Pause shortly after to ensure that the algorithm only // evaluates one generation algorithm.StartContinue(); Thread.Sleep(5000); algorithm.RequestPause(); // Wait for the evaluation of the generation to be done while (algorithm.RunState != RunState.Paused) { Thread.Sleep(100); } // Save population after evaluating the generation var doc = NeatGenomeXmlIO.SaveComplete(algorithm.GenomeList, false); doc.Save(FileUtility.GetUserResultPath(username) + "Population.xml"); string action = ""; if (fitness == 1) { action = "0"; } else { action = "1"; } string sequence = HttpContext.Session["sequence"].ToString(); HttpContext.Session.Add("sequence", sequence + action); for (int i = 0; i < algorithm.GenomeList.Count; i++) { string folderName = i.ToString(); string videoPath = ""; if (i == 0) { videoPath = FileUtility.GetVideoPathWithoutDecoding(username, folderName); } else { videoPath = FileUtility.DecodeArchiveAndGetVideoPath(username, folderName); } Evolution evolution = new Evolution() { ID = i, DirectoryPath = FileUtility.GetUserResultVideoPath(username, folderName), BranchID = i, Username = HttpContext.Session["userId"].ToString() }; evolutions.Add(evolution); FileUtility.SaveCurrentGenome(username, i.ToString(), algorithm.GenomeList[i]); } } } else { // Perform new evolution experiment.Initialize("Minecraft", xmlConfig.DocumentElement); // Create folders for the user FileUtility.CreateUserFolder(username); // Create a new evolution algorithm object with an initial generation algorithm = experiment.CreateEvolutionAlgorithm(); // Save population after evaluating the generation var doc = NeatGenomeXmlIO.SaveComplete(algorithm.GenomeList, false); doc.Save(FileUtility.GetUserResultPath(username) + "Population.xml"); for (int i = 0; i < algorithm.GenomeList.Count; i++) { string folderName = i.ToString(); string videoPath = FileUtility.DecodeArchiveAndGetVideoPath(username, folderName); Evolution evolution = new Evolution() { ID = i, DirectoryPath = FileUtility.GetUserResultVideoPath(username, folderName), BranchID = -1, Sequence = "" }; evolutions.Add(evolution); FileUtility.SaveCurrentGenome(username, i.ToString(), algorithm.GenomeList[i]); } HttpContext.Session.Add("branchId", "-1"); return(View("FirstEvolution", evolutions)); } return(View(evolutions)); }
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); }
/// <summary> /// This method is called at the end of every generation and logs the progress of the EA. /// </summary> public static void logEvolutionProgress(object sender, EventArgs e) { var maxFitness = _ea.GenomeList.Max(g => g.EvaluationInfo.Fitness); var maxAltFitness = _ea.GenomeList.Max(g => g.EvaluationInfo.AlternativeFitness); var genChampion = _ea.GenomeList.First(g => g.EvaluationInfo.Fitness == maxFitness); // Write the results to file. // using (TextWriter writer = new StreamWriter(cp.ResultsFile, true)) // { // Console.WriteLine("Gen {0}: {1} ({2}) Total: {3}", _ea.CurrentGeneration, // maxFitness, // maxAltFitness, // ce.Found.Count); // long sum = 0; // foreach(var s in ce.Found) // { // Console.WriteLine(s); // try{ // Console.WriteLine("\t{0}", PasswordCrackingEvaluator.Passwords == null ? "NULL" : "NOT NULL"); // Console.WriteLine("\t{0}", PasswordCrackingEvaluator.Passwords.Count); // Console.WriteLine("\t{0}", PasswordCrackingEvaluator.Passwords[s] == null ? "NULL" : "NOT NULL"); // Console.WriteLine("\t{0}", PasswordCrackingEvaluator.Passwords[s].Accounts); // }catch(Exception ex){ // Console.WriteLine(ex.Message); // } // sum += PasswordCrackingEvaluator.Passwords[s].Accounts; // Console.WriteLine("\t{0}", sum); // } // Console.WriteLine("Done with that."); // Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}", _ea.CurrentGeneration, // maxFitness, // maxAltFitness, // _ea.GenomeList.Average(g => g.EvaluationInfo.Fitness), // _ea.GenomeList.Average(g => g.EvaluationInfo.AlternativeFitness), // ce.Found.Sum(s => PasswordCrackingEvaluator.Passwords[s].Accounts), // ce.Found.Count // ); // writer.WriteLine("{0},{1},{2},{3},{4},{5},{6}", _ea.CurrentGeneration, // maxFitness, // maxAltFitness, // _ea.GenomeList.Average(g => g.EvaluationInfo.Fitness), // _ea.GenomeList.Average(g => g.EvaluationInfo.AlternativeFitness), // ce.Found.Sum(s => PasswordCrackingEvaluator.Passwords[s].Accounts), // ce.Found.Count); // } if (_gens <= cp.Generations) { // Save the best genome to file var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>() { genChampion }, true); doc.Save(cp.ChampionFilePath + "_gen_" + _gens + ".xml"); } // If we've reached the maximum number of generations, // tell the algorithm to stop. if (_gens >= cp.Generations) { _ea.Stop(); } _gens++; }
/// <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; } }