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

📄 form1.cs

📁 一个使用免疫算法实现物流调度的源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using immune_algorithm.wlddDataSetTableAdapters;


namespace immune_algorithm
{
    public partial class Form1 : Form
    {
        public int truckNum;
        public string coalCategoryName;
        public string projectName; 
        public int GarageNum;
        public Array arrayBack;
        public ArrayListClone listBack;
        public ArrayList list= new ArrayList();//存取节点的初始化值
        public static Array array;////转化节点的初始化值存进array里
        public ArrayList pathList = new ArrayList();//一个抗体路径节点顺序
        public ArrayList generateList = new ArrayList();//存储一代抗体*(50)
        public ArrayList nextGenerateList = new ArrayList();//存储下一代抗体(50)
        public int   goodsWeight = 0;//发货的车载货物重量
        public int carFromGarageNum ;//记录正在发货的车场号
        public int Q = 20;
        public Graphics g = null;
        public int currentCity;//代表在array中的下标
        public ArrayList listAllLinks;
        public int pathlength = 0;
        public Random rand=new Random(unchecked((int)DateTime.Now.Ticks));
        public Classcity cityflag = new Classcity();//用来在pathlist中标志从站点开始
        public generateResult shortestResult;
        public Guid  projectId;
        public static Array GetArray()//在form中传递array值
        { 
            return array;
        }

        public Form1()
        {
            InitializeComponent();         
        }
        public Form1(Guid projectId,string projectName,string coalCategoryName,int truckNum)
        {
            InitializeComponent();
            this.projectId = projectId;
            this.projectName = projectName;
            this.coalCategoryName = coalCategoryName;
            this.truckNum = truckNum;     
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);        
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.label1.Text = "【项目名称:" + this.projectName + " —> 煤运种类:" + this.coalCategoryName.Trim() + " —> 车辆数:" + this.truckNum + "】";
            this.projectCmb.Text = "3000";
            getCitysOrientValue();
            list = (ArrayList)listBack.Clone();
                  
            for(int i=0;i<list.Count;i++)
            {
                if (((Classcity)list[i]).flag == 0)
                {
                     this.GarageNum = i;
                     break;
                }                   
            }
            this.carFromGarageNum = this.GarageNum;
            
            for (int i = 0; i < 100; i++)//随机获取100个抗体
            {
                list = (ArrayList)listBack.Clone();              
                array = list.ToArray();    
                getCityLink(); 
                Generate();     //进行一次迭代,获得一个抗体解
                generateResult result = new generateResult();
                result.length = this.pathlength;//计算出每次路径的长度
                result.list = this.pathList;
                this.generateList.Add(result);
                this.pathList = new ArrayList();
                this.list = new ArrayList();
                this.pathlength = 0;          
            }
            System.Console.Out.WriteLine("第一代抗体中的最佳路径。。。。。。。。。。。。。。。。。。。。。。");          
            generateResult shortResult= selectAntigen();//对generateList按照 result.length 进行排序;
            System.Console.Out.WriteLine(shortResult.length+"length");
            ArrayList shortList = shortResult.list;
           
        }

        public void variance(ArrayList affinityList)//抗体变异
        {
            Array arrayGenerate = generateList.ToArray();
            for (int i = 0; i < arrayGenerate.Length; i++)
            {

            }
        }

        public generateResult selectAntigen()//选取目标函数最小的可行解作为抗原
        {
            IComparer compare = new myLengthCompare();
            generateList.Sort(compare);
            Array arrayGenerate = generateList.ToArray();
            generateResult result  = (generateResult)arrayGenerate.GetValue(0);          
            return result;

        }

        public ArrayList CalmulicateAffinity(ArrayList DesenessList,generateResult GenerateResult)//计算所有抗体的亲和力
        {
            ArrayList affinityList = new ArrayList();
            Array arrayDenseness = DesenessList.ToArray();
            double allLength = 0;
            for (int i = 0; i < arrayDenseness.Length; i++)
            {
                allLength = allLength + (double)arrayDenseness.GetValue(i);
            }
            for (int i = 0; i < arrayDenseness.Length; i++)//转换成计算亲和力需要的抗体浓度
            { 
                double denseness =allLength/((double)arrayDenseness.GetValue(i)*arrayDenseness.Length);
                arrayDenseness.SetValue(denseness,i);
            }

            Array generateArray = this.generateList.ToArray();
            for (int i = 0; i < arrayDenseness.Length; i++)
            {
                double affinity = 1 / (1 + Math.Abs(((generateResult)generateArray.GetValue(i)).length - GenerateResult.length)) * Math.Exp(-(double)arrayDenseness.GetValue(i));
                affinityList.Add(affinity);
            }
            return affinityList;
        }

        public ArrayList CalculateDenseness()//计算抗体的浓度,注意值越小浓度越大
        {
            Array array = generateList.ToArray();
            ArrayList DesenessList = new ArrayList();

            for (int i = 0; i < array.Length; i++)
            {
                generateResult result = (generateResult)array.GetValue(i);
                double length=0;
                for (int j = 0; j < array.Length; j++)
                {
                    length=length + Math.Pow((result.length - ((generateResult)array.GetValue(j)).length), 2);//此值越小,说明抗体的浓度越大                    
                }
                DesenessList.Add(length);
            }               
            return DesenessList;
        }

        public void ArrayListToArray()
        {
            array = list.ToArray();
        }

        public void begin()//获取虚拟车场
        {
            Node nodenew = new Node();
            {           
                nodenew.x = ((Node)array.GetValue(0)).x;
                nodenew.y = ((Node)array.GetValue(0)).y;
                nodenew.value = ((Node)array.GetValue(0)).value;
            }

            pathList.Add(new ClassPathNode((Classcity)array.GetValue(0),0));       
        }

        public int GetGarageNum()//随机获取车场节点
        {
            int i = -1;
            int p1 = 1;
            int p2 = 4;
            i = GetRand(p1, p2);
            return i;
        }

        public int GetMessionNum()//随机获取任务节点
        {
            int i = -1;
            int p1 = 1;
            int p2 = GetCurrentLinkCout();//获取当前的节点所连接的城市数         
            i = GetRand(p1,p2);          
            return i;
        }

        private int GetRand(int p1, int p2)
        {
         int i = this.rand.Next(p1,p2);
            return i;           
        }

        
        public void calculateNodeValue(int nodeNum)//每经过一个节点时计算节点的货物需求量                                                //和汽车的货物剩余数
        {
            Classcity city = (Classcity)array.GetValue(nodeNum);
            int loadValue;
            if (this.goodsWeight >= city.needWeight)
            {
                this.goodsWeight = this.goodsWeight - city.needWeight;
                loadValue=city.needWeight;
                city.needWeight = 0;
            }
            else
            {
                city.needWeight = city.needWeight - this.goodsWeight;
                loadValue=this.goodsWeight;
                this.goodsWeight = 0;
            }
            
            this.pathList.Add(new ClassPathNode((Classcity)array.GetValue(nodeNum),loadValue));
            this.pathlength = this.pathlength + getPathLength(nodeNum);         
        }

        public bool IsBackToGarage(int number)//判断是否返回发车地点
        {
            if (this.carFromGarageNum == number)
                return true;
            else
                return false;               
        }

        public void BeginFromGarage()//从车场装货出发
        {         
            this.goodsWeight = this.Q;         
            this.pathList.Add(new ClassPathNode((Classcity)array.GetValue(this.GarageNum),0));       
        }

        public void Generate()
        {                   
            bool FinishFlag = false;//判断是不是所有的任务点都送货完成

            while (!FinishFlag)//如果任务点没有送完,循环找任务点送货
            {
                BeginFromGarage();//从车场装货出发 
                this.currentCity = GarageNum;
                int messionNum = GetNextMissionNum();//随机获取任务点              
                while (!IsBackToGarage(messionNum))

⌨️ 快捷键说明

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