private static void CheckAndRegisterEvolAlg(string userName) { var evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); if (evolutionAlgorithm != null) { LookForEvolutionAlgorithm(userName); } }
/// <summary> /// Checks the module ID of the last neuron in one (the first) genome of /// the population. If (and only if) this is "0" this means the genome /// is not complete, and so a new (first) module is needed. /// </summary> static bool CheckNewModuleNeeded(string userName) { NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); int latestModuleID; int lastNeuronIndex = evolutionAlgorithm.GenomeList[0].NeuronGeneList.Count - 1; latestModuleID = evolutionAlgorithm.GenomeList[0].NeuronGeneList[lastNeuronIndex].ModuleId; return(latestModuleID == 0); }
static void StartEvolution(string userName) { NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); evolutionAlgorithm.MakeEvolutionReady(); System.Diagnostics.Debug.WriteLine("\nEvolution ready, starting iterations"); SaveDataIfRunning(userName); if (ActiveUsersList <NeatGenome> .IsUserRunning(userName)) { evolutionAlgorithm.StartContinue(); } }
/// <summary> /// Newly created genomes will only have an empty "carcass" with some /// basic elements (input and output connections and a regulatoty neuron). /// We need to create a module with all the internal connections. The most /// basic module will include "local input" and "local output" neurons as /// interface between all inputs and outputs (in the future it will be /// possible to connect only to a subset of these). /// /// This step is not necessary if an old (complete) population has been /// loaded. /// </summary> static void AddFirstModuleIfNeeded(string userName) { if (CheckNewModuleNeeded(userName)) { NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); // If a first module is needed, then all genomes are equal (yet). // We ensure the champion is a valid genome: evolutionAlgorithm.CurrentChampGenome = evolutionAlgorithm.GenomeList[0]; evolutionAlgorithm.AddFirstModule(); } }
static void ReInitialize(string userName) { MyExperiment currentExperiment = CreateNewExperiment(userName); var myEvolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); string populationPath = PopulationReadWrite.PopulationPathForUser(userName); currentExperiment.ReInitializeEvolution(myEvolutionAlgorithm, populationPath); SaveDataIfRunning(userName); AddFirstModuleIfNeeded(userName); System.Diagnostics.Debug.WriteLine("added module!"); }
private void UpdateCandidatePaths(string userName) { // With the user name we find the EvolutionAlgorithm in ActiveUsersList, we access // GenomeScreenOrder in the evolution alg. and there we get the IndexToName dictionary. // The user existence has just been confirmed Dictionary <int, string> indexToName = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName).GenomeScreenOrder.IndexToName; for (int index = 0; index < candidates.Count; ++index) { string candidateName = ""; if (indexToName.ContainsKey(index)) { candidateName = indexToName[index]; } candidates[index].CreatePathsForEvolutionCandidate(userName, candidateName); } }
/// <summary> /// Evolution algorithm is not a static class. This means we cannot subscribe it to the /// event handler at the constructor. We cannot do it either right after raising the "start evolution" /// event, since it takes a while to take effect. This method is used before trying to raise /// the "next generation" event. If the evolution algorithm is foundm, it is subscribed. /// </summary> private static void LookForEvolutionAlgorithm(string userName) { if (!startedUsers.Contains(userName)) { //ActiveUsersList<NeatGenome>.ListUsers(); if (ActiveUsersList <NeatGenome> .ContainsUser(userName)) { var evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); startedUsers.Add(userName); NextGenerationEvent += new EventHandler <EvolutionEventsArgs>(evolutionAlgorithm.ProceedNextGeneration); SaveCandidate += new EventHandler <EvolutionEventsArgs>(evolutionAlgorithm.SaveGenomeAndExit); ResetEvolutionEvent += new EventHandler <EvolutionEventsArgs>(EvolutionCoordination.OnResetEvolution); StopEvolutionEvent += new EventHandler <EvolutionEventsArgs>(EvolutionCoordination.OnStopEvolution); BranchEvent += new EventHandler <EvolutionEventsArgs>(EvolutionCoordination.OnBranch); } } }
static public void DessubscribeEventsFromUser(string userName) { NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = null; try { evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Null algorithm at DessubscribeEventsFromUser in EvolutionCoordination: " + ex.Message); } if (evolutionAlgorithm != null) { evolutionAlgorithm.UpdateEvent -= new EventHandler <EvolutionEventsArgs>(WhenUpdateEvent); evolutionAlgorithm.PausedEvent -= new EventHandler <EvolutionEventsArgs>(WhenPauseEvent); } }
static void WhenUpdateEvent(object sender, EvolutionEventsArgs arguments) { string userName = arguments.userName; NeatEvolutionAlgorithm <NeatGenome> evolutionAlgorithm = null; try { evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Null algorithm at WhenUpdateEvent in EvolutionCoordination: " + ex.Message); } if (evolutionAlgorithm != null) { System.Diagnostics.Debug.WriteLine(string.Format("gen={0:N0} bestFitness={1:N6}", evolutionAlgorithm.CurrentGeneration, evolutionAlgorithm.Statistics._maxFitness)); SaveDataIfRunning(userName); } }
static List <NeatGenome> TryGetChampion(string userName) { List <NeatGenome> genome = null; try { // The exception comes from old users trying to access their evolution algorithm when they // have been removed from the ActiveUsersList! var evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); genome = new List <NeatGenome>() { evolutionAlgorithm.CurrentChampGenome }; } catch (NullReferenceException ex) { string errorLine = "User " + userName + " encountered an exception during TryGetChampion in PopulationReadWrite: " + ex.Message; WriteErrorForDebug(errorLine); } return(genome); }
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)); } }
private static void TryStopEvolution(string userName) { // We should not be able to ask to stop a process that has not started! // (And thus we ALWAYS expect this check to be true) if (ActiveUsersList <NeatGenome> .ContainsUser(userName)) { if (ActiveUsersList <NeatGenome> .IsUserRunning(userName)) { System.Diagnostics.Debug.WriteLine("Stopping evolution process..."); ActiveUsersList <NeatGenome> .SetRunningStatus(userName, false); ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName).StopEvolution(); } else { System.Diagnostics.Debug.WriteLine("Evolution is already stopped: skipping stop."); } } else { System.Diagnostics.Debug.WriteLine("Requested to stop " + userName + " but it was not found in the dictionary!"); } }