Minor hacks trying to understand why some rules aren't working as expected.

This commit is contained in:
simon 2014-07-04 11:01:08 +01:00
parent ac92181ab3
commit 9b52b7229b
2 changed files with 45 additions and 36 deletions

View file

@ -59,18 +59,18 @@
* `generations` an (integer) number of generations." * `generations` an (integer) number of generations."
[world init-rules rules generations] [world init-rules rules generations]
(let [state {:world (transform-world world init-rules) :rules rules}] (let [state {:world (transform-world world init-rules) :rules rules}]
(dorun (take generations (iterate transform-world-state state))))) (take generations (iterate transform-world-state state))))
(defn animate-world ;; (defn animate-world
"Run this world with these rules for this number of generations, and return nil ;; "Run this world with these rules for this number of generations, and return nil
to avoid cluttering the screen. Principally for debugging. ;; to avoid cluttering the screen. Principally for debugging.
* `world` a world as discussed above; ;; * `world` a world as discussed above;
* `init-rules` a sequence of rules as defined above, to be run once to initialise the world; ;; * `init-rules` a sequence of rules as defined above, to be run once to initialise the world;
* `rules` a sequence of rules as definied above, to be run iteratively for each generation; ;; * `rules` a sequence of rules as definied above, to be run iteratively for each generation;
* `generations` an (integer) number of generations." ;; * `generations` an (integer) number of generations."
[world init-rules rules generations] ;; [world init-rules rules generations]
(let [state (list (transform-world world init-rules) rules)] ;; (let [state (list (transform-world world init-rules) rules)]
(dorun ;; (dorun
(take generations (iterate transform-world-state state))) ;; (take generations (iterate transform-world-state state)))
world)) ;; state))

View file

@ -28,7 +28,7 @@
(cond (and (cond (and
(= (:state cell) :heath) (= (:state cell) :heath)
;; browsing limit really ought to vary with soil fertility, but... ;; browsing limit really ought to vary with soil fertility, but...
(< (+ (population cell :deer)(population cell :sheep)) 6) (< (+ (get-int cell :deer)(get-int cell :sheep)) 6)
(< (get-int cell :altitude) treeline)) (< (get-int cell :altitude) treeline))
(merge cell {:state :scrub}))) (merge cell {:state :scrub})))
(fn [cell world] (cond (= (:state cell) :scrub) (merge cell {:state :forest}))) (fn [cell world] (cond (= (:state cell) :scrub) (merge cell {:state :forest})))
@ -75,59 +75,68 @@
;; rules describing herbivore behaviour ;; rules describing herbivore behaviour
(def herbivore-rules (def herbivore-rules
(list (list
;; if there are too many deer for the fertility of the area to sustain,
;; some die or move on.
(fn [cell world]
(cond (> (get-int cell :deer) (get-int cell :fertility))
(merge cell {:deer (get-int cell :fertility)})))
;; deer arrive occasionally at the edge of the map. ;; deer arrive occasionally at the edge of the map.
(fn [cell world] (fn [cell world]
(cond (and (< (count (get-neighbours world cell)) 8) (cond (and (< (count (get-neighbours world cell)) 8)
(< (rand 50) 1) (< (rand 50) 1)
(= (population cell :deer) 0)) (> (get-int cell :fertility) 0)
(= (get-int cell :deer) 0))
(merge cell {:deer 2}))) (merge cell {:deer 2})))
;; if there are too many deer for the fertility of the area to sustain,
;; some die or move on.
(fn [cell world]
(cond (> (* (population cell :deer) 10) (get-int cell :fertility))
(merge cell {:deer (int (/ (get-int cell :fertility) 10))})))
;; deer gradually spread through the world by breeding or migrating. ;; deer gradually spread through the world by breeding or migrating.
(fn [cell world] (fn [cell world]
(let [n (apply + (map #(population % :deer) (get-neighbours world cell)))] (let [n (apply + (map #(get-int % :deer) (get-neighbours world cell)))]
(cond (and (cond (and
(= (population cell :deer) 0) (> (get-int cell :fertility) 0)
(= (get-int cell :deer) 0)
(>= n 2)) (>= n 2))
(merge cell {:deer (int (/ n 2))})))) (merge cell {:deer (int (/ n 2))}))))
;; deer breed. ;; deer breed.
(fn [cell world] (fn [cell world]
(cond (cond
(>= (population cell :deer) 2) (>= (get-int cell :deer) 2)
(merge cell {:deer (int (* (:deer cell) 4))}))))) (merge cell {:deer (int (* (:deer cell) 2))})))))
;; rules describing predator behaviour ;; rules describing predator behaviour
(def predator-rules (def predator-rules
(list (list
;; wolves eat deer
(fn [cell world]
(cond
(>= (get-int cell :wolves) 1)
(merge cell {:deer (max 0 (- (get-int cell :deer) (get-int cell :wolves)))})))
;; ;; not more than eight wolves in a pack, for now (hack because wolves are not dying)
;; (fn [cell world]
;; (cond (> (get-int cell :wolves) 8) (merge cell {:wolves 8})))
;; if there are not enough deer to sustain the get-int of wolves,
;; some wolves die or move on.
(fn [cell world]
(cond (> (get-int cell :wolves) (get-int cell :deer))
(merge cell {:wolves 0})))
;; wolves arrive occasionally at the edge of the map. ;; wolves arrive occasionally at the edge of the map.
(fn [cell world] (fn [cell world]
(cond (and (< (count (get-neighbours world cell)) 8) (cond (and (< (count (get-neighbours world cell)) 8)
(< (rand 50) 1) (< (rand 50) 1)
(= (population cell :wolves) 0)) (not (= (:state cell) :water))
(= (get-int cell :wolves) 0))
(merge cell {:wolves 2}))) (merge cell {:wolves 2})))
;; if there are not enough deer to sustain the population of wolves,
;; some wolves die or move on.
(fn [cell world]
(cond (> (population cell :wolves) (population cell :deer))
(merge cell {:wolves 0})))
;; wolves gradually spread through the world by breeding or migrating. ;; wolves gradually spread through the world by breeding or migrating.
(fn [cell world] (fn [cell world]
(let [n (apply + (map #(population % :wolves) (get-neighbours world cell)))] (let [n (apply + (map #(get-int % :wolves) (get-neighbours world cell)))]
(cond (and (cond (and
(= (population cell :wolves) 0) (not (= (:state cell) :water))
(= (get-int cell :wolves) 0)
(>= n 2)) (>= n 2))
(merge cell {:wolves 2})))) (merge cell {:wolves 2}))))
;; wolves breed. ;; wolves breed.
(fn [cell world] (fn [cell world]
(cond (cond
(>= (population cell :wolves) 2) (>= (get-int cell :wolves) 2)
(merge cell {:wolves (int (* (:wolves cell) 2))}))) (merge cell {:wolves (int (* (:wolves cell) 2))})))
;; wolves eat deer
(fn [cell world]
(merge cell {:deer (- (population cell :deer) (population cell :wolves))}))
)) ))
;; rules which initialise the world ;; rules which initialise the world