some things work

This commit is contained in:
2023-01-26 14:58:41 +01:00
parent be83cbd988
commit 966db6e5ad
24 changed files with 242 additions and 68 deletions
Binary file not shown.
+13 -9
View File
@@ -12,29 +12,33 @@ class AutoProductionAgent(BaseAgent):
self.worker=worker
self.employment_rate=employment_rate
self.employment_index=worker
self.target=0
def set_worker(self,workers):
if workers>1:
self.worker=workers
else:
self.worker=1
def set_target(self,qty):
self.target=qty
def can_produce(self):
# If can produce item
for k,cost in self.prod["craft"].items():
if cost > self.business.inventory[k]:
return False
for com in self.prod["prod"]:
for k,v in com.items():
if v > self.business.inventory[k]:
return False
# check if we should produce
if self.business.resource_in_possesion()>self.target:
return False
return True
def tick(self,step,epi):
for i in range(self.worker):
if not self.can_produce():
self.employment_index-=self.employment_rate
continue
self.employment_index+=self.employment_rate
self.set_worker(int(self.employment_index))
# remove cost from inventory
for k,cost in self.prod["craft"].items():
self.business.inventory[k]-=cost
for com in self.prod["prod"]:
for k,cost in com.items():
self.business.inventory[k]-=cost
# add commodity
self.business.inventory[self.prod['name']]+=self.prod["amount"]
+1 -1
View File
@@ -18,7 +18,7 @@ class BaseAgent(ABC):
assert "No Tick method has been provided"
pass
def reset(self):
def reset(self,episode):
"""
Resets agent to new episode.
"""
+3 -2
View File
@@ -92,8 +92,9 @@ class Base_Aquire_Agent(BaseAgent,ABC):
cx=self.exchanges[cx_id]
orders=self.orders[cx_id]
if len(orders)>0:
for o in orders:
trades.append(cx.order_trades_map[o.order_id])
for k,o in orders.items():
if k in cx.order_trades_map:
trades.append(cx.order_trades_map[k])
self.trades=trades
return trades
+14 -11
View File
@@ -1,5 +1,5 @@
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.
@@ -8,14 +8,13 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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.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,tick,episode):
if self.price_believe==-1:
self.price_believe=self.max_price
order_error=self.open_qty+self.target_error()
if order_error<0:
@@ -23,7 +22,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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(0,order)
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()
@@ -32,14 +31,16 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
self.tick_open_orders()
def select_best_cx(self):
best_id=0
best_id=-1
best=0
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
potential=cx.total_supply[self.resource]*self.price_believe[cx_id]
potential=cx.get_total_supply(self.resource)*self.price_believe[cx_id]
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):
@@ -63,7 +64,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
if o.leaves_qty==0:
#order is done
self.open_orders[cx_id].remove(i) # remove order from open
self.update_believe(-1) # update price believe
self.update_believe(cx_id,-1) # update price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
continue
@@ -75,7 +76,7 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
cx.cancel_order(i["id"])
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_believe(1)
self.update_believe(cx_id,1)
self.open_orders[cx_id].remove(i)
def update_believe(self,cx_id,modifier):
@@ -85,8 +86,10 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
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):
def reset(self,episode):
# Clean shop for today
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
@@ -98,4 +101,4 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
# book keeping
self.update_trades()
return super().reset()
return super().reset(episode)
+16 -14
View File
@@ -1,5 +1,5 @@
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.
@@ -8,31 +8,31 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
super().__init__(simulation, business, resource, exchanges)
self.lr=lr
self.max_price_adj_rate=max_price_adj_rate
self.price_believe=-1
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):
if self.price_believe==-1:
self.price_believe=self.min_price
def tick(self,step,episode):
order_error=self.target_error()
if order_error>0:
# aquire based on current price belive
cx_id=self.select_best_cx()
order=self.distribute_resource(self.price_believe,order_error,cx_id)
self.register_order(0,order)
order=self.distribute_resource(self.price_believe[cx_id],order_error,cx_id)
self.register_order(cx_id,order)
self.tick_open_orders()
def select_best_cx(self):
best_id=0
best_id=-1
best=0
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
potential=cx.total_supply[self.resource]*self.price_believe[cx_id]
potential=cx.get_total_supply(self.resource)*self.price_believe[cx_id]
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):
@@ -56,7 +56,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
if o.leaves_qty==0:
#order is done
self.open_orders[cx_id].remove(i) # remove order from open
self.update_believe(1) # update price believe
self.update_believe(cx_id,1) # update price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
continue
@@ -68,7 +68,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
cx.cancel_order(i["id"])
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_believe(-1)
self.update_believe(cx_id,-1)
self.open_orders[cx_id].remove(i)
@@ -83,8 +83,10 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
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):
def reset(self,episode):
# Clean shop for today
for cx_id in range(len(self.exchanges)):
cx=self.exchanges[cx_id]
@@ -96,4 +98,4 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
# book keeping
self.update_trades()
return super().reset()
return super().reset(episode)