;;;====================================================== ;;; Intelligent Environment Sample Rules ;;; ;;; To execute, merely load, reset and run. ;;;====================================================== ;;;***************************** ;;; rule to advance simulation once no other rule is active ;;;***************************** (defglobal ?*mdpstate* = 1) (defrule update-state (declare (salience -10)) (run) ?otime <- (time ?hours ?minutes) ?opower <- (power ?usage) (light-cost ?lc) (heat-cost ?hc) (ac-cost ?acc) (lights 1 ?lstate1) (ac 1 ?acstate1) ?t1 <- (temperature 1 ?temp1) (lights 2 ?lstate2) (ac 2 ?acstate2) ?t2 <- (temperature 2 ?temp2) (lights 3 ?lstate3) (ac 3 ?acstate3) ?t3 <- (temperature 3 ?temp3) (lights 4 ?lstate4) (ac 4 ?acstate4) ?t4 <- (temperature 4 ?temp4) (lights 5 ?lstate5) (ac 5 ?acstate5) ?t5 <- (temperature 5 ?temp5) (lights 6 ?lstate6) (ac 6 ?acstate6) ?t6 <- (temperature 6 ?temp6) => (bind ?dtime 2) (bind ?minutes (+ ?minutes ?dtime)) (if (> ?minutes 59) then (bind ?hours (+ ?hours 1)) (bind ?minutes (- ?minutes 60))) (retract ?otime) (assert (time ?hours ?minutes)) (if (eq ?lstate1 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?lstate2 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?lstate3 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?lstate4 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?lstate5 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?lstate6 on) then (bind ?usage (+ ?usage (* ?lc ?dtime)))) (if (eq ?acstate1 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t1) (bind ?temp1 (max (min (+ (+ ?temp1 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 1 ?temp1))) (if (eq ?acstate1 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t1) (bind ?temp1 (max (min (+ (- ?temp1 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 1 ?temp1))) (if (eq ?acstate1 off) then (retract ?t1) (bind ?temp1 (max (min (+ ?temp1 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 1 ?temp1))) (if (eq ?acstate2 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t2) (bind ?temp2 (max (min (+ (+ ?temp2 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 2 ?temp2))) (if (eq ?acstate2 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t2) (bind ?temp2 (max (min (+ (- ?temp2 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 2 ?temp2))) (if (eq ?acstate2 off) then (retract ?t2) (bind ?temp2 (max (min (+ ?temp2 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 2 ?temp2))) (if (eq ?acstate3 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t3) (bind ?temp3 (max (min (+ (+ ?temp3 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 3 ?temp3))) (if (eq ?acstate3 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t3) (bind ?temp3 (max (min (+ (- ?temp3 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 3 ?temp3))) (if (eq ?acstate3 off) then (retract ?t3) (bind ?temp3 (max (min (+ ?temp3 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 3 ?temp3))) (if (eq ?acstate4 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t4) (bind ?temp4 (max (min (+ (+ ?temp4 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 4 ?temp4))) (if (eq ?acstate4 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t4) (bind ?temp4 (max (min (+ (- ?temp4 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 4 ?temp4))) (if (eq ?acstate4 off) then (retract ?t4) (bind ?temp4 (max (min (+ ?temp4 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 4 ?temp4))) (if (eq ?acstate5 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t5) (bind ?temp5 (max (min (+ (+ ?temp5 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 5 ?temp5))) (if (eq ?acstate5 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t5) (bind ?temp5 (max (min (+ (- ?temp5 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 5 ?temp5))) (if (eq ?acstate5 off) then (retract ?t5) (bind ?temp5 (max (min (+ ?temp5 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 5 ?temp5))) (if (eq ?acstate6 heat) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t6) (bind ?temp6 (max (min (+ (+ ?temp6 3) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 6 ?temp6))) (if (eq ?acstate6 cool) then (bind ?usage (+ ?usage (* ?hc ?dtime))) (retract ?t6) (bind ?temp6 (max (min (+ (- ?temp6 2) (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 6 ?temp6))) (if (eq ?acstate6 off) then (retract ?t6) (bind ?temp6 (max (min (+ ?temp6 (- (mod (random) 3) 1)) 90) 30)) (assert (temperature 6 ?temp6))) (retract ?opower) (assert (power ?usage)) (assert (simulate))) (defrule day-end (declare (salience 10)) (time ?hours ?minutes) ?act <- (simulate) ?run <- (run) => (bind ?ctime (+ (* ?hours 60) ?minutes)) (if (> ?ctime 1380) then (retract ?act) (retract ?run))) (defrule heat-sim ?act <- (simulate) ?loc <- (location ?room t) ?acstate <- (ac ?room ?state) ?tstate <- (temperature ?room ?temp) => (if (> ?temp 77) then (retract ?act) (retract ?acstate) (assert (ac ?room cool))) (if (< ?temp 67) then (retract ?act) (retract ?acstate) (assert (ac ?room heat)))) (deffunction move (?from ?frloc ?frlights ?frlst ?to ?toloc ?tolights ?tolst ?nstate) (if (eq ?frlst off) then (retract ?frlights) (assert (lights ?from on)) else (if (eq ?tolst off) then (retract ?tolights) (assert (lights ?to on)) else (retract ?frloc) (retract ?toloc) (retract ?frlights) (assert (location ?from f)) (assert (lights ?from off)) (assert (location ?to t)) (bind ?*mdpstate* ?nstate)))) (defrule bedroom-sim ?act <- (simulate) ?loc1 <- (location 1 t) ?loc2 <- (location 2 f) ?loc3 <- (location 3 f) (time ?hours ?minutes) ?lstate1 <- (lights 1 ?state1) ?lstate2 <- (lights 2 ?state2) ?lstate3 <- (lights 3 ?state3) => (bind ?ctime (+ (* ?hours 60) ?minutes)) (switch ?*mdpstate* (case 1 then (retract ?act) (if (> ?ctime 465) then (move 1 ?loc1 ?lstate1 ?state1 2 ?loc2 ?lstate2 ?state2 2) else (if (eq ?state1 on) then (retract ?lstate1) (assert (lights 1 off))))) (case 3 then (retract ?act) (if (eq ?state1 off) then (retract ?lstate1) (assert (lights 1 on)) else (if (eq 0 (mod (random) 5)) then (move 1 ?loc1 ?lstate1 ?state1 3 ?loc3 ?lstate3 ?state3 4)))) (case 5 then (retract ?act) (if (eq ?state1 off) then (retract ?lstate1) (assert (lights 1 on)) else (if (eq 0 (mod (random) 5)) then (move 1 ?loc1 ?lstate1 ?state1 3 ?loc3 ?lstate3 ?state3 6)))) (case 17 then (retract ?act) (if (eq ?state1 off) then (retract ?lstate1) (assert (lights 1 on)) else (if (eq 0 (mod (random) 5)) then (move 1 ?loc1 ?lstate1 ?state1 2 ?loc2 ?lstate2 ?state2 18)))) )) (defrule bathroom-sim ?act <- (simulate) ?loc3 <- (location 3 f) ?loc2 <- (location 2 t) ?loc1 <- (location 1 f) (time ?hours ?minutes) ?lstate3 <- (lights 3 ?state3) ?lstate2 <- (lights 2 ?state2) ?lstate1 <- (lights 1 ?state1) => (switch ?*mdpstate* (case 2 then (retract ?act) (if (eq ?state2 off) then (retract ?lstate2) (assert (lights 2 on)) else (if (eq 0 (mod (random) 5)) then (move 2 ?loc2 ?lstate2 ?state2 1 ?loc1 ?lstate1 ?state1 3)))) (case 18 then (retract ?act) (if (eq ?state2 off) then (retract ?lstate2) (assert (lights 2 on)) else (if (eq 0 (mod (random) 5)) then (move 2 ?loc2 ?lstate2 ?state2 1 ?loc1 ?lstate1 ?state1 19)))) )) (defrule hallway-sim ?act <- (simulate) ?loc1 <- (location 1 f) ?loc3 <- (location 3 t) ?loc4 <- (location 4 f) (time ?hours ?minutes) ?lstate1 <- (lights 1 ?state1) ?lstate3 <- (lights 3 ?state3) ?lstate4 <- (lights 4 ?state4) => (switch ?*mdpstate* (case 4 then (retract ?act) (if (eq ?state3 off) then (retract ?lstate3) (assert (lights 3 on)) else (if (eq 0 (mod (random) 5)) then (move 3 ?loc3 ?lstate3 ?state3 1 ?loc1 ?lstate1 ?state1 5) else (if (eq 0 (mod (random) 3)) then (move 3 ?loc3 ?lstate3 ?state3 4 ?loc4 ?lstate4 ?state4 7))))) (case 6 then (retract ?act) (if (eq ?state3 off) then (retract ?lstate3) (assert (lights 3 on)) else (if (eq 0 (mod (random) 5)) then (move 3 ?loc3 ?lstate3 ?state3 4 ?loc4 ?lstate4 ?state4 7)))) (case 16 then (retract ?act) (if (eq ?state3 off) then (retract ?lstate3) (assert (lights 3 on)) else (if (eq 0 (mod (random) 1)) then (move 3 ?loc3 ?lstate3 ?state3 1 ?loc1 ?lstate1 ?state1 17)))) )) (defrule den-sim ?act <- (simulate) ?loc3 <- (location 3 f) ?loc4 <- (location 4 t) ?loc5 <- (location 5 f) ?loc6 <- (location 6 f) (time ?hours ?minutes) ?lstate3 <- (lights 3 ?state3) ?lstate4 <- (lights 4 ?state4) ?lstate5 <- (lights 5 ?state5) ?lstate6 <- (lights 6 ?state6) => (switch ?*mdpstate* (case 7 then (retract ?act) (if (eq ?state4 off) then (retract ?lstate4) (assert (lights 4 on)) else (if (eq 0 (mod (random) 5)) then (move 4 ?loc4 ?lstate4 ?state4 5 ?loc5 ?lstate5 ?state5 8) else (if (eq 0 (mod (random) 3)) then (move 4 ?loc4 ?lstate4 ?state4 6 ?loc6 ?lstate6 ?state6 9))))) (case 10 then (retract ?act) (if (eq ?state4 off) then (retract ?lstate4) (assert (lights 4 on)) else (retract ?lstate4) (assert (lights 4 off)) (retract ?loc4) (assert (location 4 f)) (bind ?*mdpstate* 11))) (case 12 then (retract ?act) (if (eq ?state4 off) then (retract ?lstate4) (assert (lights 4 on)) else (if (eq 0 (mod (random) 2)) then (move 4 ?loc4 ?lstate4 ?state4 5 ?loc5 ?lstate5 ?state5 13) else (move 4 ?loc4 ?lstate4 ?state4 6 ?loc6 ?lstate6 ?state6 14)))) (case 15 then (retract ?act) (if (eq ?state4 off) then (retract ?lstate4) (assert (lights 4 on)) else (move 4 ?loc4 ?lstate4 ?state4 3 ?loc3 ?lstate3 ?state3 16))) )) (defrule livingroom-sim ?act <- (simulate) ?loc4 <- (location 4 f) ?loc5 <- (location 5 t) ?loc6 <- (location 6 f) (time ?hours ?minutes) ?lstate4 <- (lights 4 ?state4) ?lstate5 <- (lights 5 ?state5) ?lstate6 <- (lights 6 ?state6) => (switch ?*mdpstate* (case 8 then (retract ?act) (if (eq ?state5 off) then (retract ?lstate5) (assert (lights 5 on)) else (if (eq 0 (mod (random) 4)) then (move 5 ?loc5 ?lstate5 ?state5 4 ?loc4 ?lstate4 ?state4 10)))) (case 13 then (retract ?act) (if (eq ?state5 off) then (retract ?lstate5) (assert (lights 5 on)) else (if (eq 0 (mod (random) 8)) then (move 5 ?loc5 ?lstate5 ?state5 4 ?loc4 ?lstate4 ?state4 15)))) )) (defrule kitchen-sim ?act <- (simulate) ?loc4 <- (location 4 f) ?loc6 <- (location 6 t) (time ?hours ?minutes) ?lstate4 <- (lights 4 ?state4) ?lstate6 <- (lights 6 ?state6) => (switch ?*mdpstate* (case 9 then (retract ?act) (if (eq ?state6 off) then (retract ?lstate6) (assert (lights 6 on)) else (if (eq 0 (mod (random) 4)) then (move 6 ?loc6 ?lstate6 ?state6 4 ?loc4 ?lstate4 ?state4 10)))) (case 14 then (retract ?act) (if (eq ?state6 off) then (retract ?lstate6) (assert (lights 6 on)) else (if (eq 0 (mod (random) 7)) then (move 6 ?loc6 ?lstate6 ?state6 4 ?loc4 ?lstate4 ?state4 15)))) )) (defrule return-sim ?act <- (simulate) ?loc1 <- (location 1 ?) ?loc2 <- (location 2 ?) ?loc3 <- (location 3 ?) ?loc4 <- (location 4 f) ?loc5 <- (location 5 ?) ?loc6 <- (location 6 ?) (time ?hours ?minutes) ?lstate4 <- (lights 4 ?state4) => (bind ?ctime (+ (* ?hours 60) ?minutes)) (switch ?*mdpstate* (case 11 then (if (> ?ctime 1020) then (retract ?act) (retract ?lstate4) (assert (lights 4 on)) (retract ?loc1) (retract ?loc2) (retract ?loc3) (retract ?loc4) (retract ?loc5) (retract ?loc6) (assert (location 1 f)) (assert (location 2 f)) (assert (location 3 f)) (assert (location 4 t)) (assert (location 5 f)) (assert (location 6 f)) (bind ?*mdpstate* 12))) )) (defrule intruder-sim ?act <- (simulate) ?loc4 <- (location 4 ?state4) ?loc5 <- (location 5 ?state5) ?loc3 <- (location 3 ?state3) (time ?hours ?minutes) => (bind ?ctime (+ (* ?hours 60) ?minutes)) (switch ?*mdpstate* (case 11 then (retract ?act) (if (eq ?state4 t) then (if (eq 0 (mod (random) 3)) then (retract ?loc4) (assert (location 4 f)) (if (< ?ctime 900) then (if (eq 0 (mod (random) 2)) then (retract ?loc3) (assert (location 3 t)) else (retract ?loc5) (assert (location 5 t))))) else (if (eq ?state5 t) then (if (eq 0 (mod (random) 3)) then (retract ?loc5) (assert (location 5 f)) (if (eq 0 (mod (random) 2)) then (retract ?loc3) (assert (location 3 t)) else (retract ?loc4) (assert (location 4 t)))) else (if (eq ?state3 t) then (if (eq 0 (mod (random) 3)) then (retract ?loc3) (assert (location 3 f)) (if (eq 0 (mod (random) 2)) then (retract ?loc4) (assert (location 4 t)) else (retract ?loc5) (assert (location 5 t)))) else (if (eq 0 (mod (random) 20)) then (retract ?loc4) (assert (location 4 t))))))) ))