def binary_ne_grid(kenken_grid): n = kenken_grid[0][0] variables, constraints = [], [] for i in range(n): variables.append([ Variable("%d%d" % (i, j), domain=list(range(1, n + 1))) for j in range(n) ]) tuples = [t for t in permutations(list(range(1, n + 1)), 2)] for i in range(n): for j in range(n): for k in range(j + 1, n): cons = Constraint("r%d%d%d" % (i, j, k), [variables[i][j], variables[i][k]]) cons.add_satisfying_tuples(tuples) constraints.append(cons) cons = Constraint("c%d%d%d" % (i, j, k), [variables[j][i], variables[k][i]]) cons.add_satisfying_tuples(tuples) constraints.append(cons) csp = CSP("binary_ne_grid", [v for row in variables for v in row]) for cons in constraints: csp.add_constraint(cons) return csp, variables
def create_week_model( store = None ): #employees_availability, man_req, special_const ): availabilities = store.get_availabilities() required_workers_mat=store.n_workers_table bool_exp = [w.experience for w in store.get_all_employees()] shifts_csp = CSP('skiPass_week') shifts_mat = create_variables_w(shifts_csp, availabilities) constraints__list = make_constraints_w(shifts_csp,shifts_mat, required_workers_mat,store) return shifts_csp
def create_daily_model(store = None,day= -1 ): #employees_availability, man_req, special_const ): availabilities = store.get_availabilities() required_workers_mat=store.n_workers_table csp_list = [] if day == -1: for d in range(7): daily_csp = CSP('skiPass_daily{}'.format(d)) shifts_list = create_variables_d(daily_csp, availabilities,d) constraints__list = make_constraints_d(daily_csp,shifts_list, required_workers_mat,d, store) csp_list.append(daily_csp) return csp_list else: daily_csp = CSP('skiPass_daily{}'.format(day)) shifts_list = create_variables_d(daily_csp, availabilities,day) constraints__list = make_constraints_d(daily_csp,shifts_list, required_workers_mat,day, store) return daily_csp
def prop_FC(csp: CSP, newVar=None): """Do forward checking. That is check constraints with only one uninstantiated variable. Remember to keep track of all pruned variable,value pairs and return """ pruned_variable_values = [] if newVar is not None: constraints = csp.get_cons_with_var(var=newVar) else: constraints = csp.get_all_cons() constraints = filter(lambda c: c.get_n_unasgn() == 1, constraints) for constraint in constraints: unassigned_variable = constraint.get_unasgn_vars()[0] for possible_value in unassigned_variable.cur_domain(): if not constraint.has_support(unassigned_variable, possible_value): pruned_variable_values.append((unassigned_variable, possible_value)) unassigned_variable.prune_value(possible_value) if len(unassigned_variable.cur_domain()) == 0: return False, pruned_variable_values return True, pruned_variable_values
def binary_ne_grid(kenken_grid): # Initialize dim = kenken_grid[0][0] Vars = [] Vars_1d = [] cons = [] # construct list of lists of Variable objects for i in range(dim): Vars.append([]) for j in range(dim): var = Variable("Cell_r%i_c%i"%(i+1,j+1), domain = [z for z in range(1, dim+1)]) Vars_1d.append(var) Vars[i].append(var) # construct csp for i in range(dim): for j in range(dim-1): for k in range(j+1, dim): #for row con = Constraint("ConstraintRow_r%i_c%i_c%i"%(i+1,j+1,k+1), [Vars[i][j],Vars[i][k]]) tuples = [] for Tuple in permutations(list(range(1,dim+1)),2): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) #for col con = Constraint("ConstraintCol_c%i_r%i_r%i"%(i+1,j+1,k+1), [Vars[j][i],Vars[k][i]]) tuples = [] for Tuple in permutations(list(range(1,dim+1)),2): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) csp = CSP("binary_ne_grid", Vars_1d) for con in cons: csp.add_constraint(con) return csp, Vars
def nary_ad_grid(kenken_grid): # Get the size of the grid and buid the domain of each variable size = kenken_grid[0][0] domain = list(range(1, size + 1)) # Instantiate all variables in size x size list and instantiate csp vars = [] for i in domain: vars.append([Variable(str(i) + str(j), domain) for j in domain]) csp = CSP('kenken', [var for row in vars for var in row]) # Create n-ary constraints valid = list(permutations(domain, size)) for i in range(size): cons_x = Constraint('r' + str(i), vars[i]) cons_y = Constraint('c' + str(i), [v[i] for v in vars]) cons_x.add_satisfying_tuples(valid) cons_y.add_satisfying_tuples(valid) csp.add_constraint(cons_x) csp.add_constraint(cons_y) return csp, vars
def binary_ne_grid(kenken_grid): # Get the size of the grid and buid the domain of each variable size = kenken_grid[0][0] domain = list(range(1, size + 1)) # Instantiate all variables in size x size list and instantiate csp vars = [] for i in domain: vars.append([Variable(str(i) + str(j), domain) for j in domain]) csp = CSP('kenken', [var for row in vars for var in row]) # Create binary constraints valid = list(permutations(domain, 2)) for i in range(size): for j in range(size): for k in range(j + 1, size): cons_x = Constraint('{0}{1}-{0}{2}'.format(i, j, k), [vars[i][j], vars[i][k]]) cons_y = Constraint('{1}{0}-{2}{0}'.format(i, j, k), [vars[j][i], vars[k][i]]) cons_x.add_satisfying_tuples(valid) cons_y.add_satisfying_tuples(valid) csp.add_constraint(cons_x) csp.add_constraint(cons_y) return csp, vars
def kenken_csp_model(kenken_grid): ''' Use nary_ad_grid ''' # Initialize dim = kenken_grid[0][0] Vars = [] Vars_1d = [] cons = [] # construct list of lists of Variable objects for i in range(dim): Vars.append([]) for j in range(dim): var = Variable("Cell_r%i_c%i"%(i+1,j+1), domain = [z for z in range(1, dim+1)]) Vars_1d.append(var) Vars[i].append(var) # nary_ad_grid for i in range(dim): #for row con = Constraint("ConstraintRow_r%i"%i, Vars[i]) tuples = [] for Tuple in permutations(list(range(1, dim+1))): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) #for col con = Constraint("ConstraintCol_r%i"%i, [(Vars[row][i]) for row in range(dim)]) tuples = [] for Tuple in permutations(list(range(1, dim+1))): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) # construct kenken constraints for idx, element in enumerate(kenken_grid[1:]): if len(element) == 2: con = Constraint("KenKen_%i"%idx, [(Vars[(element[0]//10)-1][(element[0]%10)-1])]) tuples = [[element[1]]] else: con = Constraint("KenKen_%i"%idx, [(Vars[(i//10)-1][(i%10)-1]) for i in element[:-2]]) tuples = [] for comb in product(list(range(1,dim+1)), repeat = (len(element)-2)): if element[-1] == 0: oper = operator.add elif element[-1] == 1: oper = operator.sub elif element[-1] == 2: oper = operator.truediv elif element[-1] == 3: oper = operator.mul if reduce(oper, comb) == element[-2]: for permu in permutations(comb): if permu not in tuples: tuples.append(permu) con.add_satisfying_tuples(tuples) cons.append(con) csp = CSP("kenken_csp_model", Vars_1d) for con in cons: csp.add_constraint(con) return csp, Vars