model product firm
This commit is contained in:
105
model.py
105
model.py
@@ -1,6 +1,7 @@
|
||||
import agentpy as ap
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import random
|
||||
import networkx as nx
|
||||
from firm import FirmAgent
|
||||
from product import ProductAgent
|
||||
@@ -8,8 +9,13 @@ from product import ProductAgent
|
||||
sample = 0
|
||||
seed = 0
|
||||
n_iter = 3
|
||||
dct_list_init_remove_firm_prod = {0: ['1.4.4'], 2: ['1.1.3']}
|
||||
n_max_trial = 2
|
||||
# dct_list_init_remove_firm_prod = {133: ['1.4.4.1'], 2: ['1.1.3']}
|
||||
dct_list_init_remove_firm_prod = {
|
||||
135: ['1.3.2.1'],
|
||||
133: ['1.4.4.1'],
|
||||
2: ['1.1.3']
|
||||
}
|
||||
n_max_trial = 5
|
||||
dct_sample_para = {
|
||||
'sample': sample,
|
||||
'seed': seed,
|
||||
@@ -22,6 +28,7 @@ dct_sample_para = {
|
||||
class Model(ap.Model):
|
||||
def setup(self):
|
||||
self.sample = self.p.sample
|
||||
self.random = random.Random(self.p.seed)
|
||||
self.nprandom = np.random.default_rng(self.p.seed)
|
||||
self.int_n_iter = int(self.p.n_iter)
|
||||
self.int_n_max_trial = int(self.p.n_max_trial)
|
||||
@@ -166,15 +173,10 @@ class Model(ap.Model):
|
||||
f"product {product.code} not in firm {firm.code}"
|
||||
firm.a_list_product_removed.append(product)
|
||||
|
||||
def update(self):
|
||||
# update the firm that is removed
|
||||
self.dct_list_remove_firm_prod = {}
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.a_list_product_removed) > 0:
|
||||
self.dct_list_remove_firm_prod[
|
||||
firm] = firm.a_list_product_removed
|
||||
# print(self.dct_list_remove_firm_prod)
|
||||
# draw network
|
||||
self.draw_network()
|
||||
|
||||
def update(self):
|
||||
# stop simulation if reached terminal number of iteration
|
||||
if self.t == self.int_n_iter or len(
|
||||
self.dct_list_remove_firm_prod) == 0:
|
||||
@@ -182,22 +184,31 @@ class Model(ap.Model):
|
||||
|
||||
def step(self):
|
||||
# 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].shuffle()
|
||||
for key in dct_key_list
|
||||
}
|
||||
# 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].shuffle()
|
||||
# for key in dct_key_list
|
||||
# }
|
||||
# print(self.dct_list_remove_firm_prod)
|
||||
|
||||
print('\n', '=' * 20, 'step', self.t, '=' * 20)
|
||||
print(
|
||||
'dct_list_remove_firm_prod', {
|
||||
key.name: value.code
|
||||
for key, value in self.dct_list_remove_firm_prod.items()
|
||||
})
|
||||
|
||||
# remove_edge_to_cus_and_cus_up_prod
|
||||
for firm, a_list_product in self.dct_list_remove_firm_prod.items():
|
||||
for product in a_list_product:
|
||||
firm.remove_edge_to_cus_and_cus_up_prod(product)
|
||||
firm.remove_edge_to_cus_remove_cus_up_prod(product)
|
||||
|
||||
for n_trial in range(self.int_n_max_trial):
|
||||
print('=' * 20, n_trial, '=' * 20)
|
||||
print('=' * 10, 'trial', n_trial, '=' * 10)
|
||||
# seek_alt_supply
|
||||
# shuffle self.a_list_total_firms
|
||||
self.a_list_total_firms = self.a_list_total_firms.shuffle()
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.a_list_up_product_removed) > 0:
|
||||
# print(firm.name)
|
||||
@@ -205,6 +216,8 @@ class Model(ap.Model):
|
||||
firm.seek_alt_supply()
|
||||
|
||||
# handle_request
|
||||
# shuffle self.a_list_total_firms
|
||||
self.a_list_total_firms = self.a_list_total_firms.shuffle()
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.dct_request_prod_from_firm) > 0:
|
||||
firm.handle_request()
|
||||
@@ -214,6 +227,57 @@ class Model(ap.Model):
|
||||
# do not use:
|
||||
# self.a_list_total_firms.dct_request_prod_from_firm = {} why?
|
||||
|
||||
# based on a_list_up_product_removed,
|
||||
# update a_list_product_disrupted / a_list_product_removed / dct_list_remove_firm_prod
|
||||
self.dct_list_remove_firm_prod = {}
|
||||
for firm in self.a_list_total_firms:
|
||||
if len(firm.a_list_up_product_removed) > 0:
|
||||
for product in firm.a_list_product:
|
||||
n_up_product_removed = 0
|
||||
for up_product_removed in firm.a_list_up_product_removed:
|
||||
if product in up_product_removed.a_successors():
|
||||
n_up_product_removed += 1
|
||||
if n_up_product_removed == 0:
|
||||
continue
|
||||
else:
|
||||
# update a_list_product_disrupted
|
||||
if product not in firm.a_list_product_disrupted:
|
||||
firm.a_list_product_disrupted.append(product)
|
||||
# update a_list_product_removed / dct_list_remove_firm_prod
|
||||
lost_percent = n_up_product_removed / len(
|
||||
product.a_predecessors())
|
||||
list_revenue_log = self.a_list_total_firms.revenue_log
|
||||
std_size = (firm.revenue_log - min(list_revenue_log) +
|
||||
1) / (max(list_revenue_log) -
|
||||
min(list_revenue_log) + 1)
|
||||
p_remove = 1 - std_size * (1 - lost_percent)
|
||||
flag = self.nprandom.choice([1, 0],
|
||||
p=[p_remove, 1 - p_remove])
|
||||
if flag == 1:
|
||||
firm.a_list_product_removed.append(product)
|
||||
# if firm in
|
||||
# self.dct_list_remove_firm_prod[firm] = firm.a_list_product_removed
|
||||
if firm in self.dct_list_remove_firm_prod.keys():
|
||||
self.dct_list_remove_firm_prod[firm].append(
|
||||
product)
|
||||
else:
|
||||
self.dct_list_remove_firm_prod[
|
||||
firm] = ap.AgentList(
|
||||
self.model, [product])
|
||||
|
||||
# # update the firm that is removed
|
||||
# self.dct_list_remove_firm_prod = {}
|
||||
# for firm in self.a_list_total_firms:
|
||||
# if len(firm.a_list_product_removed) > 0:
|
||||
# self.dct_list_remove_firm_prod[
|
||||
# firm] = firm.a_list_product_removed
|
||||
# print(self.dct_list_remove_firm_prod)
|
||||
print(
|
||||
'dct_list_remove_firm_prod', {
|
||||
key.name: value.code
|
||||
for key, value in self.dct_list_remove_firm_prod.items()
|
||||
})
|
||||
|
||||
def end(self):
|
||||
pass
|
||||
|
||||
@@ -252,7 +316,4 @@ class Model(ap.Model):
|
||||
|
||||
|
||||
model = Model(dct_sample_para)
|
||||
model.setup()
|
||||
model.draw_network()
|
||||
model.update()
|
||||
model.step()
|
||||
model.run()
|
||||
|
||||
Reference in New Issue
Block a user