seams to start working
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -94,6 +94,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
if k in cx.order_trades_map:
|
||||
trades.append(cx.order_trades_map[k])
|
||||
self.trades=trades
|
||||
|
||||
return trades
|
||||
|
||||
|
||||
|
||||
@@ -104,5 +104,6 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
# book keeping
|
||||
self.update_trades()
|
||||
|
||||
self.open_orders={i: [] for i in range(len(self.exchanges))}
|
||||
self.orders={i: {} for i in range(len(self.exchanges))}
|
||||
return super().reset(episode)
|
||||
@@ -101,5 +101,6 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
# book keeping
|
||||
self.update_trades()
|
||||
|
||||
self.open_orders={i: [] for i in range(len(self.exchanges))}
|
||||
self.orders={i: {} for i in range(len(self.exchanges))}
|
||||
return super().reset(episode)
|
||||
@@ -31,7 +31,7 @@ class Price_Believe_Business(Business):
|
||||
return self.distribute.open_qty+self.inventory[self.production["name"]]
|
||||
|
||||
def close_business(self):
|
||||
for a in self.aquire:
|
||||
for k,a in self.aquire.items():
|
||||
a.unregister()
|
||||
self.distribute.unregister()
|
||||
self.production.unregister()
|
||||
self.craft.unregister()
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+20
-19
@@ -33,6 +33,7 @@ class Exchange():
|
||||
self.demand={}
|
||||
self.total_supply={}
|
||||
self.supply={}
|
||||
self.traded_commoditys={}
|
||||
|
||||
def add_to_account(self,account_id,resource,amount):
|
||||
"""
|
||||
@@ -241,6 +242,7 @@ class Exchange():
|
||||
|
||||
self.best_ask[resource]=best_ask
|
||||
self.best_bid[resource]=best_bid
|
||||
self.traded_commoditys[resource]=True
|
||||
|
||||
def get_account_resource_amount(self,account_id,resource):
|
||||
"""
|
||||
@@ -260,25 +262,23 @@ class Exchange():
|
||||
return self.total_supply[resource]
|
||||
return 0
|
||||
|
||||
def log_step(self,name,episode,episode_length,step):
|
||||
def log_step(self,name,episode,episode_length,step,autolog=True):
|
||||
timepoint=episode*episode_length+step
|
||||
localStepDB={}
|
||||
data={}
|
||||
data["cxid"]=name
|
||||
data["episode"]=episode
|
||||
data["step"]=step
|
||||
data["tstep"]=timepoint
|
||||
data["instrm"]="None"
|
||||
data["best_ask"]=-1
|
||||
data["best_bid"]=-1
|
||||
data["total_demand"]=-1
|
||||
data["total_supply"]=-1
|
||||
data["market_rate"]=-1
|
||||
|
||||
for id,order in self.orders.items():
|
||||
t=data.copy()
|
||||
t["instrm"]=order.instmt
|
||||
localStepDB[order.instmt]=t
|
||||
for instrm,order in self.traded_commoditys.items():
|
||||
data={}
|
||||
data["cxid"]=name
|
||||
data["episode"]=episode
|
||||
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
|
||||
localStepDB[instrm]=data
|
||||
|
||||
## add best ask/best bid
|
||||
for item , value in self.best_ask.items():
|
||||
@@ -299,9 +299,10 @@ class Exchange():
|
||||
if value!=None:
|
||||
localStepDB[item]["market_rate"]=value
|
||||
|
||||
for id,item in localStepDB.items():
|
||||
log.EXBooksData.append(item)
|
||||
return data
|
||||
if autolog:
|
||||
for id,item in localStepDB.items():
|
||||
log.EXBooksData.append(item)
|
||||
return localStepDB
|
||||
|
||||
def log_episode(self,name,episode):
|
||||
for t in self.executed_trades:
|
||||
|
||||
+54
-14
@@ -4,13 +4,15 @@ from .exchange import Exchange
|
||||
from .business import Price_Believe_Business
|
||||
import uuid
|
||||
import time
|
||||
|
||||
|
||||
import logging
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import log
|
||||
class Simulation():
|
||||
"""
|
||||
Class for controlling the different calculation steps of the Simulation.
|
||||
"""
|
||||
def __init__(self) -> None:
|
||||
self.timings={}
|
||||
self.tick_funcs={}
|
||||
self.reset_funcs={}
|
||||
self.tick_count=0
|
||||
@@ -19,6 +21,9 @@ class Simulation():
|
||||
self.populated_cells={}
|
||||
self.businesses=[]
|
||||
self.production_util={}
|
||||
self.taskpool=ThreadPoolExecutor()
|
||||
|
||||
self.setup_timing(["dss","dsa","dab","dbl","dt"])
|
||||
for k in cm.productions.items():
|
||||
l=k[1]
|
||||
for i in l:
|
||||
@@ -75,11 +80,19 @@ class Simulation():
|
||||
for b in self.businesses:
|
||||
b.tick(self.tick_count)
|
||||
if b.balance<=0:
|
||||
self.close_business(b.id)
|
||||
self.remove_business(b.id)
|
||||
busstep=time.time()
|
||||
self.tick_count+=1
|
||||
self.log_cxs_tick(episode_length)
|
||||
self.log_business_tick(episode_length)
|
||||
logstep=time.time()
|
||||
self.timings["dss"]+=setup-start
|
||||
self.timings["dsa"]+=agentstep-setup
|
||||
self.timings["dab"]+=busstep-agentstep
|
||||
self.timings["dbl"]+=logstep-busstep
|
||||
self.timings["dt"]+=logstep-start
|
||||
|
||||
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
@@ -134,28 +147,35 @@ class Simulation():
|
||||
|
||||
def remove_business(self,id):
|
||||
bus=None
|
||||
for i,busl in self.businesses:
|
||||
for busl in self.businesses:
|
||||
if busl.id==id:
|
||||
bus=i
|
||||
bus=busl
|
||||
break
|
||||
|
||||
bus.close_business()
|
||||
self.businesses.remove(bus)
|
||||
self.production_util[bus.production["id"]]-=1
|
||||
|
||||
|
||||
def get_max_profit_prod(self):
|
||||
"""
|
||||
Returns the prod with the highest profit in last episode
|
||||
"""
|
||||
profit=self.businesses[0].balance-self.businesses[0].balance_history[-1]
|
||||
prod=self.businesses[0].production
|
||||
profit={}
|
||||
|
||||
for b in self.businesses:
|
||||
id=b.production["id"]
|
||||
diff=b.balance-b.balance_history[-1]
|
||||
if diff>profit:
|
||||
profit=diff
|
||||
prod=b.production
|
||||
return prod["id"]
|
||||
if id not in profit:
|
||||
profit[id]=0
|
||||
profit[id]+=diff
|
||||
best=None
|
||||
bestv=0
|
||||
for k,v in profit.items():
|
||||
if bestv<v:
|
||||
bestv=v
|
||||
best=k
|
||||
return best
|
||||
|
||||
|
||||
|
||||
@@ -184,13 +204,29 @@ class Simulation():
|
||||
return max_keys
|
||||
|
||||
def log_cxs_tick(self,epi_length):
|
||||
items=[]
|
||||
for id,_ in self.populated_cells.items():
|
||||
cell=self.cells[id]
|
||||
lcx=cell.exchange
|
||||
name="lcx_{}".format(id)
|
||||
lcx.log_step(name,self.episode_count,epi_length,self.tick_count)
|
||||
items.append([lcx,name,self.episode_count,epi_length,self.tick_count])
|
||||
results=map(self.launch_cxs_log,items)
|
||||
for result in results:
|
||||
for id,item in result.items():
|
||||
log.EXBooksData.append(item)
|
||||
self.cx.log_step("cx",self.episode_count,epi_length,self.tick_count)
|
||||
|
||||
|
||||
def launch_cxs_log(self,args):
|
||||
"""
|
||||
Runs in a conncurrent map
|
||||
"""
|
||||
cx=args[0]
|
||||
name=args[1]
|
||||
epi=args[2]
|
||||
epilen=args[3]
|
||||
tick=args[4]
|
||||
return cx.log_step(name,epi,epilen,tick,False)
|
||||
|
||||
def log_cxs_episode(self):
|
||||
for id,_ in self.populated_cells.items():
|
||||
cell=self.cells[id]
|
||||
@@ -201,4 +237,8 @@ class Simulation():
|
||||
|
||||
def log_business_tick(self,episode_length):
|
||||
for bus in self.businesses:
|
||||
bus.log(self.episode_count,episode_length,self.tick_count)
|
||||
bus.log(self.episode_count,episode_length,self.tick_count)
|
||||
|
||||
def setup_timing(self,metrics):
|
||||
for met in metrics:
|
||||
self.timings[met]=0
|
||||
Reference in New Issue
Block a user