This is a automatan generating model that generates scenarios of prices and transactions among consumers and a commodity.
from __future__ import division
# Prototype for abstracting the concepts of demand, supply, and price.
# simple form is demand / supply = price, but that can be heavily modified.
# another aaddition is the 'demand coeffecient', that decides the string of the quotient
# (demand / suuply) * d_coef = price
# going further, handling of consumers can be added
class Consumer(object):
"""
this class models consumer data and behavior
their own supply is added to the supply from sale in the formula
"""
def __init__(self, demand, start_cash):
self.demand = demand
self.cash = start_cash
self.supply = 0 # must start with nothing, needs to buy
def __repr__(self):
return "(demand:{} cash:{} supply:{})".format(self.demand, self.cash, self.supply)
class SaleModel(object):
def __init__(self, name, main_supply, main_cash):
self.name = name
self.main_supply = main_supply
self.consumers = []
self.main_cash = main_cash
self.check_price_diff = True
def __repr__(self):
base_str = "___Sale_Model___\nname:" + self.name + "\n"
base_str += "main_supply:{}\nmain_cash:{}\n".format(self.main_supply, self.main_cash)
for c in self.consumers:
base_str += str(c) + "\n"
base_str += "________________________"
return base_str
def turnoff_price_diff(self):
self.check_price_diff = False
def new_consumer(self, demand, start_cash):
self.consumers.append(Consumer(demand, start_cash))
def get_consumer_price(self, consumer):
return consumer.demand / (self.main_supply + consumer.supply)
def get_main_price(self, consumer):
return sum([c.demand for c in self.consumers]) / self.main_supply
def do_transaction(self, consumer):
cons_price = self.get_consumer_price(consumer)
main_price = self.get_main_price(consumer)
eff_price_diff = (main_price - cons_price) / main_price
if eff_price_diff < 0.9:
# price diff is too large to accept the sale
consumer.supply += 1
consumer.cash -= cons_price
self.main_cash += cons_price
self.main_supply -= 1
else:
if not self.check_price_diff:
consumer.supply += 1
consumer.cash -= cons_price
self.main_cash += cons_price
self.main_supply -= 1
def do_all_transactions(self):
for c in self.consumers:
self.do_transaction(c)
"""
this automatan model attempts to pit a consumer's demand against the entire demand,
and compare the prices they are willing to pay. If the consumers demand is too low relative to the
population, they won't be able to purchase.
"""
if __name__ == '__main__':
model = SaleModel("gold", 50.0, 500.0)
model.new_consumer(20.0, 50.0)
model.new_consumer(1.0, 40.0)
model.new_consumer(5.0, 30.0)
print model
for i in range(20):
model.do_all_transactions()
print model
"""
Sample Run:
___Sale_Model___
name:gold
main_supply:50.0
main_cash:500.0
(demand:20.0 cash:50.0 supply:0)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:30.0 supply:0)
________________________
___Sale_Model___
name:gold
main_supply:48.0
main_cash:500.502040816
(demand:20.0 cash:49.6 supply:1)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.8979591837 supply:1)
________________________
___Sale_Model___
name:gold
main_supply:46.0
main_cash:501.014370748
(demand:20.0 cash:49.1918367347 supply:2)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.793792517 supply:2)
________________________
___Sale_Model___
name:gold
main_supply:44.0
main_cash:501.537420394
(demand:20.0 cash:48.775170068 supply:3)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.6874095383 supply:3)
________________________
___Sale_Model___
name:gold
main_supply:42.0
main_cash:502.071647961
(demand:20.0 cash:48.3496381531 supply:4)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.5787138861 supply:4)
________________________
___Sale_Model___
name:gold
main_supply:40.0
main_cash:502.617541681
(demand:20.0 cash:47.9148555444 supply:5)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.467602775 supply:5)
________________________
___Sale_Model___
name:gold
main_supply:38.0
main_cash:503.175622489
(demand:20.0 cash:47.4704111 supply:6)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.3539664114 supply:6)
________________________
___Sale_Model___
name:gold
main_supply:36.0
main_cash:503.746447013
(demand:20.0 cash:47.0158656454 supply:7)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.2376873416 supply:7)
________________________
___Sale_Model___
name:gold
main_supply:34.0
main_cash:504.330610911
(demand:20.0 cash:46.5507493664 supply:8)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:29.1186397225 supply:8)
________________________
___Sale_Model___
name:gold
main_supply:32.0
main_cash:504.928752607
(demand:20.0 cash:46.0745588902 supply:9)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.996688503 supply:9)
________________________
___Sale_Model___
name:gold
main_supply:30.0
main_cash:505.541557485
(demand:20.0 cash:45.5867540121 supply:10)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.871688503 supply:10)
________________________
___Sale_Model___
name:gold
main_supply:28.0
main_cash:506.169762613
(demand:20.0 cash:45.0867540121 supply:11)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.7434833748 supply:11)
________________________
___Sale_Model___
name:gold
main_supply:26.0
main_cash:506.814162073
(demand:20.0 cash:44.5739334993 supply:12)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.6119044275 supply:12)
________________________
___Sale_Model___
name:gold
main_supply:24.0
main_cash:507.475612998
(demand:20.0 cash:44.0476177098 supply:13)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.4767692923 supply:13)
________________________
___Sale_Model___
name:gold
main_supply:22.0
main_cash:508.155042427
(demand:20.0 cash:43.5070771693 supply:14)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.3378804034 supply:14)
________________________
___Sale_Model___
name:gold
main_supply:20.0
main_cash:508.853455126
(demand:20.0 cash:42.9515216137 supply:15)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.1950232606 supply:15)
________________________
___Sale_Model___
name:gold
main_supply:18.0
main_cash:509.571942521
(demand:20.0 cash:42.3800930423 supply:16)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.0479644371 supply:16)
________________________
___Sale_Model___
name:gold
main_supply:17.0
main_cash:510.160177815
(demand:20.0 cash:41.7918577482 supply:17)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.0479644371 supply:16)
________________________
___Sale_Model___
name:gold
main_supply:16.0
main_cash:510.748413109
(demand:20.0 cash:41.2036224541 supply:18)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.0479644371 supply:16)
________________________
___Sale_Model___
name:gold
main_supply:15.0
main_cash:511.336648403
(demand:20.0 cash:40.61538716 supply:19)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.0479644371 supply:16)
________________________
___Sale_Model___
name:gold
main_supply:14.0
main_cash:511.924883697
(demand:20.0 cash:40.0271518658 supply:20)
(demand:1.0 cash:40.0 supply:0)
(demand:5.0 cash:28.0479644371 supply:16)
________________________
"""