remove firm that denied request from candidate list
This commit is contained in:
parent
aac2607284
commit
7e8980d0c6
|
@ -8,9 +8,13 @@
|
||||||
"name": "Python: Current File",
|
"name": "Python: Current File",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "C:\\Users\\ASUS\\OneDrive\\Project\\ScrAbm\\Dissertation\\IIabm\\analysis.py",
|
"program": "C:\\Users\\ASUS\\OneDrive\\Project\\ScrAbm\\Dissertation\\IIabm\\main.py",
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"justMyCode": true
|
"justMyCode": true,
|
||||||
|
"args": [
|
||||||
|
"--exp", "test",
|
||||||
|
"--reset_db", "True"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -21,7 +21,8 @@ class Computation:
|
||||||
|
|
||||||
# lock this row by update is_done_flag to 0
|
# lock this row by update is_done_flag to 0
|
||||||
self.c_db.lock_the_sample(sample_random)
|
self.c_db.lock_the_sample(sample_random)
|
||||||
print(f"Pid {self.pid} ({str_code}) is running sample {sample_random.id} at {datetime.datetime.now()}")
|
print(
|
||||||
|
f"Pid {self.pid} ({str_code}) is running sample {sample_random.id} at {datetime.datetime.now()}")
|
||||||
|
|
||||||
dct_exp = {column: getattr(sample_random.experiment, column)
|
dct_exp = {column: getattr(sample_random.experiment, column)
|
||||||
for column in sample_random.experiment.__table__.c.keys()}
|
for column in sample_random.experiment.__table__.c.keys()}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
db_name_prefix: not_test
|
db_name_prefix: test
|
||||||
|
|
|
@ -52,7 +52,7 @@ class ControllerDB:
|
||||||
dct = {code: [product_code]}
|
dct = {code: [product_code]}
|
||||||
list_dct.append(dct)
|
list_dct.append(dct)
|
||||||
# list_dct = [{'140': ['1.4.5.1']}]
|
# list_dct = [{'140': ['1.4.5.1']}]
|
||||||
# list_dct = [{'133': ['1.4.4.1']}]
|
list_dct = [{'133': ['1.4.4.1']}]
|
||||||
# list_dct = [{'2': ['1.1.3']}]
|
# list_dct = [{'2': ['1.1.3']}]
|
||||||
# list_dct = [{'135': ['1.3.2.1']}]
|
# list_dct = [{'135': ['1.3.2.1']}]
|
||||||
# list_dct = [{'79': ['2.1.3.4']}]
|
# list_dct = [{'79': ['2.1.3.4']}]
|
||||||
|
|
65
firm.py
65
firm.py
|
@ -18,7 +18,8 @@ class FirmAgent(ap.Agent):
|
||||||
self.a_lst_product_disrupted = ap.AgentList(self.model, [])
|
self.a_lst_product_disrupted = ap.AgentList(self.model, [])
|
||||||
self.a_lst_product_removed = ap.AgentList(self.model, [])
|
self.a_lst_product_removed = ap.AgentList(self.model, [])
|
||||||
|
|
||||||
self.dct_n_trial_up_product_removed = {}
|
self.dct_n_trial_up_prod_removed = {}
|
||||||
|
self.dct_cand_alt_supply_up_prod_removed = {}
|
||||||
self.dct_request_prod_from_firm = {}
|
self.dct_request_prod_from_firm = {}
|
||||||
|
|
||||||
def remove_edge_to_cus_remove_cus_up_prod(self, remove_product):
|
def remove_edge_to_cus_remove_cus_up_prod(self, remove_product):
|
||||||
|
@ -50,32 +51,39 @@ class FirmAgent(ap.Agent):
|
||||||
customer.a_lst_up_product_removed:
|
customer.a_lst_up_product_removed:
|
||||||
customer.a_lst_up_product_removed.append(
|
customer.a_lst_up_product_removed.append(
|
||||||
remove_product)
|
remove_product)
|
||||||
customer.dct_n_trial_up_product_removed[
|
customer.dct_n_trial_up_prod_removed[
|
||||||
remove_product] = 0
|
remove_product] = 0
|
||||||
|
|
||||||
def seek_alt_supply(self):
|
def seek_alt_supply(self):
|
||||||
for product in self.a_lst_up_product_removed:
|
for product in self.a_lst_up_product_removed:
|
||||||
# print(f"{self.name} seek alt supply for {product.code}")
|
print(f"{self.name} seek alt supply for {product.code}")
|
||||||
if self.dct_n_trial_up_product_removed[
|
if self.dct_n_trial_up_prod_removed[
|
||||||
product] <= self.model.int_n_max_trial:
|
product] <= self.model.int_n_max_trial:
|
||||||
|
if self.dct_n_trial_up_prod_removed[product] == 0:
|
||||||
# select a list of candidate firm that has the product
|
# select a list of candidate firm that has the product
|
||||||
candidate_alt_supply = self.model.a_lst_total_firms.select([
|
self.dct_cand_alt_supply_up_prod_removed[product] = \
|
||||||
|
self.model.a_lst_total_firms.select([
|
||||||
product in firm.a_lst_product
|
product in firm.a_lst_product
|
||||||
and product not in firm.a_lst_product_removed
|
and product not in firm.a_lst_product_removed
|
||||||
for firm in self.model.a_lst_total_firms
|
for firm in self.model.a_lst_total_firms
|
||||||
])
|
])
|
||||||
if not candidate_alt_supply:
|
if not self.dct_cand_alt_supply_up_prod_removed[product]:
|
||||||
continue
|
continue
|
||||||
# select based on size
|
# select based on size
|
||||||
lst_prob = [
|
lst_prob = [
|
||||||
size / sum(candidate_alt_supply.revenue_log)
|
size /
|
||||||
for size in candidate_alt_supply.revenue_log
|
sum(self.dct_cand_alt_supply_up_prod_removed[
|
||||||
|
product].revenue_log)
|
||||||
|
for size in self.dct_cand_alt_supply_up_prod_removed[
|
||||||
|
product].revenue_log
|
||||||
]
|
]
|
||||||
select_alt_supply = self.model.nprandom.choice(
|
select_alt_supply = self.model.nprandom.choice(
|
||||||
candidate_alt_supply, p=lst_prob)
|
self.dct_cand_alt_supply_up_prod_removed[product],
|
||||||
# print(
|
p=lst_prob)
|
||||||
# f"{self.name} selct alt supply for {product.code} from {select_alt_supply.name}"
|
print(
|
||||||
# )
|
f"{self.name} selct alt supply for {product.code} "
|
||||||
|
f"from {select_alt_supply.name}"
|
||||||
|
)
|
||||||
assert product in select_alt_supply.a_lst_product, \
|
assert product in select_alt_supply.a_lst_product, \
|
||||||
f"{select_alt_supply} \
|
f"{select_alt_supply} \
|
||||||
does not produce requested product {product}"
|
does not produce requested product {product}"
|
||||||
|
@ -95,10 +103,10 @@ class FirmAgent(ap.Agent):
|
||||||
# select_alt_supply.dct_request_prod_from_firm.items()
|
# select_alt_supply.dct_request_prod_from_firm.items()
|
||||||
# })
|
# })
|
||||||
|
|
||||||
self.dct_n_trial_up_product_removed[product] += 1
|
self.dct_n_trial_up_prod_removed[product] += 1
|
||||||
|
|
||||||
def handle_request(self):
|
def handle_request(self):
|
||||||
# print(self.name, 'handle_request')
|
print(self.name, 'handle_request')
|
||||||
for product, lst_firm in self.dct_request_prod_from_firm.items():
|
for product, lst_firm in self.dct_request_prod_from_firm.items():
|
||||||
if self.dct_prod_capacity[product] > 0:
|
if self.dct_prod_capacity[product] > 0:
|
||||||
if len(lst_firm) == 0:
|
if len(lst_firm) == 0:
|
||||||
|
@ -114,8 +122,12 @@ class FirmAgent(ap.Agent):
|
||||||
in_edges = self.model.firm_network.graph.in_edges(
|
in_edges = self.model.firm_network.graph.in_edges(
|
||||||
self.model.firm_network.positions[firm], keys=True)
|
self.model.firm_network.positions[firm], keys=True)
|
||||||
lst_adj_firm = []
|
lst_adj_firm = []
|
||||||
lst_adj_firm += [ap.AgentIter(self.model, edge[1]).to_list()[0].code for edge in out_edges]
|
lst_adj_firm += \
|
||||||
lst_adj_firm += [ap.AgentIter(self.model, edge[0]).to_list()[0].code for edge in in_edges]
|
[ap.AgentIter(self.model, edge[1]).to_list()[
|
||||||
|
0].code for edge in out_edges]
|
||||||
|
lst_adj_firm += \
|
||||||
|
[ap.AgentIter(self.model, edge[0]).to_list()[
|
||||||
|
0].code for edge in in_edges]
|
||||||
if self.code in lst_adj_firm:
|
if self.code in lst_adj_firm:
|
||||||
lst_firm_connect.append(firm)
|
lst_firm_connect.append(firm)
|
||||||
if len(lst_firm_connect) == 0:
|
if len(lst_firm_connect) == 0:
|
||||||
|
@ -124,18 +136,20 @@ class FirmAgent(ap.Agent):
|
||||||
lst_prob = [
|
lst_prob = [
|
||||||
size / sum(lst_firm_size) for size in lst_firm_size
|
size / sum(lst_firm_size) for size in lst_firm_size
|
||||||
]
|
]
|
||||||
select_customer = self.model.nprandom.choice(lst_firm,
|
select_customer = \
|
||||||
p=lst_prob)
|
self.model.nprandom.choice(lst_firm, p=lst_prob)
|
||||||
self.accept_request(select_customer, product)
|
self.accept_request(select_customer, product)
|
||||||
elif len(lst_firm_connect) == 1:
|
elif len(lst_firm_connect) == 1:
|
||||||
self.accept_request(lst_firm_connect[0], product)
|
self.accept_request(lst_firm_connect[0], product)
|
||||||
elif len(lst_firm_connect) > 1:
|
elif len(lst_firm_connect) > 1:
|
||||||
# handling based on size of firm that has connection
|
# handling based on size of firm that has connection
|
||||||
lst_firm_size = [firm.revenue_log for firm in lst_firm_connect]
|
lst_firm_size = [
|
||||||
|
firm.revenue_log for firm in lst_firm_connect]
|
||||||
lst_prob = [
|
lst_prob = [
|
||||||
size / sum(lst_firm_size) for size in lst_firm_size
|
size / sum(lst_firm_size) for size in lst_firm_size
|
||||||
]
|
]
|
||||||
select_customer = self.model.nprandom.choice(lst_firm_connect,
|
select_customer = \
|
||||||
|
self.model.nprandom.choice(lst_firm_connect,
|
||||||
p=lst_prob)
|
p=lst_prob)
|
||||||
self.accept_request(select_customer, product)
|
self.accept_request(select_customer, product)
|
||||||
|
|
||||||
|
@ -156,13 +170,16 @@ class FirmAgent(ap.Agent):
|
||||||
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_lst_up_product_removed.remove(product)
|
down_firm.a_lst_up_product_removed.remove(product)
|
||||||
# print(
|
print(
|
||||||
# f"{self.name} accept {product.code} request from {down_firm.name}"
|
f"{self.name} accept {product.code} request "
|
||||||
# )
|
f"from {down_firm.name}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
down_firm.dct_cand_alt_supply_up_prod_removed[product].remove(self)
|
||||||
|
|
||||||
def clean_before_trial(self):
|
def clean_before_trial(self):
|
||||||
self.dct_request_prod_from_firm = {}
|
self.dct_request_prod_from_firm = {}
|
||||||
|
|
||||||
def clean_before_time_step(self):
|
def clean_before_time_step(self):
|
||||||
self.dct_n_trial_up_product_removed = {}
|
self.dct_n_trial_up_prod_removed = {}
|
||||||
self.a_lst_up_product_removed = ap.AgentList(self.model, [])
|
self.a_lst_up_product_removed = ap.AgentList(self.model, [])
|
||||||
|
|
21
orm.py
21
orm.py
|
@ -23,9 +23,11 @@ with open('conf_db_prefix.yaml') as file:
|
||||||
|
|
||||||
str_login = 'mysql://{}:{}@{}:{}/{}'.format(dct_conf_db['user_name'], dct_conf_db['password'],
|
str_login = 'mysql://{}:{}@{}:{}/{}'.format(dct_conf_db['user_name'], dct_conf_db['password'],
|
||||||
dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name'])
|
dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name'])
|
||||||
print('DB is {}:{}/{}'.format(dct_conf_db['address'], dct_conf_db['port'], dct_conf_db['db_name']))
|
print('DB is {}:{}/{}'.format(dct_conf_db['address'],
|
||||||
|
dct_conf_db['port'], dct_conf_db['db_name']))
|
||||||
|
|
||||||
engine = create_engine(str_login, poolclass=NullPool) # must be null pool to avoid connection lost error
|
# must be null pool to avoid connection lost error
|
||||||
|
engine = create_engine(str_login, poolclass=NullPool)
|
||||||
ins = inspect(engine)
|
ins = inspect(engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
@ -48,7 +50,8 @@ class Experiment(Base):
|
||||||
dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False)
|
dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False)
|
||||||
g_bom = Column(Text(4294000000), nullable=False)
|
g_bom = Column(Text(4294000000), nullable=False)
|
||||||
|
|
||||||
sample = relationship('Sample', back_populates='experiment', lazy='dynamic')
|
sample = relationship(
|
||||||
|
'Sample', back_populates='experiment', lazy='dynamic')
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<Experiment: {self.id}>'
|
return f'<Experiment: {self.id}>'
|
||||||
|
@ -57,18 +60,21 @@ class Experiment(Base):
|
||||||
class Sample(Base):
|
class Sample(Base):
|
||||||
__tablename__ = f"{db_name_prefix}_sample"
|
__tablename__ = f"{db_name_prefix}_sample"
|
||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
e_id = Column(Integer, ForeignKey('{}.id'.format(f"{db_name_prefix}_experiment")), nullable=False)
|
e_id = Column(Integer, ForeignKey('{}.id'.format(
|
||||||
|
f"{db_name_prefix}_experiment")), nullable=False)
|
||||||
|
|
||||||
idx_sample = Column(Integer, nullable=False)
|
idx_sample = Column(Integer, nullable=False)
|
||||||
seed = Column(BigInteger, nullable=False)
|
seed = Column(BigInteger, nullable=False)
|
||||||
is_done_flag = Column(Integer, nullable=False) # -1, waiting; 0, running; 1, done
|
# -1, waiting; 0, running; 1, done
|
||||||
|
is_done_flag = Column(Integer, nullable=False)
|
||||||
computer_name = Column(String(64), nullable=True)
|
computer_name = Column(String(64), nullable=True)
|
||||||
ts_done = Column(DateTime(timezone=True), onupdate=func.now())
|
ts_done = Column(DateTime(timezone=True), onupdate=func.now())
|
||||||
stop_t = Column(Integer, nullable=True)
|
stop_t = Column(Integer, nullable=True)
|
||||||
|
|
||||||
g_firm = Column(Text(4294000000), nullable=True)
|
g_firm = Column(Text(4294000000), nullable=True)
|
||||||
|
|
||||||
experiment = relationship('Experiment', back_populates='sample', uselist=False)
|
experiment = relationship(
|
||||||
|
'Experiment', back_populates='sample', uselist=False)
|
||||||
result = relationship('Result', back_populates='sample', lazy='dynamic')
|
result = relationship('Result', back_populates='sample', lazy='dynamic')
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -78,7 +84,8 @@ class Sample(Base):
|
||||||
class Result(Base):
|
class Result(Base):
|
||||||
__tablename__ = f"{db_name_prefix}_result"
|
__tablename__ = f"{db_name_prefix}_result"
|
||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
s_id = Column(Integer, ForeignKey('{}.id'.format(f"{db_name_prefix}_sample")), nullable=False)
|
s_id = Column(Integer, ForeignKey('{}.id'.format(
|
||||||
|
f"{db_name_prefix}_sample")), nullable=False)
|
||||||
|
|
||||||
id_firm = Column(String(10), nullable=False)
|
id_firm = Column(String(10), nullable=False)
|
||||||
id_product = Column(String(10), nullable=False)
|
id_product = Column(String(10), nullable=False)
|
||||||
|
|
Loading…
Reference in New Issue