diff --git a/__pycache__/controller_db.cpython-38.pyc b/__pycache__/controller_db.cpython-38.pyc index e33cf51..c395cda 100644 Binary files a/__pycache__/controller_db.cpython-38.pyc and b/__pycache__/controller_db.cpython-38.pyc differ diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index d3d3508..a8701b3 100644 Binary files a/__pycache__/firm.cpython-38.pyc and b/__pycache__/firm.cpython-38.pyc differ diff --git a/__pycache__/model.cpython-38.pyc b/__pycache__/model.cpython-38.pyc index bd07305..ed4194c 100644 Binary files a/__pycache__/model.cpython-38.pyc and b/__pycache__/model.cpython-38.pyc differ diff --git a/controller_db.py b/controller_db.py index 9dacf8e..2cf8691 100644 --- a/controller_db.py +++ b/controller_db.py @@ -16,10 +16,7 @@ class ControllerDB: db_name_prefix: str = None reset_flag: int - lst_saved_s_id_3: list - lst_saved_s_id_1_2: list - - # n_sample_1_2: int + lst_saved_s_id: list def __init__(self, prefix, reset_flag=0): with open('conf_experiment.yaml') as yaml_file: @@ -51,8 +48,8 @@ class ControllerDB: for product_code in row.index[row == 1].to_list(): dct = {code: [product_code]} list_dct.append(dct) - break - break + # break + # break # list_dct = [{'140': ['1.4.5.1']}] for idx_exp, dct in enumerate(list_dct): self.add_experiment_1(idx_exp, self.dct_parameter['n_max_trial'], diff --git a/firm.py b/firm.py index bbda2fa..77e1b64 100644 --- a/firm.py +++ b/firm.py @@ -31,7 +31,7 @@ class FirmAgent(ap.Agent): # print(n1, n2, key, product_code) 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] list_in_edges = list( 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]) 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.positions[self], self.firm_network.positions[down_firm], { diff --git a/model.py b/model.py index 93221d1..e169b59 100644 --- a/model.py +++ b/model.py @@ -106,11 +106,20 @@ class Model(ap.Model): size / sum(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) + list_choose_firm = list(set(list_choose_firm)) list_added_edges = [(node, succ_firm, { 'Product': product_code - })] + }) for succ_firm in list_choose_firm] G_Firm.add_edges_from(list_added_edges) # print('-' * 20) @@ -189,7 +198,7 @@ class Model(ap.Model): firm.a_list_product_removed.append(product) # draw network - self.draw_network() + # self.draw_network() def update(self): self.a_list_total_firms.clean_before_time_step() diff --git a/network.png b/network.png index de4123f..b9607cc 100644 Binary files a/network.png and b/network.png differ diff --git a/test.ipynb b/test.ipynb index 908176b..d3e1e55 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 9, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -20,17 +20,50 @@ "17\n", "81\n" ] + }, + { + "data": { + "text/plain": [ + "array([2, 2])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\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", "rng = np.random.default_rng(0)\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" ] }, {