seams to start working
This commit is contained in:
@@ -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