//交叉
        public void Crossover()
        {
            Random rand = new Random();

            for (int i = 0; i < TSPDATA.SPECIES_Num; i++)
            {
                float rate = (float)rand.NextDouble();
                for (int j = 0; j < TSPDATA.LessonNum - 1; j++)
                {
                    if (rate > TSPDATA.pcl && rate < TSPDATA.pch)
                    {
                        //string str = genes[i+1, j];
                        Lesson le1 = Lesson.GetLesson(genes[i, j]);
                        Lesson le2 = Lesson.GetLesson(genes[i, j + 1]);
                        if (le1.Time != -1 && le2.Time != -1)
                        {
                            int s, k, g;
                            s               = le1.Teacher;
                            k               = le1.Course;
                            g               = le1.Classm;
                            le1.Teacher     = le2.Teacher;
                            le1.Course      = le2.Course;
                            le1.Classm      = le2.Classm;
                            le2.Classm      = g;
                            le2.Course      = k;
                            le2.Teacher     = s;
                            genes[i, j]     = Lesson.Getstr(le1);
                            genes[i, j + 1] = Lesson.Getstr(le2);
                        }
                    }
                }
            }
        }
        //得到Lesson列表
        public List <Lesson> GetAll(string[] Ls)
        {
            List <Lesson> l1 = new List <Lesson>();

            foreach (string str in Ls)
            {
                l1.Add(Lesson.GetLesson(str));
            }
            return(l1);
        }
Пример #3
0
        static void Main(string[] args)
        {
            SpeciesIndividual sp = new SpeciesIndividual();

            sp.CreateByRandomGenes();

            for (int i = 0; i < 5; i++)
            {
                sp.Select();
                sp.Crossover();
                sp.Mutate();


                sp.CalFitness();
                for (int j = 0; j < TSPDATA.SPECIES_Num; j++)
                {
                    Console.WriteLine("存在{0}节课,适应度为{0}", sp.genes.Length, sp.fitness[j]);
                }
            }

            sp.Deletect();

            for (int j = 0; j < TSPDATA.LessonNum; j++)
            {
                Lesson le = Lesson.GetLesson(sp.genes[sp.GetBest(), j]);
                if (le.Course != 9)
                {
                    Mycourse mc = new Mycourse();
                    mc.Time    = TSPDATA.Time[le.Time];
                    mc.Course  = TSPDATA.Course[le.Course];
                    mc.Classm  = TSPDATA.Classm[le.Classm];
                    mc.Room    = TSPDATA.Room[le.Room];
                    mc.Teacher = TSPDATA.Teacher[le.Teacher];
                    Console.WriteLine("时间:{0},教师:{1},教室:{2},课程:{3},班级:{4}", mc.Time, mc.Teacher, mc.Room, mc.Course, mc.Classm);
                    if (Method.insertlesson(mc))
                    {
                        Console.WriteLine("插入第{0}节课成功", j);
                    }
                }
            }



            Console.ReadKey();
        }
 public void Deletect()
 {
     for (int i = 0; i < TSPDATA.SPECIES_Num; i++)
     {
         for (int j = 0; j < TSPDATA.LessonNum - 1; j++)
         {
             Lesson le  = Lesson.GetLesson(genes[i, j]);
             Lesson lec = Lesson.GetLesson(genes[i, j + 1]);
             if (le.Time.Equals(lec.Time))
             {
                 if (le.Teacher.Equals(lec.Teacher) || le.Course.Equals(lec.Course) || le.Classm.Equals(lec.Classm))
                 {
                     le.Course   = 9;
                     genes[i, j] = Lesson.Getstr(le);
                 }
             }
         }
     }
 }
        //变异
        public void Mutate()
        {
            Random rand = new Random();
            float  rate = (float)rand.NextDouble();

            for (int i = 0; i < TSPDATA.SPECIES_Num; i++)
            {
                for (int j = 0; j < TSPDATA.LessonNum - 1; j++)
                {
                    if (rate < TSPDATA.pm)
                    {
                        Lesson l1 = Lesson.GetLesson(genes[i, j]);
                        l1.Teacher  = rand.Next(TSPDATA.Teacher.Length);
                        l1.Course   = rand.Next(TSPDATA.Course.Length);
                        l1.Classm   = rand.Next(TSPDATA.Classm.Length);
                        genes[i, j] = Lesson.Getstr(l1);
                    }
                }
            }
        }
        //适应度函数
        public void CalFitness()
        {
            for (int j = 0; j < fitness.Length; j++)
            {
                fitness[j] = 100;
            }

            for (int i = 0; i < TSPDATA.SPECIES_Num; i++)
            {
                for (int j = 0; j < TSPDATA.LessonNum - 1; j++)
                {
                    Lesson le  = Lesson.GetLesson(genes[i, j]);
                    Lesson lec = Lesson.GetLesson(genes[i, j + 1]);
                    if (le.Time.Equals(lec.Time) && le.Course != 9)
                    {
                        if (le.Teacher.Equals(lec.Teacher) || le.Course.Equals(lec.Course) || le.Classm.Equals(lec.Classm))
                        {
                            fitness[i]--;
                        }
                    }
                }
            }
            //int flag = 0;
            //string[] tempa = new string[TSPDATA.Room.Length];


            //遍历种群
            //for (int i = 0; i < TSPDATA.SPECIES_Num; i++)
            //{
            //    int temp = 0;
            //    //遍历种群中一个个体的课程
            //    for (int j = 0; j < TSPDATA.LessonNum; j++)
            //    {
            //        string str = genes[i, j];
            //        string time = str.Substring(0, 1);
            //        //如果时间相等
            //        if (time.Equals(TSPDATA.Time[temp]))
            //        {
            //            //取得时间相同的课的集合
            //            tempa[flag] = str;
            //            flag++;
            //        }
            //        else
            //        {
            //            temp++;

            //            //遍历时间相同的课
            //            for (int q = 0; q < tempa.Length; q++)
            //            {
            //                for (int w = tempa.Length - 1; w > q; w--)
            //                {
            //                    string teacher1 = tempa[q].Substring(7, 3);
            //                    string teacher2 = tempa[w].Substring(7, 3);
            //                    string course1 = tempa[q].Substring(10, 5);
            //                    string course2 = tempa[w].Substring(10, 5);
            //                    string clss1 = tempa[q].Substring(15, 3);
            //                    string clss2 = tempa[w].Substring(15, 3);
            //                    if (teacher1.Equals(teacher2) || course1.Equals(course2) || clss1.Equals(clss2))
            //                    {
            //                        fitness[i]--;
            //                    }
            //                    //if(course1.Equals(course2))
            //                    //{
            //                    //    fitness[i]--;
            //                    //}
            //                    //if (clss1.Equals(clss2))
            //                    //{
            //                    //    fitness[i]--;
            //                    //}
            //                }
            //            }

            //            flag = 0;
            //            break;
            //        }
            //}
        }