this shit is it
This commit is contained in:
@@ -6,7 +6,7 @@ spec:
|
|||||||
prod:
|
prod:
|
||||||
Raw_Agriculture_Plot: 1
|
Raw_Agriculture_Plot: 1
|
||||||
- name: Fruit
|
- name: Fruit
|
||||||
amount: 1000
|
amount: 100
|
||||||
prod:
|
prod:
|
||||||
Raw_Agriculture_Plot: 1
|
Raw_Agriculture_Plot: 1
|
||||||
# - name: Wood
|
# - name: Wood
|
||||||
@@ -20,10 +20,10 @@ spec:
|
|||||||
# Wood: 1
|
# Wood: 1
|
||||||
|
|
||||||
- name: Food
|
- name: Food
|
||||||
amount: 50
|
amount: 100
|
||||||
prod:
|
prod:
|
||||||
# Fuel: 1
|
# Fuel: 1
|
||||||
Fruit: 1
|
# Fruit: 1
|
||||||
Grain: 2
|
Grain: 2
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
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):
|
def reset(self, episode):
|
||||||
#self.tqty=0
|
#self.tqty=0
|
||||||
self.qty_offset=0
|
self.qty_offset=0
|
||||||
|
self.expense_offset=0
|
||||||
self.trades=[]
|
self.trades=[]
|
||||||
return super().reset(episode)
|
return super().reset(episode)
|
||||||
@@ -60,6 +60,8 @@ 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
|
||||||
|
|||||||
@@ -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.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.25
|
self.hp_threshold=0.70
|
||||||
self.lp_threshold=0.90
|
self.lp_threshold=0.99
|
||||||
|
|
||||||
def tick(self, tick, episode):
|
def tick(self, tick, episode):
|
||||||
|
|
||||||
@@ -52,9 +52,11 @@ class Price_Believe_Aquire_Agent(Base_Aquire_Agent):
|
|||||||
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
|
'leaves': order.leaves_qty
|
||||||
})
|
})
|
||||||
if order.leaves_qty != order.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.
|
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.lr_at_sales=at_sales
|
||||||
self.max_price_adj_rate = max_price_adj_rate
|
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: 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.lp_threshold=0.10
|
self.lp_threshold=0.05
|
||||||
self.hp_threshold=0.90
|
self.hp_threshold=0.80
|
||||||
|
|
||||||
def tick(self, step, episode):
|
def tick(self, step, episode):
|
||||||
|
|
||||||
@@ -50,9 +51,11 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
|||||||
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
|
'leaves': order.leaves_qty
|
||||||
})
|
})
|
||||||
if order.leaves_qty != order.qty:
|
if order.leaves_qty != order.qty:
|
||||||
@@ -80,7 +83,7 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
|||||||
modifier=1
|
modifier=1
|
||||||
elif succsess<=self.lp_threshold:
|
elif succsess<=self.lp_threshold:
|
||||||
modifier=-1
|
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_balance_from_cxs()
|
||||||
self.collect_resource_from_cxs(self.resource)
|
self.collect_resource_from_cxs(self.resource)
|
||||||
self.update_trades()
|
self.update_trades()
|
||||||
@@ -100,36 +103,33 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
|
|||||||
# timeout
|
# timeout
|
||||||
self.update_trades()
|
self.update_trades()
|
||||||
succsess=self.calc_order_success(cx,o)
|
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"])
|
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, modifier)
|
self.update_believe(cx_id,o.price, succsess)
|
||||||
self.open_orders[cx_id].remove(i)
|
self.open_orders[cx_id].remove(i)
|
||||||
def calc_order_success(self,cx, o):
|
def calc_order_success(self,cx, o):
|
||||||
"""
|
"""
|
||||||
Calculate how we should adjust the price belive
|
Calculate how we should adjust the price belive
|
||||||
"""
|
"""
|
||||||
sold = o.qty-o.leaves_qty
|
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
|
dem = cx.total_demand[self.resource]+sold
|
||||||
if dem == 0:
|
if dem == 0:
|
||||||
dem = 1
|
dem = 1
|
||||||
coverage = sold/dem
|
score=sold-nsold
|
||||||
base_success=max([coverage,soldperc])
|
return score
|
||||||
return base_success
|
|
||||||
|
|
||||||
def update_believe(self, cx_id, modifier):
|
def update_believe(self, cx_id, used_price, 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_at_sales)*self.lr
|
||||||
|
|
||||||
self.price_believe[cx_id] = round(self.price_believe[cx_id], 2)
|
self.price_believe[cx_id] = round(self.price_believe[cx_id], 2)
|
||||||
if self.price_believe[cx_id] < 1:
|
if self.price_believe[cx_id] < 1:
|
||||||
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.max_storage = 10
|
||||||
self.expense_per_unit = -1
|
self.expense_per_unit = -1
|
||||||
|
self.expense_per_prod=-1
|
||||||
self.income_per_unit = -1
|
self.income_per_unit = -1
|
||||||
self.distribute = Price_Believe_Distribiute_Agent(
|
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.craft = AutoProductionAgent(simulation, self)
|
||||||
self.aquire = {}
|
self.aquire = {}
|
||||||
|
|
||||||
for k, v in production["prod"].items():
|
for k, v in production["prod"].items():
|
||||||
a = Price_Believe_Aquire_Agent(
|
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_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(0)
|
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):
|
||||||
@@ -95,7 +97,7 @@ class Price_Believe_Business(Business):
|
|||||||
|
|
||||||
ExPerProd = agent.expense/covers
|
ExPerProd = agent.expense/covers
|
||||||
experunit += ExPerProd/amount
|
experunit += ExPerProd/amount
|
||||||
|
|
||||||
if qtyNewProds > 0:
|
if qtyNewProds > 0:
|
||||||
# confirm new units
|
# confirm new units
|
||||||
for key, agent in self.aquire.items():
|
for key, agent in self.aquire.items():
|
||||||
@@ -116,6 +118,7 @@ class Price_Believe_Business(Business):
|
|||||||
estimatedQTYUnits = qtyNewUnits+self.resource_in_possesion()
|
estimatedQTYUnits = qtyNewUnits+self.resource_in_possesion()
|
||||||
new_ex = ex_total/estimatedQTYUnits
|
new_ex = ex_total/estimatedQTYUnits
|
||||||
self.expense_per_unit = new_ex
|
self.expense_per_unit = new_ex
|
||||||
|
self.expense_per_prod=self.expense_per_unit*amount
|
||||||
|
|
||||||
def update_income_per_unit(self, step):
|
def update_income_per_unit(self, step):
|
||||||
qty_sold = self.distribute.qty
|
qty_sold = self.distribute.qty
|
||||||
@@ -140,6 +143,7 @@ class Price_Believe_Business(Business):
|
|||||||
data["tstep"] = step+episode*episode_length
|
data["tstep"] = step+episode*episode_length
|
||||||
data["production"] = self.production["name"]
|
data["production"] = self.production["name"]
|
||||||
data["expense"] = self.expense_per_unit
|
data["expense"] = self.expense_per_unit
|
||||||
|
data["expense_prod"]=self.expense_per_prod
|
||||||
data["income"] = self.income_per_unit
|
data["income"] = self.income_per_unit
|
||||||
data["inventory"] = self.inventory[self.production["name"]]
|
data["inventory"] = self.inventory[self.production["name"]]
|
||||||
data["retain"] = self.distribute.target
|
data["retain"] = self.distribute.target
|
||||||
|
|||||||
Binary file not shown.
@@ -127,10 +127,11 @@ class Exchange():
|
|||||||
# calculate price for complete order fullfilment
|
# calculate price for complete order fullfilment
|
||||||
prev=amount
|
prev=amount
|
||||||
amount=int(amount)
|
amount=int(amount)
|
||||||
|
price=round(price)
|
||||||
if amount<1:
|
if amount<1:
|
||||||
# invalid order
|
# invalid order
|
||||||
return None
|
return None
|
||||||
full_price=round(price*amount,2)
|
full_price=round(price*amount)
|
||||||
|
|
||||||
|
|
||||||
# Move resources into escrow
|
# Move resources into escrow
|
||||||
@@ -294,11 +295,11 @@ class Exchange():
|
|||||||
#data["step"]=step
|
#data["step"]=step
|
||||||
data["tstep"]=timepoint
|
data["tstep"]=timepoint
|
||||||
data["instrm"]=instrm
|
data["instrm"]=instrm
|
||||||
data["best_ask"]=0
|
data["best_ask"]=None
|
||||||
data["best_bid"]=0
|
data["best_bid"]=None
|
||||||
data["total_demand"]=0
|
data["total_demand"]=None
|
||||||
data["total_supply"]=0
|
data["total_supply"]=None
|
||||||
data["market_rate"]=0
|
data["market_rate"]=None
|
||||||
localStepDB[instrm]=data
|
localStepDB[instrm]=data
|
||||||
|
|
||||||
## add best ask/best bid
|
## add best ask/best bid
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class Simulation():
|
|||||||
self.populated_cells={}
|
self.populated_cells={}
|
||||||
self.businesses=[]
|
self.businesses=[]
|
||||||
self.production_util={}
|
self.production_util={}
|
||||||
|
self.episode_tax=1
|
||||||
self.taskpool=ThreadPoolExecutor()
|
self.taskpool=ThreadPoolExecutor()
|
||||||
|
|
||||||
self.setup_timing(["dss","dsa","dab","dbl","dt"])
|
self.setup_timing(["dss","dsa","dab","dbl","dt"])
|
||||||
@@ -113,6 +114,8 @@ class Simulation():
|
|||||||
cell.setup_supply_for_episode()
|
cell.setup_supply_for_episode()
|
||||||
toclose=[]
|
toclose=[]
|
||||||
for b in self.businesses:
|
for b in self.businesses:
|
||||||
|
#tax business
|
||||||
|
b.balance-=self.episode_tax
|
||||||
b.tick_episode(self.episode_count)
|
b.tick_episode(self.episode_count)
|
||||||
if b.balance<=0:
|
if b.balance<=0:
|
||||||
toclose.append(b.id)
|
toclose.append(b.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user