private static void removeSameDayStationsAndCategories(Models.Group group, Models.TimeSlot timeSlot, ref IList <Models.Station> eligibleStations) { int dayNum = timeSlot.Start.DayOfYear; if (dailyAssignments.ContainsKey(dayNum) && dailyAssignments[dayNum].ContainsKey(group)) { foreach (Models.Station station in dailyAssignments[dayNum][group]) { //remove all eligible stations with the same category as the current station if (station.Category != null) { for (int eligibleStationNum = 0; eligibleStationNum < eligibleStations.Count; eligibleStationNum++) { Models.Station eligibleStation = eligibleStations[eligibleStationNum]; if (eligibleStation.Category == station.Category) { eligibleStations.Remove(eligibleStation); eligibleStationNum--; } } } //remove the current station if it still exists if (eligibleStations.Contains(station)) { eligibleStations.Remove(station); } } } }
public static bool assignStation(IList <Models.Station> eligibleStations, Models.Group group, Models.TimeSlot timeSlot, IList <Models.TimeSlot> sortedTimeSlots, IList <Models.Activity> generatedSchedule) { IList <Models.Station> nonActivityPinStations = new List <Models.Station>(); IList <Models.Station> activityPinStations = filterActivityPinStations(eligibleStations, out nonActivityPinStations); if (activityPinStations.Count > 0 && generateAssignment( group, timeSlot, sortedTimeSlots, activityPinStations, generatedSchedule)) { return(true); } else { return(generateAssignment(group, timeSlot, sortedTimeSlots, nonActivityPinStations, generatedSchedule)); } }
public static bool generateAssignment( Models.Group assignedGroup, Models.TimeSlot timeSlot, IList <Models.TimeSlot> sortedTimeSlots, IList <Models.Station> eligibleStations, IList <Models.Activity> generatedSchedule) { if (eligibleStations.Count > 0) { int stationNumber = random.Next(eligibleStations.Count); Models.Station assignedStation = eligibleStations[stationNumber]; //we could be scheduling either an activity pin station or a regular station if (assignedStation.isActivityPin) { int timeSlotIndex = sortedTimeSlots.IndexOf(timeSlot); if (timeSlotIndex < 0 || sortedTimeSlots.Count <= (timeSlotIndex + 1)) { return(false); } int currentTimeSlotDayNum = sortedTimeSlots[timeSlotIndex].Start.DayOfYear; int nextTimeSlotDayNum = sortedTimeSlots[timeSlotIndex + 1].Start.DayOfYear; if (currentTimeSlotDayNum != nextTimeSlotDayNum || sortedTimeSlots[timeSlotIndex].isGeneral || sortedTimeSlots[timeSlotIndex + 1].isGeneral) { return(false); } activityNumber++; Models.Activity activity = new Models.Activity(); activity.ID = activityNumber; activity.Group = assignedGroup; activity.Station = assignedStation; activity.TimeSlot = sortedTimeSlots[timeSlotIndex]; generatedSchedule.Add(activity); Models.Activity activity1 = new Models.Activity(); activity1.ID = activityNumber; activity1.Group = assignedGroup; activity1.Station = assignedStation; activity1.TimeSlot = sortedTimeSlots[timeSlotIndex + 1]; generatedSchedule.Add(activity1); nextTimeSlotUnassignedStations.Remove(assignedStation); nextTimeSlotUnassignedGroups.Remove(assignedGroup); addAssignmentToDailyAssignments(activity); } else { activityNumber++; Models.Activity activity = new Models.Activity(); activity.ID = activityNumber; activity.Group = assignedGroup; activity.Station = assignedStation; activity.TimeSlot = timeSlot; generatedSchedule.Add(activity); addAssignmentToDailyAssignments(activity); } if (assignedGroup.Preference1 == assignedStation) { groupPreferenceGiven[assignedGroup][0] = true; } else if (assignedGroup.Preference2 == assignedStation) { groupPreferenceGiven[assignedGroup][1] = true; } else if (assignedGroup.Preference3 == assignedStation) { groupPreferenceGiven[assignedGroup][2] = true; } else if (assignedGroup.Preference4 == assignedStation) { groupPreferenceGiven[assignedGroup][3] = true; } else if (assignedGroup.Preference5 == assignedStation) { groupPreferenceGiven[assignedGroup][4] = true; } groupStationVisitRange[assignedGroup][assignedStation].decrementNumVisits(); groupStationAssignments[assignedGroup][assignedStation]++; unassignedGroups.Remove(assignedGroup); stationCapacities[assignedStation]--; if (stationCapacities[assignedStation] <= 0) { unassignedStations.Remove(assignedStation); } return(true); } return(false); }
public static IEnumerable <Models.Activity> getSchedule(IEnumerable <Models.Group> groups, IEnumerable <Models.Station> stations, IEnumerable <Models.SchedulingConstraint> constraints, IEnumerable <Models.TimeSlot> slots, IEnumerable <Models.Activity> oldSchedule, Models.TimeSlot startingTimeSlot) { ZeroOut(); List <Models.Activity> schedule = new List <Models.Activity>(); AllGroups = new List <Group>(); AllStations = new List <Station>(); AllConstraints = new List <Constraint>(); int i, j, k; convertTimeSlotsToDaySlots(slots); List <Models.Group> tmpallGroups = new List <Models.Group>(groups.ToArray()); List <Models.Station> tmpallStations = new List <Models.Station>(stations.ToArray()); List <Models.TimeSlot> tmpallSlots = new List <Models.TimeSlot>(slots.ToArray()); foreach (Models.Station s in stations) { AllStations.Add(new Station(s.ID, s.Name, s.Capacity, s.Category, s.isActivityPin, timeSlotsToAvailability(s.AvailableTimeSlots))); } foreach (Models.Group x in groups) { int p1 = -1, p2 = -1, p3 = -1, p4 = -1, p5 = -1; for (i = 0; i < AllStations.Count; i++) { if (x.Preference1 != null && x.Preference1.ID == AllStations[i].ID) { p1 = i; } if (x.Preference2 != null && x.Preference2.ID == AllStations[i].ID) { p2 = i; } if (x.Preference3 != null && x.Preference3.ID == AllStations[i].ID) { p3 = i; } if (x.Preference4 != null && x.Preference4.ID == AllStations[i].ID) { p4 = i; } if (x.Preference5 != null && x.Preference5.ID == AllStations[i].ID) { p5 = i; } } AllGroups.Add(new Group(x.ID, x.Name, x.Type.ID, p1, p2, p3, p4, p5)); } foreach (Models.SchedulingConstraint c in constraints) { Group g = AllGroups[0]; Station s = AllStations[0]; for (i = 0; i < AllStations.Count; i++) { if (AllStations[i].ID == c.Station.ID) { s = AllStations[i]; } } if (c.Group != null) { for (i = 0; i < AllGroups.Count; i++) { if (AllGroups[i].ID == c.Group.ID) { g = AllGroups[i]; } } AllConstraints.Add(new Constraint(g, s, c.VisitNum)); } else if (c.GroupType != null) { for (i = 0; i < AllGroups.Count; i++) { if (c.GroupType.ID == AllGroups[i].Rank) { AllConstraints.Add(new Constraint(g, s, c.VisitNum)); } } } else if (c.Group == null && c.GroupType == null) { for (i = 0; i < AllGroups.Count; i++) { AllConstraints.Add(new Constraint(AllGroups[i], s, c.VisitNum)); } } } KeyValuePair <int, int> startDaySlot = timeSlotToDaySlot(startingTimeSlot); if (startDaySlot.Key == 1 && startDaySlot.Value == 1) { generateNewScheduleFromScracth = true; } else { generateNewScheduleFromScracth = false; } addOldScheduleToNewScheduleTillTimeSlot(oldSchedule, startDaySlot.Key, startDaySlot.Value); Schedule(startDaySlot.Key, startDaySlot.Value); /* * Dictionary<string, string>[,] Q = new Dictionary<string, string>[10, 20]; * * for(i=0;i<10;i++) * for(j=0;j<20;j++) * { * Q[i,j] = new Dictionary<string,string>(); * * foreach (KeyValuePair<int, int> P in masterSchedule[i, j]) * { * Q[i, j].Add(AllGroups[P.Key].Name, AllStations[P.Value].Name); * } * } * */ for (i = 1; i <= 5; i++) { for (j = 1; j <= nSlots[i]; j++) { int slotID = daySlotToTimeSlot(i, j); Models.TimeSlot T = new Models.TimeSlot(); foreach (Models.TimeSlot t in slots) { if (t.ID == slotID) { T = t; } } foreach (KeyValuePair <int, int> P in masterSchedule[i, j]) { Models.Activity A = new Models.Activity(); A.Group = tmpallGroups[P.Key]; A.Station = tmpallStations[P.Value]; A.TimeSlot = T; schedule.Add(A); } } } return(schedule); }
private static KeyValuePair <int, int> timeSlotToDaySlot(Models.TimeSlot T) { return(timeSlotsDaySlotsPairs[T.ID]); }
public static IEnumerable <Models.Activity> Schedule(IEnumerable <Models.Group> groups, IEnumerable <Models.Station> stations, IEnumerable <Models.SchedulingConstraint> constraints, IEnumerable <Models.TimeSlot> slots, IEnumerable <Models.Activity> oldSchedule, Models.TimeSlot startingTimeSlot) { IEnumerable <Models.Activity> Greedy = new List <Models.Activity>(); IEnumerable <Models.Activity> HillClimbing = new List <Models.Activity>(); long GreedyScore = 0; long HillClimbingScore = 0; Greedy = GreedyAlgorithm.GreedyScheduler.getSchedule(groups, stations, constraints, slots, oldSchedule, startingTimeSlot); // A schedule needs to be generated from scratch if (slots.First().ID == startingTimeSlot.ID) { HillClimbing = HillClimbingAlgorithm.GenerateSchedule(groups, stations, constraints, slots); GreedyScore = Score.ScoreSchedule(Greedy, groups, stations, constraints, slots); HillClimbingScore = Score.ScoreSchedule(HillClimbing, groups, stations, constraints, slots); // compare scores if (GreedyScore >= HillClimbingScore) { return(Greedy); } return(HillClimbing); } return(Greedy); }
public static IEnumerable<Models.Activity> getSchedule(IEnumerable<Models.Group> groups, IEnumerable<Models.Station> stations, IEnumerable<Models.SchedulingConstraint> constraints, IEnumerable<Models.TimeSlot> slots, IEnumerable<Models.Activity> oldSchedule, Models.TimeSlot startingTimeSlot) { ZeroOut(); List<Models.Activity> schedule = new List<Models.Activity>(); AllGroups = new List<Group>(); AllStations = new List<Station>(); AllConstraints = new List<Constraint>(); int i,j,k; convertTimeSlotsToDaySlots(slots); List<Models.Group> tmpallGroups = new List<Models.Group>(groups.ToArray()); List<Models.Station> tmpallStations = new List<Models.Station>(stations.ToArray()); List<Models.TimeSlot> tmpallSlots = new List<Models.TimeSlot>(slots.ToArray()); foreach (Models.Station s in stations) AllStations.Add(new Station(s.ID, s.Name, s.Capacity, s.Category, s.isActivityPin, timeSlotsToAvailability(s.AvailableTimeSlots))); foreach (Models.Group x in groups) { int p1 = -1, p2 = -1, p3 = -1, p4 = -1, p5 = -1; for (i = 0; i < AllStations.Count; i++) { if (x.Preference1 != null && x.Preference1.ID == AllStations[i].ID) p1 = i; if (x.Preference2 != null && x.Preference2.ID == AllStations[i].ID) p2 = i; if (x.Preference3 != null && x.Preference3.ID == AllStations[i].ID) p3 = i; if (x.Preference4 != null && x.Preference4.ID == AllStations[i].ID) p4 = i; if (x.Preference5 != null && x.Preference5.ID == AllStations[i].ID) p5 = i; } AllGroups.Add(new Group(x.ID, x.Name, x.Type.ID, p1, p2, p3, p4, p5)); } foreach (Models.SchedulingConstraint c in constraints) { Group g = AllGroups[0]; Station s = AllStations[0]; for (i = 0; i < AllStations.Count; i++) if (AllStations[i].ID == c.Station.ID) s = AllStations[i]; if (c.Group != null) { for (i = 0; i < AllGroups.Count; i++) if (AllGroups[i].ID == c.Group.ID) g = AllGroups[i]; AllConstraints.Add(new Constraint(g, s, c.VisitNum)); } else if (c.GroupType != null) { for (i = 0; i < AllGroups.Count; i++) if (c.GroupType.ID == AllGroups[i].Rank) AllConstraints.Add(new Constraint(g, s, c.VisitNum)); } else if( c.Group == null && c.GroupType == null) for (i = 0; i < AllGroups.Count; i++) AllConstraints.Add(new Constraint(AllGroups[i], s, c.VisitNum)); } KeyValuePair<int, int> startDaySlot = timeSlotToDaySlot(startingTimeSlot); if (startDaySlot.Key == 1 && startDaySlot.Value == 1) generateNewScheduleFromScracth = true; else generateNewScheduleFromScracth = false; addOldScheduleToNewScheduleTillTimeSlot(oldSchedule, startDaySlot.Key, startDaySlot.Value); Schedule(startDaySlot.Key, startDaySlot.Value); /* Dictionary<string, string>[,] Q = new Dictionary<string, string>[10, 20]; for(i=0;i<10;i++) for(j=0;j<20;j++) { Q[i,j] = new Dictionary<string,string>(); foreach (KeyValuePair<int, int> P in masterSchedule[i, j]) { Q[i, j].Add(AllGroups[P.Key].Name, AllStations[P.Value].Name); } } * */ for (i = 1; i <= 5; i++) { for (j = 1; j <= nSlots[i]; j++) { int slotID = daySlotToTimeSlot(i, j); Models.TimeSlot T = new Models.TimeSlot(); foreach (Models.TimeSlot t in slots) if (t.ID == slotID) T = t; foreach (KeyValuePair<int, int> P in masterSchedule[i, j]) { Models.Activity A = new Models.Activity(); A.Group = tmpallGroups[P.Key]; A.Station = tmpallStations[P.Value]; A.TimeSlot = T; schedule.Add(A); } } } return schedule; }