增加end处理result数据表

This commit is contained in:
Cricial 2024-09-24 19:21:59 +08:00
parent f88ed2b5e4
commit fe5ea1a284
7 changed files with 36 additions and 19 deletions

View File

@ -66,13 +66,6 @@
</Attribute>
</value>
</entry>
<entry key="\input_data\测试 Firm_amended 0-49.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\input_data\测试 Firm_amended 170.csv">
<value>
<Attribute>
@ -80,13 +73,6 @@
</Attribute>
</value>
</entry>
<entry key="\input_data\测试 Firm_amended.csv">
<value>
<Attribute>
<option name="separator" value="," />
</Attribute>
</value>
</entry>
<entry key="\测试数据 companies_devices.csv">
<value>
<Attribute>

Binary file not shown.

Binary file not shown.

View File

@ -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

View File

@ -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

View File

@ -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,6 +294,7 @@ class MyModel(Model):
disrupted_products = [product for product in self.product_agents if product.unique_id in lst_product]
# 将公司与其受干扰的产品映射到字典中
if firm is not None:
t_dct[firm] = disrupted_products
# 更新 self.dct_lst_init_disrupt_firm_prod 字典,存储公司及其受干扰的产品
@ -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()