model + firm

This commit is contained in:
HaoYizhi 2023-02-26 21:58:05 +08:00
parent bf7bb217af
commit f8d91df954
3 changed files with 40 additions and 13 deletions

Binary file not shown.

View File

@ -15,3 +15,8 @@ class FirmAgent(ap.Agent):
self.dct_product_is_disrupted = dict.fromkeys(list_product, False)
self.dct_product_is_removed = dict.fromkeys(list_product, False)
def remove_edge_to_customer_if_removed(self, remove_product):
t = self.firm_network.graph.out_edges(
self.firm_network.positions[self], keys=True, data=True)
print(t)

View File

@ -86,8 +86,10 @@ class Model(ap.Model):
self.firm_network = ap.Network(self, G_Firm)
# print([node.label for node in self.firm_network.nodes])
# print([list(self.firm_network.graph.predecessors(node)) for node in self.firm_network.nodes])
# print([self.firm_network.graph.nodes[node.label]['Name'] for node in self.firm_network.nodes])
# print([list(self.firm_network.graph.predecessors(node))
# for node in self.firm_network.nodes])
# print([self.firm_network.graph.nodes[node.label]['Name']
# for node in self.firm_network.nodes])
# print([v for v in self.firm_network.graph.nodes(data=True)])
# init firm
@ -103,10 +105,9 @@ class Model(ap.Model):
capacity=self.firm_network.graph.out_degree(ag_node))
self.firm_network.add_agents([firm_agent], [ag_node])
self.a_list_total_firms = ap.AgentList(self, self.firm_network.agents)
# print(
# list(
# zip(self.a_list_total_firms.code, self.a_list_total_firms.name,
# self.a_list_total_firms.capacity)))
# print(list(zip(self.a_list_total_firms.code,
# self.a_list_total_firms.name,
# self.a_list_total_firms.capacity)))
# set the initial firm product that are removed
for firm_code, list_product in self.dct_list_remove_firm_prod.items():
@ -118,17 +119,36 @@ class Model(ap.Model):
firm.dct_product_is_removed[product] = True
def update(self):
# Update list of unhappy people
# self.agents.update_happiness()
# self.unhappy = self.agents.select(self.agents.happy == False)
# Update the firm that is removed
self.dct_list_remove_firm_prod = {}
for firm in self.a_list_total_firms:
for product, flag in firm.dct_product_is_removed.items():
if flag is True:
if firm.code in self.dct_list_remove_firm_prod.keys():
self.dct_list_remove_firm_prod[firm.code].append(
product)
else:
self.dct_list_remove_firm_prod[firm.code] = [product]
# print(self.dct_list_remove_firm_prod)
# Stop simulation if reached terminal number of iteration
if self.t == self.int_n_iter:
if self.t == self.int_n_iter or len(
self.dct_list_remove_firm_prod) == 0:
self.stop()
def step(self):
# Move unhappy people to new location
self.unhappy.find_new_home()
# shuffle self.dct_list_remove_firm_prod
dct_key_list = list(self.dct_list_remove_firm_prod.keys())
self.nprandom.shuffle(dct_key_list)
self.dct_list_remove_firm_prod = {
key: self.dct_list_remove_firm_prod[key]
for key in dct_key_list
}
for firm_code, list_product in self.dct_list_remove_firm_prod.items():
firm = self.a_list_total_firms.select(
self.a_list_total_firms.code == firm_code)[0]
for product in list_product:
firm.remove_edge_to_customer_if_removed(product)
def end(self):
pass
@ -169,4 +189,6 @@ class Model(ap.Model):
model = Model(dct_sample_para)
model.setup()
model.draw_network()
model.update()
model.step()
# model.draw_network()