From 8b3639edd501a96846d13cd387f4129e73f03d6b Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 10 Jul 2023 10:44:33 +0100 Subject: [PATCH] Logging of flows; better `member?` --- src/cljc/mw_engine/core.clj | 7 ++----- src/cljc/mw_engine/flow.clj | 12 +++++++++--- src/cljc/mw_engine/utils.clj | 19 +++++-------------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/cljc/mw_engine/core.clj b/src/cljc/mw_engine/core.clj index 60ef26a..1483c57 100644 --- a/src/cljc/mw_engine/core.clj +++ b/src/cljc/mw_engine/core.clj @@ -67,7 +67,6 @@ (and result source) (merge result {:rule source}) :else result)))) - (defn- apply-rules "Derive a cell from this `cell` of this `world` by applying these `rules`." [world cell rules] @@ -94,12 +93,10 @@ :stacktrace (map #(.toString %) (.getStackTrace e)) :state :error})))) - (defn transform-world "Return a world derived from this `world` by applying these `rules` to each cell." - [world rules] - (map-world world transform-cell (list rules))) - + ([world rules] + (map-world world transform-cell (list rules)))) (defn run-world "Run this world with these rules for this number of generations. diff --git a/src/cljc/mw_engine/flow.clj b/src/cljc/mw_engine/flow.clj index b045cf6..778d8a7 100644 --- a/src/cljc/mw_engine/flow.clj +++ b/src/cljc/mw_engine/flow.clj @@ -1,7 +1,7 @@ (ns mw-engine.flow "Allow flows of values between cells in the world." (:require [mw-engine.utils :refer [get-cell get-num merge-cell]] - [taoensso.timbre :refer [warn]])) + [taoensso.timbre :refer [info warn]])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -95,12 +95,18 @@ to its destination." [world flow] (try - (let [source (get-cell world (-> flow :source :x) (-> flow :source :y)) - dest (get-cell world (-> flow :destination :x) (-> flow :destination :y)) + (let [sx (-> flow :source :x) + sy (-> flow :source :y) + source (get-cell world sx sy) + dx (-> flow :destination :x) + dy (-> flow :destination :y) + dest (get-cell world dx dy) p (:property flow) q (min (:quantity flow) (get-num source p)) s' (assoc source p (- (source p) q)) d' (assoc dest p (+ (get-num dest p) q))] + (info (format "Moving %f units of %s from %d,%d to %d,%d" + (float q) (name p) sx sy dx dy)) (merge-cell (merge-cell world s') d')) (catch Exception e (warn (format "Failed to execute flow %s: %s" flow (.getMessage e))) diff --git a/src/cljc/mw_engine/utils.clj b/src/cljc/mw_engine/utils.clj index 338dc95..2254f77 100644 --- a/src/cljc/mw_engine/utils.clj +++ b/src/cljc/mw_engine/utils.clj @@ -29,8 +29,8 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn member? - "True if elt is a member of col." - [elt col] (some #(= elt %) col)) + "Return 'true' if elt is a member of col, else 'false'." + [elt col] (boolean ((set col) elt))) (defn get-int-or-zero "Return the value of this `property` from this `map` if it is a integer; @@ -97,7 +97,6 @@ row))) world)))) - (defn map-world "Apply this `function` to each cell in this `world` to produce a new world. the arguments to the function will be the world, the cell, and any @@ -198,16 +197,11 @@ Gets the neighbours within the specified distance of the cell at coordinates [x,y] in this world." ([world x y depth] - (remove nil? - (map #(get-cell world (first %) (first (rest %))) - (remove #(= % (list x y)) - (combo/cartesian-product - (range (- x depth) (+ x depth 1)) - (range (- y depth) (+ y depth 1))))))) + (memo-get-neighbours world x y depth)) ([world cell depth] (memo-get-neighbours world (:x cell) (:y cell) depth)) ([world cell] - (get-neighbours world cell 1))) + (memo-get-neighbours world (:x cell) (:y cell) 1))) (defn get-neighbours-with-property-value "Get the neighbours to distance depth of the cell at x, y in this world which @@ -254,7 +248,6 @@ ([world cell state] (get-neighbours-with-state world cell 1 state))) - (defn get-least-cell "Return the cell from among these `cells` which has the lowest numeric value for this `property`." @@ -265,8 +258,7 @@ "Return the cell from among these `cells` which has the highest numeric value for this `property`." [cells property] - (last (sort-by property cells))) - + (last (sort-by property (filter #(number? (property %)) cells)))) (defn- set-cell-property "If this `cell`s x and y properties are equal to these `x` and `y` values, @@ -278,7 +270,6 @@ (merge cell {property value :rule "Set by user"}) :else cell)) - (defn set-property "Return a world like this `world` but with the value of exactly one `property` of one `cell` changed to this `value`"