//初始物种基因(随机) public void CreateByRandomGenes() { int temp; Random rand = new Random(); for (int i = 0; i < TSPDATA.SPECIES_Num; i++) { temp = 0; for (int j = 0; j < TSPDATA.Time.Length; j++) { for (int k = 0; k < TSPDATA.Room.Length; k++) { Lesson lesson = new Lesson(); lesson.Classm = rand.Next(TSPDATA.Classm.Length); lesson.Course = rand.Next(TSPDATA.Course.Length); lesson.Room = k; lesson.Teacher = rand.Next(TSPDATA.Teacher.Length); lesson.Time = j; genes[i, temp] = Lesson.Getstr(lesson); temp++; } } } }
//交叉 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); } } } } }
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); } } } }