Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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();
        }