Ejemplo n.º 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;
 }
 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;
 }
Ejemplo n.º 3
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (_cancellation == null)
            {
                _cancellation = new CancellationTokenSource();
                GeneticAlgorithmSettings settings = new GeneticAlgorithmSettings {
                    PopulationSize = Int32.Parse(txtMonkeysPerGeneration.Text)
                };

                txtBestMatch.BackColor   = SystemColors.Window;
                lblGenerations.BackColor = SystemColors.Control;
                lblGenPerSec.Text        = lblGenerations.Text = "-";
                lblElapsedTime.Text      = "0";
                btnRun.Text         = "Cancel";
                chkParallel.Visible = false;

                _startTime = _lastTime = DateTimeOffset.Now;
                timerElapsedTime.Start();

                // Run the work in the background
                _cancellation = new CancellationTokenSource();
                var  token       = _cancellation.Token;
                bool runParallel = chkParallel.Checked;
                Task.Factory.StartNew(() =>
                {
                    // Create the new genetic algorithm
                    var ga = new TextMatchGeneticAlgorithm(runParallel, _targetText, settings);
                    TextMatchGenome?bestGenome = null;

                    // Iterate until a solution is found or until cancellation is requested
                    for (_currentIteration = 1;; _currentIteration++)
                    {
                        token.ThrowIfCancellationRequested();

                        // 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;
                            _uiTasks.StartNew(() => txtBestMatch.Text = bestGenome.Value.Text);

                            // If we've found the solution, bail.
                            if (bestGenome.Value.Text == _targetText)
                            {
                                break;
                            }
                        }
                    }

                    // When the task completes, update the UI
                }, token).ContinueWith(t =>
                {
                    timerElapsedTime.Stop();
                    chkParallel.Visible = true;
                    btnRun.Text         = "Start";
                    _cancellation       = null;

                    switch (t.Status)
                    {
                    case TaskStatus.Faulted:
                        MessageBox.Show(this, t.Exception.ToString(), "Error");
                        break;

                    case TaskStatus.RanToCompletion:
                        txtBestMatch.BackColor   = Color.LightGreen;
                        lblGenerations.BackColor = Color.LemonChiffon;
                        break;
                    }
                }, _uiTasks.Scheduler);
            }
            else
            {
                _cancellation.Cancel();
            }
        }
Ejemplo n.º 4
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (_cancellation == null)
            {
                _cancellation = new CancellationTokenSource();
                GeneticAlgorithmSettings settings = new GeneticAlgorithmSettings { PopulationSize = Int32.Parse(txtMonkeysPerGeneration.Text) };

                txtBestMatch.BackColor = SystemColors.Window;
                lblGenerations.BackColor = SystemColors.Control;
                lblGenPerSec.Text = lblGenerations.Text = "-";
                lblElapsedTime.Text = "0";
                btnRun.Text = "Cancel";
                chkParallel.Visible = false;

                _startTime = _lastTime = DateTimeOffset.Now;
                timerElapsedTime.Start();

                // Run the work in the background
                _cancellation = new CancellationTokenSource();
                var token = _cancellation.Token;
                bool runParallel = chkParallel.Checked;
                Task.Factory.StartNew(() =>
                {
                    // Create the new genetic algorithm
                    var ga = new TextMatchGeneticAlgorithm(runParallel, _targetText, settings);
                    TextMatchGenome? bestGenome = null;

                    // Iterate until a solution is found or until cancellation is requested
                    for (_currentIteration = 1; ; _currentIteration++)
                    {
                        token.ThrowIfCancellationRequested();

                        // 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;
                            _uiTasks.StartNew(() => txtBestMatch.Text = bestGenome.Value.Text);

                            // If we've found the solution, bail.
                            if (bestGenome.Value.Text == _targetText) break;
                        }
                    }

                    // When the task completes, update the UI
                }, token).ContinueWith(t =>
                {
                    timerElapsedTime.Stop();
                    chkParallel.Visible = true;
                    btnRun.Text = "Start";
                    _cancellation = null;

                    switch (t.Status)
                    {
                        case TaskStatus.Faulted:
                            MessageBox.Show(this, t.Exception.ToString(), "Error");
                            break;
                        case TaskStatus.RanToCompletion:
                            txtBestMatch.BackColor = Color.LightGreen;
                            lblGenerations.BackColor = Color.LemonChiffon;
                            break;
                    }
                }, _uiTasks.Scheduler);
            }
            else _cancellation.Cancel();
        }