Logging of flows; better member?

This commit is contained in:
Simon Brooke 2023-07-10 10:44:33 +01:00
parent f60fdb944b
commit 8b3639edd5
3 changed files with 16 additions and 22 deletions

View file

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

View file

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

View file

@ -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`"