Prolog Applying Constraints to Original List Items in Matrix -
i working on project involves matrix. stuck in attempt apply constraints specific elements in matrix. matrix , domain definitions of main class/predicate. so, first 3 predicates creating matrix , defining domains while last 2 predicates labels different variables in regards domain , prints out.
test(solution, n) :- length(solution,8), maplist(length_(8), solution), %the variables maplist(define_domain, solution), %the domains constraint(solution), maplist(labeling([]), solution), %labeling variables maplist(print_row, solution). length_(length, list) :- length(list, length). define_domain(x):- x ins 0..9. ww(x):- write(x). print_row(row):- %nested maplist call works on each element in maplist(ww, row), nl.
so, constraints predicate applies constraint on variables having problems. have facts need capture tried using findall loop through facts , use them determine elements inside list of matrix need apply constraints to. facts contains row, column , length.
the nested findall predicate call uses row determine list inside matrix, column determine index of element of list take , extracting sublist based on index of element , length. outer findall predicate finds position predicates , collects sublists. therefore, have collection of sublists contains elements of matrix constraint needs applied. constraint applied in constraint_apply predicate.
however, discovered findall predicate creates copies of original list instead of using actual elements/variables in original list after struggling many hours. so, constraints being applied affects copies , not originals.
right now, thinking maybe using findall predicate wrongly apply constraints , trying out different ways use it.
i grateful if explain me better way propagate constraints in scenario.any helps/tips appreciated. how apply constraints original list , elements in matrix above?
constraint_apply([]). constraint_apply([x|xs]):- sum(x, #<, 10), all_different(x), constraint_apply(xs). %extract slice lsit constraint(xs):- findall(x, (position(r, c, l), end c+l-1, findall(y, (nth1(r, xs, n),between(c, end, m), nth1(m, n,y)), x)), row), constraint_apply(row).
i had similar problem, , solved using bagof/3 instead of findall/3. cannot test code, here hint needed syntax
constraint(xs):- bagof(x, r^c^l^end^( position(r, c, l), end c+l-1, bagof(y, n^m^(nth1(r, xs, n), between(c, end, m), nth1(m, n, y)), x) ), row), constraint_apply(row).
Comments
Post a Comment