// note new inflight unit from the current study plan private void PickUnit(UnitInPlan unit) { oldPosition = unit.semester; plan.Remove(unit); inFlightUnitCode = unit.code; inFlightStudyArea = unit.studyArea; }
private static StudyPlan scheduleRemaning(IEnumerable <PlannedUnit> remainingUnits, StudyPlan plannedUnits) { try { if (!remainingUnits.Any()) { return(plannedUnits); } PlannedUnit schedulableUnit = remainingUnits.First( unit => unit.possibleSemesters.Where( sem => StudyPlannerModel.isEnrollableIn(unit.code, sem, plannedUnits) ).Any() ); foreach (var sem in schedulableUnit.possibleSemesters) { if (StudyPlannerModel.isEnrollableIn(schedulableUnit.code, sem, plannedUnits)) { UnitInPlan newUnitPlan = new UnitInPlan(schedulableUnit.code, schedulableUnit.studyArea, sem); StudyPlan scheduleEachSem = scheduleRemaning(remainingUnits.Where(unit => unit.code != schedulableUnit.code), plannedUnits.Concat(new UnitInPlan[] { newUnitPlan })); if (scheduleEachSem != null) { return(scheduleEachSem); } } } }catch (Exception ex) //if can't find any schedulable unit while there are still units needed to add to the plan { return(null); } return(null); //if can't add all the remaining units }
public static StudyPlan scheduleRemaining(List <PlannedUnit> boundUnits, List <UnitInPlan> plan) { if (boundUnits.Count == 0) // all units have been scheduled, so return plan { return(plan); } PlannedUnit firstUnit = boundUnits.FirstOrDefault(unit => unit.possibleSemesters .Any(sem => StudyPlannerModel.isEnrollableIn(unit.code, sem, plan))); if (firstUnit is null) // we have failed in finding an enrollable unit, so cannot complete schedule { return(null); } // otherwise schedule in possible semester foreach (Semester sem in firstUnit.possibleSemesters) { if (StudyPlannerModel.isEnrollableIn(firstUnit.code, sem, plan)) { UnitInPlan newUnit = new UnitInPlan(firstUnit.code, firstUnit.studyArea, sem); IEnumerable <UnitInPlan> newPlan = plan.Append(newUnit); // add new unit to plan IEnumerable <PlannedUnit> newBound = boundUnits.Where(subject => subject.code != newUnit.code); // remove new unit from planned units StudyPlan final = scheduleRemaining(newBound.ToList(), newPlan.ToList()); // try to schedule the rest of the planned units // if we finish with a plan that is not null, then we hav succeeded and return plan if (!(final is null)) { return(final); } } } // otherwise no possible choices return(null); }