diff --git a/db/production.yml b/db/production.yml index a4f40f2..890055e 100644 --- a/db/production.yml +++ b/db/production.yml @@ -6,7 +6,7 @@ spec: prod: Raw_Agriculture_Plot: 1 - name: Fruit - amount: 1000 + amount: 100 prod: Raw_Agriculture_Plot: 1 # - name: Wood @@ -20,10 +20,10 @@ spec: # Wood: 1 - name: Food - amount: 50 + amount: 100 prod: # Fuel: 1 - Fruit: 1 +# Fruit: 1 Grain: 2 diff --git a/econ/__pycache__/exchange.cpython-310.pyc b/econ/__pycache__/exchange.cpython-310.pyc index 07df5ba..b1cd649 100644 Binary files a/econ/__pycache__/exchange.cpython-310.pyc and b/econ/__pycache__/exchange.cpython-310.pyc differ diff --git a/econ/__pycache__/simulation.cpython-310.pyc b/econ/__pycache__/simulation.cpython-310.pyc index e0d151b..8ecbbd5 100644 Binary files a/econ/__pycache__/simulation.cpython-310.pyc and b/econ/__pycache__/simulation.cpython-310.pyc differ diff --git a/econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc b/econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc index 4eebc45..698aba8 100644 Binary files a/econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc and b/econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc differ diff --git a/econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc b/econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc index 3ea6b7b..faf9622 100644 Binary files a/econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc and b/econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc differ diff --git a/econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc b/econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc index ed68de6..0f892d1 100644 Binary files a/econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc and b/econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc differ diff --git a/econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc b/econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc index 5fca0f9..cd88ea6 100644 Binary files a/econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc and b/econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc differ diff --git a/econ/agents/base_aquire_agent.py b/econ/agents/base_aquire_agent.py index 9a07876..7b32a16 100644 --- a/econ/agents/base_aquire_agent.py +++ b/econ/agents/base_aquire_agent.py @@ -127,5 +127,6 @@ class Base_Aquire_Agent(BaseAgent,ABC): def reset(self, episode): #self.tqty=0 self.qty_offset=0 + self.expense_offset=0 self.trades=[] return super().reset(episode) \ No newline at end of file diff --git a/econ/agents/base_distribution_agent.py b/econ/agents/base_distribution_agent.py index 5946805..9b1e208 100644 --- a/econ/agents/base_distribution_agent.py +++ b/econ/agents/base_distribution_agent.py @@ -60,6 +60,8 @@ class Base_Distribution_Agent(BaseAgent,ABC): # Get exchange cx=self.exchanges[cx_id] + #if amount>=self.max_batch_qty: + # amount=self.max_batch_qty if not self.business.inventory[self.resource]>=amount: return False # we dont have enough balance diff --git a/econ/agents/price_believe_aquire.py b/econ/agents/price_believe_aquire.py index b308a90..eb6cda4 100644 --- a/econ/agents/price_believe_aquire.py +++ b/econ/agents/price_believe_aquire.py @@ -14,8 +14,8 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent): self.price_believe = {i: 1 for i in range(len(self.exchanges))} self.open_orders = {i: [] for i in range(len(self.exchanges))} self.open_qty = 0 - self.hp_threshold=0.25 - self.lp_threshold=0.90 + self.hp_threshold=0.70 + self.lp_threshold=0.99 def tick(self, tick, episode): @@ -52,9 +52,11 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent): return best_id def register_order(self, cx_id, order): + half=self.max_price_adj_rate/2 + ran=random.randrange(-half,half) self.open_orders[cx_id].append({ 'id': order.order_id, - 'lifetime': self.max_price_adj_rate, + 'lifetime': self.max_price_adj_rate+ran, 'leaves': order.leaves_qty }) if order.leaves_qty != order.qty: diff --git a/econ/agents/price_believe_distribute.py b/econ/agents/price_believe_distribute.py index 5007bf9..3d1365f 100644 --- a/econ/agents/price_believe_distribute.py +++ b/econ/agents/price_believe_distribute.py @@ -7,15 +7,16 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent): Aquire agent with internal price believe system. """ - def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None: + def __init__(self, simulation, business, resource, exchanges: list, lr,at_sales, max_price_adj_rate) -> None: super().__init__(simulation, business, resource, exchanges) self.lr = lr + self.lr_at_sales=at_sales self.max_price_adj_rate = max_price_adj_rate self.price_believe = {i: 1 for i in range(len(self.exchanges))} self.open_orders = {i: [] for i in range(len(self.exchanges))} self.open_qty = 0 - self.lp_threshold=0.10 - self.hp_threshold=0.90 + self.lp_threshold=0.05 + self.hp_threshold=0.80 def tick(self, step, episode): @@ -50,9 +51,11 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent): return best_id def register_order(self, cx_id, order): + half=self.max_price_adj_rate/2 + ran=random.randrange(-half,half) self.open_orders[cx_id].append({ 'id': order.order_id, - 'lifetime': self.max_price_adj_rate, + 'lifetime': self.max_price_adj_rate+ran, 'leaves': order.leaves_qty }) if order.leaves_qty != order.qty: @@ -80,7 +83,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent): modifier=1 elif succsess<=self.lp_threshold: modifier=-1 - self.update_believe(cx_id, modifier) # update price believe + self.update_believe(cx_id,o.price, succsess) # update price believe self.collect_balance_from_cxs() self.collect_resource_from_cxs(self.resource) self.update_trades() @@ -100,36 +103,33 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent): # timeout self.update_trades() succsess=self.calc_order_success(cx,o) - modifier=0 - if succsess>=self.hp_threshold: - modifier=1 - elif succsess<=self.lp_threshold: - modifier=-1 + cx.cancel_order(i["id"]) self.collect_balance_from_cxs() self.collect_resource_from_cxs(self.resource) - self.update_believe(cx_id, modifier) + self.update_believe(cx_id,o.price, succsess) self.open_orders[cx_id].remove(i) def calc_order_success(self,cx, o): """ Calculate how we should adjust the price belive """ sold = o.qty-o.leaves_qty - soldperc=sold/o.qty + nsold=min(o.leaves_qty,cx.total_demand[self.resource]) dem = cx.total_demand[self.resource]+sold if dem == 0: dem = 1 - coverage = sold/dem - base_success=max([coverage,soldperc]) - return base_success + score=sold-nsold + return score - def update_believe(self, cx_id, modifier): + def update_believe(self, cx_id, used_price, modifier): """ Updates the believe based on the modifier. If positive will add lr to believe If negative will sub lr to believe """ - self.price_believe[cx_id] += modifier*self.lr + + self.price_believe[cx_id]+=(modifier/self.lr_at_sales)*self.lr + self.price_believe[cx_id] = round(self.price_believe[cx_id], 2) if self.price_believe[cx_id] < 1: self.price_believe[cx_id] = 1 diff --git a/econ/business/Price_Believe_Business.py b/econ/business/Price_Believe_Business.py index 8bd3a22..2e81584 100644 --- a/econ/business/Price_Believe_Business.py +++ b/econ/business/Price_Believe_Business.py @@ -11,19 +11,21 @@ class Price_Believe_Business(Business): self.max_storage = 10 self.expense_per_unit = -1 + self.expense_per_prod=-1 self.income_per_unit = -1 self.distribute = Price_Believe_Distribiute_Agent( - simulation, self, production["name"], exchange, 0.2, 50) + simulation, self, production["name"], exchange, 0.2,production["amount"], 20) self.craft = AutoProductionAgent(simulation, self) self.aquire = {} for k, v in production["prod"].items(): a = Price_Believe_Aquire_Agent( - simulation, self, k, exchange, 0.2, 5) + simulation, self, k, exchange, 0.2, 20) a.set_target(v*2) a.set_price_max(10) self.aquire[k] = a self.distribute.set_price_min(0) + self.distribute.max_batch_qty=production["amount"] self.distribute.set_target(0) def tick_business_decisions(self, step): @@ -95,7 +97,7 @@ class Price_Believe_Business(Business): ExPerProd = agent.expense/covers experunit += ExPerProd/amount - + if qtyNewProds > 0: # confirm new units for key, agent in self.aquire.items(): @@ -116,6 +118,7 @@ class Price_Believe_Business(Business): estimatedQTYUnits = qtyNewUnits+self.resource_in_possesion() new_ex = ex_total/estimatedQTYUnits self.expense_per_unit = new_ex + self.expense_per_prod=self.expense_per_unit*amount def update_income_per_unit(self, step): qty_sold = self.distribute.qty @@ -140,6 +143,7 @@ class Price_Believe_Business(Business): data["tstep"] = step+episode*episode_length data["production"] = self.production["name"] data["expense"] = self.expense_per_unit + data["expense_prod"]=self.expense_per_prod data["income"] = self.income_per_unit data["inventory"] = self.inventory[self.production["name"]] data["retain"] = self.distribute.target diff --git a/econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc b/econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc index 057777a..deaacd4 100644 Binary files a/econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc and b/econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc differ diff --git a/econ/exchange.py b/econ/exchange.py index c0ab438..97cc6f4 100644 --- a/econ/exchange.py +++ b/econ/exchange.py @@ -127,10 +127,11 @@ class Exchange(): # calculate price for complete order fullfilment prev=amount amount=int(amount) + price=round(price) if amount<1: # invalid order return None - full_price=round(price*amount,2) + full_price=round(price*amount) # Move resources into escrow @@ -294,11 +295,11 @@ class Exchange(): #data["step"]=step data["tstep"]=timepoint data["instrm"]=instrm - data["best_ask"]=0 - data["best_bid"]=0 - data["total_demand"]=0 - data["total_supply"]=0 - data["market_rate"]=0 + data["best_ask"]=None + data["best_bid"]=None + data["total_demand"]=None + data["total_supply"]=None + data["market_rate"]=None localStepDB[instrm]=data ## add best ask/best bid diff --git a/econ/simulation.py b/econ/simulation.py index 7439f45..dc3785e 100644 --- a/econ/simulation.py +++ b/econ/simulation.py @@ -21,6 +21,7 @@ class Simulation(): self.populated_cells={} self.businesses=[] self.production_util={} + self.episode_tax=1 self.taskpool=ThreadPoolExecutor() self.setup_timing(["dss","dsa","dab","dbl","dt"]) @@ -113,6 +114,8 @@ class Simulation(): cell.setup_supply_for_episode() toclose=[] for b in self.businesses: + #tax business + b.balance-=self.episode_tax b.tick_episode(self.episode_count) if b.balance<=0: toclose.append(b.id)