seams to start working
This commit is contained in:
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal 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
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
econ/__pycache__/exchange.cpython-310.pyc
Normal file
BIN
econ/__pycache__/exchange.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
econ/__pycache__/simulation.cpython-310.pyc
Normal file
BIN
econ/__pycache__/simulation.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
econ/agents/__pycache__/autoproduction.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/autoproduction.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/agents/__pycache__/base_agent.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/base_agent.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/base_aquire_agent.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/base_distribution_agent.cpython-310.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/price_believe_aquire.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc
Normal file
BIN
econ/agents/__pycache__/price_believe_distribute.cpython-310.pyc
Normal file
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()
|
||||
BIN
econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc
Normal file
BIN
econ/business/__pycache__/Price_Believe_Business.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/business/__pycache__/base_business.cpython-310.pyc
Normal file
BIN
econ/business/__pycache__/base_business.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/cells/__pycache__/cell.cpython-310.pyc
Normal file
BIN
econ/cells/__pycache__/cell.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/cells/__pycache__/db.cpython-310.pyc
Normal file
BIN
econ/cells/__pycache__/db.cpython-310.pyc
Normal file
Binary file not shown.
BIN
econ/commoditys/__pycache__/commoditys.cpython-310.pyc
Normal file
BIN
econ/commoditys/__pycache__/commoditys.cpython-310.pyc
Normal file
Binary file not shown.
@@ -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:
|
||||
|
||||
@@ -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
3
influx/creds.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
admin
|
||||
adminadmin
|
||||
ywMlW7zpVYFcGr1krHa83R_J_HSrd2wEq8MgtB7YX7Nc6QoTZSD6H-iiKlXYZUZaZY2MbNrL3XxfioaSGe9_bA==
|
||||
BIN
influx/influxd.exe
Normal file
BIN
influx/influxd.exe
Normal file
Binary file not shown.
BIN
log/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
log/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
7
main.py
7
main.py
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user