增加end处理result数据表
This commit is contained in:
parent
f88ed2b5e4
commit
fe5ea1a284
|
@ -66,13 +66,6 @@
|
||||||
</Attribute>
|
</Attribute>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</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">
|
<entry key="\input_data\测试 Firm_amended 170.csv">
|
||||||
<value>
|
<value>
|
||||||
<Attribute>
|
<Attribute>
|
||||||
|
@ -80,13 +73,6 @@
|
||||||
</Attribute>
|
</Attribute>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
<entry key="\input_data\测试 Firm_amended.csv">
|
|
||||||
<value>
|
|
||||||
<Attribute>
|
|
||||||
<option name="separator" value="," />
|
|
||||||
</Attribute>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="\测试数据 companies_devices.csv">
|
<entry key="\测试数据 companies_devices.csv">
|
||||||
<value>
|
<value>
|
||||||
<Attribute>
|
<Attribute>
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,7 +2,6 @@ import os
|
||||||
import datetime
|
import datetime
|
||||||
from mesa import Model
|
from mesa import Model
|
||||||
|
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from my_model import MyModel
|
from my_model import MyModel
|
||||||
|
@ -37,8 +36,9 @@ class Computation:
|
||||||
dct_sample_para = {'sample': sample_random,
|
dct_sample_para = {'sample': sample_random,
|
||||||
'seed': sample_random.seed,
|
'seed': sample_random.seed,
|
||||||
**dct_exp}
|
**dct_exp}
|
||||||
|
|
||||||
model = MyModel(dct_sample_para)
|
model = MyModel(dct_sample_para)
|
||||||
for i in range(100):
|
for i in range(1):
|
||||||
model.step()
|
model.step()
|
||||||
|
print(i, datetime.datetime.now())
|
||||||
|
model.end()
|
||||||
return False
|
return False
|
||||||
|
|
2
main.py
2
main.py
|
@ -40,7 +40,7 @@ def do_computation(c_db):
|
||||||
exp = Computation(c_db)
|
exp = Computation(c_db)
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(random.uniform(0, 5))
|
time.sleep(random.uniform(0, 1))
|
||||||
is_all_done = exp.run()
|
is_all_done = exp.run()
|
||||||
if is_all_done:
|
if is_all_done:
|
||||||
break
|
break
|
||||||
|
|
31
my_model.py
31
my_model.py
|
@ -1,5 +1,6 @@
|
||||||
import json
|
import json
|
||||||
from random import shuffle
|
from random import shuffle
|
||||||
|
import platform
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
@ -9,6 +10,7 @@ from mesa.datacollection import DataCollector
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from firm import FirmAgent
|
from firm import FirmAgent
|
||||||
|
from orm import db_session, Result
|
||||||
from product import ProductAgent
|
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]
|
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
|
t_dct[firm] = disrupted_products
|
||||||
|
|
||||||
# 更新 self.dct_lst_init_disrupt_firm_prod 字典,存储公司及其受干扰的产品
|
# 更新 self.dct_lst_init_disrupt_firm_prod 字典,存储公司及其受干扰的产品
|
||||||
|
@ -478,3 +481,31 @@ class MyModel(Model):
|
||||||
|
|
||||||
# Increment the time step
|
# Increment the time step
|
||||||
self.t += 1
|
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()
|
||||||
|
|
Loading…
Reference in New Issue