pycityagent 2.0.0a44__cp311-cp311-macosx_11_0_arm64.whl → 2.0.0a46__cp311-cp311-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycityagent/agent.py +5 -4
- pycityagent/message/messager.py +12 -5
- pycityagent/simulation/agentgroup.py +32 -12
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/METADATA +1 -1
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/RECORD +9 -9
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/LICENSE +0 -0
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/WHEEL +0 -0
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/entry_points.txt +0 -0
- {pycityagent-2.0.0a44.dist-info → pycityagent-2.0.0a46.dist-info}/top_level.txt +0 -0
pycityagent/agent.py
CHANGED
@@ -53,7 +53,7 @@ class Agent(ABC):
|
|
53
53
|
type: AgentType = AgentType.Unspecified,
|
54
54
|
llm_client: Optional[LLM] = None,
|
55
55
|
economy_client: Optional[EconomyClient] = None,
|
56
|
-
messager: Optional[Messager] = None,
|
56
|
+
messager: Optional[Messager] = None, # type:ignore
|
57
57
|
simulator: Optional[Simulator] = None,
|
58
58
|
mlflow_client: Optional[MlflowClient] = None,
|
59
59
|
memory: Optional[Memory] = None,
|
@@ -101,7 +101,7 @@ class Agent(ABC):
|
|
101
101
|
del state["_llm_client"]
|
102
102
|
return state
|
103
103
|
|
104
|
-
def set_messager(self, messager: Messager):
|
104
|
+
def set_messager(self, messager: Messager): # type:ignore
|
105
105
|
"""
|
106
106
|
Set the messager of the agent.
|
107
107
|
"""
|
@@ -544,7 +544,7 @@ class CitizenAgent(Agent):
|
|
544
544
|
mlflow_client: Optional[MlflowClient] = None,
|
545
545
|
memory: Optional[Memory] = None,
|
546
546
|
economy_client: Optional[EconomyClient] = None,
|
547
|
-
messager: Optional[Messager] = None,
|
547
|
+
messager: Optional[Messager] = None, # type:ignore
|
548
548
|
avro_file: Optional[dict] = None,
|
549
549
|
) -> None:
|
550
550
|
super().__init__(
|
@@ -659,7 +659,7 @@ class InstitutionAgent(Agent):
|
|
659
659
|
mlflow_client: Optional[MlflowClient] = None,
|
660
660
|
memory: Optional[Memory] = None,
|
661
661
|
economy_client: Optional[EconomyClient] = None,
|
662
|
-
messager: Optional[Messager] = None,
|
662
|
+
messager: Optional[Messager] = None, # type:ignore
|
663
663
|
avro_file: Optional[dict] = None,
|
664
664
|
) -> None:
|
665
665
|
super().__init__(
|
@@ -680,6 +680,7 @@ class InstitutionAgent(Agent):
|
|
680
680
|
await self._bind_to_economy()
|
681
681
|
|
682
682
|
async def _bind_to_economy(self):
|
683
|
+
print("Debug:", self._economy_client, self._has_bound_to_economy)
|
683
684
|
if self._economy_client is None:
|
684
685
|
logger.debug("Economy client is not set")
|
685
686
|
return
|
pycityagent/message/messager.py
CHANGED
@@ -3,15 +3,17 @@ import json
|
|
3
3
|
import logging
|
4
4
|
import math
|
5
5
|
from typing import Any, List, Union
|
6
|
-
|
6
|
+
|
7
7
|
import ray
|
8
|
+
from aiomqtt import Client
|
8
9
|
|
9
10
|
logger = logging.getLogger("pycityagent")
|
10
11
|
|
12
|
+
|
11
13
|
@ray.remote
|
12
14
|
class Messager:
|
13
15
|
def __init__(
|
14
|
-
self, hostname:str, port:int=1883, username=None, password=None, timeout=60
|
16
|
+
self, hostname: str, port: int = 1883, username=None, password=None, timeout=60
|
15
17
|
):
|
16
18
|
self.client = Client(
|
17
19
|
hostname, port=port, username=username, password=password, timeout=timeout
|
@@ -19,7 +21,7 @@ class Messager:
|
|
19
21
|
self.connected = False # 是否已连接标志
|
20
22
|
self.message_queue = asyncio.Queue() # 用于存储接收到的消息
|
21
23
|
self.receive_messages_task = None
|
22
|
-
|
24
|
+
|
23
25
|
async def __aexit__(self, exc_type, exc_value, traceback):
|
24
26
|
await self.stop()
|
25
27
|
|
@@ -45,7 +47,9 @@ class Messager:
|
|
45
47
|
"""检查是否成功连接到 Broker"""
|
46
48
|
return self.connected
|
47
49
|
|
48
|
-
async def subscribe(
|
50
|
+
async def subscribe(
|
51
|
+
self, topics: Union[str, List[str]], agents: Union[Any, List[Any]]
|
52
|
+
):
|
49
53
|
if not self.is_connected():
|
50
54
|
logger.error(
|
51
55
|
f"Cannot subscribe to {topics} because not connected to the Broker."
|
@@ -83,6 +87,9 @@ class Messager:
|
|
83
87
|
logger.error("Cannot start listening because not connected to the Broker.")
|
84
88
|
|
85
89
|
async def stop(self):
|
90
|
+
assert self.receive_messages_task is not None
|
86
91
|
self.receive_messages_task.cancel()
|
87
|
-
await asyncio.gather(
|
92
|
+
await asyncio.gather(
|
93
|
+
self.receive_messages_task, return_exceptions=True
|
94
|
+
)
|
88
95
|
await self.disconnect()
|
@@ -5,7 +5,7 @@ import time
|
|
5
5
|
import uuid
|
6
6
|
from datetime import datetime, timezone
|
7
7
|
from pathlib import Path
|
8
|
-
from typing import Any
|
8
|
+
from typing import Any, Optional
|
9
9
|
from uuid import UUID
|
10
10
|
|
11
11
|
import fastavro
|
@@ -64,7 +64,7 @@ class AgentGroup:
|
|
64
64
|
pass
|
65
65
|
|
66
66
|
self.messager = Messager.remote(
|
67
|
-
hostname=config["simulator_request"]["mqtt"]["server"],
|
67
|
+
hostname=config["simulator_request"]["mqtt"]["server"], # type:ignore
|
68
68
|
port=config["simulator_request"]["mqtt"]["port"],
|
69
69
|
username=config["simulator_request"]["mqtt"].get("username", None),
|
70
70
|
password=config["simulator_request"]["mqtt"].get("password", None),
|
@@ -290,11 +290,21 @@ class AgentGroup:
|
|
290
290
|
|
291
291
|
await asyncio.sleep(0.5)
|
292
292
|
|
293
|
-
async def save_status(
|
293
|
+
async def save_status(
|
294
|
+
self, simulator_day: Optional[int] = None, simulator_t: Optional[int] = None
|
295
|
+
):
|
294
296
|
_statuses_time_list: list[tuple[dict, datetime]] = []
|
295
297
|
if self.enable_avro:
|
296
|
-
logger.debug(f"-----Saving status for group {self._uuid}")
|
298
|
+
logger.debug(f"-----Saving status for group {self._uuid} with Avro")
|
297
299
|
avros = []
|
300
|
+
if simulator_day is not None:
|
301
|
+
_day = simulator_day
|
302
|
+
else:
|
303
|
+
_day = await self.simulator.get_simulator_day()
|
304
|
+
if simulator_t is not None:
|
305
|
+
_t = await self.simulator.get_simulator_second_from_start_of_day()
|
306
|
+
else:
|
307
|
+
_t = simulator_t
|
298
308
|
if not issubclass(type(self.agents[0]), InstitutionAgent):
|
299
309
|
for agent in self.agents:
|
300
310
|
_date_time = datetime.now(timezone.utc)
|
@@ -313,8 +323,8 @@ class AgentGroup:
|
|
313
323
|
action = action["intention"]
|
314
324
|
avro = {
|
315
325
|
"id": agent._uuid,
|
316
|
-
"day":
|
317
|
-
"t":
|
326
|
+
"day": _day,
|
327
|
+
"t": _t,
|
318
328
|
"lng": lng,
|
319
329
|
"lat": lat,
|
320
330
|
"parent_id": parent_id,
|
@@ -378,8 +388,8 @@ class AgentGroup:
|
|
378
388
|
employees = []
|
379
389
|
avro = {
|
380
390
|
"id": agent._uuid,
|
381
|
-
"day":
|
382
|
-
"t":
|
391
|
+
"day": _day,
|
392
|
+
"t": _t,
|
383
393
|
"type": await agent.memory.get("type"),
|
384
394
|
"nominal_gdp": nominal_gdp,
|
385
395
|
"real_gdp": real_gdp,
|
@@ -398,6 +408,15 @@ class AgentGroup:
|
|
398
408
|
with open(self.avro_file["status"], "a+b") as f:
|
399
409
|
fastavro.writer(f, INSTITUTION_STATUS_SCHEMA, avros, codec="snappy")
|
400
410
|
if self.enable_pgsql:
|
411
|
+
logger.debug(f"-----Saving status for group {self._uuid} with PgSQL")
|
412
|
+
if simulator_day is not None:
|
413
|
+
_day = simulator_day
|
414
|
+
else:
|
415
|
+
_day = await self.simulator.get_simulator_day()
|
416
|
+
if simulator_t is not None:
|
417
|
+
_t = await self.simulator.get_simulator_second_from_start_of_day()
|
418
|
+
else:
|
419
|
+
_t = simulator_t
|
401
420
|
# data already acquired from Avro part
|
402
421
|
if len(_statuses_time_list) > 0:
|
403
422
|
for _status_dict, _date_time in _statuses_time_list:
|
@@ -430,8 +449,8 @@ class AgentGroup:
|
|
430
449
|
action = action["intention"]
|
431
450
|
_status_dict = {
|
432
451
|
"id": agent._uuid,
|
433
|
-
"day":
|
434
|
-
"t":
|
452
|
+
"day": _day,
|
453
|
+
"t": _t,
|
435
454
|
"lng": lng,
|
436
455
|
"lat": lat,
|
437
456
|
"parent_id": parent_id,
|
@@ -499,8 +518,8 @@ class AgentGroup:
|
|
499
518
|
employees = []
|
500
519
|
_status_dict = {
|
501
520
|
"id": agent._uuid,
|
502
|
-
"day":
|
503
|
-
"t":
|
521
|
+
"day": _day,
|
522
|
+
"t": _t,
|
504
523
|
"lng": lng,
|
505
524
|
"lat": lat,
|
506
525
|
"parent_id": parent_id,
|
@@ -577,5 +596,6 @@ class AgentGroup:
|
|
577
596
|
|
578
597
|
except Exception as e:
|
579
598
|
import traceback
|
599
|
+
|
580
600
|
logger.error(f"模拟器运行错误: {str(e)}\n{traceback.format_exc()}")
|
581
601
|
raise RuntimeError(str(e)) from e
|
@@ -1,7 +1,13 @@
|
|
1
|
+
pycityagent-2.0.0a46.dist-info/RECORD,,
|
2
|
+
pycityagent-2.0.0a46.dist-info/LICENSE,sha256=n2HPXiupinpyHMnIkbCf3OTYd3KMqbmldu1e7av0CAU,1084
|
3
|
+
pycityagent-2.0.0a46.dist-info/WHEEL,sha256=DQkJubmXvyOE1DlaPsfq9czNuGB2yvzYGbMABjt_JVM,109
|
4
|
+
pycityagent-2.0.0a46.dist-info/entry_points.txt,sha256=BZcne49AAIFv-hawxGnPbblea7X3MtAtoPyDX8L4OC4,132
|
5
|
+
pycityagent-2.0.0a46.dist-info/top_level.txt,sha256=yOmeu6cSXmiUtScu53a3s0p7BGtLMaV0aff83EHCTic,43
|
6
|
+
pycityagent-2.0.0a46.dist-info/METADATA,sha256=h9PjpSlP8GxC4R8wnt_cTYmBWgmIcyn-U0EEppW9xUU,9125
|
1
7
|
pycityagent/pycityagent-sim,sha256=n96jlVZRINlBec5SPOGAdUmeLWMoEKGgoH29iOVJ0wE,34081890
|
2
8
|
pycityagent/__init__.py,sha256=fv0mzNGbHBF6m550yYqnuUpB8iQPWS-7EatYRK7DO4s,693
|
3
9
|
pycityagent/pycityagent-ui,sha256=cHZjqtrQ4Fh4qtRahFNCNbT2DNHLmUexiDAa-72Z3RQ,40333378
|
4
|
-
pycityagent/agent.py,sha256=
|
10
|
+
pycityagent/agent.py,sha256=A40nN1xUJ6AqhoQ4pXt5yAehc15BYHwLMRpIGAmMLLc,30051
|
5
11
|
pycityagent/metrics/mlflow_client.py,sha256=g_tHxWkWTDijtbGL74-HmiYzWVKb1y8-w12QrY9jL30,4449
|
6
12
|
pycityagent/metrics/__init__.py,sha256=X08PaBbGVAd7_PRGLREXWxaqm7nS82WBQpD1zvQzcqc,128
|
7
13
|
pycityagent/metrics/utils/const.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
@@ -23,10 +29,10 @@ pycityagent/memory/faiss_query.py,sha256=Z0JS4udyPYCIzHMq464QtHscnswu35gh9fQptik
|
|
23
29
|
pycityagent/memory/state.py,sha256=TYItiyDtehMEQaSBN7PpNrnNxdDM5jGppr9R9Ufv3kA,5134
|
24
30
|
pycityagent/simulation/simulation.py,sha256=Vrt2hr9AsKG9VEwY90R9mmUR9TxifshUfSYpzW5LOXo,23317
|
25
31
|
pycityagent/simulation/__init__.py,sha256=P5czbcg2d8S0nbbnsQXFIhwzO4CennAhZM8OmKvAeYw,194
|
26
|
-
pycityagent/simulation/agentgroup.py,sha256=
|
32
|
+
pycityagent/simulation/agentgroup.py,sha256=BY6J3zwYvYTlOPycszcKxSYeq3E5LK8qeYzD2oL7VOE,25933
|
27
33
|
pycityagent/simulation/storage/pg.py,sha256=5itxKOkNPlOzN7z2_3oKU1ZK0uLTDugfld8ZkRbD69I,8407
|
28
34
|
pycityagent/message/__init__.py,sha256=TCjazxqb5DVwbTu1fF0sNvaH_EPXVuj2XQ0p6W-QCLU,55
|
29
|
-
pycityagent/message/messager.py,sha256=
|
35
|
+
pycityagent/message/messager.py,sha256=uKXArXOMZGK2fE86XiMtVrI_N0yTLA1EL3KdtCVK5uk,3217
|
30
36
|
pycityagent/utils/avro_schema.py,sha256=AlADbzV8FxiSfvhQhiX9KhrwMjrx0lGT-lED4TI1gJM,4152
|
31
37
|
pycityagent/utils/__init__.py,sha256=xli0FuRffR9Wp4aRcMnlfeDHuGxB8Y5paDjyAt0UoeE,462
|
32
38
|
pycityagent/utils/survey_util.py,sha256=Be9nptmu2JtesFNemPgORh_2GsN7rcDYGQS9Zfvc5OI,2169
|
@@ -73,9 +79,3 @@ pycityagent/environment/interact/interact.py,sha256=ifxPPzuHeqLHIZ_6zvfXMoBOnBsX
|
|
73
79
|
pycityagent/survey/models.py,sha256=YE50UUt5qJ0O_lIUsSY6XFCGUTkJVNu_L1gAhaCJ2fs,3546
|
74
80
|
pycityagent/survey/__init__.py,sha256=rxwou8U9KeFSP7rMzXtmtp2fVFZxK4Trzi-psx9LPIs,153
|
75
81
|
pycityagent/survey/manager.py,sha256=S5IkwTdelsdtZETChRcfCEczzwSrry_Fly9MY4s3rbk,1681
|
76
|
-
pycityagent-2.0.0a44.dist-info/RECORD,,
|
77
|
-
pycityagent-2.0.0a44.dist-info/LICENSE,sha256=n2HPXiupinpyHMnIkbCf3OTYd3KMqbmldu1e7av0CAU,1084
|
78
|
-
pycityagent-2.0.0a44.dist-info/WHEEL,sha256=DQkJubmXvyOE1DlaPsfq9czNuGB2yvzYGbMABjt_JVM,109
|
79
|
-
pycityagent-2.0.0a44.dist-info/entry_points.txt,sha256=BZcne49AAIFv-hawxGnPbblea7X3MtAtoPyDX8L4OC4,132
|
80
|
-
pycityagent-2.0.0a44.dist-info/top_level.txt,sha256=yOmeu6cSXmiUtScu53a3s0p7BGtLMaV0aff83EHCTic,43
|
81
|
-
pycityagent-2.0.0a44.dist-info/METADATA,sha256=x-LLU7rf5y1qTIScRBY7gosF2Cx2Th0Ga380F_SMOpU,9125
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|