Added logging; simulation ALMOST runs

This commit is contained in:
Simon Brooke 2019-05-15 07:18:09 +01:00
parent 984feb850a
commit 28db4866eb
9 changed files with 59 additions and 30 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -40,6 +40,24 @@
#(= (:location %) (:location g)) #(= (:location %) (:location g))
(vals (:gossips world))))))}}))) (vals (:gossips world))))))}})))
(defn move-gossip
"Return a world like this `world` but with this `gossip` moved to this
`new-location`. Many gossips are essentially shadow-records of agents of
other types, and the movement if the gossip should be controlled by the
run function of the type of the record they shadow. The [[#run]] function
below does NOT call this function."
[gossip world new-location]
(let [id (cond
(map? gossip)
(:id (-> world :gossipe gossip)
(keyword? gossip)
gossip))]
(deep-merge
world
{:gossips
{id
{:location new-location}}})))
(defn run (defn run
"Return a world like this `world`, with news items exchanged between gossip "Return a world like this `world`, with news items exchanged between gossip
agents." agents."

View file

@ -1,6 +1,7 @@
(ns the-great-game.merchants.markets (ns the-great-game.merchants.markets
"Adjusting quantities and prices in markets." "Adjusting quantities and prices in markets."
(:require [the-great-game.utils :refer [deep-merge]] (:require [taoensso.timbre :as l :refer [info error]]
[the-great-game.utils :refer [deep-merge]]
[the-great-game.world.world :refer [actual-price default-world]])) [the-great-game.world.world :refer [actual-price default-world]]))
(defn new-price (defn new-price
@ -38,12 +39,15 @@
(> su st) (> su st)
(- su st) (- su st)
true 0) true 0)
price (new-price p st su d)] n (new-price p st su d)]
(if
(not (= p n))
(l/info "Price of " commodity " at " id " has changed from " (float p) " to " (float n)))
{:cities {id {:cities {id
{:stock {:stock
{commodity (+ (- st decrement) increment)} {commodity (+ (- st decrement) increment)}
:prices :prices
{commodity price}}}})) {commodity n}}}}))
(defn update-markets (defn update-markets

View file

@ -2,6 +2,7 @@
"Trade planning for merchants, primarily." "Trade planning for merchants, primarily."
(:require [taoensso.timbre :as l :refer [info error]] (:require [taoensso.timbre :as l :refer [info error]]
[the-great-game.utils :refer [deep-merge]] [the-great-game.utils :refer [deep-merge]]
[the-great-game.gossip.gossip :refer [move-gossip]]
[the-great-game.world.routes :refer [find-route]] [the-great-game.world.routes :refer [find-route]]
[the-great-game.world.world :refer [actual-price default-world]])) [the-great-game.world.world :refer [actual-price default-world]]))
@ -309,16 +310,19 @@
:cash (+ (:cash market) p)}}}) :cash (+ (:cash market) p)}}})
;; if no plan, then if at home stay put ;; if no plan, then if at home stay put
(= (:location m) (:home m)) (= (:location m) (:home m))
{} (do
(l/info "Merchant " id " remains at home in " location)
{})
;; else move towards home ;; else move towards home
true true
(let [route (find-route world location (:home m)) (let [route (find-route world location (:home m))
next-location (nth route 1)] next-location (nth route 1)]
(l/info "No trade possible at " location "; merchant " id " moves to " next-location) (l/info "No trade possible at " location "; merchant " id " moves to " next-location)
{:merchants (merge
{:merchants
{id {id
{:location next-location}}}))))) {:location next-location}}}
(move-gossip id world next-location)))))))
(defn re-plan (defn re-plan
"Having failed to sell a cargo at current location, re-plan a route to "Having failed to sell a cargo at current location, re-plan a route to
@ -338,7 +342,6 @@
{id {id
{:plan plan}}}))) {:plan plan}}})))
(defn sell-and-buy (defn sell-and-buy
"Return a new world like this `world`, in which this `merchant` has sold "Return a new world like this `world`, in which this `merchant` has sold
their current stock in their current location, and planned a new trade, and their current stock in their current location, and planned a new trade, and
@ -381,7 +384,6 @@
;; else ;; else
(re-plan merchant world)))) (re-plan merchant world))))
(defn move-merchant (defn move-merchant
"Handle general en route movement of this `merchant` in this `world`." "Handle general en route movement of this `merchant` in this `world`."
[merchant world] [merchant world]
@ -396,18 +398,23 @@
next-location (if plan next-location (if plan
(nth 1 (find-route world (:location m) (:destination plan))) (nth 1 (find-route world (:location m) (:destination plan)))
(:location m))] (:location m))]
(l/info "Merchant " id " at " (:location m)) (l/info "Merchant " id " has moved from " (:location m) " to " next-location)
(cond at-destination? (cond
(sell-and-buy merchant world plan) ;; if the merchant is at the destination of their current plan
(nil? (:plan m)) ;; sell all cargo and repurchase.
(plan-and-buy merchant world) at-destination?
true (sell-and-buy merchant world plan)
{:merchants ;; if they don't have a plan, seek to create one
{id (nil? (:plan m))
{:id id (plan-and-buy merchant world)
:location next-location ;; otherwise, move one step towards their destination
:known-prices (add-known-prices m world)}}}))) true
(deep-merge
{:merchants
{id
{:location next-location
:known-prices (add-known-prices m world)}}}
(move-gossip id world next-location)))))
(defn run (defn run
"Return a world like this `world`, but with each merchant moved." "Return a world like this `world`, but with each merchant moved."

View file

@ -185,6 +185,6 @@
(defn run (defn run
"Return a world like this `world` with only the `:date` value updated "Return a world like this `world` with only the `:date` value updated
(incremented by one). For running other aspects of the simulation, see (incremented by one). For running other aspects of the simulation, see
[[the-great-game.world.run#var-run]]." [[the-great-game.world.run]]."
[world] [world]
(assoc world :date (inc (or (:date world) 0)))) (assoc world :date (inc (or (:date world) 0))))