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
.vscode/launch.json vendored Normal file
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.

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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.

View File

@@ -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:

View File

@@ -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

3
influx/creds.txt Normal file
View File

@@ -0,0 +1,3 @@
admin
adminadmin
ywMlW7zpVYFcGr1krHa83R_J_HSrd2wEq8MgtB7YX7Nc6QoTZSD6H-iiKlXYZUZaZY2MbNrL3XxfioaSGe9_bA==

BIN
influx/influxd.exe Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -29,14 +29,15 @@ cx.submit_order(w,"Wood",0,1000,Side.BUY)
cx.submit_order(2,"Gem",1,10,Side.SELL)
sim=Simulation()
sim.seed(45223)
sim.set_cells(cells)
sim.reset()
#sim.tick(1)
# create info
while len(sim.get_underutelized_prods(1))>0:
sim.create_bussiness(1)
while len(sim.get_underutelized_prods(2))>0:
sim.create_bussiness(2)
#sim.reset()
for a in tqdm(range(30)):
for a in tqdm(range(50)):
for i in range(100):
sim.tick(100)
sim.reset()

BIN
tmp.prof Normal file

Binary file not shown.