Compare commits
13
Commits
4cec35935f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eec9c82a83 | ||
|
|
8d5ad7c62d | ||
|
|
010d1b6d3c | ||
|
|
da598c4475 | ||
|
|
7554071405 | ||
|
|
7d506625bd | ||
|
|
1ca6b3d036 | ||
|
|
9ed01abea4 | ||
|
|
4d4d609cea | ||
|
|
4bfe5245a9 | ||
|
|
509c59bb88 | ||
|
|
b7dc7b99d1 | ||
|
|
5514f3f1d9 |
Vendored
+5
-1
@@ -1,3 +1,7 @@
|
||||
{
|
||||
"ansible.python.interpreterPath": "c:\\Users\\handg\\miniconda3\\python.exe"
|
||||
"ansible.python.interpreterPath": "c:\\Users\\handg\\miniconda3\\python.exe",
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.autopep8"
|
||||
},
|
||||
"python.formatting.provider": "none"
|
||||
}
|
||||
+5
-4
@@ -4,13 +4,14 @@ spec:
|
||||
max_world_price: 100
|
||||
|
||||
- name: "Food"
|
||||
max_world_price: 5
|
||||
|
||||
max_world_price: 30
|
||||
|
||||
- name: "Grain"
|
||||
max_world_price: 5
|
||||
|
||||
|
||||
- name: "Fruit"
|
||||
- name: "Fuel"
|
||||
max_world_price: 1
|
||||
|
||||
- name: "Fruit"
|
||||
max_world_price: 5
|
||||
|
||||
@@ -3,3 +3,4 @@ spec:
|
||||
- name: basic
|
||||
res:
|
||||
- 'Food': 1
|
||||
# - 'Fruit': 0.1
|
||||
|
||||
+17
-7
@@ -2,19 +2,29 @@ kind: production
|
||||
spec:
|
||||
|
||||
- name: Grain
|
||||
amount: 10
|
||||
amount: 20
|
||||
prod:
|
||||
- Raw_Agriculture_Plot: 1
|
||||
Raw_Agriculture_Plot: 1
|
||||
- name: Fruit
|
||||
amount: 10
|
||||
amount: 20
|
||||
prod:
|
||||
- Raw_Agriculture_Plot: 1
|
||||
|
||||
Raw_Agriculture_Plot: 1
|
||||
# - name: Wood
|
||||
# amount: 1000
|
||||
# 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
|
||||
|
||||
|
||||
|
||||
|
||||
+4
-2
@@ -6,10 +6,12 @@ spec:
|
||||
|
||||
- name: grass
|
||||
res:
|
||||
- 'Raw_Agriculture_Plot': 100
|
||||
- 'Raw_Agriculture_Plot': 0.5
|
||||
- 'Raw_Forrest': 0.5
|
||||
|
||||
- name: forrest
|
||||
res:
|
||||
- 'Raw_Agriculture_Plot': 100
|
||||
- 'Raw_Agriculture_Plot': 0.5
|
||||
- 'Raw_Forrest': 0.5
|
||||
|
||||
|
||||
|
||||
+10
-1
@@ -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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -5,7 +5,7 @@ class AutoProductionAgent(BaseAgent):
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self,sim,business,worker=1,employment_rate=0) -> None:
|
||||
def __init__(self,sim,business,worker=-1,employment_rate=0) -> None:
|
||||
super().__init__(sim)
|
||||
self.business=business
|
||||
self.prod=business.production
|
||||
@@ -13,32 +13,56 @@ class AutoProductionAgent(BaseAgent):
|
||||
self.employment_rate=employment_rate
|
||||
self.employment_index=worker
|
||||
self.target=0
|
||||
self.prod_counter=0
|
||||
|
||||
def set_worker(self,workers):
|
||||
if workers>1:
|
||||
self.worker=workers
|
||||
elif workers==-1:
|
||||
self.worker=-1
|
||||
else:
|
||||
self.worker=1
|
||||
def set_target(self,qty):
|
||||
self.target=qty
|
||||
def can_produce(self):
|
||||
# If can produce item
|
||||
for com in self.prod["prod"]:
|
||||
for k,v in com.items():
|
||||
if v > self.business.inventory[k]:
|
||||
return False
|
||||
# how much can we produce
|
||||
minprod=[]
|
||||
|
||||
for k,v in self.prod["prod"].items():
|
||||
minprod.append(self.business.inventory[k]/v)
|
||||
possibleProds=int(min(minprod))
|
||||
|
||||
return possibleProds*self.prod["amount"]
|
||||
|
||||
def should_produce(self):
|
||||
# check if we should produce
|
||||
if self.business.resource_in_possesion()>self.target:
|
||||
return False
|
||||
return True
|
||||
if self.business.resource_in_possesion()>=self.target:
|
||||
return 0
|
||||
|
||||
return self.can_produce()
|
||||
|
||||
def tick(self,step,epi):
|
||||
for i in range(self.worker):
|
||||
if not self.can_produce():
|
||||
continue
|
||||
can=self.should_produce()
|
||||
if can==0:
|
||||
return
|
||||
|
||||
run=True
|
||||
i=self.worker
|
||||
while run:
|
||||
if self.should_produce()==0:
|
||||
return
|
||||
if i==0:
|
||||
return
|
||||
# remove cost from inventory
|
||||
for com in self.prod["prod"]:
|
||||
for k,cost in com.items():
|
||||
self.business.inventory[k]-=cost
|
||||
|
||||
for k,cost in self.prod["prod"].items():
|
||||
self.business.inventory[k]-=cost
|
||||
# add commodity
|
||||
self.business.inventory[self.prod['name']]+=self.prod["amount"]
|
||||
self.prod_counter+=1
|
||||
i-=1
|
||||
|
||||
def reset_qty(self):
|
||||
self.prod_counter=0
|
||||
def qty(self):
|
||||
return self.prod_counter
|
||||
@@ -29,7 +29,7 @@ class BaseAgent(ABC):
|
||||
"""
|
||||
Disables agent by removing it from the simulation
|
||||
"""
|
||||
self.simulation.unregister_agent(id)
|
||||
self.simulation.unregister_agent(self.id)
|
||||
def register(self):
|
||||
"""
|
||||
Enables agent by adding it to the simulation:
|
||||
|
||||
@@ -17,6 +17,11 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
self.orders={i: {} for i in range(len(self.exchanges))}
|
||||
self.target=0
|
||||
self.trades=[]
|
||||
self.tqty=0
|
||||
self.qty_offset=0
|
||||
|
||||
self.texpense=0
|
||||
self.expense_offset=0
|
||||
self.max_price=-1
|
||||
super().__init__(simulation)
|
||||
|
||||
@@ -34,7 +39,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
If err < 0 then agent needs to aquire resources
|
||||
"""
|
||||
err=self.business.inventory[self.resource]-self.target
|
||||
return err
|
||||
return int(err)
|
||||
|
||||
def set_price_max(self,price: int):
|
||||
"""
|
||||
@@ -58,9 +63,10 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
|
||||
self.business.balance-=total_price
|
||||
cx.add_to_account(self.id,"balance",total_price) # prepaid charge account for cx
|
||||
cx.register_account(self.id,self.business.id)
|
||||
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.BUY)
|
||||
if order==None: # Order failed
|
||||
return False
|
||||
return None
|
||||
self.orders[cx_id][order.order_id]=order
|
||||
self.update_trades()
|
||||
return order
|
||||
@@ -73,6 +79,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
amount=cx.get_account_resource_amount(self.id,"balance")
|
||||
cx.remove_from_account(self.id,"balance",amount)
|
||||
self.business.balance+=amount
|
||||
|
||||
|
||||
def collect_resource_from_cxs(self,resource):
|
||||
"""
|
||||
@@ -88,13 +95,39 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
Returns a list of all trades performed by this agent
|
||||
"""
|
||||
trades=[]
|
||||
self.tqty=0
|
||||
self.texpense=0
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
orders=self.orders[cx_id]
|
||||
if len(orders)>0:
|
||||
for k,o in orders.items():
|
||||
if k in cx.order_trades_map:
|
||||
trades.append(cx.order_trades_map[k])
|
||||
trades.extend(cx.order_trades_map[k])
|
||||
|
||||
self.trades=trades
|
||||
for t in trades:
|
||||
self.tqty+=t.trade_qty
|
||||
self.texpense+=round(t.trade_qty*t.trade_price,2)
|
||||
return trades
|
||||
|
||||
|
||||
# Confirm a purchase of x amount to reduce qty and expense counter
|
||||
def confirm_purchase(self,confirmed_qty):
|
||||
expensePer=self.expense/self.qty
|
||||
confirmedExpense=expensePer*confirmed_qty
|
||||
self.expense_offset-=confirmedExpense
|
||||
self.qty_offset-=confirmed_qty
|
||||
|
||||
@property
|
||||
def qty(self):
|
||||
return self.tqty+self.qty_offset
|
||||
@property
|
||||
def expense(self):
|
||||
return self.texpense+self.expense_offset
|
||||
|
||||
def reset(self, episode):
|
||||
#self.tqty=0
|
||||
self.qty_offset=0
|
||||
self.expense_offset=0
|
||||
self.trades=[]
|
||||
return super().reset(episode)
|
||||
@@ -2,6 +2,7 @@ from .base_agent import BaseAgent
|
||||
from ..exchange import Exchange
|
||||
from lightmatchingengine.lightmatchingengine import Order,Side
|
||||
from abc import ABC
|
||||
from decimal import *
|
||||
class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
|
||||
def __init__(self,simulation,business,resource,exchanges: list) -> None:
|
||||
@@ -17,12 +18,20 @@ 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
|
||||
self.tincome=0
|
||||
self.tqty=0
|
||||
self.income_offset=0
|
||||
self.qty_offset=0
|
||||
self.trade_curser=0
|
||||
super().__init__(simulation)
|
||||
|
||||
def set_target(self,target: int):
|
||||
"""
|
||||
Sets the amount of resources the agent should keep in business inventory
|
||||
"""
|
||||
if target<0:
|
||||
target=0
|
||||
self.target=target
|
||||
|
||||
|
||||
@@ -40,7 +49,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
If err < 0 then agent needs to distribute resources
|
||||
"""
|
||||
err=self.business.inventory[self.resource]-self.target
|
||||
return err
|
||||
return int(err)
|
||||
|
||||
|
||||
def distribute_resource(self,price_per,amount,cx_id):
|
||||
@@ -51,12 +60,16 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
# Get exchange
|
||||
cx=self.exchanges[cx_id]
|
||||
|
||||
#if amount>=self.max_batch_qty:
|
||||
# amount=self.max_batch_qty
|
||||
|
||||
if not self.business.inventory[self.resource]>=amount:
|
||||
return False # we dont have enough balance
|
||||
|
||||
|
||||
self.business.inventory[self.resource]-=amount
|
||||
|
||||
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
|
||||
cx.register_account(self.id,self.business.id)
|
||||
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)
|
||||
if order==None: # Order failed
|
||||
return False
|
||||
@@ -71,6 +84,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
amount=cx.get_account_resource_amount(self.id,"balance")
|
||||
cx.remove_from_account(self.id,"balance",amount)
|
||||
self.business.balance+=amount
|
||||
|
||||
|
||||
def collect_resource_from_cxs(self,resource):
|
||||
"""
|
||||
@@ -86,16 +100,42 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
||||
Returns a list of all trades performed by this agent
|
||||
"""
|
||||
trades=[]
|
||||
self.tqty=0
|
||||
self.tincome=0
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
orders=self.orders[cx_id]
|
||||
if len(orders)>0:
|
||||
for k,o in orders.items():
|
||||
if k in cx.order_trades_map:
|
||||
trades.append(cx.order_trades_map[k])
|
||||
trades.extend(cx.order_trades_map[k])
|
||||
self.trades=trades
|
||||
|
||||
for t in trades:
|
||||
self.tqty+=t.trade_qty
|
||||
self.tincome+=t.trade_qty*t.trade_price
|
||||
self.tincome=round(self.tincome,2)
|
||||
return trades
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def qty(self):
|
||||
return self.tqty+self.qty_offset
|
||||
@property
|
||||
def income(self):
|
||||
return self.tincome+self.income_offset
|
||||
|
||||
def confirm_distribution(self,dis_qty,step):
|
||||
income_per=self.income/self.qty
|
||||
income_to_confirm=income_per*dis_qty
|
||||
income_to_confirm=round(income_to_confirm,2)
|
||||
self.income_offset-=income_to_confirm
|
||||
self.qty_offset-=dis_qty
|
||||
self.income_offset=round(self.income_offset,2)
|
||||
self.trade_curser=len(self.trades)-1
|
||||
|
||||
def reset(self, episode):
|
||||
self.qty_offset=0
|
||||
self.tqty=0
|
||||
self.income_offset=0
|
||||
self.tincome=0
|
||||
self.trade_curser=0
|
||||
self.trades=[]
|
||||
return super().reset(episode)
|
||||
@@ -1,109 +1,147 @@
|
||||
from .base_aquire_agent import Base_Aquire_Agent
|
||||
import random
|
||||
|
||||
|
||||
class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
||||
"""
|
||||
Aquire agent with internal price believe system.
|
||||
"""
|
||||
|
||||
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
||||
super().__init__(simulation, business, resource, exchanges)
|
||||
self.lr=lr
|
||||
self.max_price_adj_rate=max_price_adj_rate
|
||||
self.price_believe={i: 1 for i in range(len(self.exchanges))}
|
||||
self.open_orders={i: [] for i in range(len(self.exchanges))}
|
||||
self.open_qty=0
|
||||
|
||||
self.lr = lr
|
||||
self.max_price_adj_rate = max_price_adj_rate
|
||||
self.price_believe = {i: 1 for i in range(len(self.exchanges))}
|
||||
self.open_orders = {i: [] for i in range(len(self.exchanges))}
|
||||
self.open_qty = 0
|
||||
self.hp_threshold=0.70
|
||||
self.lp_threshold=0.99
|
||||
|
||||
def tick(self,tick,episode):
|
||||
def tick(self, tick, episode):
|
||||
|
||||
|
||||
order_error=self.open_qty+self.target_error()
|
||||
if order_error<0:
|
||||
order_error = self.open_qty+self.target_error()
|
||||
if order_error < 0:
|
||||
# aquire based on current price believe
|
||||
cx_id=self.select_best_cx()
|
||||
order=self.order_resource(self.price_believe[cx_id],order_error*-1,cx_id)
|
||||
if not order==None:
|
||||
self.register_order(cx_id,order)
|
||||
cx_id = self.select_best_cx()
|
||||
order = self.order_resource(
|
||||
self.price_believe[cx_id], order_error*-1, cx_id)
|
||||
if not order == None:
|
||||
self.register_order(cx_id, order)
|
||||
else:
|
||||
# order failed due to missing balance.. we need to adjust our price believe
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_believe(cx_id,-1)
|
||||
self.tick_open_orders()
|
||||
self.update_believe(cx_id, -1)
|
||||
self.tick_open_orders()
|
||||
|
||||
def select_best_cx(self):
|
||||
best_id=-1
|
||||
best=999999999
|
||||
best_id = -1
|
||||
best = 999999999
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
available=(cx.get_total_supply(self.resource)>0)
|
||||
cx = self.exchanges[cx_id]
|
||||
available = (cx.get_total_supply(self.resource) > 0)
|
||||
if available:
|
||||
potential=1*self.price_believe[cx_id]
|
||||
potential = 1*self.price_believe[cx_id]
|
||||
else:
|
||||
continue
|
||||
if potential<best:
|
||||
best=potential
|
||||
best_id=cx_id
|
||||
if best_id==-1:
|
||||
best_id=random.randint(0,len(self.exchanges)-1)
|
||||
if potential < best:
|
||||
best = potential
|
||||
best_id = cx_id
|
||||
if best_id == -1:
|
||||
best_id = random.randint(0, len(self.exchanges)-1)
|
||||
return best_id
|
||||
|
||||
def register_order(self,cx_id,order):
|
||||
|
||||
def register_order(self, cx_id, order):
|
||||
half=self.max_price_adj_rate/2
|
||||
ran=random.randrange(-half,half)
|
||||
self.open_orders[cx_id].append({
|
||||
'id': order.order_id,
|
||||
'lifetime': self.max_price_adj_rate,
|
||||
'lifetime': self.max_price_adj_rate+ran,
|
||||
'leaves': order.leaves_qty
|
||||
})
|
||||
if order.leaves_qty != order.qty:
|
||||
self.update_trades()
|
||||
|
||||
def tick_open_orders(self) -> int:
|
||||
"""
|
||||
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
||||
Returns ids of orders that timeed out.
|
||||
"""
|
||||
self.open_qty=0
|
||||
self.open_qty = 0
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
cx_orders=self.open_orders[cx_id]
|
||||
cx = self.exchanges[cx_id]
|
||||
cx_orders = self.open_orders[cx_id]
|
||||
for i in cx_orders:
|
||||
# Check for each order if it is fullfiled or if it is timed
|
||||
o=self.orders[cx_id][i["id"]]
|
||||
if o.leaves_qty==0:
|
||||
#order is done
|
||||
self.open_orders[cx_id].remove(i) # remove order from open
|
||||
self.update_believe(cx_id,-1) # update price believe
|
||||
# Check for each order if it is fullfiled or if it is timed
|
||||
o = self.orders[cx_id][i["id"]]
|
||||
leaves = o.leaves_qty
|
||||
if o.leaves_qty == 0:
|
||||
# order is done
|
||||
self.open_orders[cx_id].remove(i) # remove order from open
|
||||
self.update_believe(cx_id, -1) # update price believe
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
|
||||
continue
|
||||
if i["lifetime"]>0:
|
||||
self.open_qty+=o.leaves_qty
|
||||
i["lifetime"]-=1 # subtract lifetime
|
||||
if not (i["leaves"] == leaves):
|
||||
# update in order
|
||||
i["leaves"] = leaves
|
||||
self.update_trades()
|
||||
# reset lifetime
|
||||
i["lifetime"] = self.max_price_adj_rate
|
||||
if i["lifetime"] > 0:
|
||||
self.open_qty += o.leaves_qty
|
||||
i["lifetime"] -= 1 # subtract lifetime
|
||||
else:
|
||||
# timeout
|
||||
self.update_trades()
|
||||
|
||||
modifier =0
|
||||
success=self.calc_order_success(cx,o)
|
||||
if success>=self.lp_threshold:
|
||||
modifier=-1
|
||||
elif success<=self.hp_threshold:
|
||||
modifier=1
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_believe(cx_id,1)
|
||||
self.update_believe(cx_id, modifier)
|
||||
self.open_orders[cx_id].remove(i)
|
||||
|
||||
def update_believe(self,cx_id,modifier):
|
||||
def calc_order_success(self, cx, o):
|
||||
"""
|
||||
Calculate how we should adjust the price belive
|
||||
"""
|
||||
buy = o.qty-o.leaves_qty
|
||||
buyperc = buy/o.qty
|
||||
dem = cx.total_supply[self.resource]+buy
|
||||
if dem == 0:
|
||||
dem = 1
|
||||
coverage = buy/dem
|
||||
base_success = max([coverage, buyperc])
|
||||
return base_success
|
||||
|
||||
def update_believe(self, cx_id, modifier):
|
||||
"""
|
||||
Updates the believe based on the modifier.
|
||||
If positive will add lr to believe
|
||||
If negative will sub lr to believe
|
||||
"""
|
||||
self.price_believe[cx_id]+=modifier*self.lr
|
||||
if self.price_believe[cx_id]<0:
|
||||
self.price_believe[cx_id]=0
|
||||
self.price_believe[cx_id] += modifier*self.lr
|
||||
if self.price_believe[cx_id] < 0:
|
||||
self.price_believe[cx_id] = 0
|
||||
|
||||
def reset(self,episode):
|
||||
def reset(self, episode):
|
||||
# Clean shop for today
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
cx_orders=self.open_orders[cx_id]
|
||||
cx = self.exchanges[cx_id]
|
||||
cx_orders = self.open_orders[cx_id]
|
||||
for i in cx_orders:
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
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)
|
||||
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)
|
||||
|
||||
@@ -1,106 +1,162 @@
|
||||
from .base_distribution_agent import Base_Distribution_Agent
|
||||
import random
|
||||
|
||||
|
||||
class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
"""
|
||||
Aquire agent with internal price believe system.
|
||||
"""
|
||||
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
||||
super().__init__(simulation, business, resource, exchanges)
|
||||
self.lr=lr
|
||||
self.max_price_adj_rate=max_price_adj_rate
|
||||
self.price_believe={i: 1 for i in range(len(self.exchanges))}
|
||||
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()
|
||||
if order_error>0:
|
||||
def __init__(self, simulation, business, resource, exchanges: list, lr,at_sales, max_price_adj_rate) -> None:
|
||||
super().__init__(simulation, business, resource, exchanges)
|
||||
self.lr = lr
|
||||
|
||||
self.lr_at_sales=at_sales
|
||||
self.max_price_adj_rate = max_price_adj_rate
|
||||
self.price_believe = {i: 1 for i in range(len(self.exchanges))}
|
||||
self.lr_moment={i: 0 for i in range(len(self.exchanges))}
|
||||
self.open_orders = {i: [] for i in range(len(self.exchanges))}
|
||||
self.open_qty = 0
|
||||
self.beta=0.8
|
||||
self.lp_threshold=0.05
|
||||
self.hp_threshold=0.80
|
||||
|
||||
def tick(self, step, episode):
|
||||
|
||||
order_error = self.target_error()
|
||||
if (order_error > 0) & (self.open_qty==0):
|
||||
# aquire based on current price belive
|
||||
cx_id=self.select_best_cx()
|
||||
order=self.distribute_resource(self.price_believe[cx_id],order_error,cx_id)
|
||||
self.register_order(cx_id,order)
|
||||
self.tick_open_orders()
|
||||
cx_id = self.select_best_cx()
|
||||
|
||||
price = round(self.price_believe[cx_id],2)
|
||||
if price==30.0:
|
||||
print("Ple")
|
||||
if price < self.min_price:
|
||||
price = self.min_price
|
||||
order = self.distribute_resource(
|
||||
price, order_error, cx_id)
|
||||
self.register_order(cx_id, order)
|
||||
self.tick_open_orders()
|
||||
|
||||
def select_best_cx(self):
|
||||
best_id=-1
|
||||
best=-1
|
||||
best_id = -1
|
||||
best = -1
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
available=(cx.get_total_demand(self.resource)>0)
|
||||
cx = self.exchanges[cx_id]
|
||||
available = (cx.get_total_demand(self.resource) > 0)
|
||||
if available:
|
||||
potential=1*self.price_believe[cx_id]
|
||||
potential = 1*self.price_believe[cx_id]
|
||||
else:
|
||||
continue
|
||||
if potential>best:
|
||||
best=potential
|
||||
best_id=cx_id
|
||||
if best_id==-1:
|
||||
best_id=random.randint(0,len(self.exchanges)-1)
|
||||
if potential > best:
|
||||
best = potential
|
||||
best_id = cx_id
|
||||
if best_id == -1:
|
||||
best_id = random.randint(0, len(self.exchanges)-1)
|
||||
return best_id
|
||||
|
||||
def register_order(self,cx_id,order):
|
||||
|
||||
def register_order(self, cx_id, order):
|
||||
half=self.max_price_adj_rate/2
|
||||
ran=random.randrange(-half,half)
|
||||
self.open_orders[cx_id].append({
|
||||
'id': order.order_id,
|
||||
'lifetime': self.max_price_adj_rate,
|
||||
'lifetime': self.max_price_adj_rate+ran,
|
||||
'leaves': order.leaves_qty
|
||||
})
|
||||
if order.leaves_qty != order.qty:
|
||||
self.update_trades()
|
||||
|
||||
def tick_open_orders(self) -> int:
|
||||
"""
|
||||
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
||||
Returns ids of orders that timeed out.
|
||||
"""
|
||||
self.open_qty=0
|
||||
self.open_qty = 0
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
cx_orders=self.open_orders[cx_id]
|
||||
cx = self.exchanges[cx_id]
|
||||
cx_orders = self.open_orders[cx_id]
|
||||
for i in cx_orders:
|
||||
# Check for each order if it is fullfiled or if it is timed
|
||||
o=self.orders[cx_id][i["id"]]
|
||||
if o.leaves_qty==0:
|
||||
#order is done
|
||||
# Check for each order if it is fullfiled or if it is timed
|
||||
o = self.orders[cx_id][i["id"]]
|
||||
leaves = o.leaves_qty
|
||||
if o.leaves_qty == 0:
|
||||
# order is done
|
||||
self.open_orders[cx_id].remove(i) # remove order from open
|
||||
self.update_believe(cx_id,1) # update price believe
|
||||
self.update_trades()
|
||||
succsess=self.calc_order_success(cx,o)
|
||||
modifier=0
|
||||
if succsess>=self.hp_threshold:
|
||||
modifier=1
|
||||
elif succsess<=self.lp_threshold:
|
||||
modifier=-1
|
||||
self.update_believe(cx_id,o.price, succsess,o.qty) # update price believe
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
|
||||
continue
|
||||
if i["lifetime"]>0:
|
||||
self.open_qty+=o.leaves_qty
|
||||
i["lifetime"]-=1 # subtract lifetime
|
||||
|
||||
if not (i["leaves"] == leaves):
|
||||
# update in order
|
||||
i["leaves"] = leaves
|
||||
self.update_trades()
|
||||
# reset lifetime
|
||||
i["lifetime"] = self.max_price_adj_rate
|
||||
|
||||
if i["lifetime"] > 0:
|
||||
self.open_qty += o.leaves_qty
|
||||
i["lifetime"] -= 1 # subtract lifetime
|
||||
else:
|
||||
# timeout
|
||||
self.update_trades()
|
||||
succsess=self.calc_order_success(cx,o)
|
||||
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_believe(cx_id,-1)
|
||||
self.update_believe(cx_id,o.price, succsess,o.qty)
|
||||
self.open_orders[cx_id].remove(i)
|
||||
|
||||
def calc_order_success(self,cx, o):
|
||||
"""
|
||||
Calculate how we should adjust the price belive
|
||||
"""
|
||||
sold = o.qty-o.leaves_qty
|
||||
nsold=min(o.leaves_qty,cx.total_demand[self.resource])
|
||||
dem = cx.total_demand[self.resource]+sold
|
||||
if dem == 0:
|
||||
dem = 1
|
||||
score=sold-nsold
|
||||
return score
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def update_believe(self,cx_id,modifier):
|
||||
def update_believe(self, cx_id, used_price, modifier,qty):
|
||||
"""
|
||||
Updates the believe based on the modifier.
|
||||
If positive will add lr to believe
|
||||
If negative will sub lr to believe
|
||||
"""
|
||||
self.price_believe[cx_id]+=modifier*self.lr
|
||||
if self.price_believe[cx_id]<0:
|
||||
self.price_believe[cx_id]=0
|
||||
|
||||
def reset(self,episode):
|
||||
clipMod=max(-qty,min(modifier,qty))
|
||||
moment=clipMod/qty
|
||||
#clip mod so that price believe more smooth
|
||||
|
||||
self.lr_moment[cx_id]=self.lr_moment[cx_id]*self.beta + (1-self.beta)*moment
|
||||
update=self.lr_moment[cx_id]*self.lr
|
||||
self.price_believe[cx_id]+=update
|
||||
|
||||
self.price_believe[cx_id] = round(self.price_believe[cx_id], 2)
|
||||
if self.price_believe[cx_id] < 1:
|
||||
self.price_believe[cx_id] = 1
|
||||
|
||||
def reset(self, episode):
|
||||
# Clean shop for today
|
||||
self.update_trades()
|
||||
for cx_id in range(len(self.exchanges)):
|
||||
cx=self.exchanges[cx_id]
|
||||
cx_orders=self.open_orders[cx_id]
|
||||
cx = self.exchanges[cx_id]
|
||||
cx_orders = self.open_orders[cx_id]
|
||||
for i in cx_orders:
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
@@ -2,36 +2,148 @@ from .base_business import Business
|
||||
from ..agents.price_believe_aquire import Price_Believe_Aquire_Agent
|
||||
from ..agents.price_believe_distribute import Price_Believe_Distribiute_Agent
|
||||
from ..agents.autoproduction import AutoProductionAgent
|
||||
import log
|
||||
|
||||
|
||||
class Price_Believe_Business(Business):
|
||||
def __init__(self, id, production, balance,exchange,simulation) -> None:
|
||||
super().__init__(id, production, balance)
|
||||
|
||||
self.distribute=Price_Believe_Distribiute_Agent(simulation,self,production["name"],exchange,0.5,10)
|
||||
self.craft=AutoProductionAgent(simulation,self)
|
||||
self.aquire={}
|
||||
for l in production["prod"]:
|
||||
for k,v in l.items():
|
||||
a=Price_Believe_Aquire_Agent(simulation,self,k,exchange,1,10)
|
||||
a.set_target(v*2)
|
||||
a.set_price_max(10)
|
||||
self.aquire[k]=a
|
||||
self.distribute.set_price_min(10)
|
||||
self.distribute.set_target(0)
|
||||
def __init__(self, id, production, balance, exchange, simulation, location) -> None:
|
||||
super().__init__(id, production, balance, location)
|
||||
|
||||
def tick_business_decisions(self,step):
|
||||
for comp in self.production["prod"]:
|
||||
for k,v in comp.items():
|
||||
modifier=1
|
||||
self.aquire[k].set_target(v*modifier)
|
||||
max_amount=self.production["amount"]*5
|
||||
self.craft.set_target(max_amount)
|
||||
self.max_storage = 10
|
||||
self.expense_per_unit = -1
|
||||
self.expense_per_prod=-1
|
||||
self.income_per_unit = -1
|
||||
self.distribute = Price_Believe_Distribiute_Agent(
|
||||
simulation, self, production["name"], exchange, 0.08,production["amount"]*2, 6)
|
||||
self.craft = AutoProductionAgent(simulation, self)
|
||||
self.aquire = {}
|
||||
|
||||
for k, v in production["prod"].items():
|
||||
a = Price_Believe_Aquire_Agent(
|
||||
simulation, self, k, exchange, 0.02, 6)
|
||||
a.set_target(v*2)
|
||||
a.set_price_max(10)
|
||||
self.aquire[k] = a
|
||||
self.distribute.set_price_min(0)
|
||||
self.distribute.max_batch_qty=production["amount"]
|
||||
self.distribute.set_target(0)
|
||||
|
||||
def tick_business_decisions(self, step):
|
||||
amount= self.production["amount"]
|
||||
if step == 0:
|
||||
|
||||
max_amount = self.production["amount"]*5
|
||||
self.craft.set_target(max_amount)
|
||||
# Update bookkeeping
|
||||
self.update_ex_per_unit()
|
||||
self.update_income_per_unit(step)
|
||||
|
||||
# calc descision
|
||||
orderForNewProds = self.max_storage
|
||||
if self.income_per_unit<=0 or self.expense_per_unit<=0:
|
||||
# dont have data
|
||||
retain=0
|
||||
else:
|
||||
ie = (self.income_per_unit/self.expense_per_unit)-1
|
||||
ie=max(0,min(ie,1))
|
||||
retain=((self.max_storage-ie*self.max_storage)*amount)-amount
|
||||
#retain=0
|
||||
|
||||
|
||||
# set target for aquire
|
||||
|
||||
prod = self.production["prod"]
|
||||
|
||||
for k, v in prod.items():
|
||||
self.aquire[k].set_target(v*orderForNewProds)
|
||||
|
||||
# update production
|
||||
targetUnit = self.production["amount"]*orderForNewProds
|
||||
self.craft.set_target(targetUnit)
|
||||
|
||||
# set min distribute
|
||||
self.distribute.set_price_min(self.expense_per_unit)
|
||||
self.distribute.set_target(retain)
|
||||
|
||||
def tick_business_episode(self, episode_count):
|
||||
start_balance = self.balance_history[-1]
|
||||
diff = self.balance-start_balance
|
||||
|
||||
def resource_in_possesion(self):
|
||||
return self.distribute.open_qty+self.inventory[self.production["name"]]
|
||||
return self.distribute.open_qty+self.inventory[self.production["name"]]
|
||||
|
||||
def update_ex_per_unit(self):
|
||||
experunit = 0
|
||||
prod = self.production["prod"]
|
||||
amount = self.production["amount"]
|
||||
qtyNewProds = -1
|
||||
for key, agent in self.aquire.items():
|
||||
agent.update_trades()
|
||||
if agent.qty == 0:
|
||||
return
|
||||
if agent.qty < 0:
|
||||
print("pla")
|
||||
req = prod[key]
|
||||
|
||||
covers = agent.qty/req
|
||||
|
||||
if qtyNewProds == -1:
|
||||
qtyNewProds = covers
|
||||
if qtyNewProds > covers:
|
||||
qtyNewProds = covers
|
||||
|
||||
ExPerProd = agent.expense/covers
|
||||
experunit += ExPerProd/amount
|
||||
|
||||
if qtyNewProds > 0:
|
||||
# confirm new units
|
||||
for key, agent in self.aquire.items():
|
||||
qtyToConfirm = prod[key]*qtyNewProds
|
||||
agent.confirm_purchase(qtyToConfirm)
|
||||
a = 1+2
|
||||
|
||||
qtyNewUnits = qtyNewProds*amount
|
||||
|
||||
# if we havent set the expense_per_unit
|
||||
if self.expense_per_unit == -1:
|
||||
self.expense_per_unit = experunit
|
||||
return
|
||||
# update expense per unit
|
||||
prev_ex_total = self.expense_per_unit*self.resource_in_possesion()
|
||||
new_ex_total = qtyNewUnits*experunit
|
||||
ex_total = prev_ex_total+new_ex_total
|
||||
estimatedQTYUnits = qtyNewUnits+self.resource_in_possesion()
|
||||
new_ex = ex_total/estimatedQTYUnits
|
||||
self.expense_per_unit = new_ex
|
||||
self.expense_per_prod=self.expense_per_unit*amount
|
||||
|
||||
def update_income_per_unit(self, step):
|
||||
qty_sold = self.distribute.qty
|
||||
income = self.distribute.income
|
||||
if income == 0:
|
||||
return
|
||||
self.income_per_unit = qty_sold/income
|
||||
self.distribute.confirm_distribution(qty_sold, step)
|
||||
a=self.distribute.trade_curser
|
||||
|
||||
def close_business(self):
|
||||
for k,a in self.aquire.items():
|
||||
a.unregister()
|
||||
self.distribute.unregister()
|
||||
self.craft.unregister()
|
||||
for k, a in self.aquire.items():
|
||||
a.unregister()
|
||||
self.distribute.unregister()
|
||||
self.craft.unregister()
|
||||
|
||||
def log(self, episode, episode_length, step):
|
||||
data = {}
|
||||
data["id"] = self.id
|
||||
data["episode"] = episode
|
||||
data["step"] = step
|
||||
data["tstep"] = step+episode*episode_length
|
||||
data["production"] = self.production["name"]
|
||||
data["expense"] = self.expense_per_unit
|
||||
data["expense_prod"]=self.expense_per_prod
|
||||
data["income"] = self.income_per_unit
|
||||
data["inventory"] = self.inventory[self.production["name"]]
|
||||
data["retain"] = self.distribute.target
|
||||
data["balance"] = self.balance
|
||||
data["location"] = self.location
|
||||
log.BUSINESSData.append(data)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
from abc import ABC
|
||||
import log
|
||||
class Business(ABC):
|
||||
def __init__(self,id,production,balance) -> None:
|
||||
def __init__(self,id,production,balance,location) -> None:
|
||||
"""production (dict): {
|
||||
name: 'Gem',
|
||||
amount: 4,
|
||||
@@ -13,14 +13,17 @@ class Business(ABC):
|
||||
balance (int): Starting Balance
|
||||
"""
|
||||
self.id=id
|
||||
self.location=location
|
||||
self.production=production
|
||||
self.balance_history=[balance]
|
||||
self.balance=balance
|
||||
self.expense=0
|
||||
self.income=0
|
||||
#Setup Inventory
|
||||
self.inventory={production["name"]: 0}
|
||||
for l in production["prod"]:
|
||||
for k,v in l.items():
|
||||
self.inventory[k]=0
|
||||
|
||||
for k,v in production["prod"].items():
|
||||
self.inventory[k]=0
|
||||
pass
|
||||
|
||||
def tick(self,step):
|
||||
@@ -34,8 +37,10 @@ class Business(ABC):
|
||||
"""
|
||||
Call for resetting for the next episode
|
||||
"""
|
||||
self.balance_history.append(self.balance)
|
||||
self.tick_business_episode(episode_count)
|
||||
self.balance_history.append(self.balance)
|
||||
|
||||
|
||||
|
||||
|
||||
def tick_business_episode(self,episode_count):
|
||||
@@ -47,12 +52,3 @@ class Business(ABC):
|
||||
def close_business(self):
|
||||
assert "close_business has not been provided"
|
||||
|
||||
def log(self,episode,episode_length,step):
|
||||
data={}
|
||||
data["id"]=self.id
|
||||
data["episode"]=episode
|
||||
data["step"]=step
|
||||
data["tstep"]=step+episode*episode_length
|
||||
data["production"]=self.production["name"]
|
||||
data["balance"]=self.balance
|
||||
log.BUSINESSData.append(data)
|
||||
Binary file not shown.
+2
-1
@@ -24,7 +24,7 @@ class Cell:
|
||||
exchange: None
|
||||
|
||||
def __init__(self,name,x,y,area,pop,demand,world) -> None:
|
||||
pop=pop*1000
|
||||
pop=pop*10
|
||||
self.name=name
|
||||
self.loc_x=x
|
||||
self.loc_y=y
|
||||
@@ -63,6 +63,7 @@ class Cell:
|
||||
qty=value/episode_length
|
||||
comm=cm.commoditys[demand_key]
|
||||
max_price=comm["max_world_price"]*qty
|
||||
self.exchange.register_account(self.name,self.name)
|
||||
self.exchange.add_to_account(self.name,"balance",max_price)
|
||||
self.exchange.submit_order(self.name,demand_key,qty,comm["max_world_price"],Side.BUY)
|
||||
def setup_supply_for_episode(self):
|
||||
|
||||
Binary file not shown.
@@ -40,10 +40,10 @@ def search_yaml_files(directory = "db"):
|
||||
productions[comm['name']]=[]
|
||||
productions[comm['name']].append(comm)
|
||||
productions_id_map[comm["id"]]=comm
|
||||
for comp in comm["prod"]:
|
||||
k=list(comp.keys())[0]
|
||||
if k not in productions_uses:
|
||||
productions_uses[k]=[]
|
||||
productions_uses[k].append(comm)
|
||||
k=comm["prod"].keys()
|
||||
for component in k:
|
||||
if component not in productions_uses:
|
||||
productions_uses[component]=[]
|
||||
productions_uses[component].append(comm)
|
||||
|
||||
|
||||
|
||||
+101
-35
@@ -6,8 +6,13 @@ from influxdb_client import InfluxDBClient, Point
|
||||
import time
|
||||
import pandas as pd
|
||||
import uuid
|
||||
import threading
|
||||
|
||||
software_start=int(time.time())
|
||||
|
||||
|
||||
|
||||
|
||||
class Exchange():
|
||||
"""
|
||||
Basic Commodity exchange.
|
||||
@@ -25,21 +30,30 @@ class Exchange():
|
||||
self.order_account_map={}
|
||||
self.orders={}
|
||||
self.executed_trades=[]
|
||||
self.trade_curser=0
|
||||
self.account_business={}
|
||||
|
||||
self.order_trades_map={}
|
||||
self.market_rate={}
|
||||
self.best_ask={}
|
||||
self.best_bid={}
|
||||
self.total_demand={}
|
||||
self.demand={}
|
||||
#self.demand={}
|
||||
self.total_supply={}
|
||||
self.supply={}
|
||||
#self.supply={}
|
||||
self.traded_commoditys={}
|
||||
|
||||
self.lock=threading.Lock()
|
||||
|
||||
|
||||
def register_account(self,account,business):
|
||||
self.account_business[account]=business
|
||||
|
||||
def add_to_account(self,account_id,resource,amount):
|
||||
"""
|
||||
Adds resources to account escrow
|
||||
"""
|
||||
#check if account exists
|
||||
self.lock.acquire()
|
||||
if account_id not in self.account:
|
||||
self.account[account_id]={_bal: 0}
|
||||
#check if ressource exists
|
||||
@@ -47,12 +61,14 @@ class Exchange():
|
||||
self.account[account_id][resource]=0
|
||||
|
||||
self.account[account_id][resource]+=amount
|
||||
self.lock.release()
|
||||
|
||||
def remove_from_account(self,account_id,resource,amount) -> int:
|
||||
"""
|
||||
Remove resources from account. Returns amount retrieved
|
||||
"""
|
||||
#check if account exists
|
||||
self.lock.acquire()
|
||||
if account_id not in self.account:
|
||||
self.account[account_id]={_bal: 0}
|
||||
#check if ressource exists
|
||||
@@ -65,6 +81,7 @@ class Exchange():
|
||||
self.account[account_id][resource]=0
|
||||
else:
|
||||
self.account[account_id][resource]-=amount
|
||||
self.lock.release()
|
||||
return ret
|
||||
|
||||
def _move_to_escrow(self,account_id,resource,amount):
|
||||
@@ -119,8 +136,13 @@ class Exchange():
|
||||
Returns an order if the order is active. Returns None if submit has failed.
|
||||
"""
|
||||
# calculate price for complete order fullfilment
|
||||
prev=amount
|
||||
amount=int(amount)
|
||||
full_price=price*amount
|
||||
price=round(price)
|
||||
if amount<1:
|
||||
# invalid order
|
||||
return None
|
||||
full_price=round(price*amount)
|
||||
|
||||
|
||||
# Move resources into escrow
|
||||
@@ -133,13 +155,14 @@ class Exchange():
|
||||
# no sufficient resources
|
||||
return None
|
||||
# create order and execude any trades
|
||||
|
||||
self.lock.acquire()
|
||||
order,trades=self.lme.add_order(resource,price,amount,side)
|
||||
|
||||
self.orders[order.order_id]=order
|
||||
self.order_account_map[order.order_id]=account_id
|
||||
self._execute_trades(trades)
|
||||
self.calculate_resource_metrics(resource)
|
||||
self.lock.release()
|
||||
return order
|
||||
|
||||
def cancel_order(self,order_id):
|
||||
@@ -152,6 +175,7 @@ class Exchange():
|
||||
order=self.orders[order_id]
|
||||
if self._is_order_complete(order):
|
||||
return False
|
||||
self.lock.acquire()
|
||||
order=self.lme.cancel_order(order_id,order.instmt)
|
||||
|
||||
if order_id in self.order_trades_map: # has order any kind of trades
|
||||
@@ -171,6 +195,7 @@ class Exchange():
|
||||
qty_traded+=trade.trade_qty
|
||||
qty_diff=qty_submitted-qty_traded
|
||||
self._move_to_account(self.order_account_map[order_id],order.instmt,qty_diff)
|
||||
self.lock.release()
|
||||
return True
|
||||
|
||||
|
||||
@@ -222,21 +247,29 @@ class Exchange():
|
||||
|
||||
def calculate_resource_metrics(self, resource):
|
||||
order_book = self.lme.order_books.setdefault(resource, OrderBook())
|
||||
best_bid = max(order_book.bids.keys()) if len(order_book.bids) > 0 else None
|
||||
best_ask = max(order_book.asks.keys()) if len(order_book.asks) > 0 else None
|
||||
best_bid = None
|
||||
best_ask = None
|
||||
self.total_demand[resource]=0
|
||||
self.demand[resource]={}
|
||||
#self.demand[resource]={}
|
||||
for k,v in order_book.bids.items():
|
||||
self.demand[resource][k]=0
|
||||
# self.demand[resource][k]=0
|
||||
if best_bid==None:
|
||||
best_bid=k
|
||||
if best_bid<=k:
|
||||
best_bid=k
|
||||
for o in v:
|
||||
self.demand[resource][k]+=o.leaves_qty
|
||||
# self.demand[resource][k]+=o.leaves_qty
|
||||
self.total_demand[resource]+=o.leaves_qty
|
||||
self.supply[resource]={}
|
||||
#self.supply[resource]={}
|
||||
self.total_supply[resource]=0
|
||||
for k,v in order_book.asks.items():
|
||||
self.supply[resource][k]=0
|
||||
# self.supply[resource][k]=0
|
||||
if best_ask==None:
|
||||
best_ask=k
|
||||
if best_ask<=k:
|
||||
best_ask=k
|
||||
for o in v:
|
||||
self.supply[resource][k]+=o.leaves_qty
|
||||
# self.supply[resource][k]+=o.leaves_qty
|
||||
self.total_supply[resource]+=o.leaves_qty
|
||||
|
||||
|
||||
@@ -270,34 +303,53 @@ 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
|
||||
data["best_bid"]=0
|
||||
data["total_demand"]=0
|
||||
data["total_supply"]=0
|
||||
data["market_rate"]=0
|
||||
data["best_ask"]=None
|
||||
data["best_bid"]=None
|
||||
data["total_demand"]=None
|
||||
data["total_supply"]=None
|
||||
data["market_rate"]=None
|
||||
localStepDB[instrm]=data
|
||||
|
||||
## 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)
|
||||
#Trade
|
||||
new_trades=self.executed_trades[self.trade_curser:]
|
||||
self.trade_curser=len(self.executed_trades)-1
|
||||
for t in new_trades:
|
||||
if t.trade_side==1:
|
||||
# will get logged twice so only log one side
|
||||
#
|
||||
account_id=self.order_account_map[t.order_id]
|
||||
data={}
|
||||
data["cxid"]=name
|
||||
data["tstep"]=timepoint
|
||||
data["episode"]=int(episode)
|
||||
data["account"]=account_id
|
||||
data["business"]=self.account_business[account_id]
|
||||
data["order_id"]=t.order_id
|
||||
data["instmt"]=t.instmt
|
||||
data["price"]=int(t.trade_price)
|
||||
data["qty"]=int(t.trade_qty)
|
||||
log.EXTradeDataDetail.append(data)
|
||||
|
||||
if autolog:
|
||||
for id,item in localStepDB.items():
|
||||
@@ -305,15 +357,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)
|
||||
|
||||
|
||||
+61
-23
@@ -21,6 +21,7 @@ class Simulation():
|
||||
self.populated_cells={}
|
||||
self.businesses=[]
|
||||
self.production_util={}
|
||||
self.episode_tax=1
|
||||
self.taskpool=ThreadPoolExecutor()
|
||||
|
||||
self.setup_timing(["dss","dsa","dab","dbl","dt"])
|
||||
@@ -34,6 +35,7 @@ class Simulation():
|
||||
def set_cells(self,cells):
|
||||
for c in cells:
|
||||
self.cells[c.name]=c
|
||||
self.populated_cells[c.name]=0
|
||||
|
||||
def set_businesses(self,bus):
|
||||
self.businesses=bus
|
||||
@@ -61,36 +63,52 @@ class Simulation():
|
||||
"""
|
||||
keys=list(self.tick_funcs.keys())
|
||||
random.shuffle(keys)
|
||||
tasks=[]
|
||||
for k in keys:
|
||||
fun=self.tick_funcs[k]
|
||||
#row=[fun,self.tick_count,self.episode_count]
|
||||
#tasks.append(row)
|
||||
#self.taskpool.submit(self._execute_tick,row)
|
||||
|
||||
fun(self.tick_count,self.episode_count)
|
||||
#self.taskpool.map(self._execute_tick,tasks)
|
||||
|
||||
|
||||
|
||||
def _execute_tick(self,args):
|
||||
fun=args[0]
|
||||
fun(args[1],args[2])
|
||||
|
||||
return True
|
||||
|
||||
def tick(self,episode_length):
|
||||
"""
|
||||
Executes a tick of the simulation
|
||||
"""
|
||||
start=time.time()
|
||||
|
||||
|
||||
|
||||
|
||||
self.tick_agents_random_order()
|
||||
agentstep=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.remove_business(b.id)
|
||||
|
||||
busstep=time.time()
|
||||
self.tick_count+=1
|
||||
self.log_cxs_tick(episode_length)
|
||||
self.log_business_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["dsap"]=self.timings["dsa"]/(len(self.tick_funcs)+1)
|
||||
self.timings["dab"]+=(busstep-agentstep)/(len(self.businesses)+1)
|
||||
self.timings["dbl"]+=logstep-busstep
|
||||
self.timings["dt"]+=logstep-start
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -99,11 +117,25 @@ class Simulation():
|
||||
Resets all agents to new Episode
|
||||
"""
|
||||
self.log_cxs_episode()
|
||||
self.log_business_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()
|
||||
|
||||
toclose=[]
|
||||
for b in self.businesses:
|
||||
#tax business
|
||||
b.balance-=self.episode_tax
|
||||
b.tick_episode(self.episode_count)
|
||||
if b.balance<=0:
|
||||
toclose.append(b.id)
|
||||
for close in toclose:
|
||||
self.remove_business(close)
|
||||
self.timings["numBus"]=len(self.businesses)
|
||||
self.timings["tickfuncs"]=len(self.tick_funcs)
|
||||
self.timings["episode"]=self.episode_count
|
||||
log.PerformanceData.append(self.timings.copy())
|
||||
self.setup_timing(["dss","dsa","dsap","dab","dbl","dt"])
|
||||
self.tick_count=0
|
||||
self.episode_count+=1
|
||||
|
||||
@@ -139,10 +171,10 @@ 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)
|
||||
business=Price_Believe_Business.Price_Believe_Business(uuid.UUID(int=random.getrandbits(128)),prod,1000,cxs,self,cell_id)
|
||||
self.businesses.append(business)
|
||||
self.production_util[selected_prod]+=1
|
||||
self.populated_cells[cell_id]+=1
|
||||
return business
|
||||
|
||||
def remove_business(self,id):
|
||||
@@ -155,6 +187,7 @@ class Simulation():
|
||||
bus.close_business()
|
||||
self.businesses.remove(bus)
|
||||
self.production_util[bus.production["id"]]-=1
|
||||
self.populated_cells[bus.location]-=1
|
||||
|
||||
|
||||
def get_max_profit_prod(self):
|
||||
@@ -165,12 +198,14 @@ class Simulation():
|
||||
|
||||
for b in self.businesses:
|
||||
id=b.production["id"]
|
||||
diff=b.balance-b.balance_history[-1]
|
||||
if len(b.balance_history)<2:
|
||||
continue
|
||||
diff=b.balance-b.balance_history[-2]
|
||||
if id not in profit:
|
||||
profit[id]=0
|
||||
profit[id]+=diff
|
||||
best=None
|
||||
bestv=0
|
||||
bestv=-1000000000000
|
||||
for k,v in profit.items():
|
||||
if bestv<v:
|
||||
bestv=v
|
||||
@@ -190,16 +225,15 @@ class Simulation():
|
||||
cx=cell.exchange
|
||||
# Best prod resource availability
|
||||
# Using the exchange in that cell, lookup supply or demand of resources. Supply +1 / Demand -1
|
||||
for prod_item in prod["prod"]:
|
||||
key=list(prod_item.keys())[0]
|
||||
val=prod_item[key]
|
||||
for key,val in prod["prod"].items():
|
||||
a=cx.get_total_supply(key)
|
||||
cell_score[cell.name]+=(cx.get_total_supply(key)/val)
|
||||
cell_score[cell.name]-=(cx.get_total_demand(key)/val)
|
||||
# If demand for prod item in cell then score +1/ score -1 if supply is already there
|
||||
a=cx.get_total_demand(prod["name"])
|
||||
cell_score[cell.name]+=cx.get_total_demand(prod["name"])
|
||||
cell_score[cell.name]-=cx.get_total_supply(prod["name"])
|
||||
b=cx.get_total_supply(prod["name"])
|
||||
cell_score[cell.name]+=a
|
||||
cell_score[cell.name]-=b
|
||||
max_keys = [key for key, value in cell_score.items() if value == max(cell_score.values())]
|
||||
return max_keys
|
||||
|
||||
@@ -228,17 +262,21 @@ class Simulation():
|
||||
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]
|
||||
lcx=cell.exchange
|
||||
name="lcx_{}".format(id)
|
||||
lcx.log_episode(name,self.episode_count)
|
||||
for id,v in self.populated_cells.items():
|
||||
if v>0:
|
||||
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)
|
||||
|
||||
def log_business_episode(self):
|
||||
for bus in self.businesses:
|
||||
bus.log(self.episode_count,1,0)
|
||||
|
||||
def setup_timing(self,metrics):
|
||||
for met in metrics:
|
||||
self.timings[met]=0
|
||||
+32
-10
@@ -15,7 +15,9 @@ write_client = None
|
||||
EXInit=False
|
||||
EXBooksData=[]
|
||||
EXTradeData=[]
|
||||
EXTradeDataDetail=[]
|
||||
BUSINESSData=[]
|
||||
PerformanceData=[]
|
||||
def get_client():
|
||||
global write_client
|
||||
if write_client==None:
|
||||
@@ -23,19 +25,39 @@ 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}")
|
||||
df=pd.DataFrame(EXTradeDataDetail)
|
||||
df.to_sql("cx_trades_detailed",con=db, if_exists='replace',
|
||||
index=False,chunksize=10000)
|
||||
t3=time.time()
|
||||
print(f"cx_trades_detailed completed: {t3-t2}/{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}")
|
||||
def writePerformanceData():
|
||||
db = create_engine(posturl)
|
||||
df=pd.DataFrame(PerformanceData)
|
||||
df.to_sql('performance', con=db, if_exists='replace',
|
||||
index=False,chunksize=10000)
|
||||
print(f"performance completed: {df.size}")
|
||||
def clearlog():
|
||||
EXBooksData=[]
|
||||
EXTradeData=[]
|
||||
BUSINESSData=[]
|
||||
Binary file not shown.
@@ -6,6 +6,7 @@ from econ.commoditys import commoditys
|
||||
from econ.cells import db, cell
|
||||
import log
|
||||
from tqdm import tqdm
|
||||
import uuid
|
||||
db.load_world("db/world.json")
|
||||
|
||||
commoditys.search_yaml_files()
|
||||
@@ -25,24 +26,37 @@ 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)
|
||||
EPISODE_LEN=20
|
||||
NUM_EPISODES=20
|
||||
NUM_SEED_BUS=10
|
||||
|
||||
|
||||
|
||||
sim=Simulation()
|
||||
sim.seed(45223)
|
||||
sim.seed(43)
|
||||
sim.set_cells(cells)
|
||||
sim.reset()
|
||||
#sim.tick(1)
|
||||
for i in range(EPISODE_LEN):
|
||||
sim.tick(EPISODE_LEN)
|
||||
sim.reset()
|
||||
# create info
|
||||
while len(sim.get_underutelized_prods(2))>0:
|
||||
sim.create_bussiness(2)
|
||||
#sim.reset()
|
||||
for a in tqdm(range(50)):
|
||||
for i in range(100):
|
||||
sim.tick(100)
|
||||
for a in tqdm(range(NUM_EPISODES)):
|
||||
for i in range(EPISODE_LEN):
|
||||
sim.tick(EPISODE_LEN)
|
||||
print(f"|Stats| Num of Businesses: {len(sim.businesses)} Num Tickfunk: {len(sim.tick_funcs)} Time Agent Tick: {sim.timings['dsa']} Time Logging {sim.timings['dbl']}")
|
||||
sim.reset()
|
||||
sim.create_bussiness(1)
|
||||
for i in range(NUM_SEED_BUS):
|
||||
sim.create_bussiness(1)
|
||||
|
||||
|
||||
|
||||
log.writeEXData()
|
||||
log.writeBusinessData()
|
||||
log.writePerformanceData()
|
||||
|
||||
print("help")
|
||||
+2
-1
@@ -2,4 +2,5 @@ lightmatchingengine
|
||||
pyyaml
|
||||
influxdb-client
|
||||
pandas
|
||||
sqlalchemy
|
||||
sqlalchemy
|
||||
tensorflow
|
||||
Reference in New Issue
Block a user