CLIPS check the boundaries of a (read) value - clips

I'm in the process of designing an expert system from a decision tree and one of the tests is to check the wildlife score. The user is asked to enter the wildlife score and 3 possible outcomes are decided.
A score of at least 20 rejects the proposal and ends the program.
A score of more than 10 but less than 20 moves on to test 4 but asserts the outcome will be second-best.
A score of no more than 10 simply moves to test 4.
The read line from the previous test:
(defrule wildlife-score(or(energy-level 2)(energy-level 3))
=> (printout t "What is the wildlife impact score?" crlf)
(assert(wildlife-impact(read))))
The following is where I am having trouble in comparing the read value to the outcome values. Any help would be appreciated.
(defrule reject-wildlife
(wildlife-impact ? (> ?wildlife-impact 20))
=> (assert(reject))
(printout t "Reject - completely unsuitable due to wildlife impact." crlf))

The correct syntax for your comparison is (wildlife-impact ?varname&:(> ?varname 20)).
(defrule reject-wildlife
(wildlife-impact ?score&:(> ?score 20))
=>
(assert (reject))
(printout t "Reject - completely unsuitable due to wildlife impact." crlf))

Related

How to OR and AND operator in CLIPS?

I need to OR and AND operator while expression evaluation, but when I use it to CLIPSDOS it give wrong result.
CLIPS (6.31 6/12/19)
CLIPS> ( and 0 1 )
TRUE
CLIPS>
I expect the output FALSE but it give TRUE
What could be wrong?
In C, the integer 0 is false and any other integer is true.
In CLIPS, the symbol FALSE is false and any other value is true.
CLIPS (6.31 6/12/19)
CLIPS> (and 0 1)
TRUE
CLIPS> (and 1 2)
TRUE
CLIPS> (and FALSE TRUE)
FALSE
CLIPS>

clips: modifying assert's variable in antecedent

I've been trying to modify one variable from one assert in the antecedent, but I haven't been able to do it.
In the consequent I can modify variables, with bind, assignments, etc. but I'd like to do the following:
(defrule test
?h<-(Currentposition ?x ?y)
(not (Explored (+ ?x 1) ?y))
=> (whatever)
So, the problem is that I have a character moving through a map, and I want to explore unknown cells, so I want to go to them, and in order to do so, I mark them as Explored.
In the example, I want to check if the south cell has been explored, how can I do it? I add 1 to x because I move 1 row below, x-> rows, y-> columns
Thanks
Use =, the return value constraint:
(defrule test
?h <- (Currentposition ?x ?y)
(not =(Explored (+ ?x 1) ?y))
=>)

I'm working with structures in scheme and need to extract and compare info inside of the structures

I'm making a structure in scheme to represent a person. The structure contains the information for the persons name age and gender
(define-struct person (name height gender)
Thats my code for the structure. Now i need to add a function that will categorize a person as tall or short based on their height. I don't know exactly how to go about adding a condition to a structure to 1) Identify if the person is a male of female
(define-struct person (name height gender)
(cond
[gender? ('male)]
Something along those lines i assume?
2) I also have to find out how tall the person is and declare whether or not they are tall based on if their height (in inches) exceeds 69 for a woman and 72 inches for men. And then add a way to print out that persons name along with whether or not they are tall or short.
Here is a little example. The example shows how to determine a whether a person is tall (I am leaving it as an exercise for you to figure out how to use different limits for men and women).
(define (tall? a-person)
(> (person-height a-person) 69)
(define (categorize a-person)
(cond
[(tall? a-person) "tall"]
[else "short"]))
(categorize (person "Susan" 172 'female))

Is there a way to delay execution in CLIPS

I'm writing a clips code that goes through for loop , and it print facts .
I wanna know if there is a way to delay execution for 10 seconds , after printing the first fact , then another 10 second at the second iteration ?
so is there a delay function like java?
There's nothing built-in that allows you to sleep the CLIPS process for a fix amount of time, but you can write a function which loops for a specified amount of time before returning:
CLIPS>
(deffunction pause (?delay)
(bind ?start (time))
(while (< (time) (+ ?start ?delay)) do))
CLIPS> (pause 5)
FALSE
CLIPS>

Jess and FuzzyJ assistance

I'm trying to learn Jess and FuzzyJ but am having problems getting a simple program to run. I have looked at it for hours and am no quite sure why it doesn't run. If someone could point me in the right direction it would be very much appreciated.
;;;;;;;;;;;;;;;;;;;;;;;;;
Fuzzy Variables
;;;;;;;;;;;;;;;;;;;;;;;;;;
(defglobal ?*income* =
(new nrc.fuzzy.FuzzyVariable "income" 0.0 230000.00 "dollars"))
(defglobal ?*stability* =
(new nrc.fuzzy.FuzzyVariable "stability" 0.0 1.0 "index"))
(defglobal ?*liquidity* =
(new nrc.fuzzy.FuzzyVariable "liquidity" 0.0 1.0 "index"))
(defrule initial-terms
(declare (salience 100))
=>
(import nrc.fuzzy.*)
(load-package nrc.fuzzy.jess.FuzzyFunctions)
;;;;;;;;;;;;;;;;;;;;;
Primary Terms
;;;;;;;;;;;;;;;;;;;;;;;
(?*income* addTerm "low" (new ZFuzzySet 30000.00 80000.00))
(?*income* addTerm "medium" (new PIFuzzySet 100000.00 60000.00))
(?*income* addTerm "high" (new SFuzzySet 80000.00 190000.00))
(?*stability* addTerm "low" (new ZFuzzySet .3 .5))
(?*stability* addTerm "medium" (new PIFuzzySet .6 .4))
(?*stability* addTerm "high" (new SFuzzySet .7 .9))
(?*liquidity* addTerm "low" (new ZFuzzySet .3 .5))
(?*liquidity* addTerm "medium" (new PIFuzzySet .6 .4))
(?*liquidity* addTerm "high" (new SFuzzySet .7 . 9))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Fuzzy Rules
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defrule rule-1 "low income => liquidity very low"
(theIncome ?x &: (fuzzy-match ?x "low"))
=>
(assert(theLiquidity (new nrc.fuzzy.FuzzyValue ?*liquidity* "very low")))
(defrule rule-2 "high income & high stability => very high liquidity"
(theIncome ?x &: (fuzzy-match ?x "high"))
(theStability ?y (fuzzy-match ?y "high"))
=>
(assert(theLiquidity (new nrc.fuzzy.FuzzyValue ?*liquidity* "very high"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Defuzzification
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defrule defuzzification-and-display-liquidity
(declare (salience -1))
?f <- (theLiquidity ?z)
=>
(printout t (str-cat "Liquidity: " (?z momentDefuzzify)))
retract( ?f)
(halt))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Start up Rule/Fuzzify
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defrule assert-income-and-stability "initialization"
=>
(printout t "Enter the income(ex. 52000): ")
(bind ?income-value (float (readline t)))
(printout t "Enter the stability index(ex. 0.64): ")
(bind ?stability-value (float(readline t)))
(assert(theIncome
(new nrc.fuzzy.FuzzyValue ?*income*
(new nrc.fuzzy.TriangleFuzzySet
(- ?income-value 3000.0)
?income-value
(+ ?income-value 3000.0)))))
(assert(theStability
(new nrc.fuzzy.FuzzyValue ?*stability*
(new nrc.fuzzy.TriangleFuzzySet
(- ?stability-value 3000.0)
?stability-value
(+ ?stability-value 3000.0))))))
(reset)
(run)
There are many small syntax errors in this program; in general the Jess interpreter does a good job of pointing them out. First of all, in each of your comment blocks, you've got the actual text of the comment... not commented. So add a semicolon to the beginning of the lines like "Fuzzy Variables", for instance.
Second, on the line
(?*liquidity* addTerm "high" (new SFuzzySet .7 . 9))
there ought to be no space after that last decimal point.
Third, the rules rule-1 and rule-2 don't have enough close-parentheses at the end. Any decent programmer's editor able to format Lisp code should be able to help you fix that.
Fourth, on the line
(theStability ?y (fuzzy-match ?y "high"))
you're missing the "&:" before the predicate function -- see the previous line.
Finally, I think, the line
retract( ?f)
is malformed -- should be (retract ?f) .

Resources