capacity init network

This commit is contained in:
HaoYizhi 2023-02-28 16:56:12 +08:00
parent c29a75177c
commit cf11c0c9bb
5 changed files with 71 additions and 26 deletions

Binary file not shown.

21
firm.py
View File

@ -29,12 +29,23 @@ class FirmAgent(ap.Agent):
# remove edge
# print(n1, n2, key, product_code)
self.firm_network.graph.remove_edge(n1, n2, key)
# remove customer up product
# remove customer up product if does not have alternative
customer = ap.AgentIter(self.model, n2).to_list()[0]
if remove_product not in customer.a_list_up_product_removed:
customer.a_list_up_product_removed.append(remove_product)
customer.dct_num_trial_up_product_removed[
remove_product] = 0
list_in_edges = list(
self.firm_network.graph.in_edges(n2,
keys=True,
data='Product'))
select_edges = [
edge for edge in list_in_edges
if edge[-1] == remove_product.code
]
if len(select_edges) == 0:
if remove_product not in \
customer.a_list_up_product_removed:
customer.a_list_up_product_removed.append(
remove_product)
customer.dct_num_trial_up_product_removed[
remove_product] = 0
# # disrupt customer
# customer = ap.AgentIter(self.model, n2).to_list()[0]

View File

@ -71,20 +71,29 @@ class Model(ap.Model):
G_Firm.nodes[succ_firm]['Revenue_Log']
for succ_firm in list_succ_firms
]
# list_prob = [
# (v - min(list_revenue_log) + 1) /
# (max(list_revenue_log) - min(list_revenue_log) + 1)
# for v in list_revenue_log
# ]
# list_flag = [
# self.nprandom.choice([1, 0], p=[prob, 1 - prob])
# for prob in list_prob
# ]
# # print(list(zip(list_succ_firms,list_flag,list_prob)))
# list_added_edges = [(node, succ_firm, {
# 'Product': product_code
# }) for succ_firm, flag in zip(list_succ_firms, list_flag)
# if flag == 1]
list_prob = [
(v - min(list_revenue_log) + 1) /
(max(list_revenue_log) - min(list_revenue_log) + 1)
for v in list_revenue_log
size / sum(list_revenue_log)
for size in list_revenue_log
]
list_flag = [
self.nprandom.choice([1, 0], p=[prob, 1 - prob])
for prob in list_prob
]
# print(list(zip(list_succ_firms,list_flag,list_prob)))
succ_firm = self.nprandom.choice(list_succ_firms,
p=list_prob)
list_added_edges = [(node, succ_firm, {
'Product': product_code
}) for succ_firm, flag in zip(list_succ_firms, list_flag)
if flag == 1]
})]
G_Firm.add_edges_from(list_added_edges)
# print('-' * 20)
@ -118,16 +127,19 @@ class Model(ap.Model):
code in attr['Product_Code']
for code in self.a_list_total_products.code
]))
# init capacity as the degree of out edges of a specific product
list_out_edges = list(
self.firm_network.graph.out_edges(ag_node,
keys=True,
data='Product'))
# init capacity based on discrete uniform distribution
# list_out_edges = list(
# self.firm_network.graph.out_edges(ag_node,
# keys=True,
# data='Product'))
# for product in firm_agent.a_list_product:
# capacity = len([
# edge for edge in list_out_edges if edge[-1] ==
# product.code])
# firm_agent.dct_prod_capacity[product] = capacity
for product in firm_agent.a_list_product:
capacity = len([
edge for edge in list_out_edges if edge[-1] == product.code
])
firm_agent.dct_prod_capacity[product] = capacity
firm_agent.dct_prod_capacity[product] = self.nprandom.integers(
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])
@ -184,7 +196,7 @@ class Model(ap.Model):
firm.remove_edge_to_cus_and_cus_up_prod(product)
for n_trial in range(self.int_n_max_trial):
print('='*20, n_trial, '='*20)
print('=' * 20, n_trial, '=' * 20)
# seek_alt_supply
for firm in self.a_list_total_firms:
if len(firm.a_list_up_product_removed) > 0:
@ -241,6 +253,6 @@ class Model(ap.Model):
model = Model(dct_sample_para)
model.setup()
model.draw_network()
model.update()
model.step()
# model.draw_network()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

@ -1,5 +1,27 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"np.random.randint(0.5, 3.5)"
]
},
{
"cell_type": "code",
"execution_count": null,