Esempio n. 1
0
 def __init__(self, free_sections_only=True, problem=None, constraint=None):
     self.p = Problem()
     if problem is not None:
         self.p = problem
     self.free_sections_only = free_sections_only
     self.section_constraint = constraint or section_constraint
     self.clear_excluded_times()
Esempio n. 2
0
class Scheduler(object):
    """High-level API that wraps the course scheduling feature.

    ``free_sections_only``: bool. Determines if the only the available sections should be
                            used when using courses provided. Defaults to True.
    ``problem``: Optional problem instance to provide. If None, the default one is created.

    """
    def __init__(self, free_sections_only=True, problem=None):
        self.p = Problem()
        if problem is not None:
            self.p = problem
        self.free_sections_only = free_sections_only
        self.clear_excluded_times()

    def clear_excluded_times(self):
        """Clears all previously set excluded times."""
        self._excluded_times = []
        return self

    def exclude_time(self, start, end, days):
        """Added an excluded time by start, end times and the days.

        ``start`` and ``end`` are in military integer times (e.g. - 1200 1430).
        ``days`` is a collection of integers or strings of fully-spelt, lowercased days
                 of the week.
        """
        self._excluded_times.append(TimeRange(start, end, days))
        return self

    def exclude_times(self, *tuples):
        """Adds multiple excluded times by tuple of (start, end, days) or by
        TimeRange instance.

        ``start`` and ``end`` are in military integer times (e.g. - 1200 1430).
        ``days`` is a collection of integers or strings of fully-spelt, lowercased days
                 of the week.
        """
        for item in tuples:
            if isinstance(item, TimeRange):
                self._excluded_times.append(item)
            else:
                self.exclude_time(*item)
        return self

    def find_schedules(self, courses=None, generator=False, start=0):
        """Returns all the possible course combinations. Assumes no duplicate courses.

        ``return_generator``: If True, returns a generator instead of collection. Generators
            are friendlier to your memory and save computation time if not all solutions are
            used.
        """
        self.p.reset()
        self.create_variables(courses)
        self.create_constraints(courses)
        self.p.restore_point(start)
        if generator:
            return self.p.iter_solutions()
        return self.p.get_solutions()

    # internal methods -- can be overriden for custom use.
    def get_sections(self, course):
        """Internal use. Returns the sections to use for the solver for a given course.
        """
        return course.available_sections if self.free_sections_only else course.sections

    def time_conflict(self, schedule):
        """Internal use. Determines when the given time range conflicts with the set of
        excluded time ranges.
        """
        for timerange in self._excluded_times:
            if timerange.conflicts_with(schedule):
                return False
        return True

    def create_variables(self, courses):
        """Internal use. Creates all variables in the problem instance for the given
        courses. If given a dict of {course: sections}, will use the provided sections.
        """
        has_sections = isinstance(courses, dict)
        for course in courses:
            self.p.add_variable(course, courses.get(course, []) if has_sections else self.get_sections(course))

    def create_constraints(self, courses):
        """Internal use. Creates all constraints in the problem instance for the given
        courses.
        """
        for i, course1 in enumerate(courses):
            for j, course2 in enumerate(courses):
                if i <= j:
                    continue
                self.p.add_constraint(section_constraint, [course1, course2])
            self.p.add_constraint(self.time_conflict, [course1])
from csp import Problem

p = Problem()
pvars = "R2 R3 R5 R6 R7 R8 R9 R10 X Y Z".split()
# 0-151 is the possible finite range of the variables
p.addvars(pvars, xrange(152))
p.addrule("R7 == X + 11")
p.addrule("R8 == Y + 11")
p.addrule("R9 == Y + 4")
p.addrule("R10 == Z + 4")
p.addrule("R7 + R8 == 40")
p.addrule("R5 == R8 + R9")
p.addrule("R6 == R9 + R10")
p.addrule("R2 == 40 + R5")
p.addrule("R3 == R5 + R6")
p.addrule("R2 + R3 == 151")
p.addrule("Y == X + Z")
for sol in p.xsolutions():
    print[sol[k] for k in "XYZ"]
Esempio n. 4
0
 def setUp(self):
     self.p = Problem(BacktrackingSolver())
     self.p.add_variable('x', range(3))
     self.p.add_variable('y', range(3))
Esempio n. 5
0
class PointConstaintIntegrationTestForBacktracker(unittest.TestCase):
    def setUp(self):
        self.p = Problem(BacktrackingSolver())
        self.p.add_variable('x', range(3))
        self.p.add_variable('y', range(3))

    def result(self, *iter):
        return tuple(dict(x=x, y=y) for x, y in iter)

    def test_reset_problem(self):
        result1 = self.p.get_solutions()
        self.p.reset()
        self.p.add_variable('x', range(3))
        self.p.add_variable('y', range(3))
        result2 = self.p.get_solutions()
        self.assertEqual(result1, result2)

    def test_save_and_restore_iteration_position(self):
        it = self.p.iter_solutions()
        next(it)
        ref = self.p.save_point()
        second_answer = next(it)
        self.p.reset()
        self.p.add_variable('x', range(3))
        self.p.add_variable('y', range(3))
        self.p.restore_point(ref)
        answer = next(self.p.iter_solutions())
        self.assertEqual(second_answer, answer)

    def test_with_no_constraints_should_have_all_permutations(self):
        #self.p.add_constraint(lambda x, y: x != y, ['x', 'y'])
        expected = self.result((0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2))
        assertEqualContents(self.p.get_solutions(), expected)

    def test_should_have_non_equal_permutations(self):
        expected = self.result((0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1))
        self.p.add_constraint(lambda x, y: x != y, ['x', 'y'])
        assertEqualContents(self.p.get_solutions(), expected)

    def test_should_have_non_equal_permutations_or_sum_to_3(self):
        expected = self.result((0, 1), (0, 2), (1, 0), (2, 0))
        self.p.add_constraint(lambda x, y: x != y, ['x', 'y'])
        self.p.add_constraint(lambda x, y: x + y != 3, ['x', 'y'], [0, 0])
        assertEqualContents(self.p.get_solutions(), expected)

    def test_should_have_non_equal_permutations_while_x_is_lte_1(self):
        expected = self.result((0, 1), (0, 2), (1, 0), (1, 2))
        self.p.add_constraint(lambda x, y: x != y, ['x', 'y'])
        self.p.add_constraint(lambda x: x <= 1, ['x'], [0])
        assertEqualContents(self.p.get_solutions(), expected)
Esempio n. 6
0
 def setUp(self):
     self.p = Problem(BruteForceSolver())
     self.p.add_variable('x', range(3))
     self.p.add_variable('y', range(3))