private void ReportIndividual(Individual ind) { #region Initialize var timeTables = ClearTables(); #endregion for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { Slot temp = ind.Timetable[i][j]; TextBlock tempTextBlock, tempTextBlockForSemester; List<int> fullCrashes = new List<int>(); foreach (var collision in ind.CollisionList.Where(x => x.SlotId == temp.Id && x.Result >= 1)) { fullCrashes.AddRange(collision.CrashingCourses.Select(x => x.Id)); } List<int> semiCrashes = new List<int>(); foreach (var collision in ind.CollisionList.Where(x => x.SlotId == temp.Id && x.Result < 1)) { semiCrashes.AddRange(collision.CrashingCourses.Select(x => x.Id)); } #region add courses and coloring foreach (var course in temp.Courses) { tempTextBlock = new TextBlock { Text = course.PrintableName }; tempTextBlockForSemester = new TextBlock { Text = course.PrintableName }; if (fullCrashes.Contains(course.Id)) { tempTextBlock.Background = Brushes.PaleVioletRed; tempTextBlockForSemester.Background = Brushes.PaleVioletRed; } else if (semiCrashes.Contains(course.Id)) { tempTextBlock.Background = Brushes.LightSkyBlue; tempTextBlockForSemester.Background = Brushes.LightSkyBlue; } MainTimetable.ControlArray[i, j].Children.Add(tempTextBlock); if (course.Semester > 0 && course.Semester < 9) timeTables[course.Semester - 1].ControlArray[i, j].Children.Add(tempTextBlockForSemester); if (course.Type == 1) { var labTextBlock = new TextBlock { Text = course.PrintableName }; LabTimetable.ControlArray[i, j].Children.Add(labTextBlock); } } #endregion #region add faculty courses foreach (var course in temp.facultyCourses) { tempTextBlock = new TextBlock { Text = course.PrintableName, Foreground = Brushes.CadetBlue }; tempTextBlockForSemester = new TextBlock { Text = course.PrintableName, Foreground = Brushes.CadetBlue }; MainTimetable.ControlArray[i, j].Children.Add(tempTextBlock); if (course.Semester > 0 && course.Semester < 9) timeTables[course.Semester - 1].ControlArray[i, j].Children.Add(tempTextBlockForSemester); } #endregion #region add faculty labs if (temp.facultyLab > 0) { tempTextBlock = new TextBlock { Text = $"Faculty Lab (count:{temp.facultyLab})" }; MainTimetable.ControlArray[i, j].Children.Add(tempTextBlock); for (int k = 0; k < temp.facultyLab; k++) { var labTextBlock = new TextBlock { Text = $"Faculty Lab", Foreground = Brushes.CadetBlue }; LabTimetable.ControlArray[i, j].Children.Add(labTextBlock); } } #endregion #region lab constraint coloring. if (LabTimetable.ControlArray[i, j].Children.Count > 4) { LabTimetable.ControlArray[i, j].Background = Brushes.PaleVioletRed; } #endregion #region add meeting. if (temp.meetingHour) { tempTextBlock = new TextBlock { FontStyle = FontStyles.Italic, Text = $"Meeting" }; MainTimetable.ControlArray[i, j].Children.Add(tempTextBlock); for (int n = 0; n < 8; n++) { tempTextBlockForSemester = new TextBlock { FontStyle = FontStyles.Italic, Text = $"Meeting" }; timeTables[n].ControlArray[i, j].Children.Add(tempTextBlockForSemester); } } #endregion #region tooltip filling. foreach (var coll in ind.CollisionList) { if (temp.Id == coll.SlotId) { string collisionRep = $"obj:{coll.Obj} {coll.Reason}"; int count = 0; if (coll.CrashingCourses.Count > 0) collisionRep += " : "; foreach (var cc in coll.CrashingCourses) { count++; collisionRep += cc.Code; if (count != coll.CrashingCourses.Count) collisionRep += "|"; } MainTimetable.ControlArray[i, j].ToolTip += collisionRep + "\n"; } } #endregion #region teacher coloring and teacher tabs/lists foreach (var teacher in _uctproblem.ProblemObj.TeacherList) { if (teacher == "ASSISTANT") continue; if (temp.Courses.Any(x => x.Teacher == teacher)) { foreach (var course in temp.Courses.Where(x => x.Teacher == teacher)) { tempTextBlock = new TextBlock { Text = course.PrintableName }; tempTextBlockForSemester = new TextBlock { Text = course.PrintableName }; if (fullCrashes.Contains(course.Id)) { tempTextBlock.Background = Brushes.PaleVioletRed; tempTextBlockForSemester.Background = Brushes.PaleVioletRed; } TeacherTables[teacher].ControlArray[i, j].Children.Add(tempTextBlockForSemester); } } } #endregion } //hour } //day int tid = 0; foreach (var teacher in _uctproblem.ProblemObj.TeacherList) { bool hasTeacherCollision = false; string teacherColl = ""; foreach (var coll in ind.CollisionList) { if (coll.TeacherId == tid) { teacherColl += $"obj:{coll.Obj} {coll.Reason} \n"; hasTeacherCollision = true; } } TeacherPanel.Children.Add(new TextBlock { Background = hasTeacherCollision ? Brushes.PaleVioletRed : null, ToolTip = hasTeacherCollision ? teacherColl : null, Text = $"{tid}: {teacher}", Margin = new Thickness(5) }); tid++; } CollisionTab.Header = $"Coll:{ind.CollisionList.Count} T:{ind.TotalResult}"; var collindex = 1; foreach (var collision in ind.CollisionList) { CollisionList.Items.Add(collindex + " " + collision.Printable); collindex++; } }
public double HillClimb(ProblemDefinition problemObj) { double tResultImprovement = 0; if (CollisionList.Count > 0) { bool continueClimb; Individual original = new Individual(this, problemObj); original.Decode(problemObj); original.Evaluate(problemObj); int seeder = 7; Random rnd = new Random(seeder); int climbRetryCount = 0; do { climbRetryCount++; bool climb = HillClimber(problemObj, rnd); if (climb) return 0; //HillClimberF22(problemObj, rnd,22); Decode(problemObj); Evaluate(problemObj); var obj0Imp = original.Obj[0] - Obj[0]; var obj1Imp = original.Obj[1] - Obj[1]; var obj2Imp = original.Obj[2] - Obj[2]; if (obj0Imp + obj1Imp + obj2Imp == 0) { if (obj0Imp > 0) { original = new Individual(this, problemObj); original.Decode(problemObj); original.Evaluate(problemObj); } continueClimb = false; } else if (obj0Imp + obj1Imp + obj2Imp > 0) { continueClimb = false; original = new Individual(this, problemObj); original.Decode(problemObj); original.Evaluate(problemObj); } else { continueClimb = false; } tResultImprovement += obj0Imp + obj1Imp + obj2Imp; } while (continueClimb && climbRetryCount < 10); if (tResultImprovement < 0) //todo check sanki geri almıyor. { Copy(original, problemObj); Decode(problemObj); Evaluate(problemObj); tResultImprovement = 0; } } return tResultImprovement; }
public Individual(Individual ind, ProblemDefinition problem) { CollisionList = new List<Collision>(); FacultySections = new List<FacultySection>(); DiffSemesterFacultySections = new List<FacultySection>(); ElectiveFacultySections = new List<FacultySection>(); TotalResult = 0; NRealVar = ind.NRealVar; NBinVar = ind.NBinVar; NMaxBit = ind.NMaxBit; NObj = ind.NObj; NCons = ind.NCons; if (ind.NRealVar != 0) Xreal = new double[NRealVar]; if (ind.NBinVar != 0) { SlotId = new int[ind.NBinVar]; Gene = new List<List<int>>(); for (int i = 0; i < ind.NBinVar; i++) { Gene.Add(new List<int>(ind.NBinVar)); for (int j = 0; j < NMaxBit; j++) { Gene[i].Add(0); } } //Gene = new int[ind._nBinVar, ind._nMaxBit]; } Obj = new double[ind.NObj]; if (ind.NCons != 0) Constr = new double[ind.NCons]; Rank = ind.Rank; ConstrViolation = ind.ConstrViolation; CrowdDist = ind.CrowdDist; if (ind.NRealVar > 0) { for (int i = 0; i < ind.NRealVar; i++) { Xreal[i] = ind.Xreal[i]; } } if (ind.NBinVar > 0) { for (int i = 0; i < ind.NBinVar; i++) { SlotId[i] = ind.SlotId[i]; for (int j = 0; j < problem.Nbits[i]; j++) { Gene[i][j] = ind.Gene[i][j]; } } } for (int i = 0; i < ind.NObj; i++) { Obj[i] = ind.Obj[i]; } if (ind.NCons > 0) { for (int i = 0; i < ind.NCons; i++) { Constr[i] = ind.Constr[i]; } } Timetable = new List<List<Slot>>(); }
/* Function to perform mutation of an individual */ static void MutateIndividual(Individual ind, ProblemDefinition problemObj, Randomization randomizationObj) { if (problemObj.RealVariableCount != 0) { RealMutate(ind, problemObj, randomizationObj); } if (problemObj.BinaryVariableCount != 0) { BinaryMutate(ind, problemObj, randomizationObj); } }
public void Copy(Individual ind, ProblemDefinition problem) { NRealVar = ind.NRealVar; NBinVar = ind.NBinVar; NMaxBit = ind.NMaxBit; NObj = ind.NObj; NCons = ind.NCons; CollisionList.Clear(); FacultySections.Clear(); DiffSemesterFacultySections.Clear(); ElectiveFacultySections.Clear(); TotalResult = 0; Rank = ind.Rank; ConstrViolation = ind.ConstrViolation; CrowdDist = ind.CrowdDist; if (ind.NRealVar > 0) { for (int i = 0; i < ind.NRealVar; i++) { Xreal[i] = ind.Xreal[i]; } } if (ind.NBinVar > 0) { for (int i = 0; i < ind.NBinVar; i++) { SlotId[i] = ind.SlotId[i]; for (int j = 0; j < problem.Nbits[i]; j++) { Gene[i][j] = ind.Gene[i][j]; } } } for (int i = 0; i < ind.NObj; i++) { Obj[i] = ind.Obj[i]; } if (ind.NCons > 0) { for (int i = 0; i < ind.NCons; i++) { Constr[i] = ind.Constr[i]; } } }
/* Routine for usual non-domination checking It will return the following values 1 if a dominates b -1 if b dominates a 0 if both a and b are non-dominated */ static int CheckDominance(Individual a, Individual b, ProblemDefinition problemObj) { int i; int flag1; int flag2; flag1 = 0; flag2 = 0; if (a.ConstrViolation < 0 && b.ConstrViolation < 0) { if (a.ConstrViolation > b.ConstrViolation) { return 1; } if (a.ConstrViolation < b.ConstrViolation) { return -1; } return 0; } if (a.ConstrViolation < 0 && b.ConstrViolation == 0) { return -1; } if (a.ConstrViolation == 0 && b.ConstrViolation < 0) { return 1; } for (i = 0; i < problemObj.ObjectiveCount; i++) { if (a.Obj[i] < b.Obj[i]) { flag1 = 1; } else { if (a.Obj[i] > b.Obj[i]) { flag2 = 1; } } } if (flag1 == 1 && flag2 == 0) { return 1; } if (flag1 == 0 && flag2 == 1) { return -1; } return 0; }
/* Function to cross two individuals */ static void Crossover(Individual parent1, Individual parent2, Individual child1, Individual child2, ProblemDefinition problemObj, Randomization randomizationObj) { if (problemObj.RealVariableCount != 0) { RealCrossover(parent1, parent2, child1, child2, problemObj, randomizationObj); } if (problemObj.BinaryVariableCount != 0) { BinaryCrossover(parent1, parent2, child1, child2, problemObj, randomizationObj); } }
/* Routine for binary mutation of an individual */ static void BinaryMutate(Individual ind, ProblemDefinition problemObj, Randomization randomizationObj) { int j, k; double prob; for (j = 0; j < problemObj.BinaryVariableCount; j++) { for (k = 0; k < problemObj.Nbits[j]; k++) { prob = randomizationObj.RandomPercent(); if (prob <= problemObj.BinaryMutationProbability) { if (ind.Gene[j][k] == 0) { ind.Gene[j][k] = 1; } else { ind.Gene[j][k] = 0; } problemObj.BinaryMutationCount += 1; } } } }
/* Routine for two point binary Crossover */ static void BinaryCrossover(Individual parent1, Individual parent2, Individual child1, Individual child2, ProblemDefinition problemObj, Randomization randomizationObj) { int i, j; double rand; int temp, site1, site2; for (i = 0; i < problemObj.BinaryVariableCount; i++) { rand = randomizationObj.RandomPercent(); if (rand <= problemObj.BinaryCrossoverProbability) { problemObj.BinaryCrossoverCount++; site1 = randomizationObj.RandomInteger(0, problemObj.Nbits[i] - 1); site2 = randomizationObj.RandomInteger(0, problemObj.Nbits[i] - 1); if (site1 > site2) { temp = site1; site1 = site2; site2 = temp; } for (j = 0; j < site1; j++) { child1.Gene[i][j] = parent1.Gene[i][j]; child2.Gene[i][j] = parent2.Gene[i][j]; } for (j = site1; j < site2; j++) { child1.Gene[i][j] = parent2.Gene[i][j]; child2.Gene[i][j] = parent1.Gene[i][j]; } for (j = site2; j < problemObj.Nbits[i]; j++) { child1.Gene[i][j] = parent1.Gene[i][j]; child2.Gene[i][j] = parent2.Gene[i][j]; } } else { for (j = 0; j < problemObj.Nbits[i]; j++) { child1.Gene[i][j] = parent1.Gene[i][j]; child2.Gene[i][j] = parent2.Gene[i][j]; } } } }
/* Routine for binary Tournament */ static Individual Tournament(Individual ind1, Individual ind2, ProblemDefinition problemObj, Randomization randomizationObj) { var flag = CheckDominance(ind1, ind2, problemObj); if (flag == 1) { return ind1; } if (flag == -1) { return ind2; } if (ind1.CrowdDist > ind2.CrowdDist) { return ind1; } if (ind2.CrowdDist > ind1.CrowdDist) { return ind2; } if (randomizationObj.RandomPercent() <= 0.5) { return ind1; } else { return ind2; } }
/* Routine for real polynomial mutation of an individual */ static void RealMutate(Individual ind, ProblemDefinition problemObj, Randomization randomizationObj) { int j; double rnd, delta1, delta2, mutPow, deltaq; double y, yl, yu, val, xy; for (j = 0; j < problemObj.RealVariableCount; j++) { if (randomizationObj.RandomPercent() <= problemObj.RealMutationProbability) { y = ind.Xreal[j]; yl = problemObj.MinRealvar[j]; yu = problemObj.MaxRealvar[j]; delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); rnd = randomizationObj.RandomPercent(); mutPow = 1.0 / (problemObj.MutationDistributionIndex + 1.0); if (rnd <= 0.5) { xy = 1.0 - delta1; val = 2.0 * rnd + (1.0 - 2.0 * rnd) * Math.Pow(xy, problemObj.MutationDistributionIndex + 1.0); deltaq = Math.Pow(val, mutPow) - 1.0; } else { xy = 1.0 - delta2; val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * Math.Pow(xy, problemObj.MutationDistributionIndex + 1.0); deltaq = 1.0 - Math.Pow(val, mutPow); } y = y + deltaq * (yu - yl); if (y < yl) y = yl; if (y > yu) y = yu; ind.Xreal[j] = y; problemObj.RealMutationCount += 1; } } }
/* Routine for real variable SBX Crossover */ static void RealCrossover(Individual parent1, Individual parent2, Individual child1, Individual child2, ProblemDefinition problemObj, Randomization randomizationObj) { int i; double rand; double y1, y2, yl, yu; double c1, c2; double alpha, beta, betaq; if (randomizationObj.RandomPercent() <= problemObj.RealCrossoverProbability) { problemObj.RealCrossoverCount++; for (i = 0; i < problemObj.RealVariableCount; i++) { if (randomizationObj.RandomPercent() <= 0.5) { if (Math.Abs(parent1.Xreal[i] - parent2.Xreal[i]) > problemObj.Eps) { if (parent1.Xreal[i] < parent2.Xreal[i]) { y1 = parent1.Xreal[i]; y2 = parent2.Xreal[i]; } else { y1 = parent2.Xreal[i]; y2 = parent1.Xreal[i]; } yl = problemObj.MinRealvar[i]; yu = problemObj.MaxRealvar[i]; rand = randomizationObj.RandomPercent(); beta = 1.0 + 2.0 * (y1 - yl) / (y2 - y1); alpha = 2.0 - Math.Pow(beta, -(problemObj.CrossoverDistributionIndex + 1.0)); if (rand <= 1.0 / alpha) { betaq = Math.Pow(rand * alpha, 1.0 / (problemObj.CrossoverDistributionIndex + 1.0)); } else { betaq = Math.Pow(1.0 / (2.0 - rand * alpha), 1.0 / (problemObj.CrossoverDistributionIndex + 1.0)); } c1 = 0.5 * (y1 + y2 - betaq * (y2 - y1)); beta = 1.0 + 2.0 * (yu - y2) / (y2 - y1); alpha = 2.0 - Math.Pow(beta, -(problemObj.CrossoverDistributionIndex + 1.0)); if (rand <= 1.0 / alpha) { betaq = Math.Pow(rand * alpha, 1.0 / (problemObj.CrossoverDistributionIndex + 1.0)); } else { betaq = Math.Pow(1.0 / (2.0 - rand * alpha), 1.0 / (problemObj.CrossoverDistributionIndex + 1.0)); } c2 = 0.5 * (y1 + y2 + betaq * (y2 - y1)); if (c1 < yl) c1 = yl; if (c2 < yl) c2 = yl; if (c1 > yu) c1 = yu; if (c2 > yu) c2 = yu; if (randomizationObj.RandomPercent() <= 0.5) { child1.Xreal[i] = c2; child2.Xreal[i] = c1; } else { child1.Xreal[i] = c1; child2.Xreal[i] = c2; } } else { child1.Xreal[i] = parent1.Xreal[i]; child2.Xreal[i] = parent2.Xreal[i]; } } else { child1.Xreal[i] = parent1.Xreal[i]; child2.Xreal[i] = parent2.Xreal[i]; } } } else { for (i = 0; i < problemObj.RealVariableCount; i++) { child1.Xreal[i] = parent1.Xreal[i]; child2.Xreal[i] = parent2.Xreal[i]; } } }