first normal run

This commit is contained in:
HaoYizhi 2023-03-16 21:38:57 +08:00
parent 1072d4e2b0
commit 6489ecff90
9 changed files with 50 additions and 50 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
db_name_prefix: test db_name_prefix: not_test

View File

@ -52,9 +52,10 @@ class ControllerDB:
dct = {code: [product_code]} dct = {code: [product_code]}
list_dct.append(dct) list_dct.append(dct)
# list_dct = [{'140': ['1.4.5.1']}] # list_dct = [{'140': ['1.4.5.1']}]
list_dct = [{'133': ['1.4.4.1']}] # list_dct = [{'133': ['1.4.4.1']}]
# list_dct = [{'2': ['1.1.3']}] # list_dct = [{'2': ['1.1.3']}]
# list_dct = [{'135': ['1.3.2.1']}] # list_dct = [{'135': ['1.3.2.1']}]
# list_dct = [{'79': ['2.1.3.4']}]
# fill g_bom # fill g_bom
BomNodes = pd.read_csv('BomNodes.csv', index_col=0) BomNodes = pd.read_csv('BomNodes.csv', index_col=0)

34
firm.py
View File

@ -44,8 +44,8 @@ class FirmAgent(ap.Agent):
if self.model.nprandom.choice([True, False], if self.model.nprandom.choice([True, False],
p=[prod_remove, p=[prod_remove,
1 - prod_remove]): 1 - prod_remove]):
print(self.name, remove_product.code, 'affect', # print(self.name, remove_product.code, 'affect',
customer.name) # customer.name)
if remove_product not in \ if remove_product not in \
customer.a_lst_up_product_removed: customer.a_lst_up_product_removed:
customer.a_lst_up_product_removed.append( customer.a_lst_up_product_removed.append(
@ -55,7 +55,7 @@ class FirmAgent(ap.Agent):
def seek_alt_supply(self): def seek_alt_supply(self):
for product in self.a_lst_up_product_removed: for product in self.a_lst_up_product_removed:
print(f"{self.name} seek alt supply for {product.code}") # print(f"{self.name} seek alt supply for {product.code}")
if self.dct_n_trial_up_product_removed[ if self.dct_n_trial_up_product_removed[
product] <= self.model.int_n_max_trial: product] <= self.model.int_n_max_trial:
# select a list of candidate firm that has the product # select a list of candidate firm that has the product
@ -73,9 +73,9 @@ class FirmAgent(ap.Agent):
] ]
select_alt_supply = self.model.nprandom.choice( select_alt_supply = self.model.nprandom.choice(
candidate_alt_supply, p=lst_prob) candidate_alt_supply, p=lst_prob)
print( # print(
f"{self.name} selct alt supply for {product.code} from {select_alt_supply.name}" # f"{self.name} selct alt supply for {product.code} from {select_alt_supply.name}"
) # )
assert product in select_alt_supply.a_lst_product, \ assert product in select_alt_supply.a_lst_product, \
f"{select_alt_supply} \ f"{select_alt_supply} \
does not produce requested product {product}" does not produce requested product {product}"
@ -88,17 +88,17 @@ class FirmAgent(ap.Agent):
select_alt_supply.dct_request_prod_from_firm[product] = [ select_alt_supply.dct_request_prod_from_firm[product] = [
self self
] ]
print( # print(
select_alt_supply.name, 'dct_request_prod_from_firm', { # select_alt_supply.name, 'dct_request_prod_from_firm', {
key.code: [v.name for v in value] # key.code: [v.name for v in value]
for key, value in # for key, value in
select_alt_supply.dct_request_prod_from_firm.items() # select_alt_supply.dct_request_prod_from_firm.items()
}) # })
self.dct_n_trial_up_product_removed[product] += 1 self.dct_n_trial_up_product_removed[product] += 1
def handle_request(self): def handle_request(self):
print(self.name, 'handle_request') # print(self.name, 'handle_request')
for product, lst_firm in self.dct_request_prod_from_firm.items(): for product, lst_firm in self.dct_request_prod_from_firm.items():
if self.dct_prod_capacity[product] > 0: if self.dct_prod_capacity[product] > 0:
if len(lst_firm) == 0: if len(lst_firm) == 0:
@ -117,7 +117,7 @@ class FirmAgent(ap.Agent):
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[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] 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: if self.code in lst_adj_firm:
lst_firm_connect += firm lst_firm_connect.append(firm)
if len(lst_firm_connect) == 0: 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]
@ -156,9 +156,9 @@ class FirmAgent(ap.Agent):
self.dct_prod_capacity[product] -= 1 self.dct_prod_capacity[product] -= 1
self.dct_request_prod_from_firm[product].remove(down_firm) self.dct_request_prod_from_firm[product].remove(down_firm)
down_firm.a_lst_up_product_removed.remove(product) down_firm.a_lst_up_product_removed.remove(product)
print( # print(
f"{self.name} accept {product.code} request from {down_firm.name}" # f"{self.name} accept {product.code} request from {down_firm.name}"
) # )
def clean_before_trial(self): def clean_before_trial(self):
self.dct_request_prod_from_firm = {} self.dct_request_prod_from_firm = {}

View File

@ -23,7 +23,8 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='setting') parser = argparse.ArgumentParser(description='setting')
parser.add_argument('--exp', type=str, default='test') parser.add_argument('--exp', type=str, default='test')
parser.add_argument('--job', type=int, default='3') parser.add_argument('--job', type=int, default='3')
parser.add_argument('--reset', type=int, default='0') parser.add_argument('--reset_sample', type=int, default='0')
parser.add_argument('--reset_db', type=bool, default=False)
args = parser.parse_args() args = parser.parse_args()
assert args.job >= 1, 'Number of jobs should >= 1' assert args.job >= 1, 'Number of jobs should >= 1'
@ -35,11 +36,11 @@ if __name__ == '__main__':
yaml.dump({'db_name_prefix': args.exp}, file) yaml.dump({'db_name_prefix': args.exp}, file)
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_sample)
# controller_db.reset_db() # controller_db.reset_db()
# force drop # force drop
controller_db.reset_db(force_drop=True) controller_db.reset_db(force_drop=args.reset_db)
controller_db.prepare_list_sample() controller_db.prepare_list_sample()

View File

@ -107,7 +107,6 @@ class Model(ap.Model):
for product in firm_agent.a_lst_product: for product in firm_agent.a_lst_product:
firm_agent.dct_prod_capacity[product] = self.nprandom.integers( firm_agent.dct_prod_capacity[product] = self.nprandom.integers(
firm_agent.revenue_log / 5, firm_agent.revenue_log / 5 + 2) firm_agent.revenue_log / 5, firm_agent.revenue_log / 5 + 2)
# print(firm_agent.name, firm_agent.dct_prod_capacity)
self.firm_network.add_agents([firm_agent], [ag_node]) self.firm_network.add_agents([firm_agent], [ag_node])
self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents) self.a_lst_total_firms = ap.AgentList(self, self.firm_network.agents)
@ -152,12 +151,12 @@ class Model(ap.Model):
self.stop() self.stop()
def step(self): def step(self):
print('\n', '=' * 20, 'step', self.t, '=' * 20) # print('\n', '=' * 20, 'step', self.t, '=' * 20)
print( # print(
'dct_list_remove_firm_prod', { # 'dct_list_remove_firm_prod', {
key.name: value.code # key.name: value.code
for key, value in self.dct_lst_remove_firm_prod.items() # for key, value in self.dct_lst_remove_firm_prod.items()
}) # })
# remove_edge_to_cus_and_cus_up_prod # remove_edge_to_cus_and_cus_up_prod
for firm, a_lst_product in self.dct_lst_remove_firm_prod.items(): for firm, a_lst_product in self.dct_lst_remove_firm_prod.items():
@ -165,7 +164,7 @@ class Model(ap.Model):
firm.remove_edge_to_cus_remove_cus_up_prod(product) firm.remove_edge_to_cus_remove_cus_up_prod(product)
for n_trial in range(self.int_n_max_trial): for n_trial in range(self.int_n_max_trial):
print('=' * 10, 'trial', n_trial, '=' * 10) # print('=' * 10, 'trial', n_trial, '=' * 10)
# seek_alt_supply # seek_alt_supply
# shuffle self.a_lst_total_firms # shuffle self.a_lst_total_firms
self.a_lst_total_firms = self.a_lst_total_firms.shuffle() self.a_lst_total_firms = self.a_lst_total_firms.shuffle()
@ -192,9 +191,9 @@ class Model(ap.Model):
self.dct_lst_disrupt_firm_prod = {} self.dct_lst_disrupt_firm_prod = {}
for firm in self.a_lst_total_firms: for firm in self.a_lst_total_firms:
if len(firm.a_lst_up_product_removed) > 0: if len(firm.a_lst_up_product_removed) > 0:
print(firm.name, 'a_lst_up_product_removed', [ # print(firm.name, 'a_lst_up_product_removed', [
product.code for product in firm.a_lst_up_product_removed # product.code for product in firm.a_lst_up_product_removed
]) # ])
for product in firm.a_lst_product: for product in firm.a_lst_product:
n_up_product_removed = 0 n_up_product_removed = 0
for up_product_removed in firm.a_lst_up_product_removed: for up_product_removed in firm.a_lst_up_product_removed:
@ -232,24 +231,24 @@ class Model(ap.Model):
firm] = ap.AgentList( firm] = ap.AgentList(
self.model, [product]) self.model, [product])
print( # print(
'dct_list_remove_firm_prod', { # 'dct_list_remove_firm_prod', {
key.name: value.code # key.name: value.code
for key, value in self.dct_lst_remove_firm_prod.items() # for key, value in self.dct_lst_remove_firm_prod.items()
}) # })
def end(self): def end(self):
print('/' * 20, 'output', '/' * 20) # print('/' * 20, 'output', '/' * 20)
print('dct_list_remove_firm_prod') # print('dct_list_remove_firm_prod')
for t, dct in self.lst_dct_lst_remove_firm_prod: # for t, dct in self.lst_dct_lst_remove_firm_prod:
for firm, a_lst_product in dct.items(): # for firm, a_lst_product in dct.items():
for product in a_lst_product: # for product in a_lst_product:
print(t, firm.name, product.code) # print(t, firm.name, product.code)
print('dct_lst_disrupt_firm_prod') # print('dct_lst_disrupt_firm_prod')
for t, dct in self.lst_dct_lst_disrupt_firm_prod: # for t, dct in self.lst_dct_lst_disrupt_firm_prod:
for firm, a_lst_product in dct.items(): # for firm, a_lst_product in dct.items():
for product in a_lst_product: # for product in a_lst_product:
print(t, firm.name, product.code) # print(t, firm.name, product.code)
qry_result = db_session.query(Result).filter_by(s_id=self.sample.id) qry_result = db_session.query(Result).filter_by(s_id=self.sample.id)
if qry_result.count() == 0: if qry_result.count() == 0:
@ -288,7 +287,6 @@ class Model(ap.Model):
prog="twopi", prog="twopi",
args="") args="")
node_label = nx.get_node_attributes(self.firm_network.graph, 'Name') node_label = nx.get_node_attributes(self.firm_network.graph, 'Name')
# print(node_label)
node_degree = dict(self.firm_network.graph.out_degree()) node_degree = dict(self.firm_network.graph.out_degree())
node_label = { node_label = {
key: f"{node_label[key]} {node_degree[key]}" key: f"{node_label[key]} {node_degree[key]}"