示例#1
0
def create_csp():
    csp = CSP()
    csp.add_node(("physics", "John Smith"), [("648", (5, 60), "physics")])
    return csp
示例#2
0
def assigner(user_data):
    """Takes in data provided by the user and creates class schedule.

    Arguments:
        user_data {tuple} -- A tuple of lists containing the user's data information.

    Returns:
        [dict] -- Returns a map {day: a list of classes taught by professors with room numbers and times}.
    """
    def room_has_capacity(val, course):
        """Unary constraints function: checks to see if given room has
        room for all students in course.

        Arguments:
            val {tuple} -- Contains values for room and time of class.
            Course {string} -- Name of course.

        Returns:
            [bool] -- Whether or not the room has capacity for all students in course.
        """
        room, hour_and_min = val
        no_students = course_no_students[course]
        return room_capacities[room] >= no_students

    def no_class_overlap(node1, node2, course1, course2):
        """Binary constraint function: checks to see if there is overlap in times between two courses.

        Arguments:
            node1 {tuple} -- (location, (hour, minute)) of the first class.
            node2 {tuple} -- (location, (hour, minute)) of the second class.
            course1 {string} -- Name of first course to check for overlap.
            course2 {string} -- Name of second course to check for overlap.

        Returns:
            [int] -- 1 if no overlap exists between two classes, 0 if there is overlap.
        """
        _, (hours1, mins1) = node1
        _, (hours2, mins2) = node2
        course_start1, course_end1 = compute_course_start_end(
            hours1, mins1, course_mins_map, course1)
        course_start2, course_end2 = compute_course_start_end(
            hours2, mins2, course_mins_map, course2)
        if course_start1 > course_end2 or course_start2 > course_end1:
            return 0
        elif course_start1 == course_end2 or course_start2 == course_end1:
            return 2
        else:
            return 1

    def no_time_clash(val1, val2, course1, dummy):
        """Binary constraint function: checks to see if there is a time clash for a course given two rooms and times.

        Arguments:
            val1 {tuple} -- Contains first set of room and time.
            val2 {tuple} -- Contains second set of room and time.
            course1 {string} -- Name of course to check for time clash.
            dummy {string} -- Dummy parameter. Added to help with refactor.

        Returns:
            [int] -- 1 if no time clash between rooms and times for course, 0 if there is time clash.
        """
        (room1, time1) = val1
        (room2, time2) = val2
        if room1 != room2:
            return 1
        hours1, mins1 = time1
        hours2, mins2 = time2
        start_time1, end_time1 = compute_course_start_end(
            hours1, mins1, course_mins_map, course1)
        start_time2, _ = compute_course_start_end(hours2, mins2,
                                                  course_mins_map, dummy)
        if start_time1 <= start_time2 < end_time1:
            return 0
        return 1

    professors, prof_info, rooms, room_capacities, courses, \
        course_no_students, course_mins_map, course_days_weekly = user_data

    full_prof_assignment = profs_for_courses(courses, professors, prof_info)
    rooms_chosen = {}
    solution = collections.defaultdict(lambda: None)
    retry = 0
    solved = True
    while retry < 3:
        daily_courses = maps_day_to_class(course_days_weekly, courses)
        max_iters = 100 * (retry + 1)
        for day in WEEKDAYS:
            csp = CSP()
            courses = daily_courses[day]
            add_nodes(courses, rooms, rooms_chosen, full_prof_assignment,
                      prof_info, csp)
            add_unary_constraint(csp, room_has_capacity)
            add_binary_constraint(csp, course_mins_map, no_class_overlap,
                                  no_time_clash)
            min_conflict = minConflicts(csp)
            day_solution = min_conflict.solve(max_iters)
            if not day_solution:
                retry += 1
                solved = False
            else:
                solution[day] = day_solution
        if solved:
            break
        solved = True
    return solution
示例#3
0
 def setUp(self):
     self.csp = CSP()
     self.room_chosen = {}
     self.assigner = assigner(user_data=create_user_data())
示例#4
0
def create_csp2():
    csp = CSP()
    csp.add_node("class1", ["domain1"])
    csp.add_node("class2", ["domain2", "domain3", "domain4"])
    csp.add_node("class3", ["domain5", "domain6"])
    return csp
示例#5
0
def create_csp():
    csp = CSP()
    csp.add_node("class1", ["domain1"])
    return csp