Logging of flows; better member?
This commit is contained in:
parent
f60fdb944b
commit
8b3639edd5
|
@ -67,7 +67,6 @@
|
||||||
(and result source) (merge result {:rule source})
|
(and result source) (merge result {:rule source})
|
||||||
:else result))))
|
:else result))))
|
||||||
|
|
||||||
|
|
||||||
(defn- apply-rules
|
(defn- apply-rules
|
||||||
"Derive a cell from this `cell` of this `world` by applying these `rules`."
|
"Derive a cell from this `cell` of this `world` by applying these `rules`."
|
||||||
[world cell rules]
|
[world cell rules]
|
||||||
|
@ -94,12 +93,10 @@
|
||||||
:stacktrace (map #(.toString %) (.getStackTrace e))
|
:stacktrace (map #(.toString %) (.getStackTrace e))
|
||||||
:state :error}))))
|
:state :error}))))
|
||||||
|
|
||||||
|
|
||||||
(defn transform-world
|
(defn transform-world
|
||||||
"Return a world derived from this `world` by applying these `rules` to each cell."
|
"Return a world derived from this `world` by applying these `rules` to each cell."
|
||||||
[world rules]
|
([world rules]
|
||||||
(map-world world transform-cell (list rules)))
|
(map-world world transform-cell (list rules))))
|
||||||
|
|
||||||
|
|
||||||
(defn run-world
|
(defn run-world
|
||||||
"Run this world with these rules for this number of generations.
|
"Run this world with these rules for this number of generations.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns mw-engine.flow
|
(ns mw-engine.flow
|
||||||
"Allow flows of values between cells in the world."
|
"Allow flows of values between cells in the world."
|
||||||
(:require [mw-engine.utils :refer [get-cell get-num merge-cell]]
|
(: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."
|
to its destination."
|
||||||
[world flow]
|
[world flow]
|
||||||
(try
|
(try
|
||||||
(let [source (get-cell world (-> flow :source :x) (-> flow :source :y))
|
(let [sx (-> flow :source :x)
|
||||||
dest (get-cell world (-> flow :destination :x) (-> flow :destination :y))
|
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)
|
p (:property flow)
|
||||||
q (min (:quantity flow) (get-num source p))
|
q (min (:quantity flow) (get-num source p))
|
||||||
s' (assoc source p (- (source p) q))
|
s' (assoc source p (- (source p) q))
|
||||||
d' (assoc dest p (+ (get-num dest 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'))
|
(merge-cell (merge-cell world s') d'))
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(warn (format "Failed to execute flow %s: %s" flow (.getMessage e)))
|
(warn (format "Failed to execute flow %s: %s" flow (.getMessage e)))
|
||||||
|
|
|
@ -29,8 +29,8 @@
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(defn member?
|
(defn member?
|
||||||
"True if elt is a member of col."
|
"Return 'true' if elt is a member of col, else 'false'."
|
||||||
[elt col] (some #(= elt %) col))
|
[elt col] (boolean ((set col) elt)))
|
||||||
|
|
||||||
(defn get-int-or-zero
|
(defn get-int-or-zero
|
||||||
"Return the value of this `property` from this `map` if it is a integer;
|
"Return the value of this `property` from this `map` if it is a integer;
|
||||||
|
@ -97,7 +97,6 @@
|
||||||
row)))
|
row)))
|
||||||
world))))
|
world))))
|
||||||
|
|
||||||
|
|
||||||
(defn map-world
|
(defn map-world
|
||||||
"Apply this `function` to each cell in this `world` to produce a new 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
|
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
|
Gets the neighbours within the specified distance of the cell at
|
||||||
coordinates [x,y] in this world."
|
coordinates [x,y] in this world."
|
||||||
([world x y depth]
|
([world x y depth]
|
||||||
(remove nil?
|
(memo-get-neighbours world x y depth))
|
||||||
(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)))))))
|
|
||||||
([world cell depth]
|
([world cell depth]
|
||||||
(memo-get-neighbours world (:x cell) (:y cell) depth))
|
(memo-get-neighbours world (:x cell) (:y cell) depth))
|
||||||
([world cell]
|
([world cell]
|
||||||
(get-neighbours world cell 1)))
|
(memo-get-neighbours world (:x cell) (:y cell) 1)))
|
||||||
|
|
||||||
(defn get-neighbours-with-property-value
|
(defn get-neighbours-with-property-value
|
||||||
"Get the neighbours to distance depth of the cell at x, y in this world which
|
"Get the neighbours to distance depth of the cell at x, y in this world which
|
||||||
|
@ -254,7 +248,6 @@
|
||||||
([world cell state]
|
([world cell state]
|
||||||
(get-neighbours-with-state world cell 1 state)))
|
(get-neighbours-with-state world cell 1 state)))
|
||||||
|
|
||||||
|
|
||||||
(defn get-least-cell
|
(defn get-least-cell
|
||||||
"Return the cell from among these `cells` which has the lowest numeric value
|
"Return the cell from among these `cells` which has the lowest numeric value
|
||||||
for this `property`."
|
for this `property`."
|
||||||
|
@ -265,8 +258,7 @@
|
||||||
"Return the cell from among these `cells` which has the highest numeric value
|
"Return the cell from among these `cells` which has the highest numeric value
|
||||||
for this `property`."
|
for this `property`."
|
||||||
[cells property]
|
[cells property]
|
||||||
(last (sort-by property cells)))
|
(last (sort-by property (filter #(number? (property %)) cells))))
|
||||||
|
|
||||||
|
|
||||||
(defn- set-cell-property
|
(defn- set-cell-property
|
||||||
"If this `cell`s x and y properties are equal to these `x` and `y` values,
|
"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"})
|
(merge cell {property value :rule "Set by user"})
|
||||||
:else cell))
|
:else cell))
|
||||||
|
|
||||||
|
|
||||||
(defn set-property
|
(defn set-property
|
||||||
"Return a world like this `world` but with the value of exactly one `property`
|
"Return a world like this `world` but with the value of exactly one `property`
|
||||||
of one `cell` changed to this `value`"
|
of one `cell` changed to this `value`"
|
||||||
|
|
Loading…
Reference in a new issue