diff --git a/.idea/csv-editor.xml b/.idea/csv-editor.xml index 29b9e5e..c176510 100644 --- a/.idea/csv-editor.xml +++ b/.idea/csv-editor.xml @@ -66,13 +66,6 @@ - - - - - - @@ -80,13 +73,6 @@ - - - - - - diff --git a/__pycache__/computation.cpython-38.pyc b/__pycache__/computation.cpython-38.pyc index 7c87c9d..7c722c7 100644 Binary files a/__pycache__/computation.cpython-38.pyc and b/__pycache__/computation.cpython-38.pyc differ diff --git a/__pycache__/firm.cpython-38.pyc b/__pycache__/firm.cpython-38.pyc index 662cb56..7d3a051 100644 Binary files a/__pycache__/firm.cpython-38.pyc and b/__pycache__/firm.cpython-38.pyc differ diff --git a/__pycache__/my_model.cpython-38.pyc b/__pycache__/my_model.cpython-38.pyc index c0d5712..80403bf 100644 Binary files a/__pycache__/my_model.cpython-38.pyc and b/__pycache__/my_model.cpython-38.pyc differ diff --git a/computation.py b/computation.py index dba191f..1e847ff 100644 --- a/computation.py +++ b/computation.py @@ -2,7 +2,6 @@ import os import datetime from mesa import Model - from typing import TYPE_CHECKING from my_model import MyModel @@ -37,8 +36,9 @@ class Computation: dct_sample_para = {'sample': sample_random, 'seed': sample_random.seed, **dct_exp} - model = MyModel(dct_sample_para) - for i in range(100): + for i in range(1): model.step() + print(i, datetime.datetime.now()) + model.end() return False diff --git a/main.py b/main.py index 902ddfa..2ac6aa7 100644 --- a/main.py +++ b/main.py @@ -40,7 +40,7 @@ def do_computation(c_db): exp = Computation(c_db) while 1: - time.sleep(random.uniform(0, 5)) + time.sleep(random.uniform(0, 1)) is_all_done = exp.run() if is_all_done: break diff --git a/my_model.py b/my_model.py index 6b3d276..415826a 100644 --- a/my_model.py +++ b/my_model.py @@ -1,5 +1,6 @@ import json from random import shuffle +import platform import networkx as nx import pandas as pd @@ -9,6 +10,7 @@ from mesa.datacollection import DataCollector import numpy as np from firm import FirmAgent +from orm import db_session, Result from product import ProductAgent @@ -292,7 +294,8 @@ class MyModel(Model): disrupted_products = [product for product in self.product_agents if product.unique_id in lst_product] # 将公司与其受干扰的产品映射到字典中 - t_dct[firm] = disrupted_products + if firm is not None: + t_dct[firm] = disrupted_products # 更新 self.dct_lst_init_disrupt_firm_prod 字典,存储公司及其受干扰的产品 self.dct_lst_init_disrupt_firm_prod = t_dct @@ -478,3 +481,31 @@ class MyModel(Model): # Increment the time step self.t += 1 + + def end(self): + # print('/' * 20, 'output', '/' * 20) + + qry_result = db_session.query(Result).filter_by(s_id=self.sample.id) + if qry_result.count() == 0: + lst_result_info = [] + for firm in self.company_agents: + for prod, dct_status_supply in \ + firm.dct_prod_up_prod_stat.items(): + lst_is_normal = [stat == 'N' for stat, _ + in dct_status_supply['p_stat']] + if not all(lst_is_normal): + # print(f"{firm.name} {prod.code}:") + # print(dct_status_supply['p_stat']) + for status, ts in dct_status_supply['p_stat']: + db_r = Result(s_id=self.sample.id, + id_firm=firm.unique_id, + id_product=prod.unique_id, + ts=ts, + status=status) + lst_result_info.append(db_r) + db_session.bulk_save_objects(lst_result_info) + db_session.commit() + self.sample.is_done_flag = 1 + self.sample.computer_name = platform.node() + self.sample.stop_t = self.int_stop_ts + db_session.commit()