/// <summary> /// This is specially useful since it is possible that startevolution will be called /// after stop evolution (because stopping is not instantaneous!) /// </summary> static void SaveDataIfRunning(string userName) { if (ActiveUsersList <NeatGenome> .IsUserRunning(userName)) { PopulationReadWrite.SavePopulation(userName); } }
public ActionResult Create([Bind(Include = "CandidateID, Name, UserName, UserSignature, UnparsedTags, Description," + "ChildrenList, Tags")] Candidate candidate, int candidateIndex) { string user = HttpContext.UserIdentity(); if (ModelState.IsValid && candidate.Name != null) { if (CheckIfUserExists(user)) { candidate.RemoveWhiteSpaces(); string folderName = user + "\\" + candidate.Name; EventsController.RaiseSaveCandidate(candidateIndex, folderName, user); candidate.ParentID = ActiveUsersList <NeatGenome> .UserParent(user); candidate.UserName = user; candidate.LinkNameAndPath(user); candidate.ParseTags(); db.Candidates.Add(candidate); UpdateParentsChildren(candidate.ParentID, candidate.CandidateID); db.SaveChanges(); return(RedirectToAction("ShowSavedCandidates")); } } return(View(candidate)); }
protected void Page_Load(object sender, EventArgs e) { ActiveUsers locActiveUsers = new ActiveUsers(); ActiveUsersList.DataSource = locActiveUsers.GetActiveUsers(); ActiveUsersList.DataBind(); }
static void StartAndRunEvolution(string userName) { StartEvolution(userName); while (ActiveUsersList <NeatGenome> .IsUserRunning(userName)) { } System.Diagnostics.Debug.WriteLine("User: "******". End evolution: " + DateTime.Now.ToString()); }
private bool CheckIfUserExists(string userName) { if (ActiveUsersList <NeatGenome> .ContainsUser(userName)) { return(true); } return(false); }
private static void CheckAndRegisterEvolAlg(string userName) { var evolutionAlgorithm = ActiveUsersList <NeatGenome> .EvolutionAlgorithmForUser(userName); if (evolutionAlgorithm != null) { LookForEvolutionAlgorithm(userName); } }
static void WhenPauseEvent(object sender, EvolutionEventsArgs arguments) { string userName = arguments.userName; // In this version we decide to stop the program at a pause event. // evolutionRunning = false here seems reiterative? (see TryStopEvolution) ActiveUsersList <NeatGenome> .SetRunningStatus(userName, false); SaveDataIfRunning(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); }
/// <summary> /// This process takes into account the posibility that the evolution algorithm has not been /// started yet. The user is pre-registered, and the save files are moved to the correct folder, /// so that THE new evolution process will automatically read them. /// </summary> private void PrepareAndRaiseBranch(int?id, Candidate candidate, string userName) { ActiveUsersList <NeatGenome> .AddParentToUserName(userName, id); ActiveUsersList <NeatGenome> .SetUserWaitingForAlgorithm(userName); ActiveUsersList <NeatGenome> .SetUserWaitingForVideos(userName); SharpNeat.PopulationReadWrite.MoveSavedFilesToCandidateFolder(candidate.FolderPath, userName); EventsController.RaiseBranchEvent(userName); }
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!"); }
static void TryLaunchEvolution(Action <string> launchDelegate, string userName) { if (!ActiveUsersList <NeatGenome> .ContainsUser(userName) || !ActiveUsersList <NeatGenome> .IsUserRunning(userName)) { LaunchActionInThread(launchDelegate, userName); } else { System.Diagnostics.Debug.WriteLine("Evolution is already running: skipping start."); } }
public ActionResult NextGeneration(int candidateIndex, bool isNormalMutations) { string userName = HttpContext.UserIdentity(); // The user is active: send a heartbeat HeartBeat(userName); string line = NextGenerationLineForLog(candidateIndex.ToString(), isNormalMutations); WriteLineForDebug(line, userName); ActiveUsersList <NeatGenome> .SetUserWaitingForVideos(userName); EventsController.RaiseNextGenerationEvent(candidateIndex, userName, isNormalMutations); return(RedirectToAction("WaitingForCandidateVideosDisplay")); }
static void StartOrRestartEvolution(string userName) { // If the user name is not registered, then it is a new process and we need to // create the evolution algorithm. if (!ActiveUsersList <NeatGenome> .DoesUserHaveEvolAlg(userName)) { System.Diagnostics.Debug.WriteLine("Starting evolution process..."); TryLaunchEvolution(LaunchEvolution, userName); } else { System.Diagnostics.Debug.WriteLine("Re-starting evolution process..."); TryLaunchEvolution(ReLaunchEvolution, userName); } }
public ActionResult StopEvolution() { string userName = HttpContext.UserIdentity(); if (CheckIfUserExists(userName)) { ActiveUsersList <NeatGenome> .SetUserWaitingForVideos(userName); EventsController.RaiseStopEvolutionEvent(userName); return(RedirectToAction("Index", "Home")); } else { return(RedirectToAction("Index")); } }
// Perhaps this code repetition can also be improved... private bool WaitForSetup() { int waitingLoops = 0; System.Diagnostics.Debug.WriteLine("user in wait for setup: " + userName); while (!ActiveUsersList <NeatGenome> .IsUsersEvolutionReady(userName)) { System.Threading.Thread.Sleep(100); ++waitingLoops; if (waitingLoops > 200) { System.Diagnostics.Debug.WriteLine("Waiting for evolution algorithm to be ready timed out!"); return(false); } } return(true); }
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); } }
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); } }
private bool WaitForCandidates() { int waitingLoops = 0; System.Diagnostics.Debug.WriteLine("user in wait for candidates: " + userName); while (ActiveUsersList <NeatGenome> .IsUserWaitingForVideos(userName)) { System.Threading.Thread.Sleep(100); ++waitingLoops; // Remember: each loop takes about 0.1 seconds (see Thread.Sleep just above) if (waitingLoops > 350) { System.Diagnostics.Debug.WriteLine("Waiting forcandidates to be ready timed out!"); return(false); } } return(true); }
/// <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); } } }
public static bool IsFreeEvolSlot(string userName) { // If the user is already active, it will use its own allocated spot if (ActiveUsersList <NeatGenome> .ContainsUser(userName)) { return(true); } // If not already active, it will need a new spot: ActiveUsersList <NeatGenome> .RemoveInactiveUsers(); // This wait is probably innecessary, but there have been errors where new users try to // take the place of others, causing trouble. System.Threading.Thread.Sleep(100); if (ActiveUsersList <NeatGenome> .Count() >= ActiveUsersList <NeatGenome> .MaxNumberOfUsers) { WriteLineForDebug("Requested a slot but the server was full.", userName); return(false); } return(true); }
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 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 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); }
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!"); } }
public ActionResult ResetEvolution() { string userName = HttpContext.UserIdentity(); if (CheckIfUserExists(userName)) { WriteLineForDebug("Reset evolution", userName); // The user is active: send a heartbeat HeartBeat(userName); // If we were branching from a saved genome, then the user had that // genome as the parent ID. Reseting evolution means this is no longer // the parent (there is none) so we set this ID to null ActiveUsersList <NeatGenome> .AddParentToUserName(userName, null); ActiveUsersList <NeatGenome> .SetUserWaitingForVideos(userName); EventsController.RaiseResetEvolutionEvent(userName); return(RedirectToAction("WaitingForEvolutionSetup")); } else { return(RedirectToAction("Index")); } }
static void SetEvolutionStatusRunning(string userName) { ActiveUsersList <NeatGenome> .SetRunningStatus(userName, true); System.Diagnostics.Debug.WriteLine("User: "******". Start evolution: " + DateTime.Now.ToString() + "\n"); }
void HeartBeat(string userName) { // Rest timer already checks for the user ActiveUsersList <NeatGenome> .ResetTimer(userName); }