Jess and FuzzyJ assistance - fuzzy

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) .

Related

CLIPS check the boundaries of a (read) value

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))

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))
=>)

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>

lisp iterate through list

I just started GIMP script-fu scripting yesterday for a script I need but I have no idea how automate the adding of the layers in the local variables and calling the function for adding the layers to the image. I tried iterating over lists but failed gracefully. Can someone push me in the right direction?
(define (script-fu-gifslot inText inFont inFontSize inTextColor)
(let*
((theImageWidth 700) ; define our local variables
(theImageHeight 100) ; create a new image:
(theImage)
(layerX)
(inOffset)
(theImage (car (gimp-image-new theImageWidth theImageHeight RGB)))
(theText) ;a declaration for the text
(theLayer (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 1" 100 NORMAL)))
(theLayer2 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 2" 100 NORMAL)))
(theLayer3 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 3" 100 NORMAL)))
(theLayer4 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 4" 100 NORMAL))))
(define (yannis-add-new-layer layerX inOffset)
(gimp-image-add-layer theImage layerX 0)
(gimp-context-set-background '(255 255 255))
(gimp-context-set-foreground inTextColor)
(gimp-drawable-fill layerX BACKGROUND-FILL)
(set! theText (car (gimp-text-fontname theImage layerX 0 0 inText 0 TRUE inFontSize PIXELS "Sans")))
(set! theImageHeight (car (gimp-drawable-height theText)))
(gimp-layer-resize layerX theImageWidth theImageHeight 0 0)
(gimp-layer-set-offsets theText 0 (- 0 inOffset)))
(yannis-add-new-layer theLayer 0)
(yannis-add-new-layer theLayer2 10)
(yannis-add-new-layer theLayer3 20)
(yannis-add-new-layer theLayer4 30)
(gimp-display-new theImage)
(list theImage layerX theText)))
(script-fu-register
"script-fu-gifslot"
"_GGGIF WORD Slotmachine..."
"Creates an image with a user specified text string."
"Yannis De Cleene <yannisdcl#gmail.com>"
"Yannis De Cleene"
"July, 2014"
""
SF-TEXT "Text" "PASTE\nMULTILINE TEXT\nIN HERE"
SF-FONT "Font" "Sans"
SF-ADJUSTMENT "Font size (pixels)" '(100 2 1000 1 10 0 1)
SF-COLOR "Color" '(0 0 0))
(script-fu-menu-register "script-fu-gifslot" "<Image>/File/Create")
Use either map or for-each or do.
If you want to add 1 to a list you could use (map 1+ '(1 2 3)). Or if you want to define the function on the fly, use lambda: (map (lambda (x) (+ 1 x)) '(1 2 3))
If you want a more imperative style use do. You can read about it here.
You can search for functions (and other bound symbols) using the function apropos. e.j. (apropos "for") (Apparently not in gimp to my surpise)
However I sense your problem is larger than just iterate over a list. For example does it make sense to define the function over the let? why are you not explicitly passing the image to the add-new-layer-function?
To make your code more readable I would drop the 'the', use '-' instead of camelCase and don't leave one parentesis in each line. it hurts readability. I can see however it is not your fault as that style is used in the gimp's tutorial. e.j. theImage becomes image and TheLayer1 becomes layer-1.
If you want to learn more about scheme you should download racket and read how to design programs which is a good introduction to programming.

hacker news algorithm in php?

This is the Hacker News ranking algorithm, which I think is a simple way of ranking things, espcially if users are voting on items, but I really dnt understand this, can this be converted to PHP, so I can understand it fully?
; Votes divided by the age in hours to the gravityth power.
; Would be interesting to scale gravity in a slider.
(= gravity* 1.8 timebase* 120 front-threshold* 1
nourl-factor* .4 lightweight-factor* .17 gag-factor* .1)
(def frontpage-rank (s (o scorefn realscore) (o gravity gravity*))
(* (/ (let base (- (scorefn s) 1)
(if (> base 0) (expt base .8) base))
(expt (/ (+ (item-age s) timebase*) 60) gravity))
(if (no (in s!type 'story 'poll)) .8
(blank s!url) nourl-factor*
(mem 'bury s!keys) .001
(* (contro-factor s)
(if (mem 'gag s!keys)
gag-factor*
(lightweight s)
lightweight-factor*
1)))))
Directly ripped from http://amix.dk/blog/post/19574 and translated to PHP from the Python:
function calculate_score($votes, $item_hour_age, $gravity=1.8){
return ($votes - 1) / pow(($item_hour_age+2), $gravity);
}
There are write-ups about how this algorithm works. A quick search discovered: How Hacker News ranking algorithm works.
Lisp can make things seem more complicated than they really are.

Resources