From 45a406961a7cff12a56cb3ddd8c240f050cfe874 Mon Sep 17 00:00:00 2001 From: HaoYizhi Date: Tue, 14 Mar 2023 12:53:49 +0800 Subject: [PATCH] affect customer / remove customer up product conditionally --- __pycache__/firm.cpython-38.pyc | Bin 4768 -> 4868 bytes firm.py | 29 +++++++++++++++------------- test.ipynb | 33 +++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index 2264692c096138dbefd4c0e270093fa16cd4901c..d3d35081c09271a554135278ac34edfb47ea1b08 100644 GIT binary patch delta 942 zcmZ9KOKVd>6vt=oD>pah=Fx|-(1)1Rq#~uYRVgj03RS7rN?I3D2_bWnG|kJox31(v~KhZxTzc6-ESbcPz349IOi53IB@=R&YUwdzd84AMb-zsi(apW z_;|eQBVV6-dj|)3KoG%2l*G;~B!#nx2t-^*L^N z^EZo&QM|+QV*c)ym$)pw?rz{t^b+6E>Zsr@_zHf4iJBAY8gbXP5XuQe+Yo5j3h;ce zi3&3D)Ln&OL&B(uxYx1lqG6b~I-C3)jEKaet|pp9H^KxZP;B4ag6*286|$1CVuCF# z8#U9G*b(U=jIS{wvOY@cZu%c*qHN|`IwYOyxG{l#+y<1ibM zI@u5R;Xe4*bP8mDTVN+W#HGd_c-NF^2v0rJ`-g9FZ;uuU$f1pCbPUoeNEgT~hz>Hs z=JW%zZOE8RyI~pNlJ4S?X{EADv_vmJ%sGp^|EbK1X;n%F-J`h#^$yJ(_e$)YzT3O` z0>+1U?(+_e?{SBN_CRTveey*j?Hj2Ki%~ANo+cz^kep$rvb0j7LoDDQ7;mG;M29Wu zCY9Cl9+uO?|9UddkGjXQ{!P5WaNs^puts1AKVfeISIH#YbrA$uDU{L$kVTMx2RWYe wCV?k_(1dQOSPiOC^{CpRdR37^E+*Tqbj~PS)f)Bjm=3dia8!lAg;$Gz0g0N|DF6Tf delta 786 zcmZ9JO-vI}5Xax_SC=ijZP$nv1xakt8Y@vzG(rH4Ac6)(InN#l(wVtZz!=sbjv7ai8wts37ZM02o7N{zHq|BC&?X6J9x)5yiaNg@s@n#PLyZTe!ZZ|HeNfn{MOV;1WE*tD(CvieEx|;34|m zOU<41*3$%1C8$)KBb>)O?)Dh{%`z>(1k*L!&A7SAv`tkgG8NORzSM+QsFY?!0N=R> I49c47Us*M;<^TWy diff --git a/firm.py b/firm.py index 0670e2a..bbda2fa 100644 --- a/firm.py +++ b/firm.py @@ -1,4 +1,5 @@ import agentpy as ap +import math class FirmAgent(ap.Agent): @@ -29,7 +30,8 @@ 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 if does not have alternative + + # remove customer up product if does not have alternative / con customer = ap.AgentIter(self.model, n2).to_list()[0] list_in_edges = list( self.firm_network.graph.in_edges(n2, @@ -40,7 +42,8 @@ class FirmAgent(ap.Agent): if edge[-1] == remove_product.code ] # 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) if remove_product not in \ customer.a_list_up_product_removed: @@ -123,17 +126,17 @@ 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]): - self.firm_network.graph.add_edges_from([ - (self.firm_network.positions[self], - self.firm_network.positions[down_firm], { - 'Product': product.code - }) - ]) - self.dct_prod_capacity[product] -= 1 - self.dct_request_prod_from_firm[product].remove(down_firm) - down_firm.a_list_up_product_removed.remove(product) - print(f"{self.name} accept {product.code} request from {down_firm.name}") + if self.model.nprandom.choice([True, False], p=[0.1, 0.9]): + self.firm_network.graph.add_edges_from([ + (self.firm_network.positions[self], + self.firm_network.positions[down_firm], { + 'Product': product.code + }) + ]) + self.dct_prod_capacity[product] -= 1 + self.dct_request_prod_from_firm[product].remove(down_firm) + down_firm.a_list_up_product_removed.remove(product) + print(f"{self.name} accept {product.code} request from {down_firm.name}") def clean_before_trial(self): self.dct_request_prod_from_firm = {} diff --git a/test.ipynb b/test.ipynb index 825b4bc..908176b 100644 --- a/test.ipynb +++ b/test.ipynb @@ -2,24 +2,35 @@ "cells": [ { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "85\n", + "63\n", + "51\n", + "26\n", + "30\n", + "4\n", + "7\n", + "1\n", + "17\n", + "81\n" + ] } ], "source": [ "import numpy as np\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", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.9.13" }, "orig_nbformat": 4, "vscode": {