Jess Information

Jess Home
Jess 7 Features
Download Now!
Online Demo

Mailing List
Jess Wiki

More information Related Web Sites
User Contributions
JSR94 Info
Developer's Log
About This Site

JESS ®, the Rule Engine for the JavaTM Platform

Jess Wiki: Salient Ant Simulator

Salient Ant Simulator

The astute reader will note that with the ants there was a change in priority: defending the colony suddenly had higher priority over gathering food. We could turbo-charge this "sense of urgency" in a Jess rule by using a

(declare (auto-focus TRUE))

statement and specific fact patterns. We could also create it indirectly by manipulating the rule's salience.

To Run The Salient Ant Simulator

  1. Copy the code below into a Jess CLP file. Name it what you like.
  2. From the Jess command line or the JessDE, run the script several times to see how the simulation reacts to varying data.
  3. Change the salience values and try it again.

If the ant completes all his tasks without being killed or eaten, he wins! (I tweaked the default salience values to make it a bit more interesting than in my first posting. Nature has a greater role this time. - JM)

;; Salient Ant Simulator
;; by Jason Morris

;(watch all)

;; An ant's priorities
(defglobal ?*threat* = 10000)
(defglobal ?*survival* = 100)
(defglobal ?*chore* = -10)

;; Effect of nature
(defglobal ?*nature* = 0)

;; Useful variables
(defglobal ?*max* = 65536)
(defglobal ?*enemy-total* = 0)
(defglobal ?*survival-probability* = 0.5)
(defglobal ?*survival-limit* = 0.75)

;; Next, let's give our ant some behaviors
(deffunction gather-food (?food)
    (retract ?food)
    (printout t "Food item gathered OK" crlf))

(deffunction take-out-garbage (?item)
    (retract ?item)
    (printout t "Garbage emptied OK" crlf))

;; If he gets killed, then see if he was a hero to the colony
(deffunction check-hero-ant()
    (if (>= ?*enemy-total* 5) then
        (printout t "This ant was a hero!" crlf)))

(deffunction attack-ant (?ant)
    (printout t "Attacking enemy ant..." crlf)
    (bind ?survival-ratio (/ (random) ?*max*))
    (if  (>= ?*survival-probability* ?survival-ratio) then
        (printout t "Enemy ant killed :-D" crlf)
        ;; Give credit for surviving
        (bind ?*enemy-total* (+ ?*enemy-total* 1))
        ;; Account for increased experience up to a point
        (if (< ?*survival-probability* ?*survival-limit*) then
            (bind ?*survival-probability* (+ ?*survival-probability* 0.05)))
        (retract ?ant) else
        (printout t "Ant killed by enemy :-(" crlf)

;; Have nature disturb the environment in a few ways
(deffunction change-ant-environment()
    (if (>= (/ (random) ?*max*) 0.25) then
        (assert (food-source (gensym*))))
    (if (>= (/ (random) ?*max*) 0.9) then
        (assert (enemy-ant (gensym*)))))

;; We'll give a 50% chance of a predator appearing
(deffunction get-predator()
    (if (>= (/ (random) ?*max*) 0.5) then
        (assert (predator (gensym*))))
    (printout t "A predator has appeared!" crlf)
    ;; Unfortunately, predators stir up food items, so
    (assert (food-source (gensym*))))

;; We'll give a 50% chance of existing predator leaving
(deffunction remove-predator(?predator)
    (if (>= (/ (random) ?*max*) 0.5) then
        (retract ?predator)
        (printout t "Predator has gone away." crlf)))

;; And 75% chance of being eaten
(deffunction is-ant-eaten()
    (if (>= (/ (random) ?*max*) 0.25) then
        (printout t "Arrg! Ant got eaten!" crlf)
        (halt) else 
        (printout t "Ant avoided predator OK" crlf)))

;; A header
(deffunction print-banner()
    (printout t "Salient Ant Simulator" crlf)
    (printout t "---------------------" crlf))

;; Now we define what causes those behaviors to happen.
;; We can hypothesize that everything the ant does
;; influences its environment in some way.

(defrule attack-enemy-ant
    (declare (salience ?*threat*))
    ?ant <-(enemy-ant ?)
    (attack-ant ?ant))

(defrule gather-food
    (declare (salience ?*survival*))
    ?food <- (food-source ?)
    (gather-food ?food))

(defrule take-out-garbage
    (declare (salience ?*chore*))
    ?item <-(garbage-source ?)
    (take-out-garbage ?item))

(defrule food-attracts-ants
    (declare (salience ?*nature*))
    (exists (food-source ?))
    (assert(ant (gensym*))))

(defrule ant-attracts-other-ants
    (declare (salience ?*nature*))
    (exists (ant ?))
    ; twice as many 
    (assert(enemy-ant (gensym*)))
    (assert(ant (gensym*)))
    (assert(ant (gensym*))))

(defrule add-a-predator
    (declare (salience ?*nature*))
    (not (predator ?))

(defrule remove-a-predator
    (declare (salience ?*nature*))
    ?p<- (predator ?)
    (remove-predator ?p))

(defrule ant-got-eaten
    (declare (salience ?*nature*))
    (predator ?)

(defrule nothing-to-do
    (declare (salience ?*nature*))
    (not (food-source ?))       ; Nothing to eat
    (not (garbage-source ?)) ; Nothing to clean
    (not (enemy-ant ?))        ; Nothing to fight
    (printout t "Ant survived simulation!" crlf)

;; Finally, let's give the ant some things to do

(deffacts ant-environment
    (food-source 1)
    (garbage-source 1)
    (food-source 2)
    (garbage-source 2)
    (food-source 3)
    (food-source 4)
    (food-source 5)

;; Run our little ant world

Back to GoodSalience | Next to SalienceSummary

Front Page | Sandbox | Recent Changes | Powered by Friki | Last Edited: 28 March 2007