Exemplo n.º 1
0
    def solve(self, restrictions):
        """
        Calculates all possible timetable calculations for the given modules
        and the given restrictions.
        """
        if not self.initialized:
            self._initialize()

        problem = Problem()
        for key, module_lessons in self.lectures.items():
            module_lessons = sorted(module_lessons, key=lambda l: l['abbrev'])
            sub_lectures = groupby(module_lessons, lambda l: l['abbrev'])
            for abbrev, lessons in sub_lectures:
                lessons = sorted(lessons, key=lambda l: l['class'])
                combinations = []
                for classnr, cls_lessons in groupby(lessons, lambda l: l['class']):
                    cls_lessons = sorted(cls_lessons, key=lambda l: l['type'])

                    temporary_combinations = []

                    grouped_by_type = [list(v) for k, v in groupby(cls_lessons, lambda l: l['type'])]
                    if len(grouped_by_type) == 1:
                        for g in grouped_by_type:
                            combinations.append(g)
                        continue
                    for lessons_by_type in grouped_by_type:
                        lessons_by_type = sorted(lessons_by_type, key=lambda l: l['team'])
                        u_or_v = []
                        for _, lessons_by_team in groupby(lessons_by_type, lambda l: l['team']):
                            u_or_v.append([l for l in lessons_by_team])

                        if len(temporary_combinations) == 0:
                            temporary_combinations = [u_or_v]
                        else:
                            for combi in temporary_combinations:
                                for x in combi:
                                    for y in u_or_v:
                                        combinations.append(x+y)

                problem.add_variable(abbrev, combinations)

        problem.add_constraint(self._unique_timing, problem._variables.keys())
        for restriction in restrictions:
            if restriction.is_constraint:
                problem.add_constraint(restriction.constraint, problem._variables.keys())

        start = datetime.now()
        solutions = problem.get_solutions()
        print("Calculation took %s seconds" % (datetime.now() - start).total_seconds())
        print("Found %s solutions!" % len(solutions))
        return solutions
Exemplo n.º 2
0
 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()