diff --git a/docs/index.html b/docs/index.html index c5956d3..1f40cc8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,3 +1,3 @@ -The-great-game 0.1.0-SNAPSHOT

The-great-game 0.1.0-SNAPSHOT

Released under the GNU General Public License,version 2.0 or (at your option) any later version

Prototype code towards the great game I've been writing about for ten years, and know I will never finish.

Installation

To install, add the following dependency to your project or build file:

[the-great-game "0.1.0-SNAPSHOT"]

Topics

Namespaces

the-great-game.core

TODO: write docs

Public variables and functions:

the-great-game.gossip.gossip

Interchange of news events between agents agents

Public variables and functions:

the-great-game.merchants.markets

Adjusting quantities and prices in markets.

Public variables and functions:

the-great-game.merchants.merchants

Trade planning for merchants, primarily.

the-great-game.utils

TODO: write docs

Public variables and functions:

the-great-game.world.routes

Conceptual (plan level) routes, represented as tuples of location ids.

Public variables and functions:

the-great-game.world.run

Run the whole simulation

Public variables and functions:

the-great-game.world.world

Access to data about the world

Public variables and functions:

\ No newline at end of file +The-great-game 0.1.0-SNAPSHOT

The-great-game 0.1.0-SNAPSHOT

Released under the GNU General Public License,version 2.0 or (at your option) any later version

Prototype code towards the great game I've been writing about for ten years, and know I will never finish.

Installation

To install, add the following dependency to your project or build file:

[the-great-game "0.1.0-SNAPSHOT"]

Topics

Namespaces

the-great-game.core

TODO: write docs

Public variables and functions:

the-great-game.gossip.gossip

Interchange of news events between agents agents

Public variables and functions:

the-great-game.merchants.markets

Adjusting quantities and prices in markets.

Public variables and functions:

the-great-game.merchants.merchants

Trade planning for merchants, primarily.

the-great-game.utils

TODO: write docs

Public variables and functions:

the-great-game.world.routes

Conceptual (plan level) routes, represented as tuples of location ids.

Public variables and functions:

the-great-game.world.run

Run the whole simulation

Public variables and functions:

the-great-game.world.world

Access to data about the world

Public variables and functions:

\ No newline at end of file diff --git a/docs/the-great-game.gossip.gossip.html b/docs/the-great-game.gossip.gossip.html index ac9d3d8..460937c 100644 --- a/docs/the-great-game.gossip.gossip.html +++ b/docs/the-great-game.gossip.gossip.html @@ -1,3 +1,3 @@ -the-great-game.gossip.gossip documentation

the-great-game.gossip.gossip

Interchange of news events between agents agents

dialogue

(dialogue enquirer respondent world)

Dialogue between an enquirer and an agent in this world; returns a map identical to enquirer except that its :gossip collection may have additional entries.

gather-news

(gather-news world)(gather-news world gossip)

TODO: write docs

run

(run world)

Return a world like this world, with news items exchanged between gossip agents.

\ No newline at end of file +the-great-game.gossip.gossip documentation

the-great-game.gossip.gossip

Interchange of news events between agents agents

dialogue

(dialogue enquirer respondent world)

Dialogue between an enquirer and an agent in this world; returns a map identical to enquirer except that its :gossip collection may have additional entries.

gather-news

(gather-news world)(gather-news world gossip)

TODO: write docs

move-gossip

(move-gossip gossip world new-location)

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.

run

(run world)

Return a world like this world, with news items exchanged between gossip agents.

\ No newline at end of file diff --git a/docs/the-great-game.merchants.markets.html b/docs/the-great-game.merchants.markets.html index cc83ba1..51fd23c 100644 --- a/docs/the-great-game.merchants.markets.html +++ b/docs/the-great-game.merchants.markets.html @@ -1,3 +1,3 @@ -the-great-game.merchants.markets documentation

the-great-game.merchants.markets

Adjusting quantities and prices in markets.

adjust-quantity-and-price

(adjust-quantity-and-price world city commodity)

Adjust the quantity of this commodity currently in stock in this city of this world. Return a fragmentary world which can be deep-merged into this world.

new-price

(new-price old stock supply demand)

If stock is greater than the maximum of supply and demand, then there is surplus and old price is too high, so shold be reduced. If lower, then it is too low and should be increased.

run

(run world)

Return a world like this world, with quantities and prices in markets updated to reflect supply and demand.

update-markets

(update-markets world)(update-markets world city)(update-markets world city commodity)

Return a world like this world, with quantities and prices in markets updated to reflect supply and demand. If city or city and commodity are specified, return a fragmentary world with only the changes for that city (and commodity if specified) populated.

\ No newline at end of file +the-great-game.merchants.markets documentation

the-great-game.merchants.markets

Adjusting quantities and prices in markets.

adjust-quantity-and-price

(adjust-quantity-and-price world city commodity)

Adjust the quantity of this commodity currently in stock in this city of this world. Return a fragmentary world which can be deep-merged into this world.

new-price

(new-price old stock supply demand)

If stock is greater than the maximum of supply and demand, then there is surplus and old price is too high, so shold be reduced. If lower, then it is too low and should be increased.

run

(run world)

Return a world like this world, with quantities and prices in markets updated to reflect supply and demand.

update-markets

(update-markets world)(update-markets world city)(update-markets world city commodity)

Return a world like this world, with quantities and prices in markets updated to reflect supply and demand. If city or city and commodity are specified, return a fragmentary world with only the changes for that city (and commodity if specified) populated.

\ No newline at end of file diff --git a/docs/the-great-game.merchants.merchants.html b/docs/the-great-game.merchants.merchants.html index f1512a2..78436a0 100644 --- a/docs/the-great-game.merchants.merchants.html +++ b/docs/the-great-game.merchants.merchants.html @@ -1,7 +1,7 @@ -the-great-game.merchants.merchants documentation

the-great-game.merchants.merchants

Trade planning for merchants, primarily.

add-known-prices

(add-known-prices merchant world)

Add the current prices at this merchant’s location in the world to a new cacke of known prices, and return it.

add-stock

(add-stock a b)

Where a and b are both maps all of whose values are numbers, return a map whose keys are a union of the keys of a and b and whose values are the sums of their respective values.

augment-plan

(augment-plan merchant world plan)

Augment this plan constructed in this world for this merchant with the :quantity of goods which should be bought and the :expected-profit of the trade.

-

Returns the augmented plan.

burden

(burden merchant world)

The total weight of the current cargo carried by this merchant in this world.

can-afford

(can-afford merchant world commodity)

Return the number of units of this commodity which this merchant can afford to buy in this world.

can-carry

(can-carry merchant world commodity)

Return the number of units of this commodity which this merchant can carry in this world, given their current burden.

expected-price

(expected-price merchant commodity city)

Find the price anticipated, given this world, by this merchant for this commodity in this city. If no information, assume 1. merchant should be passed as a map, commodity and city should be passed as keywords.

generate-trade-plans

(generate-trade-plans merchant world commodity)

Generate all possible trade plans for this merchant and this commodity in this world.

+the-great-game.merchants.merchants documentation

the-great-game.merchants.merchants

Trade planning for merchants, primarily.

add-known-prices

(add-known-prices merchant world)

Add the current prices at this merchant’s location in the world to a new cacke of known prices, and return it.

add-stock

(add-stock a b)

Where a and b are both maps all of whose values are numbers, return a map whose keys are a union of the keys of a and b and whose values are the sums of their respective values.

augment-plan

(augment-plan merchant world plan)

Augment this plan constructed in this world for this merchant with the :quantity of goods which should be bought and the :expected-profit of the trade.

+

Returns the augmented plan.

burden

(burden merchant world)

The total weight of the current cargo carried by this merchant in this world.

can-afford

(can-afford merchant world commodity)

Return the number of units of this commodity which this merchant can afford to buy in this world.

can-carry

(can-carry merchant world commodity)

Return the number of units of this commodity which this merchant can carry in this world, given their current burden.

expected-price

(expected-price merchant commodity city)

Find the price anticipated, given this world, by this merchant for this commodity in this city. If no information, assume 1. merchant should be passed as a map, commodity and city should be passed as keywords.

generate-trade-plans

(generate-trade-plans merchant world commodity)

Generate all possible trade plans for this merchant and this commodity in this world.

Returned plans are maps with keys:

  • :merchant - the id of the merchant for whom the plan was created;
  • @@ -12,7 +12,7 @@
  • :expected-price - the price at which the merchant anticipates that commodity can be sold;
  • :distance - the number of stages in the planned journey
  • :dist-to-home - the distance from destination to the merchant’s home city.
  • -

make-target-filter

(make-target-filter targets)

Construct a filter which, when applied to a list of maps, will pass those which match these targets, where each target is a tuple [key value].

move-merchant

(move-merchant merchant world)

Handle general en route movement of this merchant in this world.

nearest-with-targets

(nearest-with-targets plans targets)

Return the distance to the nearest destination among those of these plans which match these targets. Plans are expected to be plans as returned by generate-trade-plans, q.v.; targets are expected to be as accepted by make-target-filter, q.v.

plan-and-buy

(plan-and-buy merchant world)

Return a world like this world, in which this merchant has planned a new trade, and bought appropriate stock for it. If no profitable trade can be planned, the merchant is simply moved towards their home.

plan-trade

(plan-trade merchant world commodity)

Find the best destination in this world for this commodity given this merchant and this origin. If two cities are anticipated to offer the same price, the nearer should be preferred; if two are equally distant, the ones nearer to the merchant’s home should be preferred. merchant may be passed as a map or a keyword; commodity should be passed as a keyword.

+

make-target-filter

(make-target-filter targets)

Construct a filter which, when applied to a list of maps, will pass those which match these targets, where each target is a tuple [key value].

move-merchant

(move-merchant merchant world)

Handle general en route movement of this merchant in this world.

nearest-with-targets

(nearest-with-targets plans targets)

Return the distance to the nearest destination among those of these plans which match these targets. Plans are expected to be plans as returned by generate-trade-plans, q.v.; targets are expected to be as accepted by make-target-filter, q.v.

plan-and-buy

(plan-and-buy merchant world)

Return a world like this world, in which this merchant has planned a new trade, and bought appropriate stock for it. If no profitable trade can be planned, the merchant is simply moved towards their home.

plan-trade

(plan-trade merchant world commodity)

Find the best destination in this world for this commodity given this merchant and this origin. If two cities are anticipated to offer the same price, the nearer should be preferred; if two are equally distant, the ones nearer to the merchant’s home should be preferred. merchant may be passed as a map or a keyword; commodity should be passed as a keyword.

The returned plan is a map with keys:

  • :merchant - the id of the merchant for whom the plan was created;
  • @@ -23,4 +23,4 @@
  • :expected-price - the price at which the merchant anticipates that commodity can be sold;
  • :distance - the number of stages in the planned journey
  • :dist-to-home - the distance from destination to the merchant’s home city.
  • -

re-plan

(re-plan merchant world)

Having failed to sell a cargo at current location, re-plan a route to sell the current cargo. Returns a revised world.

run

(run world)

Return a world like this world, but with each merchant moved.

select-cargo

(select-cargo merchant world)

A merchant, in a given location in a world, will choose to buy a cargo within the limit they are capable of carrying, which they can anticipate selling for a profit at a destination.

sell-and-buy

(sell-and-buy merchant world)

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 bought appropriate stock for it.

\ No newline at end of file +

re-plan

(re-plan merchant world)

Having failed to sell a cargo at current location, re-plan a route to sell the current cargo. Returns a revised world.

run

(run world)

Return a world like this world, but with each merchant moved.

select-cargo

(select-cargo merchant world)

A merchant, in a given location in a world, will choose to buy a cargo within the limit they are capable of carrying, which they can anticipate selling for a profit at a destination.

sell-and-buy

(sell-and-buy merchant world)

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 bought appropriate stock for it.

\ No newline at end of file diff --git a/docs/the-great-game.world.world.html b/docs/the-great-game.world.world.html index 7d840bd..f2d2aa0 100644 --- a/docs/the-great-game.world.world.html +++ b/docs/the-great-game.world.world.html @@ -1,3 +1,3 @@ -the-great-game.world.world documentation

the-great-game.world.world

Access to data about the world

actual-price

(actual-price world commodity city)

Find the actual current price of this commodity in this city given this world. NOTE that merchants can only know the actual prices in the city in which they are currently located.

default-world

A basic world for testing concepts

run

(run world)

Return a world like this world with only the :date value updated (incremented by one). For running other aspects of the simulation, see the-great-game.world.run#var-run.

\ No newline at end of file +the-great-game.world.world documentation

the-great-game.world.world

Access to data about the world

actual-price

(actual-price world commodity city)

Find the actual current price of this commodity in this city given this world. NOTE that merchants can only know the actual prices in the city in which they are currently located.

default-world

A basic world for testing concepts

run

(run world)

Return a world like this world with only the :date value updated (incremented by one). For running other aspects of the simulation, see the-great-game.world.run.

\ No newline at end of file diff --git a/src/the_great_game/gossip/gossip.clj b/src/the_great_game/gossip/gossip.clj index 99957a9..886c525 100644 --- a/src/the_great_game/gossip/gossip.clj +++ b/src/the_great_game/gossip/gossip.clj @@ -40,6 +40,24 @@ #(= (:location %) (:location g)) (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 "Return a world like this `world`, with news items exchanged between gossip agents." diff --git a/src/the_great_game/merchants/markets.clj b/src/the_great_game/merchants/markets.clj index 6ec2d95..cd3deb5 100644 --- a/src/the_great_game/merchants/markets.clj +++ b/src/the_great_game/merchants/markets.clj @@ -1,6 +1,7 @@ (ns the-great-game.merchants.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]])) (defn new-price @@ -38,12 +39,15 @@ (> su st) (- su st) 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 {:stock {commodity (+ (- st decrement) increment)} :prices - {commodity price}}}})) + {commodity n}}}})) (defn update-markets diff --git a/src/the_great_game/merchants/merchants.clj b/src/the_great_game/merchants/merchants.clj index 1a691ae..51c813a 100644 --- a/src/the_great_game/merchants/merchants.clj +++ b/src/the_great_game/merchants/merchants.clj @@ -2,6 +2,7 @@ "Trade planning for merchants, primarily." (:require [taoensso.timbre :as l :refer [info error]] [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.world :refer [actual-price default-world]])) @@ -309,16 +310,19 @@ :cash (+ (:cash market) p)}}}) ;; if no plan, then if at home stay put (= (:location m) (:home m)) - {} + (do + (l/info "Merchant " id " remains at home in " location) + {}) ;; else move towards home true (let [route (find-route world location (:home m)) next-location (nth route 1)] (l/info "No trade possible at " location "; merchant " id " moves to " next-location) - {:merchants + (merge + {:merchants {id - {:location next-location}}}))))) - + {:location next-location}}} + (move-gossip id world next-location))))))) (defn re-plan "Having failed to sell a cargo at current location, re-plan a route to @@ -338,7 +342,6 @@ {id {:plan plan}}}))) - (defn sell-and-buy "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 @@ -381,7 +384,6 @@ ;; else (re-plan merchant world)))) - (defn move-merchant "Handle general en route movement of this `merchant` in this `world`." [merchant world] @@ -396,18 +398,23 @@ next-location (if plan (nth 1 (find-route world (:location m) (:destination plan))) (:location m))] - (l/info "Merchant " id " at " (:location m)) - (cond at-destination? - (sell-and-buy merchant world plan) - (nil? (:plan m)) - (plan-and-buy merchant world) - true - {:merchants - {id - {:id id - :location next-location - :known-prices (add-known-prices m world)}}}))) - + (l/info "Merchant " id " has moved from " (:location m) " to " next-location) + (cond + ;; if the merchant is at the destination of their current plan + ;; sell all cargo and repurchase. + at-destination? + (sell-and-buy merchant world plan) + ;; if they don't have a plan, seek to create one + (nil? (:plan m)) + (plan-and-buy merchant world) + ;; otherwise, move one step towards their destination + true + (deep-merge + {:merchants + {id + {:location next-location + :known-prices (add-known-prices m world)}}} + (move-gossip id world next-location))))) (defn run "Return a world like this `world`, but with each merchant moved." diff --git a/src/the_great_game/world/world.clj b/src/the_great_game/world/world.clj index 1811dd0..d8a7c65 100644 --- a/src/the_great_game/world/world.clj +++ b/src/the_great_game/world/world.clj @@ -185,6 +185,6 @@ (defn run "Return a world like this `world` with only the `:date` value updated (incremented by one). For running other aspects of the simulation, see - [[the-great-game.world.run#var-run]]." + [[the-great-game.world.run]]." [world] (assoc world :date (inc (or (:date world) 0))))