accept order conditionally

This commit is contained in:
HaoYizhi 2023-03-14 17:51:17 +08:00
parent 45a406961a
commit a8d50e1f81
8 changed files with 56 additions and 14 deletions

Binary file not shown.

Binary file not shown.

View File

@ -16,10 +16,7 @@ class ControllerDB:
db_name_prefix: str = None db_name_prefix: str = None
reset_flag: int reset_flag: int
lst_saved_s_id_3: list lst_saved_s_id: list
lst_saved_s_id_1_2: list
# n_sample_1_2: int
def __init__(self, prefix, reset_flag=0): def __init__(self, prefix, reset_flag=0):
with open('conf_experiment.yaml') as yaml_file: with open('conf_experiment.yaml') as yaml_file:
@ -51,8 +48,8 @@ class ControllerDB:
for product_code in row.index[row == 1].to_list(): for product_code in row.index[row == 1].to_list():
dct = {code: [product_code]} dct = {code: [product_code]}
list_dct.append(dct) list_dct.append(dct)
break # break
break # break
# list_dct = [{'140': ['1.4.5.1']}] # list_dct = [{'140': ['1.4.5.1']}]
for idx_exp, dct in enumerate(list_dct): for idx_exp, dct in enumerate(list_dct):
self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'], self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'],

View File

@ -31,7 +31,7 @@ class FirmAgent(ap.Agent):
# print(n1, n2, key, product_code) # print(n1, n2, key, product_code)
self.firm_network.graph.remove_edge(n1, n2, key) self.firm_network.graph.remove_edge(n1, n2, key)
# remove customer up product if does not have alternative / con # remove customer up product conditionally
customer = ap.AgentIter(self.model, n2).to_list()[0] customer = ap.AgentIter(self.model, n2).to_list()[0]
list_in_edges = list( list_in_edges = list(
self.firm_network.graph.in_edges(n2, self.firm_network.graph.in_edges(n2,
@ -126,7 +126,10 @@ class FirmAgent(ap.Agent):
# print(product.code, [firm.name for firm in list_firm]) # print(product.code, [firm.name for firm in list_firm])
def accept_request(self, down_firm, product): def accept_request(self, down_firm, product):
if self.model.nprandom.choice([True, False], p=[0.1, 0.9]): # if self.model.nprandom.choice([True, False], p=[0.1, 0.9]):
lst_f_s = [firm.revenue_log for firm in self.model.a_list_total_firms if product in firm.a_list_product]
p_accept = self.revenue_log / sum(lst_f_s)
if self.model.nprandom.choice([True, False], p=[p_accept, 1-p_accept]):
self.firm_network.graph.add_edges_from([ self.firm_network.graph.add_edges_from([
(self.firm_network.positions[self], (self.firm_network.positions[self],
self.firm_network.positions[down_firm], { self.firm_network.positions[down_firm], {

View File

@ -106,11 +106,20 @@ class Model(ap.Model):
size / sum(list_revenue_log) size / sum(list_revenue_log)
for size in list_revenue_log for size in list_revenue_log
] ]
succ_firm = self.nprandom.choice(list_succ_firms, list_f_same_p = Firm['Code'][Firm[product_code] ==
1].to_list()
list_f_size_same_p = [
G_Firm.nodes[f]['Revenue_Log']
for f in list_f_same_p
]
share = G_Firm.nodes[node]['Revenue_Log'] / sum(list_f_size_same_p)
num_succ_f = round(share * len(list_succ_firms)) if round(share * len(list_succ_firms)) > 0 else 1
list_choose_firm = self.nprandom.choice(list_succ_firms, num_succ_f,
p=list_prob) p=list_prob)
list_choose_firm = list(set(list_choose_firm))
list_added_edges = [(node, succ_firm, { list_added_edges = [(node, succ_firm, {
'Product': product_code 'Product': product_code
})] }) for succ_firm in list_choose_firm]
G_Firm.add_edges_from(list_added_edges) G_Firm.add_edges_from(list_added_edges)
# print('-' * 20) # print('-' * 20)
@ -189,7 +198,7 @@ class Model(ap.Model):
firm.a_list_product_removed.append(product) firm.a_list_product_removed.append(product)
# draw network # draw network
self.draw_network() # self.draw_network()
def update(self): def update(self):
self.a_list_total_firms.clean_before_time_step() self.a_list_total_firms.clean_before_time_step()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 709 KiB

View File

@ -2,7 +2,7 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -20,17 +20,50 @@
"17\n", "17\n",
"81\n" "81\n"
] ]
},
{
"data": {
"text/plain": [
"array([2, 2])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"\n", "\n",
"np.random.randint(0.5, 3.5)\n", "np.random.randint(0.5, 3.5)\n",
"p_remove = 1\n", "p_remove = 0.9\n",
"np.random.choice([True, False], p=[p_remove, 1-p_remove])\n", "np.random.choice([True, False], p=[p_remove, 1-p_remove])\n",
"rng = np.random.default_rng(0)\n", "rng = np.random.default_rng(0)\n",
"for _ in range(10):\n", "for _ in range(10):\n",
" print(rng.integers(0,100))" " print(rng.integers(0,100))\n",
"np.random.choice([1, 2, 3], 2, p=[0.4, 0.4, 0.2])\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"share = 0.8\n",
"list_succ_firms = [1, 1]\n",
"round(share * len(list_succ_firms)) if round(share * len(list_succ_firms)) > 0 else 1"
] ]
}, },
{ {