def assign_validRecipe_everyMeal(self, csp): for meal in self.profile.meals: requests = [] for req in self.profile.requests: requests.append((req, meal)) orVar = util.get_or_variable(csp, (meal), requests, True) csp.add_unary_factor(orVar, lambda v: True if v else False)
def add_prereq_constraints(self, csp: CSP) -> None: """ Adding constraints to enforce prerequisite. A course can have multiple prerequisites. You can assume that *all courses in req.prereqs are being requested*. Note that if our parser inferred that one of your requested course has additional prerequisites that are also being requested, these courses will be added to req.prereqs. You will be notified with a message when this happens. Also note that req.prereqs apply to every single course in req.cids. If a course C has prerequisite A that is requested together with another course B (i.e. a request of 'A or B'), then taking B does not count as satisfying the prerequisite of C. You cannot take a course in a quarter unless all of its prerequisites have been taken *before* that quarter. You should take advantage of get_or_variable(). @param csp: The CSP where the additional constraints will be added to. """ # Iterate over all request courses for req in self.profile.requests: if len(req.prereqs) == 0: continue # Iterate over all possible quarters for quarter_i, quarter in enumerate(self.profile.quarters): # Iterate over all prerequisites of this request for pre_cid in req.prereqs: # Find the request with this prerequisite for pre_req in self.profile.requests: if pre_cid not in pre_req.cids: continue # Make sure this prerequisite is taken before the requested course(s) prereq_vars = [(pre_req, q) \ for i, q in enumerate(self.profile.quarters) if i < quarter_i] v = (req, quarter) orVar = get_or_variable(csp, (v, pre_cid), prereq_vars, pre_cid) # Note this constraint is enforced only when the course is taken # in `quarter` (that's why we test `not val`) csp.add_binary_factor(orVar, v, lambda o, val: not val or o)
def add_quarter_constraints(self, csp): """ If the profile explicitly wants a request to be satisfied in some given quarters, e.g. Aut2013, then add constraints to not allow that request to be satisfied in any other quarter. @param csp: The CSP where the additional constraints will be added to. """ # Problem 3a # BEGIN_YOUR_CODE (around 5 lines of code expected) for req in self.profile.requests: if len(req.quarters) == 0: continue variables = [(req, q) for q in self.profile.quarters if q not in req.quarters] orVars = [util.get_or_variable(csp, ("quarter", req, cid), variables, cid) for cid in req.cids] for v in orVars: csp.add_unary_factor(v, lambda a: not a)
def add_quarter_constraints(self, csp): """ If the profile explicitly wants a request to be satisfied in some given quarters, e.g. Aut2013, then add constraints to not allow that request to be satisfied in any other quarter. @param csp: The CSP where the additional constraints will be added to. """ # Problem 3a # BEGIN_YOUR_CODE (around 5 lines of code expected) for req in self.profile.requests: if len(req.quarters) == 0: continue variables = [(req, q) for q in self.profile.quarters if q not in req.quarters] orVars = [ util.get_or_variable(csp, ('quarter', req, cid), variables, cid) for cid in req.cids ] for v in orVars: csp.add_unary_factor(v, lambda a: not a)