handling request based on connection

This commit is contained in:
HaoYizhi 2023-03-16 16:08:45 +08:00
parent 082864814b
commit 1072d4e2b0
9 changed files with 34 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

24
firm.py
View File

@ -107,7 +107,18 @@ class FirmAgent(ap.Agent):
self.accept_request(lst_firm[0], product) self.accept_request(lst_firm[0], product)
elif len(lst_firm) > 1: elif len(lst_firm) > 1:
# handling based on connection # handling based on connection
# TBC lst_firm_connect = []
for firm in lst_firm:
out_edges = self.model.firm_network.graph.out_edges(
self.model.firm_network.positions[firm], keys=True)
in_edges = self.model.firm_network.graph.in_edges(
self.model.firm_network.positions[firm], keys=True)
lst_adj_firm = []
lst_adj_firm += [ap.AgentIter(self.model, edge[1]).to_list()[0].code for edge in out_edges]
lst_adj_firm += [ap.AgentIter(self.model, edge[0]).to_list()[0].code for edge in in_edges]
if self.code in lst_adj_firm:
lst_firm_connect += firm
if len(lst_firm_connect) == 0:
# handling based on size # handling based on size
lst_firm_size = [firm.revenue_log for firm in lst_firm] lst_firm_size = [firm.revenue_log for firm in lst_firm]
lst_prob = [ lst_prob = [
@ -116,6 +127,17 @@ class FirmAgent(ap.Agent):
select_customer = self.model.nprandom.choice(lst_firm, select_customer = self.model.nprandom.choice(lst_firm,
p=lst_prob) p=lst_prob)
self.accept_request(select_customer, product) self.accept_request(select_customer, product)
elif len(lst_firm_connect) == 1:
self.accept_request(lst_firm_connect[0], product)
elif len(lst_firm_connect) > 1:
# handling based on size of firm that has connection
lst_firm_size = [firm.revenue_log for firm in lst_firm_connect]
lst_prob = [
size / sum(lst_firm_size) for size in lst_firm_size
]
select_customer = self.model.nprandom.choice(lst_firm_connect,
p=lst_prob)
self.accept_request(select_customer, product)
def accept_request(self, down_firm, product): def accept_request(self, down_firm, product):
lst_firm_size = [ lst_firm_size = [

View File

@ -37,6 +37,8 @@ if __name__ == '__main__':
from controller_db import ControllerDB from controller_db import ControllerDB
controller_db = ControllerDB(args.exp, reset_flag=args.reset) controller_db = ControllerDB(args.exp, reset_flag=args.reset)
# controller_db.reset_db() # controller_db.reset_db()
# force drop
controller_db.reset_db(force_drop=True) controller_db.reset_db(force_drop=True)
controller_db.prepare_list_sample() controller_db.prepare_list_sample()

View File

@ -137,10 +137,6 @@ class Model(ap.Model):
# draw network # draw network
# self.draw_network() # self.draw_network()
out_file = open("myfile.json", "w")
json.dump(nx.adjacency_data(G_Firm), out_file)
out_file.close()
def update(self): def update(self):
self.a_lst_total_firms.clean_before_time_step() self.a_lst_total_firms.clean_before_time_step()
# output # output

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 MiB

After

Width:  |  Height:  |  Size: 3.0 MiB