On May 7, 2008, at 11:17 PM, Jeff Brown wrote:
On Wed, May 7, 2008 at 12:04 PM, Ernest Friedman-Hill
<[EMAIL PROTECTED]> wrote:
There really isn't a better way to express this rule. It could be
made
somewhat
more efficient by rearranging the patterns, but just in terms of
expressing
the
uniqueness of the square contents given these data structures,
that's about
it.
Can you explain how you might rearrange the patterns and how/why that
would effect efficiency? I appreciate the info.
It's exactly the kind of thing that's described in this FAQ question
http://www.jessrules.com/jess/FAQ.shtml#Q12
(I'll wait here while you go read it.)
Finished? Good.
So as written, this rule forms 9x8x7x6x5x4x3x2 + 8x7x6x5x4x3x2 +
7x6x... partial matches (a large number!) with the "square" patterns,
then winnows through all of them with the "test" patterns. This is the
thing you want to avoid in writing rules, because it
is both very memory- and compute- intensive to assemble all those
partial matches.
You can drastically reduce the number just by moving some of the tests
up higher in the rule, to the point where they first apply. For
example, the very first "test" applies as soon as the first three
square patterns have matched, so this test should be moved up to be
the fourth pattern in the rule. The second test applies after the
first six patterns, so it should become the eight pattern in the rule.
The number of partial matches is then reduces by a factor of the
square of the number of sets of 3 digits that don't add to 15
(whatever that is!)
Here's the original rule for reference:
(defrule find-solution
(square (number 1) (hasValue ?value1))
(square (number 2) (hasValue ?value2&~?value1))
(square (number 3) (hasValue ?value3&~?value2&~?value1))
(square (number 4) (hasValue ?value4&~?value3&~?value2&~?value1))
(square (number 5) (hasValue ?value5&~?value4&~?value3&~?value2&~?
value1))
(square (number 6) (hasValue
?value6&~?value5&~?value4&~?value3&~?value2&~?value1))
(square (number 7) (hasValue
?value7&~?value6&~?value5&~?value4&~?value3&~?value2&~?value1))
(square (number 8) (hasValue
?value8&~?value7&~?value6&~?value5&~?value4&~?value3&~?value2&~?value1))
(square (number 9) (hasValue
?value9&~?value8&~?value7&~?value6&~?value5&~?value4&~?value3&~?
value2&~?value1))
;; rows must sum 15
(test (= (+ ?value1 ?value2 ?value3) 15))
(test (= (+ ?value4 ?value5 ?value6) 15))
(test (= (+ ?value7 ?value8 ?value9) 15))
;; columns must sum 15
(test (= (+ ?value1 ?value4 ?value7) 15))
(test (= (+ ?value2 ?value5 ?value8) 15))
(test (= (+ ?value3 ?value6 ?value9) 15))
;; diagonals must sum 15
(test (= (+ ?value3 ?value5 ?value7) 15))
(test (= (+ ?value1 ?value5 ?value9) 15))
=>
(printout t ?value1 " " ?value2 " " ?value3 crlf)
(printout t ?value4 " " ?value5 " " ?value6 crlf)
(printout t ?value7 " " ?value8 " " ?value9 crlf crlf))
---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
PO Box 969, MS 9012 [EMAIL PROTECTED]
Livermore, CA 94550 http://www.jessrules.com
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------