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

📄 formmain.cs

📁 genetic algorithm source code, C# environment, and good, for beginners learning
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GATest
{
    public partial class FormMain : Form
    {
        private PointF[] m_drawPoints;
        private double m_xVal;
        Graphics m_g;
        public FormMain()
        {
            InitializeComponent();
            m_g = Graphics.FromHwnd(pictureBoxShow.Handle);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBoxStat.Clear();
            pictureBoxShow.Refresh();
            GeneticAlgorithm gaTest = new GeneticAlgorithm(-1, 2, double.Parse(textBoxCrossRate.Text), double.Parse(textBoxVarRate.Text), 6, int.Parse(textBoxPopulationNum.Text), pictureBoxShow.Width, pictureBoxShow.Height);
            int i = 10;
            int interations = 0;
            double x, y;
            do
            {
                gaTest.DoEvaluation();
                x = Math.Round(gaTest.WinValue, 6);
                y = Math.Round(gaTest.WinFitness, 6);
                string coding = GetCoding(gaTest.WinCoding);
                this.richTextBoxStat.AppendText(string.Format("当前迭代{0},\t编码:{3},\tx={1},y={2},\r\n", interations, x, y, coding));
                i = gaTest.IndivadualNumber();
                interations++;
            } while (i > 1 && interations < int.Parse(textBoxMax.Text));
            m_drawPoints = gaTest.DrawPoints;
            m_xVal = x;
            DrawLines();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawLines();
        }
        private string GetCoding(string[] tmp)
        {
            StringBuilder stb = new StringBuilder();
            for (int i = tmp.Length - 1; i >= 0; i--)
            {
                stb.Append(tmp[i]);
            }
            return stb.ToString();
        }
        private void DrawLines()
        {
            if (m_drawPoints != null && m_xVal > 0)
            {
                pictureBoxShow.Refresh();
                PointF[] points = m_drawPoints;
                double x = m_xVal;
                Pen myPen = new Pen(Color.Green, 1.0f);
                m_g.DrawLines(myPen, points);
                PointF[] valLine = new PointF[2];
                x = (x + 1.0) * pictureBoxShow.Width / 3.0;
                valLine[0].X = (float)x;
                valLine[0].Y = 0;
                valLine[1].X = (float)x;
                valLine[1].Y = (float)pictureBoxShow.Height;
                myPen.Color = Color.Red;
                myPen.Width = 2.0f;
                m_g.DrawLine(myPen, valLine[0], valLine[1]);
                m_g.Flush();
            }
        }
    }
}

⌨️ 快捷键说明

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