private static bool isConsistentForSection(int start, int end, int day, int SecId, ref List <SectionTimeTable> allSecsTable, List <TaTimetable> TaTable, SectionsVariables UNsection, ref List <RoomsTimeTables> rooms, ref int roomid, ref List <string> tasids) { CollegeDatabaseEntities10 db = new CollegeDatabaseEntities10(); int indexrom = 0; int indexsec = allSecsTable.IndexOf(allSecsTable.Where(x => x.Sectionid == SecId).Single()); bool islab = false; if (UNsection.HaveLab) { islab = true; } else { islab = false; } int roomId = findRoom(start, end, day, rooms, ref indexrom, islab); if (roomId == -1) { return(false); } roomid = roomId; for (int i = start; i <= end; i++) { if (allSecsTable[indexsec].Week[day].Slots[i] == true) { return(false); } } //TA var tas = db.LinkDoctorCourses.Where(x => x.Course_id == UNsection.CourseId && x.AspNetUser.type == "Ta").ToList(); List <TaTimetable> tasforcrs = new List <TaTimetable>(); foreach (var item in tas) { TaTimetable query = TaTable.Where(x => x.Id == item.Doctor_id).SingleOrDefault(); tasforcrs.Add(query); } List <string> ta_assigned = new List <string>(); int count = 0; int noftas = 1; /*if (UNsection.HaveLab) * noftas = 2; * else * noftas++;*/ for (int i = 0; i < tasforcrs.Count; i++) { var tatimetable = tasforcrs[i].Course_hours.Where(x => x.Key == UNsection.CourseId).Single(); if (tatimetable.Value >= (end - start + 1)) { bool f = true; for (int o = start; o <= end; o++) { if (tasforcrs[i].Week[day].Slots[o] == true) { f = false; break; } } if (f) { var allowedhourss = db.Groups.Where(x => x.ID == UNsection.GroupId).Select(x => x.AcademicYear).Single(); var ind = TaTable.IndexOf(TaTable.Where(x => x.Id == tasforcrs[i].Id).Single()); TaTable[ind].Week[day].addinterval(start, end); /*if ((TaTable[ind].Week[day].Gap > allowedhourss.AllowedGapHours || TaTable[ind].Week[day].NumfHours > allowedhourss.DayWorkHours) || TaTable[ind].FreeDaysCount() < 1) * { * TaTable[ind].Week[day].Removeinterval(start, end); * * } * else * {*/ var h = TaTable[ind].Course_hours.IndexOf(TaTable[ind].Course_hours.Where(x => x.Key == UNsection.CourseId).Single()); KeyValuePair <int, int?> temppair = new KeyValuePair <int, int?>(TaTable[ind].Course_hours[h].Key, TaTable[ind].Course_hours[h].Value - (end - start + 1)); TaTable[ind].Course_hours.RemoveAt(h); TaTable[ind].Course_hours.Add(temppair); ta_assigned.Add(tasforcrs[i].Id); count++; //} } } if (count == noftas) { break; } } if (count < noftas) { for (int g = 0; g < count; g++) { var ind = TaTable.IndexOf(TaTable.Where(x => x.Id == ta_assigned[g]).Single()); var h = TaTable[ind].Course_hours.IndexOf(TaTable[ind].Course_hours.Where(x => x.Key == UNsection.CourseId).Single()); KeyValuePair <int, int?> temppair = new KeyValuePair <int, int?>(TaTable[ind].Course_hours[h].Key, TaTable[ind].Course_hours[h].Value + end - start + 1); TaTable[ind].Week[day].Removeinterval(start, end); TaTable[ind].Course_hours.RemoveAt(h); TaTable[ind].Course_hours.Add(temppair); } ta_assigned.Clear(); return(false); } allSecsTable[indexsec].Week[day].Addslots(start, end); rooms[indexrom].Week[day].ReserveInterval(start, end); //var allowedHours = db.Groups.Where(x => x.ID == UNsection.GroupId).Select(x => x.AcademicYear).Single(); /*if (allSecsTable[indexsec].Week[day].Gap > allowedHours.AllowedGapHours || allSecsTable[indexsec].Week[day].NumfHours > allowedHours.DayWorkHours) * { * allSecsTable[indexsec].Week[day].Removeslots(start, end); * return false; * }*/ /*if (allSecsTable[indexsec].FreeDaysCount() < 1) * { * allSecsTable[indexsec].Week[day].Removeslots(start, end); * * return false; * * }*/ tasids = ta_assigned; return(true); }
private static List <SectionAssignement> Recursive_Backtracking_Search_Sections(List <SectionAssignement> assignment, List <SectionsVariables> variables, ref List <SectionTimeTable> SecTimeTable, List <AvailTimeforgroup> availTimeGrp, List <TaTimetable> taTimetable, ref List <RoomsTimeTables> roomsTimeTable) { if (assignment.Count == variables.Count) { return(assignment); } SectionsVariables UnSection = Select_UnAssigned_Section(assignment, variables); int noofhours = 0; if (UnSection.HaveLab) { noofhours = UnSection.LabHours; } else { noofhours = UnSection.SectionHours; } var grpAvailTimeTable = availTimeGrp.Where(x => x.Groupid == UnSection.GroupId).Single(); int START = 0; int dayNumber = 0; int startSlot = 0; int EndSlot = 0; for (int i = 0; i < grpAvailTimeTable.Week.Length; i++) { if (grpAvailTimeTable.Week[i].AvailableTimes.Count >= noofhours) { int count = 1; for (int j = START + 1; j < grpAvailTimeTable.Week[i].AvailableTimes.Count; j++) { if (grpAvailTimeTable.Week[i].AvailableTimes[j] - grpAvailTimeTable.Week[i].AvailableTimes[j - 1] == 1) { count++; } else { count = 1; } if (count == noofhours) { EndSlot = grpAvailTimeTable.Week[i].AvailableTimes[j]; startSlot = EndSlot - count + 1; dayNumber = i; int roomid = -3; List <string> tasids = new List <string>(); if (isConsistentForSection(startSlot, EndSlot, dayNumber, UnSection.SectionId, ref SecTimeTable, taTimetable, UnSection, ref roomsTimeTable, ref roomid, ref tasids)) { assignment.Add(new SectionAssignement(UnSection, startSlot, dayNumber, roomid, tasids, UnSection.HaveLab, UnSection.HaveSections)); var result = Recursive_Backtracking_Search_Sections(assignment, variables, ref SecTimeTable, availTimeGrp, taTimetable, ref roomsTimeTable); if (result[0].RoomId != -22) { return(result); } else { var removedass = assignment.Where(x => x.CourseId == UnSection.CourseId && x.SectionId == UnSection.SectionId && x.IsLab == UnSection.HaveLab && x.IsSection == UnSection.HaveSections).Single(); int ind = SecTimeTable.IndexOf(SecTimeTable.Where(x => x.Sectionid == UnSection.SectionId).Single()); SecTimeTable[ind].Week[dayNumber].Removeslots(startSlot, EndSlot); int indofroom = roomsTimeTable.IndexOf(roomsTimeTable.Where(x => x.Roomid == removedass.RoomId).Single()); roomsTimeTable[indofroom].Week[dayNumber].Remove(startSlot, EndSlot); List <string> removedtas = new List <string>(); removedtas = removedass.Tasids; for (int n = 0; n < removedtas.Count; n++) { var indofta = taTimetable.IndexOf(taTimetable.Where(x => x.Id == removedtas[n]).Single()); var h = taTimetable[indofta].Course_hours.IndexOf(taTimetable[indofta].Course_hours.Where(x => x.Key == UnSection.CourseId).Single()); KeyValuePair <int, int?> tempPair = new KeyValuePair <int, int?>(taTimetable[indofta].Course_hours[h].Key, taTimetable[indofta].Course_hours[h].Value + EndSlot - startSlot + 1); taTimetable[indofta].Week[dayNumber].Removeinterval(startSlot, EndSlot); taTimetable[indofta].Course_hours.RemoveAt(h); taTimetable[indofta].Course_hours.Add(tempPair); } assignment.Remove(assignment.Where(x => x.CourseId == UnSection.CourseId && x.SectionId == UnSection.SectionId && x.IsLab == UnSection.HaveLab && x.IsSection == UnSection.HaveSections).Single()); count = 1; } } else { count = 1; } } } } } List <string> failurestate = new List <string>(); assignment.Add(new SectionAssignement(UnSection, -1, -1, -1, failurestate, false, false)); //return new List<SectionAssignement> { new SectionAssignement(UnSection, -22, -22, -22, failurestate,false,false) }; return(Recursive_Backtracking_Search_Sections(assignment, variables, ref SecTimeTable, availTimeGrp, taTimetable, ref roomsTimeTable)); }