affect customer / remove customer up product conditionally

This commit is contained in:
HaoYizhi 2023-03-14 12:53:49 +08:00
parent 4b34dab443
commit 45a406961a
3 changed files with 38 additions and 24 deletions

Binary file not shown.

29
firm.py
View File

@ -1,4 +1,5 @@
import agentpy as ap import agentpy as ap
import math
class FirmAgent(ap.Agent): class FirmAgent(ap.Agent):
@ -29,7 +30,8 @@ class FirmAgent(ap.Agent):
# remove edge # remove edge
# 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
# remove customer up product if does not have alternative / con
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,
@ -40,7 +42,8 @@ class FirmAgent(ap.Agent):
if edge[-1] == remove_product.code if edge[-1] == remove_product.code
] ]
# print(select_edges) # print(select_edges)
if len(select_edges) == 0: p_remove = math.exp(-1 * len(select_edges))
if self.model.nprandom.choice([True, False], p=[p_remove, 1-p_remove]):
print(self.name, remove_product.code, 'affect', customer.name) print(self.name, remove_product.code, 'affect', customer.name)
if remove_product not in \ if remove_product not in \
customer.a_list_up_product_removed: customer.a_list_up_product_removed:
@ -123,17 +126,17 @@ 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]):
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], {
'Product': product.code 'Product': product.code
}) })
]) ])
self.dct_prod_capacity[product] -= 1 self.dct_prod_capacity[product] -= 1
self.dct_request_prod_from_firm[product].remove(down_firm) self.dct_request_prod_from_firm[product].remove(down_firm)
down_firm.a_list_up_product_removed.remove(product) down_firm.a_list_up_product_removed.remove(product)
print(f"{self.name} accept {product.code} request from {down_firm.name}") print(f"{self.name} accept {product.code} request from {down_firm.name}")
def clean_before_trial(self): def clean_before_trial(self):
self.dct_request_prod_from_firm = {} self.dct_request_prod_from_firm = {}

View File

@ -2,24 +2,35 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "name": "stdout",
"text/plain": [ "output_type": "stream",
"1" "text": [
] "85\n",
}, "63\n",
"execution_count": 11, "51\n",
"metadata": {}, "26\n",
"output_type": "execute_result" "30\n",
"4\n",
"7\n",
"1\n",
"17\n",
"81\n"
]
} }
], ],
"source": [ "source": [
"import numpy as np\n", "import numpy as np\n",
"\n", "\n",
"np.random.randint(0.5, 3.5)" "np.random.randint(0.5, 3.5)\n",
"p_remove = 1\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))"
] ]
}, },
{ {
@ -46,7 +57,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.8.8" "version": "3.9.13"
}, },
"orig_nbformat": 4, "orig_nbformat": 4,
"vscode": { "vscode": {