Example #1
0
 public TextMatchGeneticAlgorithm(bool runParallel, string targetText, GeneticAlgorithmSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException("settings");
     }
     if (targetText == null)
     {
         throw new ArgumentNullException("targetText");
     }
     _runParallel = runParallel;
     _settings    = settings;
     _targetText  = targetText;
 }
Example #2
0
        public void StartTyping(string targetText, bool parallel)
        {
            var settings = new GeneticAlgorithmSettings {
                PopulationSize = 200
            };
            var token = cancellation.Token;


            currentTask = currentTask.ContinueWith((previous) =>
            {
                // Create the new genetic algorithm
                var ga = new TextMatchGeneticAlgorithm(parallel, targetText, settings);
                TextMatchGenome?bestGenome = null;
                DateTime startedAt         = DateTime.Now;

                Hub.GetClients <MonkeyHub>().started(targetText);

                // Iterate until a solution is found or until cancellation is requested
                for (int generation = 1; ; generation++)
                {
                    if (token.IsCancellationRequested)
                    {
                        Hub.GetClients <MonkeyHub>().cancelled();
                        break;
                    }

                    // Move to the next generation
                    ga.MoveNext();

                    // If we've found the best solution thus far, update the UI
                    if (bestGenome == null ||
                        ga.CurrentBest.Fitness < bestGenome.Value.Fitness)
                    {
                        bestGenome = ga.CurrentBest;

                        int generationsPerSecond = generation / Math.Max(1, (int)((DateTime.Now - startedAt).TotalSeconds));
                        Hub.GetClients <MonkeyHub>().updateProgress(bestGenome.Value.Text, generation, generationsPerSecond);

                        if (bestGenome.Value.Fitness == 0)
                        {
                            Hub.GetClients <MonkeyHub>().complete();
                            break;
                        }
                    }
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
Example #3
0
        public void StartTyping(string targetText, bool parallel)
        {
            var settings = new GeneticAlgorithmSettings { PopulationSize = 200 };
            var token = cancellation.Token;

            currentTask = currentTask.ContinueWith((previous) =>
            {
                // Create the new genetic algorithm
                var ga = new TextMatchGeneticAlgorithm(parallel, targetText, settings);
                TextMatchGenome? bestGenome = null;
                DateTime startedAt = DateTime.Now;

                Hub.GetClients<MonkeyHub>().started(targetText);

                // Iterate until a solution is found or until cancellation is requested
                for (int generation = 1; ; generation++)
                {
                    if (token.IsCancellationRequested)
                    {
                        Hub.GetClients<MonkeyHub>().cancelled();
                        break;
                    }

                    // Move to the next generation
                    ga.MoveNext();

                    // If we've found the best solution thus far, update the UI
                    if (bestGenome == null ||
                        ga.CurrentBest.Fitness < bestGenome.Value.Fitness)
                    {
                        bestGenome = ga.CurrentBest;

                        int generationsPerSecond = generation / Math.Max(1, (int)((DateTime.Now - startedAt).TotalSeconds));
                        Hub.GetClients<MonkeyHub>().updateProgress(bestGenome.Value.Text, generation, generationsPerSecond);

                        if (bestGenome.Value.Fitness == 0)
                        {
                            Hub.GetClients<MonkeyHub>().complete();
                            break;
                        }
                    }
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
 public TextMatchGeneticAlgorithm(bool runParallel, string targetText, GeneticAlgorithmSettings settings)
 {
     if (settings == null) throw new ArgumentNullException("settings");
     if (targetText == null) throw new ArgumentNullException("targetText");
     _runParallel = runParallel;
     _settings = settings;
     _targetText = targetText;
 }