Compare commits
15
Commits
966db6e5ad
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eec9c82a83 | ||
|
|
8d5ad7c62d | ||
|
|
010d1b6d3c | ||
|
|
da598c4475 | ||
|
|
7554071405 | ||
|
|
7d506625bd | ||
|
|
1ca6b3d036 | ||
|
|
9ed01abea4 | ||
|
|
4d4d609cea | ||
|
|
4bfe5245a9 | ||
|
|
509c59bb88 | ||
|
|
b7dc7b99d1 | ||
|
|
5514f3f1d9 | ||
|
|
4cec35935f | ||
|
|
f60dca6367 |
Vendored
+24
@@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Vendored
+7
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"ansible.python.interpreterPath": "c:\\Users\\handg\\miniconda3\\python.exe",
|
||||||
|
"[python]": {
|
||||||
|
"editor.defaultFormatter": "ms-python.autopep8"
|
||||||
|
},
|
||||||
|
"python.formatting.provider": "none"
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
kind: commodity
|
||||||
|
spec:
|
||||||
|
- name: "Gem"
|
||||||
|
max_world_price: 100
|
||||||
|
|
||||||
|
- name: "Food"
|
||||||
|
max_world_price: 5
|
||||||
|
|
||||||
|
|
||||||
|
- name: "Grain"
|
||||||
|
max_world_price: 5
|
||||||
|
|
||||||
|
|
||||||
|
- name: "Fruit"
|
||||||
|
max_world_price: 1
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
kind: demand
|
||||||
|
spec:
|
||||||
|
- name: basic
|
||||||
|
res:
|
||||||
|
- 'Food': 1
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
kind: production
|
||||||
|
spec:
|
||||||
|
- name: Food
|
||||||
|
amount: 100
|
||||||
|
prod:
|
||||||
|
- "Grain": 1
|
||||||
|
- "Fruit": 1
|
||||||
|
|
||||||
|
- name: Grain
|
||||||
|
amount: 1000
|
||||||
|
prod:
|
||||||
|
- Raw_Agriculture_Plot: 1
|
||||||
|
|
||||||
|
- name: Fruit
|
||||||
|
amount: 100
|
||||||
|
prod:
|
||||||
|
- Raw_Agriculture_Plot: 1
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
kind: world
|
||||||
|
spec:
|
||||||
|
- name: gems
|
||||||
|
res:
|
||||||
|
- 'Raw_Gem': 15
|
||||||
|
|
||||||
|
- name: grass
|
||||||
|
res:
|
||||||
|
- 'Raw_Agriculture_Plot': 0.3
|
||||||
|
|
||||||
|
- name: forrest
|
||||||
|
res:
|
||||||
|
- 'Raw_Agriculture_Plot': 1
|
||||||
|
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
+5
-4
@@ -4,13 +4,14 @@ spec:
|
|||||||
max_world_price: 100
|
max_world_price: 100
|
||||||
|
|
||||||
- name: "Food"
|
- name: "Food"
|
||||||
max_world_price: 5
|
max_world_price: 30
|
||||||
|
|
||||||
|
|
||||||
- name: "Grain"
|
- name: "Grain"
|
||||||
max_world_price: 1
|
max_world_price: 5
|
||||||
|
|
||||||
|
- name: "Fuel"
|
||||||
|
max_world_price: 1
|
||||||
|
|
||||||
- name: "Fruit"
|
- name: "Fruit"
|
||||||
max_world_price: 1
|
max_world_price: 5
|
||||||
|
|
||||||
@@ -3,3 +3,4 @@ spec:
|
|||||||
- name: basic
|
- name: basic
|
||||||
res:
|
res:
|
||||||
- 'Food': 1
|
- 'Food': 1
|
||||||
|
# - 'Fruit': 0.1
|
||||||
|
|||||||
+23
-10
@@ -1,17 +1,30 @@
|
|||||||
kind: production
|
kind: production
|
||||||
spec:
|
spec:
|
||||||
- name: Food
|
|
||||||
amount: 100
|
|
||||||
prod:
|
|
||||||
- "Grain": 1
|
|
||||||
- "Fruit": 1
|
|
||||||
|
|
||||||
- name: Grain
|
- name: Grain
|
||||||
amount: 1
|
amount: 20
|
||||||
prod:
|
prod:
|
||||||
- Raw_Agriculture_Plot: 1
|
Raw_Agriculture_Plot: 1
|
||||||
|
|
||||||
- name: Fruit
|
- name: Fruit
|
||||||
amount: 1
|
amount: 20
|
||||||
prod:
|
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:
|
||||||
|
# Fuel: 1
|
||||||
|
Fruit: 1
|
||||||
|
Grain: 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -6,10 +6,12 @@ spec:
|
|||||||
|
|
||||||
- name: grass
|
- name: grass
|
||||||
res:
|
res:
|
||||||
- 'Raw_Agriculture_Plot': 0.3
|
- 'Raw_Agriculture_Plot': 0.5
|
||||||
|
- 'Raw_Forrest': 0.5
|
||||||
|
|
||||||
- name: forrest
|
- name: forrest
|
||||||
res:
|
res:
|
||||||
- 'Raw_Agriculture_Plot': 1
|
- 'Raw_Agriculture_Plot': 0.5
|
||||||
|
- 'Raw_Forrest': 0.5
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
database:
|
||||||
|
image: 'postgres:14'
|
||||||
|
ports:
|
||||||
|
- 15432:5432
|
||||||
|
environment:
|
||||||
|
POSTGRES_PASSWORD: econ
|
||||||
|
POSTGRES_USER: admin
|
||||||
|
POSTGRES_DB: econ
|
||||||
|
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.
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)
|
super().__init__(sim)
|
||||||
self.business=business
|
self.business=business
|
||||||
self.prod=business.production
|
self.prod=business.production
|
||||||
@@ -13,32 +13,56 @@ class AutoProductionAgent(BaseAgent):
|
|||||||
self.employment_rate=employment_rate
|
self.employment_rate=employment_rate
|
||||||
self.employment_index=worker
|
self.employment_index=worker
|
||||||
self.target=0
|
self.target=0
|
||||||
|
self.prod_counter=0
|
||||||
|
|
||||||
def set_worker(self,workers):
|
def set_worker(self,workers):
|
||||||
if workers>1:
|
if workers>1:
|
||||||
self.worker=workers
|
self.worker=workers
|
||||||
|
elif workers==-1:
|
||||||
|
self.worker=-1
|
||||||
else:
|
else:
|
||||||
self.worker=1
|
self.worker=1
|
||||||
def set_target(self,qty):
|
def set_target(self,qty):
|
||||||
self.target=qty
|
self.target=qty
|
||||||
def can_produce(self):
|
def can_produce(self):
|
||||||
# If can produce item
|
# how much can we produce
|
||||||
for com in self.prod["prod"]:
|
minprod=[]
|
||||||
for k,v in com.items():
|
|
||||||
if v > self.business.inventory[k]:
|
for k,v in self.prod["prod"].items():
|
||||||
return False
|
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
|
# check if we should produce
|
||||||
if self.business.resource_in_possesion()>self.target:
|
if self.business.resource_in_possesion()>=self.target:
|
||||||
return False
|
return 0
|
||||||
return True
|
|
||||||
|
return self.can_produce()
|
||||||
|
|
||||||
def tick(self,step,epi):
|
def tick(self,step,epi):
|
||||||
for i in range(self.worker):
|
can=self.should_produce()
|
||||||
if not self.can_produce():
|
if can==0:
|
||||||
continue
|
return
|
||||||
|
|
||||||
|
run=True
|
||||||
|
i=self.worker
|
||||||
|
while run:
|
||||||
|
if self.should_produce()==0:
|
||||||
|
return
|
||||||
|
if i==0:
|
||||||
|
return
|
||||||
# remove cost from inventory
|
# remove cost from inventory
|
||||||
for com in self.prod["prod"]:
|
|
||||||
for k,cost in com.items():
|
for k,cost in self.prod["prod"].items():
|
||||||
self.business.inventory[k]-=cost
|
self.business.inventory[k]-=cost
|
||||||
# add commodity
|
# add commodity
|
||||||
self.business.inventory[self.prod['name']]+=self.prod["amount"]
|
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
|
||||||
@@ -24,3 +24,15 @@ class BaseAgent(ABC):
|
|||||||
"""
|
"""
|
||||||
assert "No reset method has been provided"
|
assert "No reset method has been provided"
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def unregister(self):
|
||||||
|
"""
|
||||||
|
Disables agent by removing it from the simulation
|
||||||
|
"""
|
||||||
|
self.simulation.unregister_agent(self.id)
|
||||||
|
def register(self):
|
||||||
|
"""
|
||||||
|
Enables agent by adding it to the simulation:
|
||||||
|
"""
|
||||||
|
self.simulation.register_agent(self.id,self.tick,self.reset)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
Agent for aquire of resources.
|
Agent for aquire of resources.
|
||||||
business: Business to aquire resources for.
|
business: Business to aquire resources for.
|
||||||
resource: Resource to aquire.
|
resource: Resource to aquire.
|
||||||
exchanges: list of exchanges to aquire resources from
|
exchanges: list of exchanges to acquire resources from
|
||||||
"""
|
"""
|
||||||
self.business=business
|
self.business=business
|
||||||
self.resource=resource
|
self.resource=resource
|
||||||
@@ -17,6 +17,11 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
self.orders={i: {} for i in range(len(self.exchanges))}
|
self.orders={i: {} for i in range(len(self.exchanges))}
|
||||||
self.target=0
|
self.target=0
|
||||||
self.trades=[]
|
self.trades=[]
|
||||||
|
self.tqty=0
|
||||||
|
self.qty_offset=0
|
||||||
|
|
||||||
|
self.texpense=0
|
||||||
|
self.expense_offset=0
|
||||||
self.max_price=-1
|
self.max_price=-1
|
||||||
super().__init__(simulation)
|
super().__init__(simulation)
|
||||||
|
|
||||||
@@ -34,7 +39,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
If err < 0 then agent needs to aquire resources
|
If err < 0 then agent needs to aquire resources
|
||||||
"""
|
"""
|
||||||
err=self.business.inventory[self.resource]-self.target
|
err=self.business.inventory[self.resource]-self.target
|
||||||
return err
|
return int(err)
|
||||||
|
|
||||||
def set_price_max(self,price: int):
|
def set_price_max(self,price: int):
|
||||||
"""
|
"""
|
||||||
@@ -58,9 +63,10 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
|
|
||||||
self.business.balance-=total_price
|
self.business.balance-=total_price
|
||||||
cx.add_to_account(self.id,"balance",total_price) # prepaid charge account for cx
|
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)
|
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.BUY)
|
||||||
if order==None: # Order failed
|
if order==None: # Order failed
|
||||||
return False
|
return None
|
||||||
self.orders[cx_id][order.order_id]=order
|
self.orders[cx_id][order.order_id]=order
|
||||||
self.update_trades()
|
self.update_trades()
|
||||||
return order
|
return order
|
||||||
@@ -74,6 +80,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
cx.remove_from_account(self.id,"balance",amount)
|
cx.remove_from_account(self.id,"balance",amount)
|
||||||
self.business.balance+=amount
|
self.business.balance+=amount
|
||||||
|
|
||||||
|
|
||||||
def collect_resource_from_cxs(self,resource):
|
def collect_resource_from_cxs(self,resource):
|
||||||
"""
|
"""
|
||||||
Collects resouces from account inventory in cxs.
|
Collects resouces from account inventory in cxs.
|
||||||
@@ -88,13 +95,39 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
|||||||
Returns a list of all trades performed by this agent
|
Returns a list of all trades performed by this agent
|
||||||
"""
|
"""
|
||||||
trades=[]
|
trades=[]
|
||||||
|
self.tqty=0
|
||||||
|
self.texpense=0
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx=self.exchanges[cx_id]
|
||||||
orders=self.orders[cx_id]
|
orders=self.orders[cx_id]
|
||||||
if len(orders)>0:
|
if len(orders)>0:
|
||||||
for k,o in orders.items():
|
for k,o in orders.items():
|
||||||
if k in cx.order_trades_map:
|
if k in cx.order_trades_map:
|
||||||
trades.append(cx.order_trades_map[k])
|
trades.extend(cx.order_trades_map[k])
|
||||||
|
|
||||||
self.trades=trades
|
self.trades=trades
|
||||||
|
for t in trades:
|
||||||
|
self.tqty+=t.trade_qty
|
||||||
|
self.texpense+=round(t.trade_qty*t.trade_price,2)
|
||||||
return trades
|
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 ..exchange import Exchange
|
||||||
from lightmatchingengine.lightmatchingengine import Order,Side
|
from lightmatchingengine.lightmatchingengine import Order,Side
|
||||||
from abc import ABC
|
from abc import ABC
|
||||||
|
from decimal import *
|
||||||
class Base_Distribution_Agent(BaseAgent,ABC):
|
class Base_Distribution_Agent(BaseAgent,ABC):
|
||||||
|
|
||||||
def __init__(self,simulation,business,resource,exchanges: list) -> None:
|
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.orders={i: {} for i in range(len(self.exchanges))}
|
||||||
self.target=0
|
self.target=0
|
||||||
self.min_price=-1
|
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)
|
super().__init__(simulation)
|
||||||
|
|
||||||
def set_target(self,target: int):
|
def set_target(self,target: int):
|
||||||
"""
|
"""
|
||||||
Sets the amount of resources the agent should keep in business inventory
|
Sets the amount of resources the agent should keep in business inventory
|
||||||
"""
|
"""
|
||||||
|
if target<0:
|
||||||
|
target=0
|
||||||
self.target=target
|
self.target=target
|
||||||
|
|
||||||
|
|
||||||
@@ -40,7 +49,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
|||||||
If err < 0 then agent needs to distribute resources
|
If err < 0 then agent needs to distribute resources
|
||||||
"""
|
"""
|
||||||
err=self.business.inventory[self.resource]-self.target
|
err=self.business.inventory[self.resource]-self.target
|
||||||
return err
|
return int(err)
|
||||||
|
|
||||||
|
|
||||||
def distribute_resource(self,price_per,amount,cx_id):
|
def distribute_resource(self,price_per,amount,cx_id):
|
||||||
@@ -51,12 +60,16 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
|||||||
# Get exchange
|
# Get exchange
|
||||||
cx=self.exchanges[cx_id]
|
cx=self.exchanges[cx_id]
|
||||||
|
|
||||||
|
#if amount>=self.max_batch_qty:
|
||||||
|
# amount=self.max_batch_qty
|
||||||
|
|
||||||
if not self.business.inventory[self.resource]>=amount:
|
if not self.business.inventory[self.resource]>=amount:
|
||||||
return False # we dont have enough balance
|
return False # we dont have enough balance
|
||||||
|
|
||||||
self.business.inventory[self.resource]-=amount
|
self.business.inventory[self.resource]-=amount
|
||||||
|
|
||||||
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
|
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)
|
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)
|
||||||
if order==None: # Order failed
|
if order==None: # Order failed
|
||||||
return False
|
return False
|
||||||
@@ -72,6 +85,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
|||||||
cx.remove_from_account(self.id,"balance",amount)
|
cx.remove_from_account(self.id,"balance",amount)
|
||||||
self.business.balance+=amount
|
self.business.balance+=amount
|
||||||
|
|
||||||
|
|
||||||
def collect_resource_from_cxs(self,resource):
|
def collect_resource_from_cxs(self,resource):
|
||||||
"""
|
"""
|
||||||
Collects resouces from account inventory in cxs.
|
Collects resouces from account inventory in cxs.
|
||||||
@@ -86,12 +100,42 @@ class Base_Distribution_Agent(BaseAgent,ABC):
|
|||||||
Returns a list of all trades performed by this agent
|
Returns a list of all trades performed by this agent
|
||||||
"""
|
"""
|
||||||
trades=[]
|
trades=[]
|
||||||
|
self.tqty=0
|
||||||
|
self.tincome=0
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx=self.exchanges[cx_id]
|
||||||
orders=self.orders[cx_id]
|
orders=self.orders[cx_id]
|
||||||
if len(orders)>0:
|
if len(orders)>0:
|
||||||
for o in orders:
|
for k,o in orders.items():
|
||||||
trades.append(cx.order_trades_map[o.order_id])
|
if k in cx.order_trades_map:
|
||||||
|
trades.extend(cx.order_trades_map[k])
|
||||||
self.trades=trades
|
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
|
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,104 +1,147 @@
|
|||||||
from .base_aquire_agent import Base_Aquire_Agent
|
from .base_aquire_agent import Base_Aquire_Agent
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
||||||
"""
|
"""
|
||||||
Aquire agent with internal price believe system.
|
Aquire agent with internal price believe system.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
||||||
super().__init__(simulation, business, resource, exchanges)
|
super().__init__(simulation, business, resource, exchanges)
|
||||||
self.lr=lr
|
self.lr = lr
|
||||||
self.max_price_adj_rate=max_price_adj_rate
|
self.max_price_adj_rate = max_price_adj_rate
|
||||||
self.price_believe={i: 0 for i in range(len(self.exchanges))}
|
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_orders = {i: [] for i in range(len(self.exchanges))}
|
||||||
self.open_qty=0
|
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:
|
||||||
|
# aquire based on current price believe
|
||||||
order_error=self.open_qty+self.target_error()
|
cx_id = self.select_best_cx()
|
||||||
if order_error<0:
|
order = self.order_resource(
|
||||||
# aquire based on current price belive
|
self.price_believe[cx_id], order_error*-1, cx_id)
|
||||||
cx_id=self.select_best_cx()
|
if not order == None:
|
||||||
order=self.order_resource(self.price_believe[cx_id],order_error*-1,cx_id)
|
self.register_order(cx_id, order)
|
||||||
if not order==None:
|
|
||||||
self.register_order(cx_id,order)
|
|
||||||
else:
|
else:
|
||||||
# order failed due to missing balance.. we need to adjust our price believe
|
# order failed due to missing balance.. we need to adjust our price believe
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
self.update_believe(cx_id,-1)
|
self.update_believe(cx_id, -1)
|
||||||
self.tick_open_orders()
|
self.tick_open_orders()
|
||||||
|
|
||||||
def select_best_cx(self):
|
def select_best_cx(self):
|
||||||
best_id=-1
|
best_id = -1
|
||||||
best=0
|
best = 999999999
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
potential=cx.get_total_supply(self.resource)*self.price_believe[cx_id]
|
available = (cx.get_total_supply(self.resource) > 0)
|
||||||
if potential<best:
|
if available:
|
||||||
best=potential
|
potential = 1*self.price_believe[cx_id]
|
||||||
best_id=cx_id
|
else:
|
||||||
if best_id==-1:
|
continue
|
||||||
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
|
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({
|
self.open_orders[cx_id].append({
|
||||||
'id': order.order_id,
|
'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:
|
def tick_open_orders(self) -> int:
|
||||||
"""
|
"""
|
||||||
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
||||||
Returns ids of orders that timeed out.
|
Returns ids of orders that timeed out.
|
||||||
"""
|
"""
|
||||||
self.open_qty=0
|
self.open_qty = 0
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
cx_orders=self.open_orders[cx_id]
|
cx_orders = self.open_orders[cx_id]
|
||||||
for i in cx_orders:
|
for i in cx_orders:
|
||||||
# Check for each order if it is fullfiled or if it is timed
|
# Check for each order if it is fullfiled or if it is timed
|
||||||
o=self.orders[cx_id][i["id"]]
|
o = self.orders[cx_id][i["id"]]
|
||||||
if o.leaves_qty==0:
|
leaves = o.leaves_qty
|
||||||
#order is done
|
if o.leaves_qty == 0:
|
||||||
|
# order is done
|
||||||
self.open_orders[cx_id].remove(i) # remove order from open
|
self.open_orders[cx_id].remove(i) # remove order from open
|
||||||
self.update_believe(cx_id,-1) # update price believe
|
self.update_believe(cx_id, -1) # update price believe
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
if i["lifetime"]>0:
|
if not (i["leaves"] == leaves):
|
||||||
self.open_qty+=o.leaves_qty
|
# update in order
|
||||||
i["lifetime"]-=1 # subtract lifetime
|
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:
|
else:
|
||||||
# timeout
|
# 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"])
|
cx.cancel_order(i["id"])
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
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)
|
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.
|
Updates the believe based on the modifier.
|
||||||
If positive will add lr to believe
|
If positive will add lr to believe
|
||||||
If negative will sub lr to believe
|
If negative will sub lr to believe
|
||||||
"""
|
"""
|
||||||
self.price_believe[cx_id]+=modifier*self.lr
|
self.price_believe[cx_id] += modifier*self.lr
|
||||||
if self.price_believe[cx_id]<0:
|
if self.price_believe[cx_id] < 0:
|
||||||
self.price_believe[cx_id]=0
|
self.price_believe[cx_id] = 0
|
||||||
|
|
||||||
def reset(self,episode):
|
def reset(self, episode):
|
||||||
# Clean shop for today
|
# Clean shop for today
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
cx_orders=self.open_orders[cx_id]
|
cx_orders = self.open_orders[cx_id]
|
||||||
for i in cx_orders:
|
for i in cx_orders:
|
||||||
cx.cancel_order(i["id"])
|
cx.cancel_order(i["id"])
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
# book keeping
|
# book keeping
|
||||||
self.update_trades()
|
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)
|
return super().reset(episode)
|
||||||
@@ -1,101 +1,162 @@
|
|||||||
from .base_distribution_agent import Base_Distribution_Agent
|
from .base_distribution_agent import Base_Distribution_Agent
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||||
"""
|
"""
|
||||||
Aquire agent with internal price believe system.
|
Aquire agent with internal price believe system.
|
||||||
"""
|
"""
|
||||||
def __init__(self, simulation, business, resource, exchanges: list, lr, max_price_adj_rate) -> None:
|
|
||||||
|
def __init__(self, simulation, business, resource, exchanges: list, lr,at_sales, max_price_adj_rate) -> None:
|
||||||
super().__init__(simulation, business, resource, exchanges)
|
super().__init__(simulation, business, resource, exchanges)
|
||||||
self.lr=lr
|
self.lr = lr
|
||||||
self.max_price_adj_rate=max_price_adj_rate
|
|
||||||
self.price_believe={i: 0 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):
|
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
|
||||||
|
|
||||||
order_error=self.target_error()
|
def tick(self, step, episode):
|
||||||
if order_error>0:
|
|
||||||
|
order_error = self.target_error()
|
||||||
|
if (order_error > 0) & (self.open_qty==0):
|
||||||
# aquire based on current price belive
|
# aquire based on current price belive
|
||||||
cx_id=self.select_best_cx()
|
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)
|
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()
|
self.tick_open_orders()
|
||||||
|
|
||||||
def select_best_cx(self):
|
def select_best_cx(self):
|
||||||
best_id=-1
|
best_id = -1
|
||||||
best=0
|
best = -1
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
potential=cx.get_total_supply(self.resource)*self.price_believe[cx_id]
|
available = (cx.get_total_demand(self.resource) > 0)
|
||||||
if potential>best:
|
if available:
|
||||||
best=potential
|
potential = 1*self.price_believe[cx_id]
|
||||||
best_id=cx_id
|
else:
|
||||||
if best_id==-1:
|
continue
|
||||||
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
|
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({
|
self.open_orders[cx_id].append({
|
||||||
'id': order.order_id,
|
'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:
|
def tick_open_orders(self) -> int:
|
||||||
"""
|
"""
|
||||||
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
Removes 1 from pending orders timeout timer and cancels timeed out orders.
|
||||||
Returns ids of orders that timeed out.
|
Returns ids of orders that timeed out.
|
||||||
"""
|
"""
|
||||||
self.open_qty=0
|
self.open_qty = 0
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
cx_orders=self.open_orders[cx_id]
|
cx_orders = self.open_orders[cx_id]
|
||||||
for i in cx_orders:
|
for i in cx_orders:
|
||||||
# Check for each order if it is fullfiled or if it is timed
|
# Check for each order if it is fullfiled or if it is timed
|
||||||
o=self.orders[cx_id][i["id"]]
|
o = self.orders[cx_id][i["id"]]
|
||||||
if o.leaves_qty==0:
|
leaves = o.leaves_qty
|
||||||
#order is done
|
if o.leaves_qty == 0:
|
||||||
|
# order is done
|
||||||
self.open_orders[cx_id].remove(i) # remove order from open
|
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_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
if i["lifetime"]>0:
|
|
||||||
self.open_qty+=o.leaves_qty
|
if not (i["leaves"] == leaves):
|
||||||
i["lifetime"]-=1 # subtract lifetime
|
# 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:
|
else:
|
||||||
# timeout
|
# timeout
|
||||||
|
self.update_trades()
|
||||||
|
succsess=self.calc_order_success(cx,o)
|
||||||
|
|
||||||
cx.cancel_order(i["id"])
|
cx.cancel_order(i["id"])
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
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)
|
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, used_price, modifier,qty):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def update_believe(self,cx_id,modifier):
|
|
||||||
"""
|
"""
|
||||||
Updates the believe based on the modifier.
|
Updates the believe based on the modifier.
|
||||||
If positive will add lr to believe
|
If positive will add lr to believe
|
||||||
If negative will sub lr to believe
|
If negative will sub lr to believe
|
||||||
"""
|
"""
|
||||||
self.price_believe[cx_id]+=modifier*self.lr
|
clipMod=max(-qty,min(modifier,qty))
|
||||||
if self.price_believe[cx_id]<0:
|
moment=clipMod/qty
|
||||||
self.price_believe[cx_id]=0
|
#clip mod so that price believe more smooth
|
||||||
|
|
||||||
def reset(self,episode):
|
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
|
# Clean shop for today
|
||||||
|
self.update_trades()
|
||||||
for cx_id in range(len(self.exchanges)):
|
for cx_id in range(len(self.exchanges)):
|
||||||
cx=self.exchanges[cx_id]
|
cx = self.exchanges[cx_id]
|
||||||
cx_orders=self.open_orders[cx_id]
|
cx_orders = self.open_orders[cx_id]
|
||||||
for i in cx_orders:
|
for i in cx_orders:
|
||||||
cx.cancel_order(i["id"])
|
cx.cancel_order(i["id"])
|
||||||
self.collect_balance_from_cxs()
|
self.collect_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
# book keeping
|
# 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)
|
return super().reset(episode)
|
||||||
@@ -2,30 +2,148 @@ from .base_business import Business
|
|||||||
from ..agents.price_believe_aquire import Price_Believe_Aquire_Agent
|
from ..agents.price_believe_aquire import Price_Believe_Aquire_Agent
|
||||||
from ..agents.price_believe_distribute import Price_Believe_Distribiute_Agent
|
from ..agents.price_believe_distribute import Price_Believe_Distribiute_Agent
|
||||||
from ..agents.autoproduction import AutoProductionAgent
|
from ..agents.autoproduction import AutoProductionAgent
|
||||||
class Price_Believe_Business(Business):
|
import log
|
||||||
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)
|
class Price_Believe_Business(Business):
|
||||||
self.aquire={}
|
def __init__(self, id, production, balance, exchange, simulation, location) -> None:
|
||||||
for l in production["prod"]:
|
super().__init__(id, production, balance, location)
|
||||||
for k,v in l.items():
|
|
||||||
a=Price_Believe_Aquire_Agent(simulation,self,k,exchange,1,10)
|
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_target(v*2)
|
||||||
a.set_price_max(10)
|
a.set_price_max(10)
|
||||||
self.aquire[k]=a
|
self.aquire[k] = a
|
||||||
self.distribute.set_price_min(10)
|
self.distribute.set_price_min(0)
|
||||||
|
self.distribute.max_batch_qty=production["amount"]
|
||||||
self.distribute.set_target(0)
|
self.distribute.set_target(0)
|
||||||
|
|
||||||
def tick_business_decisions(self,step):
|
def tick_business_decisions(self, step):
|
||||||
for comp in self.production["prod"]:
|
amount= self.production["amount"]
|
||||||
for k,v in comp.items():
|
if step == 0:
|
||||||
modifier=self.craft.worker+1
|
|
||||||
self.aquire[k].set_target(v*modifier)
|
max_amount = self.production["amount"]*5
|
||||||
max_amount=self.production["amount"]*5
|
|
||||||
self.craft.set_target(max_amount)
|
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):
|
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()
|
||||||
|
|
||||||
|
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,6 +1,7 @@
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
|
import log
|
||||||
class Business(ABC):
|
class Business(ABC):
|
||||||
def __init__(self,id,production,balance) -> None:
|
def __init__(self,id,production,balance,location) -> None:
|
||||||
"""production (dict): {
|
"""production (dict): {
|
||||||
name: 'Gem',
|
name: 'Gem',
|
||||||
amount: 4,
|
amount: 4,
|
||||||
@@ -12,13 +13,16 @@ class Business(ABC):
|
|||||||
balance (int): Starting Balance
|
balance (int): Starting Balance
|
||||||
"""
|
"""
|
||||||
self.id=id
|
self.id=id
|
||||||
|
self.location=location
|
||||||
self.production=production
|
self.production=production
|
||||||
self.balance_history=[balance]
|
self.balance_history=[balance]
|
||||||
self.balance=balance
|
self.balance=balance
|
||||||
|
self.expense=0
|
||||||
|
self.income=0
|
||||||
#Setup Inventory
|
#Setup Inventory
|
||||||
self.inventory={production["name"]: 0}
|
self.inventory={production["name"]: 0}
|
||||||
for l in production["prod"]:
|
|
||||||
for k,v in l.items():
|
for k,v in production["prod"].items():
|
||||||
self.inventory[k]=0
|
self.inventory[k]=0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -33,8 +37,10 @@ class Business(ABC):
|
|||||||
"""
|
"""
|
||||||
Call for resetting for the next episode
|
Call for resetting for the next episode
|
||||||
"""
|
"""
|
||||||
self.balance_history.append(self.balance)
|
|
||||||
self.tick_business_episode(episode_count)
|
self.tick_business_episode(episode_count)
|
||||||
|
self.balance_history.append(self.balance)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def tick_business_episode(self,episode_count):
|
def tick_business_episode(self,episode_count):
|
||||||
@@ -43,3 +49,6 @@ class Business(ABC):
|
|||||||
"""
|
"""
|
||||||
assert "no business episode tick method has been created"
|
assert "no business episode tick method has been created"
|
||||||
|
|
||||||
|
def close_business(self):
|
||||||
|
assert "close_business has not been provided"
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
+3
-2
@@ -24,7 +24,7 @@ class Cell:
|
|||||||
exchange: None
|
exchange: None
|
||||||
|
|
||||||
def __init__(self,name,x,y,area,pop,demand,world) -> None:
|
def __init__(self,name,x,y,area,pop,demand,world) -> None:
|
||||||
pop=pop*1000
|
pop=pop*10
|
||||||
self.name=name
|
self.name=name
|
||||||
self.loc_x=x
|
self.loc_x=x
|
||||||
self.loc_y=y
|
self.loc_y=y
|
||||||
@@ -63,13 +63,14 @@ class Cell:
|
|||||||
qty=value/episode_length
|
qty=value/episode_length
|
||||||
comm=cm.commoditys[demand_key]
|
comm=cm.commoditys[demand_key]
|
||||||
max_price=comm["max_world_price"]*qty
|
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.add_to_account(self.name,"balance",max_price)
|
||||||
self.exchange.submit_order(self.name,demand_key,qty,comm["max_world_price"],Side.BUY)
|
self.exchange.submit_order(self.name,demand_key,qty,comm["max_world_price"],Side.BUY)
|
||||||
def setup_supply_for_episode(self):
|
def setup_supply_for_episode(self):
|
||||||
self.exchange.reset()
|
self.exchange.reset()
|
||||||
for supply_key,value in self.world.items():
|
for supply_key,value in self.world.items():
|
||||||
self.exchange.add_to_account(self.name,supply_key,value)
|
self.exchange.add_to_account(self.name,supply_key,value)
|
||||||
self.exchange.submit_order(self.name,supply_key,value,0,Side.SELL)
|
self.exchange.submit_order(self.name,supply_key,value,1,Side.SELL)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -40,10 +40,10 @@ def search_yaml_files(directory = "db"):
|
|||||||
productions[comm['name']]=[]
|
productions[comm['name']]=[]
|
||||||
productions[comm['name']].append(comm)
|
productions[comm['name']].append(comm)
|
||||||
productions_id_map[comm["id"]]=comm
|
productions_id_map[comm["id"]]=comm
|
||||||
for comp in comm["prod"]:
|
k=comm["prod"].keys()
|
||||||
k=list(comp.keys())[0]
|
for component in k:
|
||||||
if k not in productions_uses:
|
if component not in productions_uses:
|
||||||
productions_uses[k]=[]
|
productions_uses[component]=[]
|
||||||
productions_uses[k].append(comm)
|
productions_uses[component].append(comm)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+147
-14
@@ -1,5 +1,18 @@
|
|||||||
_bal="balance"
|
_bal="balance"
|
||||||
from lightmatchingengine.lightmatchingengine import LightMatchingEngine,Side,Trade,Order,OrderBook
|
from lightmatchingengine.lightmatchingengine import LightMatchingEngine,Side,Trade,Order,OrderBook
|
||||||
|
import log
|
||||||
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||||
|
from influxdb_client import InfluxDBClient, Point
|
||||||
|
import time
|
||||||
|
import pandas as pd
|
||||||
|
import uuid
|
||||||
|
import threading
|
||||||
|
|
||||||
|
software_start=int(time.time())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Exchange():
|
class Exchange():
|
||||||
"""
|
"""
|
||||||
Basic Commodity exchange.
|
Basic Commodity exchange.
|
||||||
@@ -17,20 +30,30 @@ class Exchange():
|
|||||||
self.order_account_map={}
|
self.order_account_map={}
|
||||||
self.orders={}
|
self.orders={}
|
||||||
self.executed_trades=[]
|
self.executed_trades=[]
|
||||||
|
self.trade_curser=0
|
||||||
|
self.account_business={}
|
||||||
|
|
||||||
self.order_trades_map={}
|
self.order_trades_map={}
|
||||||
self.market_rate={}
|
self.market_rate={}
|
||||||
self.best_ask={}
|
self.best_ask={}
|
||||||
self.best_bid={}
|
self.best_bid={}
|
||||||
self.total_demand={}
|
self.total_demand={}
|
||||||
self.demand={}
|
#self.demand={}
|
||||||
self.total_supply={}
|
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):
|
def add_to_account(self,account_id,resource,amount):
|
||||||
"""
|
"""
|
||||||
Adds resources to account escrow
|
Adds resources to account escrow
|
||||||
"""
|
"""
|
||||||
#check if account exists
|
#check if account exists
|
||||||
|
self.lock.acquire()
|
||||||
if account_id not in self.account:
|
if account_id not in self.account:
|
||||||
self.account[account_id]={_bal: 0}
|
self.account[account_id]={_bal: 0}
|
||||||
#check if ressource exists
|
#check if ressource exists
|
||||||
@@ -38,12 +61,14 @@ class Exchange():
|
|||||||
self.account[account_id][resource]=0
|
self.account[account_id][resource]=0
|
||||||
|
|
||||||
self.account[account_id][resource]+=amount
|
self.account[account_id][resource]+=amount
|
||||||
|
self.lock.release()
|
||||||
|
|
||||||
def remove_from_account(self,account_id,resource,amount) -> int:
|
def remove_from_account(self,account_id,resource,amount) -> int:
|
||||||
"""
|
"""
|
||||||
Remove resources from account. Returns amount retrieved
|
Remove resources from account. Returns amount retrieved
|
||||||
"""
|
"""
|
||||||
#check if account exists
|
#check if account exists
|
||||||
|
self.lock.acquire()
|
||||||
if account_id not in self.account:
|
if account_id not in self.account:
|
||||||
self.account[account_id]={_bal: 0}
|
self.account[account_id]={_bal: 0}
|
||||||
#check if ressource exists
|
#check if ressource exists
|
||||||
@@ -56,6 +81,7 @@ class Exchange():
|
|||||||
self.account[account_id][resource]=0
|
self.account[account_id][resource]=0
|
||||||
else:
|
else:
|
||||||
self.account[account_id][resource]-=amount
|
self.account[account_id][resource]-=amount
|
||||||
|
self.lock.release()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _move_to_escrow(self,account_id,resource,amount):
|
def _move_to_escrow(self,account_id,resource,amount):
|
||||||
@@ -110,7 +136,13 @@ class Exchange():
|
|||||||
Returns an order if the order is active. Returns None if submit has failed.
|
Returns an order if the order is active. Returns None if submit has failed.
|
||||||
"""
|
"""
|
||||||
# calculate price for complete order fullfilment
|
# calculate price for complete order fullfilment
|
||||||
full_price=price*amount
|
prev=amount
|
||||||
|
amount=int(amount)
|
||||||
|
price=round(price)
|
||||||
|
if amount<1:
|
||||||
|
# invalid order
|
||||||
|
return None
|
||||||
|
full_price=round(price*amount)
|
||||||
|
|
||||||
|
|
||||||
# Move resources into escrow
|
# Move resources into escrow
|
||||||
@@ -123,13 +155,14 @@ class Exchange():
|
|||||||
# no sufficient resources
|
# no sufficient resources
|
||||||
return None
|
return None
|
||||||
# create order and execude any trades
|
# create order and execude any trades
|
||||||
|
self.lock.acquire()
|
||||||
order,trades=self.lme.add_order(resource,price,amount,side)
|
order,trades=self.lme.add_order(resource,price,amount,side)
|
||||||
|
|
||||||
self.orders[order.order_id]=order
|
self.orders[order.order_id]=order
|
||||||
self.order_account_map[order.order_id]=account_id
|
self.order_account_map[order.order_id]=account_id
|
||||||
self._execute_trades(trades)
|
self._execute_trades(trades)
|
||||||
self.calculate_resource_metrics(resource)
|
self.calculate_resource_metrics(resource)
|
||||||
|
self.lock.release()
|
||||||
return order
|
return order
|
||||||
|
|
||||||
def cancel_order(self,order_id):
|
def cancel_order(self,order_id):
|
||||||
@@ -142,6 +175,7 @@ class Exchange():
|
|||||||
order=self.orders[order_id]
|
order=self.orders[order_id]
|
||||||
if self._is_order_complete(order):
|
if self._is_order_complete(order):
|
||||||
return False
|
return False
|
||||||
|
self.lock.acquire()
|
||||||
order=self.lme.cancel_order(order_id,order.instmt)
|
order=self.lme.cancel_order(order_id,order.instmt)
|
||||||
|
|
||||||
if order_id in self.order_trades_map: # has order any kind of trades
|
if order_id in self.order_trades_map: # has order any kind of trades
|
||||||
@@ -161,6 +195,7 @@ class Exchange():
|
|||||||
qty_traded+=trade.trade_qty
|
qty_traded+=trade.trade_qty
|
||||||
qty_diff=qty_submitted-qty_traded
|
qty_diff=qty_submitted-qty_traded
|
||||||
self._move_to_account(self.order_account_map[order_id],order.instmt,qty_diff)
|
self._move_to_account(self.order_account_map[order_id],order.instmt,qty_diff)
|
||||||
|
self.lock.release()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -212,26 +247,35 @@ class Exchange():
|
|||||||
|
|
||||||
def calculate_resource_metrics(self, resource):
|
def calculate_resource_metrics(self, resource):
|
||||||
order_book = self.lme.order_books.setdefault(resource, OrderBook())
|
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_bid = None
|
||||||
best_ask = max(order_book.asks.keys()) if len(order_book.asks) > 0 else None
|
best_ask = None
|
||||||
self.total_demand[resource]=0
|
self.total_demand[resource]=0
|
||||||
self.demand[resource]={}
|
#self.demand[resource]={}
|
||||||
for k,v in order_book.bids.items():
|
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:
|
for o in v:
|
||||||
self.demand[resource][k]+=o.qty
|
# self.demand[resource][k]+=o.leaves_qty
|
||||||
self.total_demand[resource]+=o.qty
|
self.total_demand[resource]+=o.leaves_qty
|
||||||
self.supply[resource]={}
|
#self.supply[resource]={}
|
||||||
self.total_supply[resource]=0
|
self.total_supply[resource]=0
|
||||||
for k,v in order_book.asks.items():
|
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:
|
for o in v:
|
||||||
self.supply[resource][k]+=o.qty
|
# self.supply[resource][k]+=o.leaves_qty
|
||||||
self.total_supply[resource]+=o.qty
|
self.total_supply[resource]+=o.leaves_qty
|
||||||
|
|
||||||
|
|
||||||
self.best_ask[resource]=best_ask
|
self.best_ask[resource]=best_ask
|
||||||
self.best_bid[resource]=best_bid
|
self.best_bid[resource]=best_bid
|
||||||
|
self.traded_commoditys[resource]=True
|
||||||
|
|
||||||
def get_account_resource_amount(self,account_id,resource):
|
def get_account_resource_amount(self,account_id,resource):
|
||||||
"""
|
"""
|
||||||
@@ -250,3 +294,92 @@ class Exchange():
|
|||||||
if resource in self.total_supply:
|
if resource in self.total_supply:
|
||||||
return self.total_supply[resource]
|
return self.total_supply[resource]
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def log_step(self,name,episode,episode_length,step,autolog=True):
|
||||||
|
timepoint=episode*episode_length+step
|
||||||
|
localStepDB={}
|
||||||
|
|
||||||
|
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"]=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"]=int(value)
|
||||||
|
for item , value in self.best_bid.items():
|
||||||
|
if value!=None:
|
||||||
|
localStepDB[item]["best_bid"]=int(value)
|
||||||
|
#demand supply
|
||||||
|
for item , value in self.total_demand.items():
|
||||||
|
if value!=None:
|
||||||
|
localStepDB[item]["total_demand"]=int(value)
|
||||||
|
for item , value in self.total_supply.items():
|
||||||
|
if value!=None:
|
||||||
|
localStepDB[item]["total_supply"]=int(value)
|
||||||
|
#last market rate
|
||||||
|
for item , value in self.market_rate.items():
|
||||||
|
if value!=None:
|
||||||
|
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():
|
||||||
|
log.EXBooksData.append(item)
|
||||||
|
return localStepDB
|
||||||
|
|
||||||
|
def log_episode(self,name,episode):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|||||||
+150
-17
@@ -3,18 +3,28 @@ from .commoditys import commoditys as cm
|
|||||||
from .exchange import Exchange
|
from .exchange import Exchange
|
||||||
from .business import Price_Believe_Business
|
from .business import Price_Believe_Business
|
||||||
import uuid
|
import uuid
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
import log
|
||||||
class Simulation():
|
class Simulation():
|
||||||
"""
|
"""
|
||||||
Class for controlling the different calculation steps of the Simulation.
|
Class for controlling the different calculation steps of the Simulation.
|
||||||
"""
|
"""
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
self.timings={}
|
||||||
self.tick_funcs={}
|
self.tick_funcs={}
|
||||||
self.reset_funcs={}
|
self.reset_funcs={}
|
||||||
self.tick_count=0
|
self.tick_count=0
|
||||||
self.episode_count=0
|
self.episode_count=-1
|
||||||
self.cells={}
|
self.cells={}
|
||||||
|
self.populated_cells={}
|
||||||
self.businesses=[]
|
self.businesses=[]
|
||||||
self.production_util={}
|
self.production_util={}
|
||||||
|
self.episode_tax=1
|
||||||
|
self.taskpool=ThreadPoolExecutor()
|
||||||
|
|
||||||
|
self.setup_timing(["dss","dsa","dab","dbl","dt"])
|
||||||
for k in cm.productions.items():
|
for k in cm.productions.items():
|
||||||
l=k[1]
|
l=k[1]
|
||||||
for i in l:
|
for i in l:
|
||||||
@@ -25,6 +35,7 @@ class Simulation():
|
|||||||
def set_cells(self,cells):
|
def set_cells(self,cells):
|
||||||
for c in cells:
|
for c in cells:
|
||||||
self.cells[c.name]=c
|
self.cells[c.name]=c
|
||||||
|
self.populated_cells[c.name]=0
|
||||||
|
|
||||||
def set_businesses(self,bus):
|
def set_businesses(self,bus):
|
||||||
self.businesses=bus
|
self.businesses=bus
|
||||||
@@ -38,37 +49,93 @@ class Simulation():
|
|||||||
self.tick_funcs[id]=tickfunc
|
self.tick_funcs[id]=tickfunc
|
||||||
self.reset_funcs[id]=resetfunc
|
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):
|
def tick_agents_random_order(self):
|
||||||
"""
|
"""
|
||||||
Ticks all agents in a Random Order
|
Ticks all agents in a Random Order
|
||||||
"""
|
"""
|
||||||
keys=list(self.tick_funcs.keys())
|
keys=list(self.tick_funcs.keys())
|
||||||
random.shuffle(keys)
|
random.shuffle(keys)
|
||||||
|
tasks=[]
|
||||||
for k in keys:
|
for k in keys:
|
||||||
fun=self.tick_funcs[k]
|
fun=self.tick_funcs[k]
|
||||||
fun(self.tick_count,self.episode_count)
|
#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):
|
def tick(self,episode_length):
|
||||||
"""
|
"""
|
||||||
Executes a tick of the simulation
|
Executes a tick of the simulation
|
||||||
"""
|
"""
|
||||||
|
start=time.time()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self.tick_agents_random_order()
|
self.tick_agents_random_order()
|
||||||
|
agentstep=time.time()
|
||||||
for cell_id,c in self.cells.items():
|
for cell_id,c in self.cells.items():
|
||||||
c.setup_demand_for_step(episode_length)
|
c.setup_demand_for_step(episode_length)
|
||||||
|
setup=time.time()
|
||||||
for b in self.businesses:
|
for b in self.businesses:
|
||||||
b.tick(self.tick_count)
|
b.tick(self.tick_count)
|
||||||
|
|
||||||
|
busstep=time.time()
|
||||||
self.tick_count+=1
|
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["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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""
|
"""
|
||||||
Resets all agents to new Episode
|
Resets all agents to new Episode
|
||||||
"""
|
"""
|
||||||
|
self.log_cxs_episode()
|
||||||
|
self.log_business_episode()
|
||||||
for k,v in self.reset_funcs.items():
|
for k,v in self.reset_funcs.items():
|
||||||
v(self.episode_count)
|
v(self.episode_count)
|
||||||
for cell_id,cell in self.cells.items():
|
for cell_id,cell in self.cells.items():
|
||||||
cell.setup_supply_for_episode()
|
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.tick_count=0
|
||||||
self.episode_count+=1
|
self.episode_count+=1
|
||||||
|
|
||||||
@@ -82,14 +149,15 @@ class Simulation():
|
|||||||
under.append(k)
|
under.append(k)
|
||||||
return under
|
return under
|
||||||
|
|
||||||
def create_bussiness(self):
|
|
||||||
|
def create_bussiness(self,min_available_per_prod):
|
||||||
"""
|
"""
|
||||||
Create a new business based on given econemy state.
|
Create a new business based on given econemy state.
|
||||||
Return Business
|
Return Business
|
||||||
"""
|
"""
|
||||||
selected_prod=None
|
selected_prod=None
|
||||||
# Create a business for every production rule out there
|
# 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:
|
if len(under_prods)>0:
|
||||||
# we need to create more businesses
|
# we need to create more businesses
|
||||||
selected_prod=random.choice(under_prods)
|
selected_prod=random.choice(under_prods)
|
||||||
@@ -103,24 +171,46 @@ class Simulation():
|
|||||||
cell_id=random.choice(cells)
|
cell_id=random.choice(cells)
|
||||||
cell=self.cells[cell_id]
|
cell=self.cells[cell_id]
|
||||||
cxs=[cell.exchange,self.cx]
|
cxs=[cell.exchange,self.cx]
|
||||||
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.businesses.append(business)
|
||||||
self.production_util[selected_prod]+=1
|
self.production_util[selected_prod]+=1
|
||||||
|
self.populated_cells[cell_id]+=1
|
||||||
return business
|
return business
|
||||||
|
|
||||||
|
def remove_business(self,id):
|
||||||
|
bus=None
|
||||||
|
for busl in self.businesses:
|
||||||
|
if busl.id==id:
|
||||||
|
bus=busl
|
||||||
|
break
|
||||||
|
|
||||||
|
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):
|
def get_max_profit_prod(self):
|
||||||
"""
|
"""
|
||||||
Returns the prod with the highest profit in last episode
|
Returns the prod with the highest profit in last episode
|
||||||
"""
|
"""
|
||||||
profit=self.businesses[0].balance-self.businesses[0].balance_history[-1]
|
profit={}
|
||||||
prod=self.businesses[0].production
|
|
||||||
|
|
||||||
for b in self.businesses:
|
for b in self.businesses:
|
||||||
diff=b.balance-b.balance_history[-1]
|
id=b.production["id"]
|
||||||
if diff>profit:
|
if len(b.balance_history)<2:
|
||||||
profit=diff
|
continue
|
||||||
prod=b.production
|
diff=b.balance-b.balance_history[-2]
|
||||||
return prod
|
if id not in profit:
|
||||||
|
profit[id]=0
|
||||||
|
profit[id]+=diff
|
||||||
|
best=None
|
||||||
|
bestv=-1000000000000
|
||||||
|
for k,v in profit.items():
|
||||||
|
if bestv<v:
|
||||||
|
bestv=v
|
||||||
|
best=k
|
||||||
|
return best
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -135,15 +225,58 @@ class Simulation():
|
|||||||
cx=cell.exchange
|
cx=cell.exchange
|
||||||
# Best prod resource availability
|
# Best prod resource availability
|
||||||
# Using the exchange in that cell, lookup supply or demand of resources. Supply +1 / Demand -1
|
# Using the exchange in that cell, lookup supply or demand of resources. Supply +1 / Demand -1
|
||||||
for prod_item in prod["prod"]:
|
for key,val in prod["prod"].items():
|
||||||
key=list(prod_item.keys())[0]
|
|
||||||
val=prod_item[key]
|
|
||||||
a=cx.get_total_supply(key)
|
a=cx.get_total_supply(key)
|
||||||
cell_score[cell.name]+=(cx.get_total_supply(key)/val)
|
cell_score[cell.name]+=(cx.get_total_supply(key)/val)
|
||||||
cell_score[cell.name]-=(cx.get_total_demand(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
|
# If demand for prod item in cell then score +1/ score -1 if supply is already there
|
||||||
a=cx.get_total_demand(prod["name"])
|
a=cx.get_total_demand(prod["name"])
|
||||||
cell_score[cell.name]+=cx.get_total_demand(prod["name"])
|
b=cx.get_total_supply(prod["name"])
|
||||||
cell_score[cell.name]-=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())]
|
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):
|
||||||
|
items=[]
|
||||||
|
for id,_ in self.populated_cells.items():
|
||||||
|
cell=self.cells[id]
|
||||||
|
lcx=cell.exchange
|
||||||
|
name="lcx_{}".format(id)
|
||||||
|
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,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
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
admin
|
||||||
|
adminadmin
|
||||||
|
ywMlW7zpVYFcGr1krHa83R_J_HSrd2wEq8MgtB7YX7Nc6QoTZSD6H-iiKlXYZUZaZY2MbNrL3XxfioaSGe9_bA==
|
||||||
Binary file not shown.
@@ -0,0 +1,63 @@
|
|||||||
|
import influxdb_client, os, time
|
||||||
|
from influxdb_client import InfluxDBClient, Point, WritePrecision
|
||||||
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
token = "AcU0_L5vtfH-9ycHnk0txF3jgHIcWRcMOElzAVtIrAGXC0oNSjwZUzPBsWCBZHNbaJmr-9x34E1Hycow59MnQg=="
|
||||||
|
org = "econ"
|
||||||
|
url = "http://localhost:8086"
|
||||||
|
bucket="econ"
|
||||||
|
|
||||||
|
posturl='postgresql://admin:econ@localhost:15432/econ'
|
||||||
|
|
||||||
|
write_client = None
|
||||||
|
EXInit=False
|
||||||
|
EXBooksData=[]
|
||||||
|
EXTradeData=[]
|
||||||
|
EXTradeDataDetail=[]
|
||||||
|
BUSINESSData=[]
|
||||||
|
PerformanceData=[]
|
||||||
|
def get_client():
|
||||||
|
global write_client
|
||||||
|
if write_client==None:
|
||||||
|
write_client=influxdb_client.InfluxDBClient(url=url, token=token, org=org)
|
||||||
|
return write_client
|
||||||
|
def writeEXData():
|
||||||
|
db = create_engine(posturl)
|
||||||
|
|
||||||
|
df=pd.DataFrame(EXBooksData)
|
||||||
|
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=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)
|
||||||
|
df=pd.DataFrame(BUSINESSData)
|
||||||
|
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.
@@ -4,7 +4,9 @@ from econ.simulation import Simulation
|
|||||||
from econ.business.Price_Believe_Business import Price_Believe_Business
|
from econ.business.Price_Believe_Business import Price_Believe_Business
|
||||||
from econ.commoditys import commoditys
|
from econ.commoditys import commoditys
|
||||||
from econ.cells import db, cell
|
from econ.cells import db, cell
|
||||||
|
import log
|
||||||
|
from tqdm import tqdm
|
||||||
|
import uuid
|
||||||
db.load_world("db/world.json")
|
db.load_world("db/world.json")
|
||||||
|
|
||||||
commoditys.search_yaml_files()
|
commoditys.search_yaml_files()
|
||||||
@@ -24,17 +26,37 @@ cxs=[cx]
|
|||||||
# Init World
|
# Init World
|
||||||
|
|
||||||
# Create Demand
|
# Create Demand
|
||||||
cx.submit_order(w,"Wood",0,1000,Side.BUY)
|
#cx.submit_order(w,"Wood",0,1000,Side.BUY)
|
||||||
cx.submit_order(2,"Gem",1,10,Side.SELL)
|
#cx.submit_order(2,"Gem",1,10,Side.SELL)
|
||||||
|
EPISODE_LEN=20
|
||||||
|
NUM_EPISODES=20
|
||||||
|
NUM_SEED_BUS=10
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sim=Simulation()
|
sim=Simulation()
|
||||||
|
sim.seed(43)
|
||||||
sim.set_cells(cells)
|
sim.set_cells(cells)
|
||||||
sim.reset()
|
sim.reset()
|
||||||
sim.tick(1)
|
for i in range(EPISODE_LEN):
|
||||||
# create info
|
sim.tick(EPISODE_LEN)
|
||||||
while len(sim.get_underutelized_prods(1))>0:
|
|
||||||
sim.create_bussiness()
|
|
||||||
sim.reset()
|
sim.reset()
|
||||||
for i in range(1000):
|
# create info
|
||||||
sim.tick(1000)
|
while len(sim.get_underutelized_prods(2))>0:
|
||||||
|
sim.create_bussiness(2)
|
||||||
|
#sim.reset()
|
||||||
|
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()
|
||||||
|
for i in range(NUM_SEED_BUS):
|
||||||
|
sim.create_bussiness(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
log.writeEXData()
|
||||||
|
log.writeBusinessData()
|
||||||
|
log.writePerformanceData()
|
||||||
|
|
||||||
print("help")
|
print("help")
|
||||||
@@ -1 +1,6 @@
|
|||||||
lightmatchingengine
|
lightmatchingengine
|
||||||
|
pyyaml
|
||||||
|
influxdb-client
|
||||||
|
pandas
|
||||||
|
sqlalchemy
|
||||||
|
tensorflow
|
||||||
Reference in New Issue
Block a user