its starting to get stable

This commit is contained in:
2023-06-26 17:03:15 +02:00
parent 4cec35935f
commit 5514f3f1d9
19 changed files with 96 additions and 50 deletions

View File

@@ -4,13 +4,14 @@ spec:
max_world_price: 100
- name: "Food"
max_world_price: 5
max_world_price: 10
- name: "Grain"
max_world_price: 5
- name: "Fuel"
max_world_price: 1
- name: "Fruit"
max_world_price: 1

View File

@@ -3,3 +3,5 @@ spec:
- name: basic
res:
- 'Food': 1
- 'Fruit': 0.1
- 'Fuel': 0.1

View File

@@ -2,19 +2,29 @@ kind: production
spec:
- name: Grain
amount: 10
amount: 100
prod:
- Raw_Agriculture_Plot: 1
- Raw_Agriculture_Plot: 1
- name: Fruit
amount: 10
prod:
- Raw_Agriculture_Plot: 1
- Raw_Agriculture_Plot: 1
- name: Wood
amount: 10
prod:
- Raw_Forrest: 1
- name: Fuel
amount: 10
prod:
- Wood: 1
- name: Food
amount: 5
prod:
- Fruit: 1
- Grain: 1
- Fuel: 1
- Fruit: 1
- Grain: 2

View File

@@ -6,10 +6,12 @@ spec:
- name: grass
res:
- 'Raw_Agriculture_Plot': 100
- 'Raw_Agriculture_Plot': 1
- 'Raw_Forrest': 1
- name: forrest
res:
- 'Raw_Agriculture_Plot': 100
- 'Raw_Agriculture_Plot': 1
- 'Raw_Forrest': 2

View File

@@ -12,7 +12,16 @@ services:
networks:
- postgres-network
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: admin@pgadmin.com
PGADMIN_DEFAULT_PASSWORD: password
PGADMIN_LISTEN_PORT: 80
ports:
- 15433:80
depends_on:
- database
networks:
postgres-network:
driver: bridge

View File

@@ -17,6 +17,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
self.orders={i: {} for i in range(len(self.exchanges))}
self.target=0
self.min_price=-1
self.max_batch_qty=10
super().__init__(simulation)
def set_target(self,target: int):
@@ -54,7 +55,8 @@ class Base_Distribution_Agent(BaseAgent,ABC):
if not self.business.inventory[self.resource]>=amount:
return False # we dont have enough balance
if amount>self.max_batch_qty:
amount=self.max_batch_qty
self.business.inventory[self.resource]-=amount
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)

View File

@@ -12,6 +12,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
self.open_orders={i: [] for i in range(len(self.exchanges))}
self.open_qty=0
def tick(self,step,episode):
order_error=self.target_error()

View File

@@ -24,7 +24,7 @@ class Cell:
exchange: None
def __init__(self,name,x,y,area,pop,demand,world) -> None:
pop=pop*1000
pop=pop*100
self.name=name
self.loc_x=x
self.loc_y=y

View File

@@ -270,7 +270,7 @@ class Exchange():
data={}
data["cxid"]=name
data["episode"]=episode
data["step"]=step
#data["step"]=step
data["tstep"]=timepoint
data["instrm"]=instrm
data["best_ask"]=0
@@ -283,21 +283,21 @@ class Exchange():
## add best ask/best bid
for item , value in self.best_ask.items():
if value!=None:
localStepDB[item]["best_ask"]=value
localStepDB[item]["best_ask"]=int(value)
for item , value in self.best_bid.items():
if value!=None:
localStepDB[item]["best_bid"]=value
localStepDB[item]["best_bid"]=int(value)
#demand supply
for item , value in self.total_demand.items():
if value!=None:
localStepDB[item]["total_demand"]=value
localStepDB[item]["total_demand"]=int(value)
for item , value in self.total_supply.items():
if value!=None:
localStepDB[item]["total_supply"]=value
localStepDB[item]["total_supply"]=int(value)
#last market rate
for item , value in self.market_rate.items():
if value!=None:
localStepDB[item]["market_rate"]=value
localStepDB[item]["market_rate"]=int(value)
if autolog:
for id,item in localStepDB.items():
@@ -305,15 +305,29 @@ class Exchange():
return localStepDB
def log_episode(self,name,episode):
for t in self.executed_trades:
data={}
data["id"]=uuid.uuid4()
data["cxid"]=name
data["episode"]=episode
data["order_id"]=t.order_id
data["instmt"]=t.instmt
data["price"]=t.trade_price
data["qty"]=t.trade_qty
data["side"]=t.trade_side
log.EXTradeData.append(data)
transformed={}
for t in self.executed_trades:
if t.trade_side==1:
# will get logged twice so only log one side
if t.instmt not in transformed:
transformed[t.instmt]={}
if t.trade_price not in transformed[t.instmt]:
transformed[t.instmt][t.trade_price]=0
transformed[t.instmt][t.trade_price]+=t.trade_qty
for inst,stats in transformed.items():
for price,qty in stats.items():
data={}
#data["id"]=uuid.uuid4()
data["cxid"]=name
data["episode"]=int(episode)
#data["order_id"]=t.order_id
data["instmt"]=t.instmt
data["price"]=int(price)
data["qty"]=int(qty)
#data["side"]=int(t.trade_side)
log.EXTradeData.append(data)

View File

@@ -79,8 +79,7 @@ class Simulation():
agentstep=time.time()
for b in self.businesses:
b.tick(self.tick_count)
if b.balance<=0:
self.remove_business(b.id)
busstep=time.time()
self.tick_count+=1
self.log_cxs_tick(episode_length)
@@ -103,7 +102,9 @@ class Simulation():
v(self.episode_count)
for cell_id,cell in self.cells.items():
cell.setup_supply_for_episode()
for b in self.businesses:
if b.balance<=0:
self.remove_business(b.id)
self.tick_count=0
self.episode_count+=1
@@ -170,7 +171,7 @@ class Simulation():
profit[id]=0
profit[id]+=diff
best=None
bestv=0
bestv=-1000000000000
for k,v in profit.items():
if bestv<v:
bestv=v

View File

@@ -23,19 +23,23 @@ def get_client():
return write_client
def writeEXData():
db = create_engine(posturl)
conn = db.connect()
df=pd.DataFrame(EXBooksData)
df.to_sql('cx_books', con=conn, if_exists='replace',
index=False)
types=df.dtypes
start=time.time()
df.to_sql('cx_books', con=db, if_exists='replace',
index=False,chunksize=200000)
t1=time.time()
print(f"cx_books completed: {t1-start}/{df.size}")
df=pd.DataFrame(EXTradeData)
df.to_sql("cx_trades",con=conn, if_exists='replace',
index=False)
conn.close()
df.to_sql("cx_trades",con=db, if_exists='replace',
index=False,chunksize=10000)
t2=time.time()
print(f"cx_trades completed: {t2-t1}/{df.size}")
def writeBusinessData():
db = create_engine(posturl)
conn = db.connect()
df=pd.DataFrame(BUSINESSData)
df.to_sql('business', con=conn, if_exists='replace',
index=False)
conn.close()
df.to_sql('business', con=db, if_exists='replace',
index=False,chunksize=10000)
print(f"business completed: {df.size}")

10
main.py
View File

@@ -25,12 +25,12 @@ cxs=[cx]
# Init World
# Create Demand
cx.submit_order(w,"Wood",0,1000,Side.BUY)
cx.submit_order(2,"Gem",1,10,Side.SELL)
#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.seed(55)
sim.set_cells(cells[:10])
sim.reset()
#sim.tick(1)
# create info
@@ -41,7 +41,7 @@ for a in tqdm(range(50)):
for i in range(100):
sim.tick(100)
sim.reset()
sim.create_bussiness(1)
sim.create_bussiness(2)
log.writeEXData()
log.writeBusinessData()

BIN
tmp.prof

Binary file not shown.