Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
update to a87
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGranite committed Jan 27, 2025
1 parent ebd65e0 commit 0db8605
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
17 changes: 5 additions & 12 deletions pycityagent/cityagent/blocks/economy_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ async def forward(self, step, context):
for i in range(len(firms_id)):
demand_each_firm.append(int(consumption_each_firm[i]//prices[i]))
real_consumption = await self.economy_client.calculate_consumption(firms_id, agent_id, demand_each_firm)
if real_consumption == -1:
node_id = await self.memory.stream.add_economy(description=f"I failed to buy goods, cause I do not have enough money.")
return {
'success': False,
'evaluation': f"I failed to buy goods, cause I do not have enough money.",
'consumed_time': 0,
'node_id': node_id
}
node_id = await self.memory.stream.add_economy(description=f"I bought some goods, and spent {real_consumption:.1f} on {intention}")
evaluation = {
'success': True,
Expand Down Expand Up @@ -281,8 +273,8 @@ async def forward(self):
work_propensity = await self.memory.status.get('work_propensity')
consumption_propensity = await self.memory.status.get('consumption_propensity')
work_hours = work_propensity * self.num_labor_hours
income = await self.economy_client.get(agent_id, 'income')
income += work_hours * work_skill
# income = await self.economy_client.get(agent_id, 'income')
income = work_hours * work_skill

wealth = await self.economy_client.get(agent_id, 'currency')
wealth += work_hours * work_skill
Expand Down Expand Up @@ -348,7 +340,7 @@ async def forward(self):
except:
self.llm_error += 1

if self.UBI and self.forward_times >= 96 and self.forward_times % 12:
if self.UBI and self.forward_times >= 96 and self.forward_times % 12 == 0:
obs_prompt = f'''
{problem_prompt} {job_prompt} {consumption_prompt} {tax_prompt} {price_prompt}
Your current savings account balance is ${wealth:.2f}. Interest rates, as set by your bank, stand at {interest_rate*100:.2f}%.
Expand All @@ -357,4 +349,5 @@ async def forward(self):
obs_prompt = prettify_document(obs_prompt)
content = await self.llm.atext_request([{'role': 'user', 'content': obs_prompt}], timeout=300)
await self.memory.status.update('ubi_opinion', [content], mode='merge')


self.forward_times += 1
1 change: 1 addition & 0 deletions pycityagent/cityagent/memory_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,6 @@ def memory_config_nbs():
"price": (float, float(np.mean(agent_skills))),
"employees": (list, []),
"employees_agent_id": (list, []),
"forward_times": (int, 0),
}
return EXTRA_ATTRIBUTES, {}, {}
25 changes: 18 additions & 7 deletions pycityagent/cityagent/metrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pycityproto.city.economy.v2.economy_pb2 as economyv2
from pycityagent.cityagent import SocietyAgent
from pycityagent.cityagent import SocietyAgent, NBSAgent

async def mobility_metric(simulation):
# 使用函数属性来存储计数
Expand All @@ -22,20 +22,31 @@ async def mobility_metric(simulation):
async def economy_metric(simulation):
# 使用函数属性来存储计数
if not hasattr(economy_metric, 'step_count'):
economy_metric.step_count = 0
economy_metric.nbs_id = None
economy_metric.nbs_uuid = None

if economy_metric.nbs_id is None:
nbs_id = await simulation.economy_client.get_org_entity_ids(economyv2.ORG_TYPE_NBS)
nbs_id = nbs_id[0]
economy_metric.nbs_id = nbs_id
nbs_uuids = await simulation.filter(types=[NBSAgent])
economy_metric.nbs_uuid = nbs_uuids[0]

nbs_id = await simulation.economy_client.get_org_entity_ids(economyv2.ORG_TYPE_NBS)
nbs_id = nbs_id[0]
try:
real_gdp = await simulation.economy_client.get(nbs_id, 'real_gdp')
except:
real_gdp = []
if len(real_gdp) > 0:
real_gdp = real_gdp[0]
await simulation.mlflow_client.log_metric(key="real_gdp", value=real_gdp, step=economy_metric.step_count)
forward_times_info = await simulation.gather("forward_times", [economy_metric.nbs_uuid])
step_count = 0
for group_gather in forward_times_info:
for agent_uuid, forward_times in group_gather.items():
if agent_uuid == economy_metric.nbs_uuid:
step_count = forward_times
await simulation.mlflow_client.log_metric(key="real_gdp", value=real_gdp, step=step_count)
other_metrics = ['prices', 'working_hours', 'depression', 'consumption_currency', 'income_currency']
other_metrics_names = ['price', 'working_hours', 'depression', 'consumption', 'income']
for metric, metric_name in zip(other_metrics, other_metrics_names):
metric_value = (await simulation.economy_client.get(nbs_id, metric))[-1]
await simulation.mlflow_client.log_metric(key=metric_name, value=metric_value, step=economy_metric.step_count)
economy_metric.step_count += 1
await simulation.mlflow_client.log_metric(key=metric_name, value=metric_value, step=step_count)
4 changes: 3 additions & 1 deletion pycityagent/cityagent/nbsagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ async def forward(self):
await self.economy_client.update(
self._agent_id, "income_currency", [income_currency], mode="merge"
)
print("nbs forward end")
print("nbs forward end")
self.forward_times += 1
await self.memory.status.update("forward_times", self.forward_times)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pycityagent"
version = "2.0.0a86" # change it for each release
version = "2.0.0a87" # change it for each release
description = "LLM-based city environment agent building library"
authors = [
{ name = "Yuwei Yan", email = "[email protected]" },
Expand Down

0 comments on commit 0db8605

Please sign in to comment.