How to fuzify a crispy variable - clips

I read some examples about how to fuzify a variable in FuzzyClips. I tried it by myself but i got some strange behaviour:
(deftemplate age-template
0 100 years ; Universe
(
(young (15 0.75) (17 1) (19 1))
(mature (18 0.75) (25 1) (30 0.25))
(adult (30 0.5) (40 1) (50 0.75))
(old (40 0.25) (50 0.75) (65 1) )
)
)
(deftemplate person
(slot name (type STRING))
(slot age (type INTEGER))
(slot city (type STRING))
)
(deffacts start
(person (name "John") (age 25) (city "London"))
(person (name "Mike") (age 55) (city "London"))
(person (name "Eva") (age 35) (city "London"))
)
(defrule fuzzify
(person (name ?name) (age ?age))
=>
(assert(age-template (?age 0.0) (?age 1.0) (?age 0.0)))
)
I don't know exactly what those "???" really mean and why each fact is being retracted after being asserted. I also have another doubt, i tried to print not only the age, but also the name and the age of each person with:
(defrule fuzzify
(person (name ?name)(age ?age))
=>
(assert (person-is ?name age-template (?age 0.0) (?age 1.0) (?age 0.0)))
)
But i got error "a function name must be a symbol".

So i solved it. I read that getting ??? is a common behaviour. So the solution is:
Create another deftemplate that will be the structure of the person name with their fuzzy age. In the rule's consecuent, instanciate the deftemplate asserting someone's name with the fuzzy age.
(deftemplate age-template
0 100 years ; Universe
(
(young (15 0.75) (17 1) (19 1))
(mature (18 0.75) (25 1) (30 0.25))
(adult (30 0.5) (40 1) (50 0.75))
(old (40 0.25) (50 0.75) (65 1) )
)
)
(deftemplate person
(slot name (type STRING))
(slot age (type INTEGER))
(slot city (type STRING))
)
(deftemplate fuzzyfication
(slot nameperson (type STRING))
(slot agefuzzy (type FUZZY-VALUE age-template))
)
(deffacts start
(person (name "John") (age 25) (city "London"))
(person (name "Mike") (age 55) (city "London"))
(person (name "Eva") (age 35) (city "London"))
)
(defrule fuzzify
(person (name ?name) (age ?age))
=>
(assert(fuzzyfication (nameperson ?name)(agefuzzy (?age 0.0) (?age 1.0) (?age 0.0))))
)
And the execution returns:

Related

Clips range find between 2 json inputs

I want to implemen a rule ;
Lets assume that ı have one input.json it consist a name value pair.Example;
{
"quality":"300"
}
I have another constant json ,Example
[{
"up":"100",
"down":"0",
"data":"xx"
},
{
"up":"200",
"down":"100",
"data":"yy"
},
,
{
"up":"300",
"down":"200",
"data":"zz"
}
]
I am trying to find propert value for data for which up and down range.
for this one ı, have to get zz because "quality":"300" is between 200-300.
how can ı success this one in clips rule.
CLIPS (6.31 6/12/19)
CLIPS>
(deftemplate pair
(slot name)
(slot value))
CLIPS>
(deffacts pairs
(pair (name "quality") (value 300)))
CLIPS>
(deftemplate data
(slot name)
(slot up)
(slot down))
CLIPS>
(deffacts data-values
(data (name "xx") (up 100) (down 0))
(data (name "yy") (up 200) (down 100))
(data (name "zz") (up 300) (down 200)))
CLIPS>
(defrule in-range
(pair (name "quality")
(value ?value))
(data (name ?name)
(up ?up)
(down ?down))
(test (and (> ?value ?down) (<= ?value ?up)))
=>
(printout t ?name " is in range." crlf))
CLIPS> (reset)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (pair (name "quality") (value 300))
f-2 (data (name "xx") (up 100) (down 0))
f-3 (data (name "yy") (up 200) (down 100))
f-4 (data (name "zz") (up 300) (down 200))
For a total of 5 facts.
CLIPS> (agenda)
0 in-range: f-1,f-4
For a total of 1 activation.
CLIPS> (run)
zz is in range.
CLIPS>

How can I get the sum of items in a multislot

I have a template like the one shown bellow. How can I get the sum of items in the multislot grades?
(deftemplate student
(multislot name)
(multislot grades)
)
Here's one way to do it. In the reg6 rule, the + function is given two arguments of 0 in addition to the grades to insure that the + function allows has at least 2 arguments; otherwise, if there were zero or one grades for the student you'd get an error.
CLIPS (6.31 2/3/18)
CLIPS>
(deftemplate student
(multislot name)
(multislot grades))
CLIPS>
(deftemplate sum
(multislot name)
(slot grade))
CLIPS>
(defrule reg6
(student (name $?name)
(grades $?grades))
=>
(assert (sum (name ?name)
(grade (+ 0 0 (expand$ ?grades))))))
CLIPS>
(assert (student (name David Green) (grades))
(student (name Sue Brown) (grades 90))
(student (name Frank Black) (grades 85 75)))
<Fact-3>
CLIPS> (run)
CLIPS> (facts)
f-0 (initial-fact)
f-1 (student (name David Green) (grades))
f-2 (student (name Sue Brown) (grades 90))
f-3 (student (name Frank Black) (grades 85 75))
f-4 (sum (name Frank Black) (grade 160))
f-5 (sum (name Sue Brown) (grade 90))
f-6 (sum (name David Green) (grade 0))
For a total of 7 facts.
CLIPS>
You can use the expand$ function. Check in the Basic Programming Guide the Multifield Expansion Function chapter to know more.
(deftemplate student
(multislot name)
(multislot grades))
(defrule grades-sum
(student (grades $?grades))
=>
(printout t "Student grades sum is " (+ (expand$ ?grades))))
(assert (student (grades (create$ 1 2 3 4 5))))
(student (name) (grades 1 2 3 4 5))
(run)
Student grades sum is 15

How can I find the ID of the variable that has the maximum value in CLIPS?

I tried to find the best student according to their exam marks. I took them from the user. I want CLIPS to give me the ID of the best student. For example, student1 mark 70, student2 mark 80 and student 3 mark 100. CLIPS should tell me "The best student is ... because his/her point is ..." I used global variables but I'm not sure if it's true because it doesn't work.
(defglobal ?*student1mark* = 0)
(defglobal ?*student2mark* = 0)
(defglobal ?*student3mark* = 0)
(defrule get-marks
=>
(printout t "What is the exam mark of student1?" crlf)
(bind ?*student1mark* (read))
(assert (stu1mark ?*student1mark*))
(printout t "What is the exam mark of student2?" crlf)
(bind ?*student2mark* (read))
(assert (stu2mark ?*student2mark*))
(printout t "What is the exam mark of student3?" crlf)
(bind ?*student3mark* (read))
(assert (stu3mark ?*student3mark*))
(build (str-cat
"(deffacts students (student student1 " ?*student1mark* " student student2 " ?*student2mark* " student student3 " ?*student3mark* "))")))
(defrule whichstudent
(student ?ID = (max ?*student1mark*" ?*student2mark*" ?*student3mark*))
=>
(printout t "The best student is " ?ID crlf))
I would not use global variables. I would go with a template and facts.
One solution with the help of a rule would be this:
CLIPS (6.30 3/17/15)
CLIPS> (deftemplate student
(slot id (type INTEGER) (default ?NONE))
(slot mark (type INTEGER) (default ?NONE))
)
CLIPS> (deffacts students
(student (id 1) (mark 80))
(student (id 2) (mark 79))
(student (id 4) (mark 60))
(student (id 3) (mark 90))
)
CLIPS> (defrule best-mark
(compare-students)
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
=>
(printout t "The best student is student no. " ?id crlf)
)
CLIPS> (reset)
CLIPS> (assert (compare-students))
<Fact-5>
CLIPS> (run)
The best student is student no. 3
The key part is
(student (id ?id) (mark ?mark))
(not
(student (id ?) (mark ?nmark&:(> ?nmark ?mark)))
)
So this rule matches with a student fact, if there is no other student with a higher mark.

How to printout all matches facts in a rule

I have a rule like below and I want to print all facts(here objct) that are compatible to this rule. I have a fact objct and there are some of these "objct"s that follow the rule. how I can do it? The printout below just prints the latest objct that player got. However I want to printout all of them. Any idea?
(defrule have_objcts
?input <- (input have|possession)
(objct (name ?n) (location player) (used 0))
=>
;WHAT SHOULD I WRITE HERE TO PRINTOUT those OBJCT's BELONG TO
PALYER???????????
(printout t crlf ?n)
(retract ?input))
Use the matches command:
CLIPS>
(deftemplate objct
(slot name)
(slot location)
(slot used))
CLIPS>
(deffacts initial
(objct (name x) (location player) (used 0))
(objct (name y) (location elsewhere) (used 1))
(objct (name z) (location player) (used 0))
(input have)
(input want)
(input possession)
(input thing))
CLIPS>
(defrule have_objcts
?input <- (input have|possession)
(objct (name ?n) (location player) (used 0))
=>
(retract ?input))
CLIPS> (reset)
CLIPS> (matches have_objcts)
Matches for Pattern 1
f-4
f-6
Matches for Pattern 2
f-1
f-3
Partial matches for CEs 1 - 2
f-6,f-3
f-6,f-1
f-4,f-3
f-4,f-1
Activations
f-6,f-3
f-6,f-1
f-4,f-3
f-4,f-1
(4 4 4)
CLIPS> (run)
CLIPS> (matches have_objcts)
Matches for Pattern 1
None
Matches for Pattern 2
f-1
f-3
Partial matches for CEs 1 - 2
None
Activations
None
(2 0 0)
CLIPS>

CLIPS counting facts or template instances that match the pattern

First I declare:
(deftemplate worker
(slot id
(type STRING)
(default ?DERIVE))
(slot salary
(type FLOAT)
(default ?DERIVE)))
then I add:
(assert(worker(id "a")(salary 30.0)))
(assert(worker(id "b")(salary 40.0)))
(assert(worker(id "c")(salary 60.0)))
(assert(worker(id "d")(salary 70.0)))
(assert(worker(id "e")(salary 10.0)))
How can I count how many 'workers' I have?
How can I count for example how many workers have salary over 30?
Use the fact-set query functions:
CLIPS>
(deftemplate worker
(slot id (type STRING) (default ?DERIVE))
(slot salary (type FLOAT) (default ?DERIVE)))
CLIPS> (assert (worker (id "a") (salary 30.0)))
<Fact-1>
CLIPS> (assert (worker (id "b") (salary 40.0)))
<Fact-2>
CLIPS> (assert (worker (id "c") (salary 60.0)))
<Fact-3>
CLIPS> (assert (worker (id "d") (salary 70.0)))
<Fact-4>
CLIPS> (assert (worker (id "e") (salary 10.0)))
<Fact-5>
CLIPS> (find-all-facts ((?f worker)) (> ?f:salary 30.0))
(<Fact-2> <Fact-3> <Fact-4>)
CLIPS> (length$ (find-all-facts ((?f worker)) (> ?f:salary 30.0)))
3
CLIPS> (do-for-all-facts ((?f worker)) (> ?f:salary 30.0) (printout t ?f:id crlf))
b
c
d
CLIPS>

Resources