⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Generalization of a Simple Genetic Algorithm (GA)
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneticAlgorithmLib;

namespace GeneticAlgorithm
{
    public partial class Form1 : Form
    {
        #region Population
        private List<IGenotype> m_Population;
        public List<IGenotype> GetPopulation(GeneticAlgorithmBase GAB, int PopSize)
        {
            if (m_Population == null)
            {
                m_Population = GAB.CreatePopulation(PopSize);
            }
            return m_Population;
        }
        #endregion

        #region Winner
        private IGenotype m_Winner;
        public IGenotype Winner
        {
            get { return m_Winner; }
            set { m_Winner = value; }
        }
        #endregion

        #region Reset
        private void Reset()
        {
            this.m_Population = null;
            this.m_Winner = null;
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void cmdGo_Click(object sender, EventArgs e)
        {
            this.cmdGo.Enabled = false;
            try
            {
                int popSize = int.Parse(this.txtPopSize.Text);
                CardProblem cp = new CardProblem(
                    int.Parse(this.txtNumberOfCards.Text),
                    int.Parse(this.txtSumTarget.Text),
                    int.Parse(this.txtProdTarget.Text),
                    double.Parse(this.txtChanceOfInfection.Text),
                    double.Parse(this.txtChanceOfMutation.Text)
                    );
                List<IGenotype> p = cp.Tournament(this.GetPopulation(cp, popSize),
                    int.Parse(this.txtNumberOfIterations.Text));
                IGenotype currWinner = p[0];
                IGenotype oldWinner = this.Winner;
                if (this.Winner == null)
                {
                    this.Winner = currWinner;
                }
                else
                {
                    List<IGenotype> t = new List<IGenotype>();
                    t.Add(currWinner);
                    t.Add(Winner);
                    List<IGenotype> winners, losers;
                    cp.ChooseWinners(t, out winners, out losers);
                    this.Winner = winners[0];
                }
                if (this.Winner != oldWinner)
                {
                    this.cmdViewResults.Enabled = true;
                }
                else
                {
                    this.cmdViewResults.Enabled = false;
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.cmdGo.Enabled = true;
            }
        }

        private void cmdViewResults_Click(object sender, EventArgs e)
        {
            FResults f = new FResults();
            f.Genotype = this.Winner;
            f.Show(this);
            cmdViewResults.Enabled = false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -