private static void Schedule(int startingDay, int startingSlot)
{
// start monday end Friday
int dayStart = startingDay, dayEnd = 5;
int Day, Slot, i, j, k;
populateStationAvailabilities(dayEnd);
for (Day = dayStart; Day <= dayEnd; Day++)
{
if (Day == dayStart)
Slot = startingSlot;
else
Slot = 1;
for (; Slot <= nSlots[Day]; Slot++)
{
if (lunchSlot[Day] == Slot)
continue;
// keep looking for assignments for this slot
while (true)
{
int groupSelected = -1;
int stationSelected = -1;
int maxScore = -1 << 30;
int minStationAssignment = 1 << 30;
int s;
// get best preference
int prefIndex = -1;
int prefScore = -1 << 30;
int prefGroup = -1;
int prefStation = -1;
int tmpIndex = -1;
// get other
int otherGroupSelected = -1;
int otherStationSelected = -1;
int otherScore = -1 << 30;
List<Group> groupsSortedByLeastAssgined = sortGroupsByLeastAssigned();
Group curGroup;
Station curStation;
int groupIndex, stationIndex;
for (i = 0; i < groupsSortedByLeastAssgined.Count; i++)
{
curGroup = groupsSortedByLeastAssgined[i];
groupIndex = getGroupIndex(curGroup);
tmpIndex = -1;
if (curGroup.nStationsPicked >= 3 || isGroupBusy[Day, Slot, groupIndex])
continue;
for (j = 0; j < 5; j++)
{
stationIndex = curGroup.StationPicks[j];
if (stationIndex == -1 || curGroup.StationPicked[j])
continue;
if (!canScheduleStationAtDaySlot(stationIndex, Day, Slot) || !canScheduleGroupToStationAtDaySlot(groupIndex, stationIndex, Day, Slot) )
continue;
tmpIndex = j;
break;
}
if (tmpIndex == -1)
continue;
s = score(AllStations[curGroup.StationPicks[tmpIndex]], curGroup, Day, Slot);
if (s > prefScore)
{
prefScore = s;
prefGroup = groupIndex;
prefIndex = tmpIndex;
prefStation = curGroup.StationPicks[prefIndex];
}
}
// get best constraint to be fullfilled
int constraintIndex = -1;
int constraintStation = -1;
int constraintGroup = -1;
int constraintScore = -1 << 30;
for (i = 0; i < AllConstraints.Count; i++)
{
if (ConstraintMet[i])
continue;
groupIndex = getGroupIndex(AllConstraints[i].G);
stationIndex = getStationIndex(AllConstraints[i].S);
if (!canScheduleStationAtDaySlot(stationIndex, Day, Slot) || !canScheduleGroupToStationAtDaySlot(groupIndex, stationIndex, Day, Slot) )
continue;
s = score(AllConstraints[i].S, AllConstraints[i].G, Day, Slot);
if (s > constraintScore)
{
constraintScore = s;
constraintIndex = i;
constraintGroup = groupIndex;
constraintStation = stationIndex;
}
}
if (prefIndex != -1 && constraintIndex == -1)
{
groupSelected = prefGroup;
stationSelected = prefStation;
maxScore = prefScore;
}
else if (prefIndex == -1 && constraintIndex != -1)
{
groupSelected = constraintGroup;
stationSelected = constraintStation;
maxScore = constraintScore;
}
else if (prefIndex != -1 && constraintIndex != -1)
{
if (prefIndex == 5)
{
groupSelected = constraintGroup;
stationSelected = constraintStation;
maxScore = constraintScore;
prefIndex = -1;
}
else
{
if (prefScore >= constraintScore)
{
groupSelected = prefGroup;
stationSelected = prefStation;
constraintIndex = -1;
maxScore = prefScore;
}
else
{
groupSelected = constraintGroup;
stationSelected = constraintStation;
prefIndex = -1;
maxScore = constraintScore;
}
}
}
for (i = 0; i < groupsSortedByLeastAssgined.Count; i++)
{
curGroup = groupsSortedByLeastAssgined[i];
groupIndex = getGroupIndex(curGroup);
if (isGroupBusy[Day, Slot, groupIndex])
continue;
for (j = 0; j < AllStations.Count; j++)
{
curStation = AllStations[j];
stationIndex = j;
if (!canScheduleStationAtDaySlot(stationIndex, Day, Slot) || !canScheduleGroupToStationAtDaySlot(groupIndex, stationIndex, Day, Slot))
continue;
s = score(curStation, curGroup, Day, Slot);
if (s > otherScore || s == otherScore && GroupStationAssignments[groupIndex, stationIndex] < minStationAssignment)
{
otherScore = s;
otherGroupSelected = groupIndex;
otherStationSelected = stationIndex;
minStationAssignment = GroupStationAssignments[groupIndex, stationIndex];
}
}
}
if (otherScore > maxScore || constraintIndex == -1 && prefIndex == -1)
{
groupSelected = otherGroupSelected;
stationSelected = otherStationSelected;
constraintIndex = prefIndex = -1;
}
if (groupSelected == -1 || stationSelected == -1)
break;
scheduleGroupToStationAtDaySlot(groupSelected, stationSelected, Day, Slot, constraintIndex, prefIndex);
}
}
}
}