this shit is it
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -127,5 +127,6 @@ class Base_Aquire_Agent(BaseAgent,ABC):
|
||||
def reset(self, episode):
|
||||
#self.tqty=0
|
||||
self.qty_offset=0
|
||||
self.expense_offset=0
|
||||
self.trades=[]
|
||||
return super().reset(episode)
|
||||
@@ -60,6 +60,8 @@ 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
|
||||
|
||||
@@ -14,8 +14,8 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
||||
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.25
|
||||
self.lp_threshold=0.90
|
||||
self.hp_threshold=0.70
|
||||
self.lp_threshold=0.99
|
||||
|
||||
def tick(self, tick, episode):
|
||||
|
||||
@@ -52,9 +52,11 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
||||
return best_id
|
||||
|
||||
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:
|
||||
|
||||
@@ -7,15 +7,16 @@ 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:
|
||||
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.open_orders = {i: [] for i in range(len(self.exchanges))}
|
||||
self.open_qty = 0
|
||||
self.lp_threshold=0.10
|
||||
self.hp_threshold=0.90
|
||||
self.lp_threshold=0.05
|
||||
self.hp_threshold=0.80
|
||||
|
||||
def tick(self, step, episode):
|
||||
|
||||
@@ -50,9 +51,11 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
return best_id
|
||||
|
||||
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:
|
||||
@@ -80,7 +83,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
modifier=1
|
||||
elif succsess<=self.lp_threshold:
|
||||
modifier=-1
|
||||
self.update_believe(cx_id, modifier) # update price believe
|
||||
self.update_believe(cx_id,o.price, succsess) # update price believe
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_trades()
|
||||
@@ -100,36 +103,33 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
||||
# timeout
|
||||
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
|
||||
|
||||
cx.cancel_order(i["id"])
|
||||
self.collect_balance_from_cxs()
|
||||
self.collect_resource_from_cxs(self.resource)
|
||||
self.update_believe(cx_id, modifier)
|
||||
self.update_believe(cx_id,o.price, succsess)
|
||||
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
|
||||
soldperc=sold/o.qty
|
||||
nsold=min(o.leaves_qty,cx.total_demand[self.resource])
|
||||
dem = cx.total_demand[self.resource]+sold
|
||||
if dem == 0:
|
||||
dem = 1
|
||||
coverage = sold/dem
|
||||
base_success=max([coverage,soldperc])
|
||||
return base_success
|
||||
score=sold-nsold
|
||||
return score
|
||||
|
||||
def update_believe(self, cx_id, modifier):
|
||||
def update_believe(self, cx_id, used_price, 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
|
||||
|
||||
self.price_believe[cx_id]+=(modifier/self.lr_at_sales)*self.lr
|
||||
|
||||
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
|
||||
|
||||
@@ -11,19 +11,21 @@ class Price_Believe_Business(Business):
|
||||
|
||||
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.2, 50)
|
||||
simulation, self, production["name"], exchange, 0.2,production["amount"], 20)
|
||||
self.craft = AutoProductionAgent(simulation, self)
|
||||
self.aquire = {}
|
||||
|
||||
for k, v in production["prod"].items():
|
||||
a = Price_Believe_Aquire_Agent(
|
||||
simulation, self, k, exchange, 0.2, 5)
|
||||
simulation, self, k, exchange, 0.2, 20)
|
||||
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):
|
||||
@@ -95,7 +97,7 @@ class Price_Believe_Business(Business):
|
||||
|
||||
ExPerProd = agent.expense/covers
|
||||
experunit += ExPerProd/amount
|
||||
|
||||
|
||||
if qtyNewProds > 0:
|
||||
# confirm new units
|
||||
for key, agent in self.aquire.items():
|
||||
@@ -116,6 +118,7 @@ class Price_Believe_Business(Business):
|
||||
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
|
||||
@@ -140,6 +143,7 @@ class Price_Believe_Business(Business):
|
||||
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
|
||||
|
||||
Binary file not shown.
@@ -127,10 +127,11 @@ class Exchange():
|
||||
# calculate price for complete order fullfilment
|
||||
prev=amount
|
||||
amount=int(amount)
|
||||
price=round(price)
|
||||
if amount<1:
|
||||
# invalid order
|
||||
return None
|
||||
full_price=round(price*amount,2)
|
||||
full_price=round(price*amount)
|
||||
|
||||
|
||||
# Move resources into escrow
|
||||
@@ -294,11 +295,11 @@ class Exchange():
|
||||
#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
|
||||
|
||||
@@ -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"])
|
||||
@@ -113,6 +114,8 @@ class Simulation():
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user