a lot of fixes
This commit is contained in:
@@ -3,6 +3,9 @@ from .commoditys import commoditys as cm
|
||||
from .exchange import Exchange
|
||||
from .business import Price_Believe_Business
|
||||
import uuid
|
||||
import time
|
||||
|
||||
|
||||
class Simulation():
|
||||
"""
|
||||
Class for controlling the different calculation steps of the Simulation.
|
||||
@@ -11,8 +14,9 @@ class Simulation():
|
||||
self.tick_funcs={}
|
||||
self.reset_funcs={}
|
||||
self.tick_count=0
|
||||
self.episode_count=0
|
||||
self.episode_count=-1
|
||||
self.cells={}
|
||||
self.populated_cells={}
|
||||
self.businesses=[]
|
||||
self.production_util={}
|
||||
for k in cm.productions.items():
|
||||
@@ -37,6 +41,14 @@ class Simulation():
|
||||
|
||||
self.tick_funcs[id]=tickfunc
|
||||
self.reset_funcs[id]=resetfunc
|
||||
|
||||
def unregister_agent(self,id):
|
||||
"""
|
||||
Unregisters agent
|
||||
"""
|
||||
self.tick_funcs.pop(id,None)
|
||||
self.reset_funcs.pop(id,None)
|
||||
|
||||
|
||||
def tick_agents_random_order(self):
|
||||
"""
|
||||
@@ -53,22 +65,32 @@ class Simulation():
|
||||
"""
|
||||
Executes a tick of the simulation
|
||||
"""
|
||||
self.tick_agents_random_order()
|
||||
start=time.time()
|
||||
|
||||
for cell_id,c in self.cells.items():
|
||||
c.setup_demand_for_step(episode_length)
|
||||
setup=time.time()
|
||||
self.tick_agents_random_order()
|
||||
agentstep=time.time()
|
||||
for b in self.businesses:
|
||||
b.tick(self.tick_count)
|
||||
|
||||
if b.balance<=0:
|
||||
self.close_business(b.id)
|
||||
busstep=time.time()
|
||||
self.tick_count+=1
|
||||
self.log_cxs_tick(episode_length)
|
||||
self.log_business_tick(episode_length)
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Resets all agents to new Episode
|
||||
"""
|
||||
self.log_cxs_episode()
|
||||
for k,v in self.reset_funcs.items():
|
||||
v(self.episode_count)
|
||||
for cell_id,cell in self.cells.items():
|
||||
cell.setup_supply_for_episode()
|
||||
|
||||
self.tick_count=0
|
||||
self.episode_count+=1
|
||||
|
||||
@@ -82,14 +104,15 @@ class Simulation():
|
||||
under.append(k)
|
||||
return under
|
||||
|
||||
def create_bussiness(self):
|
||||
|
||||
def create_bussiness(self,min_available_per_prod):
|
||||
"""
|
||||
Create a new business based on given econemy state.
|
||||
Return Business
|
||||
"""
|
||||
selected_prod=None
|
||||
# Create a business for every production rule out there
|
||||
under_prods=self.get_underutelized_prods(1)
|
||||
under_prods=self.get_underutelized_prods(min_available_per_prod)
|
||||
if len(under_prods)>0:
|
||||
# we need to create more businesses
|
||||
selected_prod=random.choice(under_prods)
|
||||
@@ -103,11 +126,23 @@ class Simulation():
|
||||
cell_id=random.choice(cells)
|
||||
cell=self.cells[cell_id]
|
||||
cxs=[cell.exchange,self.cx]
|
||||
self.populated_cells[cell_id]=True
|
||||
business=Price_Believe_Business.Price_Believe_Business(uuid.uuid4(),prod,1000,cxs,self)
|
||||
self.businesses.append(business)
|
||||
self.production_util[selected_prod]+=1
|
||||
return business
|
||||
|
||||
def remove_business(self,id):
|
||||
bus=None
|
||||
for i,busl in self.businesses:
|
||||
if busl.id==id:
|
||||
bus=i
|
||||
break
|
||||
|
||||
bus.close_business()
|
||||
self.businesses.remove(bus)
|
||||
|
||||
|
||||
def get_max_profit_prod(self):
|
||||
"""
|
||||
Returns the prod with the highest profit in last episode
|
||||
@@ -120,7 +155,7 @@ class Simulation():
|
||||
if diff>profit:
|
||||
profit=diff
|
||||
prod=b.production
|
||||
return prod
|
||||
return prod["id"]
|
||||
|
||||
|
||||
|
||||
@@ -146,4 +181,24 @@ class Simulation():
|
||||
cell_score[cell.name]+=cx.get_total_demand(prod["name"])
|
||||
cell_score[cell.name]-=cx.get_total_supply(prod["name"])
|
||||
max_keys = [key for key, value in cell_score.items() if value == max(cell_score.values())]
|
||||
return max_keys
|
||||
return max_keys
|
||||
|
||||
def log_cxs_tick(self,epi_length):
|
||||
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)
|
||||
self.cx.log_step("cx",self.episode_count,epi_length,self.tick_count)
|
||||
|
||||
def log_cxs_episode(self):
|
||||
for id,_ in self.populated_cells.items():
|
||||
cell=self.cells[id]
|
||||
lcx=cell.exchange
|
||||
name="lcx_{}".format(id)
|
||||
lcx.log_episode(name,self.episode_count)
|
||||
self.cx.log_episode("cx",self.episode_count)
|
||||
|
||||
def log_business_tick(self,episode_length):
|
||||
for bus in self.businesses:
|
||||
bus.log(self.episode_count,episode_length,self.tick_count)
|
||||
Reference in New Issue
Block a user