chess - Eight Queens Puzzle in CLIPS -
i'm trying develop solver 8 queens problem (https://en.wikipedia.org/wiki/eight_queens_puzzle) in clips, i'm newbie in language. first of all, trying make rule verify new assertion comparing column/line of previous assertions. working when inserted duplicated line, when inserted duplicated column, doesn't detect it. what's wrong code?
(defrule verificaassercaodamas ; verifica se atende regras ?novaposicao <- (d ?line ?column) ?posicao <- (d ?line2 ?column2) (test (neq ?posicao ?novaposicao)) (test (or (eq ?line2 ?line) (eq ?column column2)) ) => (retract ?novaposicao) (if (< (+ ?column 1) 9) (assert (d ?line (+ ?column 1) )) ) clips> (assert(d 0 0)) <fact-1> clips> (assert(d 1 0)) <fact-2> clips> (assert(d 0 1)) <fact-3> clips> (agenda) 0 cerificaassercaodamas: f-3, f-1 0 cerificaassercaodamas: f-1, f-3 total of 2 activations. clips>
you're using expression (eq ?column column2) comparing variable ?column symbol column2. need compare variable ?column2.
clips> (clear) clips> (defrule verificaassercaodamas ?novaposicao <- (d ?line ?column) ?posicao <- (d ?line2 ?column2) (test (neq ?posicao ?novaposicao)) (test (or (eq ?line2 ?line) (eq ?column ?column2))) => (retract ?novaposicao) (if (< (+ ?column 1) 9) (assert (d ?line (+ ?column 1))))) clips> (assert (d 0 0)) <fact-1> clips> (assert (d 1 0)) <fact-2> clips> (assert (d 0 1)) <fact-3> clips> (agenda) 0 verificaassercaodamas: f-3,f-1 0 verificaassercaodamas: f-1,f-3 0 verificaassercaodamas: f-2,f-1 0 verificaassercaodamas: f-1,f-2 total of 4 activations. clips>
if you're testing equality/inequality of numbers, should use = , != (or <>) functions these throw errors non-numeric arguments:
clips> (clear) clips> (defrule verificaassercaodamas ?novaposicao <- (d ?line ?column) ?posicao <- (d ?line2 ?column2) (test (neq ?posicao ?novaposicao)) (test (or (= ?line2 ?line) (= ?column column2))) => (retract ?novaposicao) (if (< (+ ?column 1) 9) (assert (d ?line (+ ?column 1))))) [argacces5] function = expected argument #2 of type integer or float error: (defrule main::verificaassercaodamas ?novaposicao <- (d ?line ?column) ?posicao <- (d ?line2 ?column2) (test (neq ?posicao ?novaposicao)) (test (or (= ?line2 ?line) (= ?column column2) clips>
you can remove duplicate activations checking fact index of ?novaposicao greater 1 ?posicao:
clips> (clear) clips> (defrule verificaassercaodamas ?novaposicao <- (d ?line ?column) ?posicao <- (d ?line2 ?column2) (test (< (fact-index ?posicao) (fact-index ?novaposicao))) (test (or (= ?line2 ?line) (= ?column ?column2))) => (retract ?novaposicao) (if (< (+ ?column 1) 9) (assert (d ?line (+ ?column 1))))) clips> (assert (d 0 0)) <fact-1> clips> (assert (d 1 0)) <fact-2> clips> (assert (d 0 1)) <fact-3> clips> (agenda) 0 verificaassercaodamas: f-3,f-1 0 verificaassercaodamas: f-2,f-1 total of 2 activations. clips>
Comments
Post a Comment