seams to start working

This commit is contained in:
2023-06-26 13:30:34 +02:00
parent f60dca6367
commit 4cec35935f
28 changed files with 112 additions and 40 deletions
+24
View File
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [{
"name": "Python: Current File with profiler",
"type": "python",
"request": "launch",
"module": "cProfile",
"args": [
"-o", "tmp.prof", "${file}"
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1
View File
@@ -94,6 +94,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
if k in cx.order_trades_map: if k in cx.order_trades_map:
trades.append(cx.order_trades_map[k]) trades.append(cx.order_trades_map[k])
self.trades=trades self.trades=trades
return trades return trades
+2 -1
View File
@@ -104,5 +104,6 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
self.collect_resource_from_cxs(self.resource) self.collect_resource_from_cxs(self.resource)
# book keeping # book keeping
self.update_trades() 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) return super().reset(episode)
+2 -1
View File
@@ -101,5 +101,6 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
self.collect_resource_from_cxs(self.resource) self.collect_resource_from_cxs(self.resource)
# book keeping # book keeping
self.update_trades() 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) return super().reset(episode)
+2 -2
View File
@@ -31,7 +31,7 @@ class Price_Believe_Business(Business):
return self.distribute.open_qty+self.inventory[self.production["name"]] return self.distribute.open_qty+self.inventory[self.production["name"]]
def close_business(self): def close_business(self):
for a in self.aquire: for k,a in self.aquire.items():
a.unregister() a.unregister()
self.distribute.unregister() self.distribute.unregister()
self.production.unregister() self.craft.unregister()
Binary file not shown.
Binary file not shown.
+20 -19
View File
@@ -33,6 +33,7 @@ class Exchange():
self.demand={} self.demand={}
self.total_supply={} self.total_supply={}
self.supply={} self.supply={}
self.traded_commoditys={}
def add_to_account(self,account_id,resource,amount): def add_to_account(self,account_id,resource,amount):
""" """
@@ -241,6 +242,7 @@ class Exchange():
self.best_ask[resource]=best_ask self.best_ask[resource]=best_ask
self.best_bid[resource]=best_bid self.best_bid[resource]=best_bid
self.traded_commoditys[resource]=True
def get_account_resource_amount(self,account_id,resource): def get_account_resource_amount(self,account_id,resource):
""" """
@@ -260,25 +262,23 @@ class Exchange():
return self.total_supply[resource] return self.total_supply[resource]
return 0 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 timepoint=episode*episode_length+step
localStepDB={} 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(): for instrm,order in self.traded_commoditys.items():
t=data.copy() data={}
t["instrm"]=order.instmt data["cxid"]=name
localStepDB[order.instmt]=t 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 ## add best ask/best bid
for item , value in self.best_ask.items(): for item , value in self.best_ask.items():
@@ -299,9 +299,10 @@ class Exchange():
if value!=None: if value!=None:
localStepDB[item]["market_rate"]=value localStepDB[item]["market_rate"]=value
for id,item in localStepDB.items(): if autolog:
log.EXBooksData.append(item) for id,item in localStepDB.items():
return data log.EXBooksData.append(item)
return localStepDB
def log_episode(self,name,episode): def log_episode(self,name,episode):
for t in self.executed_trades: for t in self.executed_trades:
+52 -12
View File
@@ -4,13 +4,15 @@ from .exchange import Exchange
from .business import Price_Believe_Business from .business import Price_Believe_Business
import uuid import uuid
import time import time
import logging
from concurrent.futures import ThreadPoolExecutor
import log
class Simulation(): class Simulation():
""" """
Class for controlling the different calculation steps of the Simulation. Class for controlling the different calculation steps of the Simulation.
""" """
def __init__(self) -> None: def __init__(self) -> None:
self.timings={}
self.tick_funcs={} self.tick_funcs={}
self.reset_funcs={} self.reset_funcs={}
self.tick_count=0 self.tick_count=0
@@ -19,6 +21,9 @@ class Simulation():
self.populated_cells={} self.populated_cells={}
self.businesses=[] self.businesses=[]
self.production_util={} self.production_util={}
self.taskpool=ThreadPoolExecutor()
self.setup_timing(["dss","dsa","dab","dbl","dt"])
for k in cm.productions.items(): for k in cm.productions.items():
l=k[1] l=k[1]
for i in l: for i in l:
@@ -75,11 +80,19 @@ class Simulation():
for b in self.businesses: for b in self.businesses:
b.tick(self.tick_count) b.tick(self.tick_count)
if b.balance<=0: if b.balance<=0:
self.close_business(b.id) self.remove_business(b.id)
busstep=time.time() busstep=time.time()
self.tick_count+=1 self.tick_count+=1
self.log_cxs_tick(episode_length) self.log_cxs_tick(episode_length)
self.log_business_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): def reset(self):
""" """
@@ -134,28 +147,35 @@ class Simulation():
def remove_business(self,id): def remove_business(self,id):
bus=None bus=None
for i,busl in self.businesses: for busl in self.businesses:
if busl.id==id: if busl.id==id:
bus=i bus=busl
break break
bus.close_business() bus.close_business()
self.businesses.remove(bus) self.businesses.remove(bus)
self.production_util[bus.production["id"]]-=1
def get_max_profit_prod(self): def get_max_profit_prod(self):
""" """
Returns the prod with the highest profit in last episode Returns the prod with the highest profit in last episode
""" """
profit=self.businesses[0].balance-self.businesses[0].balance_history[-1] profit={}
prod=self.businesses[0].production
for b in self.businesses: for b in self.businesses:
id=b.production["id"]
diff=b.balance-b.balance_history[-1] diff=b.balance-b.balance_history[-1]
if diff>profit: if id not in profit:
profit=diff profit[id]=0
prod=b.production profit[id]+=diff
return prod["id"] 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 return max_keys
def log_cxs_tick(self,epi_length): def log_cxs_tick(self,epi_length):
items=[]
for id,_ in self.populated_cells.items(): for id,_ in self.populated_cells.items():
cell=self.cells[id] cell=self.cells[id]
lcx=cell.exchange lcx=cell.exchange
name="lcx_{}".format(id) 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) 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): def log_cxs_episode(self):
for id,_ in self.populated_cells.items(): for id,_ in self.populated_cells.items():
cell=self.cells[id] cell=self.cells[id]
@@ -202,3 +238,7 @@ class Simulation():
def log_business_tick(self,episode_length): def log_business_tick(self,episode_length):
for bus in self.businesses: 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
+3
View File
@@ -0,0 +1,3 @@
admin
adminadmin
ywMlW7zpVYFcGr1krHa83R_J_HSrd2wEq8MgtB7YX7Nc6QoTZSD6H-iiKlXYZUZaZY2MbNrL3XxfioaSGe9_bA==
Binary file not shown.
Binary file not shown.
+4 -3
View File
@@ -29,14 +29,15 @@ cx.submit_order(w,"Wood",0,1000,Side.BUY)
cx.submit_order(2,"Gem",1,10,Side.SELL) cx.submit_order(2,"Gem",1,10,Side.SELL)
sim=Simulation() sim=Simulation()
sim.seed(45223)
sim.set_cells(cells) sim.set_cells(cells)
sim.reset() sim.reset()
#sim.tick(1) #sim.tick(1)
# create info # create info
while len(sim.get_underutelized_prods(1))>0: while len(sim.get_underutelized_prods(2))>0:
sim.create_bussiness(1) sim.create_bussiness(2)
#sim.reset() #sim.reset()
for a in tqdm(range(30)): for a in tqdm(range(50)):
for i in range(100): for i in range(100):
sim.tick(100) sim.tick(100)
sim.reset() sim.reset()
BIN
View File
Binary file not shown.