Exemple #1
0
        /**
         * Returns an List of ScheduleStates with the schedules from the currentstate updated with the
         * new schedules with course in place.
         */
        private List <ScheduleState> insertIntoSchedule(ScheduleState currentState, Course course,
                                                        int period, List <string> possibleSchedules)
        {
            List <ScheduleState> results = new List <ScheduleState>();
            //Get the domainOfCourses and pop the first one off the stack.
            Stack <Course> domainOfCourses = currentState.getCourses();
            Course         currentCourse   = domainOfCourses.Pop();

            //Loop through the possible schedules.
            foreach (string possSchedule in possibleSchedules)
            {
                //Create a new ScheduleState with the current schedules and the remaining domainOfCourses.
                ScheduleState state           = new ScheduleState(currentState.getSchedules(), domainOfCourses);
                Schedule      classSchedule   = state.getSchedule(currentCourse.getCourseRoom());
                Schedule      teacherSchedule = state.getSchedule(currentCourse.getTeacherName());

                //Update the schedule.
                for (int i = 0; i < possSchedule.Length; i++)
                {
                    switch (possSchedule[i])
                    {
                    case 'M':
                        classSchedule.setCourse(0, period, currentCourse);
                        teacherSchedule.setCourse(0, period, currentCourse);
                        break;

                    case 'T':
                        classSchedule.setCourse(1, period, currentCourse);
                        teacherSchedule.setCourse(1, period, currentCourse);
                        break;

                    case 'W':
                        classSchedule.setCourse(2, period, currentCourse);
                        teacherSchedule.setCourse(2, period, currentCourse);
                        break;

                    case 'R':
                        classSchedule.setCourse(3, period, currentCourse);
                        teacherSchedule.setCourse(3, period, currentCourse);
                        break;

                    case 'F':
                        classSchedule.setCourse(4, period, currentCourse);
                        teacherSchedule.setCourse(4, period, currentCourse);
                        break;
                    }
                }
                results.Add(state);
            }
            return(results);
        }
        /**
         * Recursive backtracking search method using the remaining problem.  This method contains the algorithm
         * for the backtracking search.
         */
        private ScheduleState recursiveBacktrackingSearch(CSPSearchAgent remainingProblem)
        {
            //Check if the problem is complete.
            if (remainingProblem.isComplete())
            {
                //If so, return the completed scheduleState
                return(remainingProblem.getScheduleState());
            }

            //Retrieve an array of successorStates from the CSPSearchAgent.
            List <ScheduleState> successorStates = remainingProblem.getSuccessors();

            //Loop through the successor states.
            foreach (ScheduleState state in successorStates)
            {
                //Create a new CSPSearchAgent using the new successor state
                CSPSearchAgent nextProblem = new CSPSearchAgent(state);

                //If the problem does not violate constraints
                if (nextProblem.checkConstraints() == true)
                {
                    //Recursively search the next set.
                    ScheduleState result = recursiveBacktrackingSearch(nextProblem);

                    //If the result from the recursion is null,
                    if (result == null)
                    {
                        //continue with the next state in the for loop.
                        continue;
                    }
                    //Otherwise,
                    else
                    {
                        //return the result.
                        return(result);
                    }
                }
            }
            //If no solution is found, return null.
            return(null);
        }
Exemple #3
0
        /**
         * Takes the course and finds all successor states for that course.  Returns an ArrayList containing the
         * successors.
         */
        private List <ScheduleState> getArrayListOfSuccessors(ScheduleState currentState, Course course)
        {
            //ArrayList of ScheduleStates to house results.
            List <ScheduleState> results = new List <ScheduleState>();

            //Get the class and teacher's schedules for the given course.
            Schedule classSchedule   = currentState.getSchedule(course.getCourseRoom());
            Schedule teacherSchedule = currentState.getSchedule(course.getTeacherName());

            //For each period in the day
            for (int period = 0; period < classSchedule.getNumberOfPeriods(); period++)
            {
                //Get the list of possible days between the teacher and class schedules
                string daysPossible = intersectionOfStrings(classSchedule.openSlotsInSchedule(course, period),
                                                            teacherSchedule.openSlotsInSchedule(course, period));

                //Get the possible schedules for the class.
                List <string> possibleSchedules = getPossibleSchedules(daysPossible, course.getDaysPerWeek());
                results.AddRange(insertIntoSchedule(currentState, course, period, possibleSchedules));
            }
            return(results);
        }
 /**
  * Constructor taking the ScheduleState and creating a search agent for it.
  */
 public CSPSearchAgent(ScheduleState scheduleState)
 {
     //Assign it as the local variable.
     this.currentState = scheduleState;
 }
        /**
         * Returns an List of ScheduleStates with the schedules from the currentstate updated with the
         * new schedules with course in place.
         */
        private List<ScheduleState> insertIntoSchedule(ScheduleState currentState, Course course, 
            int period, List<string> possibleSchedules)
        {
            List<ScheduleState> results = new List<ScheduleState>();
            //Get the domainOfCourses and pop the first one off the stack.
            Stack<Course> domainOfCourses = currentState.getCourses();
            Course currentCourse = domainOfCourses.Pop();

            //Loop through the possible schedules.
            foreach (string possSchedule in possibleSchedules)
            {
                //Create a new ScheduleState with the current schedules and the remaining domainOfCourses.
                ScheduleState state = new ScheduleState(currentState.getSchedules(), domainOfCourses);
                Schedule classSchedule = state.getSchedule(currentCourse.getCourseRoom());
                Schedule teacherSchedule = state.getSchedule(currentCourse.getTeacherName());

                //Update the schedule.
                for (int i = 0; i < possSchedule.Length; i++)
                {
                    switch (possSchedule[i])
                    {
                        case 'M':
                            classSchedule.setCourse(0, period, currentCourse);
                            teacherSchedule.setCourse(0, period, currentCourse);
                            break;
                        case 'T':
                            classSchedule.setCourse(1, period, currentCourse);
                            teacherSchedule.setCourse(1, period, currentCourse);
                            break;
                        case 'W':
                            classSchedule.setCourse(2, period, currentCourse);
                            teacherSchedule.setCourse(2, period, currentCourse);
                            break;
                        case 'R':
                            classSchedule.setCourse(3, period, currentCourse);
                            teacherSchedule.setCourse(3, period, currentCourse);
                            break;
                        case 'F':
                            classSchedule.setCourse(4, period, currentCourse);
                            teacherSchedule.setCourse(4, period, currentCourse);
                            break;
                    }
                }
                results.Add(state);
            }
            return results;
        }
        /**
         * Takes the course and finds all successor states for that course.  Returns an ArrayList containing the
         * successors.
         */
        private List<ScheduleState> getArrayListOfSuccessors(ScheduleState currentState, Course course)
        {
            //ArrayList of ScheduleStates to house results.
            List<ScheduleState> results = new List<ScheduleState>();

            //Get the class and teacher's schedules for the given course.
            Schedule classSchedule = currentState.getSchedule(course.getCourseRoom());
            Schedule teacherSchedule = currentState.getSchedule(course.getTeacherName());

            //For each period in the day
            for (int period = 0; period < classSchedule.getNumberOfPeriods(); period++)
            {

                //Get the list of possible days between the teacher and class schedules
                string daysPossible = intersectionOfStrings(classSchedule.openSlotsInSchedule(course, period),
                    teacherSchedule.openSlotsInSchedule(course, period));

                //Get the possible schedules for the class.
                List<string> possibleSchedules = getPossibleSchedules(daysPossible, course.getDaysPerWeek());
                results.AddRange(insertIntoSchedule(currentState, course, period, possibleSchedules));

            }
            return results;
        }
Exemple #7
0
 /**
  * Constructor taking the ScheduleState and creating a search agent for it.
  */
 public CSPSearchAgent(ScheduleState scheduleState)
 {
     //Assign it as the local variable.
     this.currentState = scheduleState;
 }