public bool FindSlot(int dayIndex, Assignment sol) { Timeslot timeslot = null; Assignment assignment = null; bool status = false; Solution s = new Solution(); s = s.Copy(assignments); s.assignments.RemoveAll(x => x.course.Id == sol.course.Id); Room room = null; bool hardConstraints = false; for (int j = 0; j < days; j++) { if (j == dayIndex) { continue; } for (int i = 0; i < periods_per_day - sol.course.GetPeriods(); i++) { timeslot = new Timeslot(j, i); if (CheckIfTimeslotIsAvailable(timeslot, sol.course) == false) { continue; } room = FindRoom(timeslot, sol.course, s); if (room == null) { continue; } assignment = new Assignment(sol.course, room, timeslot); if (s.SlotCurriculaConstraint(assignment) == false || s.SlotTeacherConstraint(assignment) == false) { if (i == periods_per_day - sol.course.GetPeriods() - 1) { status = false; hardConstraints = false; break; } status = false; hardConstraints = false; continue; } else { status = true; hardConstraints = true; break; } } if (status == true) { hardConstraints = true; break; } } if (hardConstraints == true) { s.assignments.Add(assignment); assignments.First(x => x.course.Id == assignment.course.Id).timeslot = assignment.timeslot.ShallowCopy(); assignments.First(x => x.course.Id == assignment.course.Id).room = assignment.room.ShallowCopy(); return(true); } else { return(false); } }
public List <Assignment> GenerateSolution() { List <Course> anotherList = new List <Course>(); //order by curricule foreach (var cur in Instance.Curricula) { foreach (var j in cur.Value.CoursesId) { if (anotherList.Where(x => x.Id == j).Count() > 0) { continue; } else { anotherList.Add(Instance.Courses[j]); } } } foreach (var c in Instance.Courses.Where(x => x.Value.GetCurriculums().Count < 1)) { anotherList.Add(c.Value); } Assignment assignment = null; Restart: foreach (var ass in Instance.FixedAssignments) { assignments.Add(ass); } foreach (var course in anotherList.Where(p => !Instance.FixedAssignments.Any(p2 => p2.course.Id == p.Id))) { bool hardConstraints = false; bool status = true; List <int> daysList = new List <int> { 0, 1, 2, 3, 4 }; while (hardConstraints == false) { int randDay = 0; Timeslot timeslot = null; Random random = new Random(); //find valid room var r = new List <Room>(); Room room = null; foreach (var i in Instance.Rooms.Values) { if (course.ConstraintValidRooms.Where(x => x.Id == i.Id).Count() < 1) { r.Add(i); } } if (r.Where(x => x.Size >= course.Students).Count() > 0) { int size = r.Where(x => x.Size >= course.Students).Count(); room = r.Where(x => x.Size >= course.Students).ElementAt(random.Next(0, size)); } else { room = r.ElementAt(random.Next(0, r.Count())); } if (daysList.Count < 1) { assignments.Clear(); goto Restart; } var g = random.Next(0, daysList.Count()); randDay = daysList.ElementAt(g); daysList.RemoveAt(g); if (assignments.FirstOrDefault(x => x.timeslot.Day == randDay) != null) { for (int i = 0; i < periods_per_day - course.GetPeriods(); i++) { timeslot = new Timeslot(randDay, i); if (CheckIfTimeslotIsAvailable(timeslot, course) == false) { continue; } assignment = new Assignment(course, room, timeslot); if (SlotRoomConstraint(assignment) == false || SlotCurriculaConstraint(assignment) == false || SlotTeacherConstraint(assignment) == false) { if (i == periods_per_day - course.GetPeriods() - 1) { hardConstraints = false; break; } continue; } else { hardConstraints = true; break; } } } else { for (int i = 0; i < periods_per_day - course.GetPeriods(); i++) { timeslot = new Timeslot(randDay, i); if (CheckIfTimeslotIsAvailable(timeslot, course) == true) { assignment = new Assignment(course, room, timeslot); hardConstraints = true; break; } } } } if (status == true) { assignments.Add(assignment); } } return(assignments); }