Compare commits

...
3 Commits
Author SHA1 Message Date
manuel eec9c82a83 This is getting better and better 2023-07-12 15:15:25 +02:00
manuel 8d5ad7c62d it just look good 2023-07-12 12:33:58 +02:00
manuel 010d1b6d3c simulator 2023-07-05 17:42:45 +02:00
20 changed files with 77 additions and 28 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ spec:
prod:
Raw_Agriculture_Plot: 1
- name: Fruit
amount: 100
amount: 20
prod:
Raw_Agriculture_Plot: 1
# - name: Wood
@@ -20,10 +20,10 @@ spec:
# Wood: 1
- name: Food
amount: 50
amount: 5
prod:
# Fuel: 1
# Fruit: 1
Fruit: 1
Grain: 2
Binary file not shown.
Binary file not shown.
+1
View File
@@ -63,6 +63,7 @@ class Base_Aquire_Agent(BaseAgent,ABC):
self.business.balance-=total_price
cx.add_to_account(self.id,"balance",total_price) # prepaid charge account for cx
cx.register_account(self.id,self.business.id)
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.BUY)
if order==None: # Order failed
return None
+1
View File
@@ -69,6 +69,7 @@ class Base_Distribution_Agent(BaseAgent,ABC):
self.business.inventory[self.resource]-=amount
cx.add_to_account(self.id,self.resource,amount) # prepaid charge account for cx
cx.register_account(self.id,self.business.id)
order=cx.submit_order(self.id,self.resource,amount,price_per,Side.SELL)
if order==None: # Order failed
return False
+17 -6
View File
@@ -10,22 +10,27 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
def __init__(self, simulation, business, resource, exchanges: list, lr,at_sales, max_price_adj_rate) -> None:
super().__init__(simulation, business, resource, exchanges)
self.lr = lr
self.lr_at_sales=at_sales
self.max_price_adj_rate = max_price_adj_rate
self.price_believe = {i: 1 for i in range(len(self.exchanges))}
self.lr_moment={i: 0 for i in range(len(self.exchanges))}
self.open_orders = {i: [] for i in range(len(self.exchanges))}
self.open_qty = 0
self.beta=0.8
self.lp_threshold=0.05
self.hp_threshold=0.80
def tick(self, step, episode):
order_error = self.target_error()
if order_error > 0:
if (order_error > 0) & (self.open_qty==0):
# aquire based on current price belive
cx_id = self.select_best_cx()
price = round(self.price_believe[cx_id],2)
if price==30.0:
print("Ple")
if price < self.min_price:
price = self.min_price
order = self.distribute_resource(
@@ -77,16 +82,17 @@ 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_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) # update price believe
self.update_believe(cx_id,o.price, succsess,o.qty) # update price believe
self.collect_balance_from_cxs()
self.collect_resource_from_cxs(self.resource)
self.update_trades()
continue
if not (i["leaves"] == leaves):
@@ -107,7 +113,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(cx_id,o.price, succsess)
self.update_believe(cx_id,o.price, succsess,o.qty)
self.open_orders[cx_id].remove(i)
def calc_order_success(self,cx, o):
"""
@@ -121,14 +127,19 @@ class Price_Believe_Distribiute_Agent(Base_Distribution_Agent):
score=sold-nsold
return score
def update_believe(self, cx_id, used_price, modifier):
def update_believe(self, cx_id, used_price, modifier,qty):
"""
Updates the believe based on the modifier.
If positive will add lr to believe
If negative will sub lr to believe
"""
clipMod=max(-qty,min(modifier,qty))
moment=clipMod/qty
#clip mod so that price believe more smooth
self.price_believe[cx_id]+=(modifier/self.lr_at_sales)*self.lr
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:
+3 -6
View File
@@ -14,13 +14,13 @@ class Price_Believe_Business(Business):
self.expense_per_prod=-1
self.income_per_unit = -1
self.distribute = Price_Believe_Distribiute_Agent(
simulation, self, production["name"], exchange, 0.2,production["amount"]*2, 20)
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.2, 20)
simulation, self, k, exchange, 0.02, 6)
a.set_target(v*2)
a.set_price_max(10)
self.aquire[k] = a
@@ -45,10 +45,7 @@ class Price_Believe_Business(Business):
retain=0
else:
ie = (self.income_per_unit/self.expense_per_unit)-1
if ie < 0:
ie = 0
if ie > 1:
ie = 1
ie=max(0,min(ie,1))
retain=((self.max_storage-ie*self.max_storage)*amount)-amount
#retain=0
Binary file not shown.
+2 -1
View File
@@ -24,7 +24,7 @@ class Cell:
exchange: None
def __init__(self,name,x,y,area,pop,demand,world) -> None:
pop=pop*100
pop=pop*10
self.name=name
self.loc_x=x
self.loc_y=y
@@ -63,6 +63,7 @@ class Cell:
qty=value/episode_length
comm=cm.commoditys[demand_key]
max_price=comm["max_world_price"]*qty
self.exchange.register_account(self.name,self.name)
self.exchange.add_to_account(self.name,"balance",max_price)
self.exchange.submit_order(self.name,demand_key,qty,comm["max_world_price"],Side.BUY)
def setup_supply_for_episode(self):
+31 -1
View File
@@ -9,6 +9,10 @@ import uuid
import threading
software_start=int(time.time())
class Exchange():
"""
Basic Commodity exchange.
@@ -26,6 +30,9 @@ class Exchange():
self.order_account_map={}
self.orders={}
self.executed_trades=[]
self.trade_curser=0
self.account_business={}
self.order_trades_map={}
self.market_rate={}
self.best_ask={}
@@ -36,7 +43,11 @@ class Exchange():
#self.supply={}
self.traded_commoditys={}
self.lock=threading.Lock()
def register_account(self,account,business):
self.account_business[account]=business
def add_to_account(self,account_id,resource,amount):
"""
Adds resources to account escrow
@@ -320,6 +331,25 @@ class Exchange():
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():
+6 -4
View File
@@ -71,7 +71,7 @@ class Simulation():
#self.taskpool.submit(self._execute_tick,row)
fun(self.tick_count,self.episode_count)
self.taskpool.map(self._execute_tick,tasks)
#self.taskpool.map(self._execute_tick,tasks)
@@ -86,12 +86,14 @@ class Simulation():
Executes a tick of the simulation
"""
start=time.time()
self.tick_agents_random_order()
agentstep=time.time()
for cell_id,c in self.cells.items():
c.setup_demand_for_step(episode_length)
setup=time.time()
self.tick_agents_random_order()
agentstep=time.time()
for b in self.businesses:
b.tick(self.tick_count)
+7
View File
@@ -15,6 +15,7 @@ write_client = None
EXInit=False
EXBooksData=[]
EXTradeData=[]
EXTradeDataDetail=[]
BUSINESSData=[]
PerformanceData=[]
def get_client():
@@ -35,8 +36,14 @@ def writeEXData():
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)
Binary file not shown.
+4 -6
View File
@@ -28,9 +28,9 @@ cxs=[cx]
# Create Demand
#cx.submit_order(w,"Wood",0,1000,Side.BUY)
#cx.submit_order(2,"Gem",1,10,Side.SELL)
EPISODE_LEN=100
NUM_EPISODES=100
NUM_SEED_BUS=5
EPISODE_LEN=20
NUM_EPISODES=20
NUM_SEED_BUS=10
@@ -51,10 +51,8 @@ for a in tqdm(range(NUM_EPISODES)):
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)
sim.create_bussiness(1)
log.writeEXData()
+2 -1
View File
@@ -2,4 +2,5 @@ lightmatchingengine
pyyaml
influxdb-client
pandas
sqlalchemy
sqlalchemy
tensorflow
BIN
View File
Binary file not shown.