Skip to content

Commit

Permalink
a cache version, not usable!
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGranite committed Sep 13, 2024
1 parent e75dd02 commit 2c9a1d9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 91 deletions.
2 changes: 1 addition & 1 deletion pycityagent/ac/citizen_actions/trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def Forward(self):
now = self._agent.Scheduler.now
if now.is_set:
'''之前已经将schedule同步至模拟器了'''
if self._agent.Hub != None and self._agent.Brain.Sence.sence_buffer['streetview'] != None and len(self._agent.Brain.Sence.sence_buffer['streetview']) > 0:
if self._agent.Hub != None and self._agent.Brain.Sence.sence_buffer['streetview'] != None:
self._agent.Hub.Update(streetview=self._agent.Brain.Sence.sence_buffer['streetview'])
else:
'''同步schedule至模拟器'''
Expand Down
11 changes: 7 additions & 4 deletions pycityagent/agent_citizen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .urbanllm import UrbanLLM
from .agent import Agent, AgentType
from .image.image import CitizenImage
from pycitysim.apphub import Waypoint
import time

class CitizenAgent(Agent):
"""
Expand Down Expand Up @@ -54,7 +56,7 @@ def __init__(
- Schedule Init
"""

self._history_trajectory: list[list[float]] = []
self._history_trajectory: list[Waypoint] = []

def Bind(self):
"""
Expand Down Expand Up @@ -126,11 +128,12 @@ async def Step(self, log:bool):
await self._simulator.GetTime()
# * 2. 拉取Agent最新状态
resp = await self._client.person_service.GetPerson({'person_id':self._id})
self.base = resp['base']
self.motion = resp['motion']
self.base = resp['person']['base']
self.motion = resp['person']['motion']
longitude = self.motion['position']['longlat_position']['longitude']
latitude = self.motion['position']['longlat_position']['latitude']
self._history_trajectory.append([longitude, latitude])
if self.state == 'trip':
self._history_trajectory.append(Waypoint([longitude, latitude], int(time.time())))
# * 3. Brain工作流
await self._brain.Run()
# * 4. Commond Controller工作流
Expand Down
10 changes: 2 additions & 8 deletions pycityagent/agent_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .image.image import Image
from .ac.hub_actions import PositionUpdate
from .utils import *
from pycitysim.apphub import Waypoint

class FuncAgent(Agent):
"""
Expand All @@ -17,7 +18,6 @@ class FuncAgent(Agent):
def __init__(
self,
name:str,
id: int,
server:str,
soul:UrbanLLM=None,
simulator=None,
Expand All @@ -34,12 +34,6 @@ def __init__(
"""

super().__init__(name, server, AgentType.Func, soul, simulator)
self._id = id
"""
- 智能体Id
- Agent's id
"""

self._image = Image(self)
"""
- Func Agent画像——支持自定义内容
Expand Down Expand Up @@ -69,7 +63,7 @@ def __init__(
"""

self._posUpdate = PositionUpdate(self)
self._history_trajectory:list[list[float]] = []
self._history_trajectory:list[Waypoint] = []

async def set_position_aoi(self, aoi_id:int):
"""
Expand Down
30 changes: 15 additions & 15 deletions pycityagent/brain/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,21 +440,21 @@ async def senceReceive(self, sence):
"""
self.sence = sence
# * social message
social_message = sence['social_messages']
for message in social_message:
print(message)
from_id = message['from']
content = message['message']
if from_id in self.msg_agent_unhandle.keys():
self.msg_agent_unhandle[from_id] += [{
'role': 'user',
'content': content
}]
else:
self.msg_agent_unhandle[from_id] = [{
'role': 'user',
'content': content
}]
# social_message = sence['social_messages']
# for message in social_message:
# print(message)
# from_id = message['from']
# content = message['message']
# if from_id in self.msg_agent_unhandle.keys():
# self.msg_agent_unhandle[from_id] += [{
# 'role': 'user',
# 'content': content
# }]
# else:
# self.msg_agent_unhandle[from_id] = [{
# 'role': 'user',
# 'content': content
# }]
# * user message
self.msg_user_unhandle = sence['user_messages']

Expand Down
2 changes: 1 addition & 1 deletion pycityagent/brain/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ async def schedule_forward(self):
self.schedule_set(True)
else:
# * 基本行程注册
if self.base_schedule_index >=0 and self.base_schedule[self.base_schedule_index].time <= self._agent._simulator.time:
if self.base_schedule_index >=0 and len(self.base_schedule) > 0 and self.base_schedule[self.base_schedule_index].time <= self._agent._simulator.time:
self.now = self.base_schedule[self.base_schedule_index]
if 'aoi_position' in self._agent.motion['position'].keys() and self._agent.motion['position']['aoi_position']['aoi_id'] == self.now.target_id_aoi:
# 直接跳过该行程
Expand Down
48 changes: 11 additions & 37 deletions pycityagent/brain/sence.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async def Sence(self):

# * streetview
if self._sence_contents == None or 'streetview' in self._sence_contents:
if self.enable_streeview:
if self.enable_streeview and 'lane_position' in self._agent.motion['position'].keys():
self.sence_buffer['streetview'] = self.PerceiveStreetView()
else:
self.sence_buffer['streetview'] = None
Expand All @@ -189,15 +189,15 @@ async def Sence(self):
self.sence_buffer['user_messages'] = []

# * agent message
if self._sence_contents == None or 'agent_message' in self._sence_contents:
self.sence_buffer['social_messages'] = await self.PerceiveMessageFromPerson()
# if self._sence_contents == None or 'agent_message' in self._sence_contents:
# self.sence_buffer['social_messages'] = await self.PerceiveMessageFromPerson()

# * 插件感知
if len(self.plugs) > 0:
for plug in self.plugs:
out_key = plug.out
out = plug.user_func(self.sence_buffer)
self.plug_buffer[out_key] = out
# if len(self.plugs) > 0:
# for plug in self.plugs:
# out_key = plug.out
# out = plug.user_func(self.sence_buffer)
# self.plug_buffer[out_key] = out

# * AOI and POI Related
async def PerceiveAoi(self, only_person:bool=False):
Expand Down Expand Up @@ -532,9 +532,6 @@ def PerceiveLane(self, radius:int=None, type:int=3, only_id:bool=False) -> Union
# * StreetView Related
def PerceiveStreetView(
self,
# engine:str="baidumap",
heading:str="front",
save:bool=False,
save_dir:str=None
):
"""
Expand Down Expand Up @@ -562,31 +559,13 @@ def PerceiveStreetView(

coords = [(self._agent.motion['position']['longlat_position']['longitude'], self._agent.motion['position']['longlat_position']['latitude'])]

if heading == "front":
heading_direction = self._agent.motion['direction']
elif heading == "back":
heading_direction += 180
elif heading == "left":
heading_direction += -90
elif heading == "right":
heading_direction += 90
else:
print("Wrong HEADING, Use FRONT")
persp = []
heading_direction = self._agent.motion['direction']
try:
if self._engine == "baidumap":
points = wgs842bd09mc(coords, self._baiduAK)
sv = BaiduStreetView.search(points[0][0], points[0][1])
eq = Equirectangular(sv)
persp.append(eq.get_perspective(120, heading_direction-90, 20, 256, 512))
persp.append(eq.get_perspective(120, heading_direction, 20, 256, 512))
persp.append(eq.get_perspective(120, heading_direction+90, 20, 256, 512))
persp.append(eq.get_perspective(120, heading_direction+180, 20, 256, 512))
if save:
date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sv.panorama.save("{}/{}_{}_panorama_{}.jpg".format(save_dir, self._agent.name, date_time))
for i in range(len(persp)):
persp[i].save("{}/{}_{}_persp_{}.jpg".format(save_dir, self._agent.name, date_time, i))
persp = eq.get_perspective(120, heading_direction, 0, 300, 2100)
return persp
elif self._engine == "googlemap":
sv = GoogleStreetView.search(
Expand All @@ -596,12 +575,7 @@ def PerceiveStreetView(
cache_dir=save_dir
)
eq = Equirectangular(sv)
persp = eq.get_perspective(120, heading_direction, 20, 256, 512)
if save:
date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sv.panorama.save("{}/{}_{}_panorama_{}.jpg".format(save_dir, self._agent.name, date_time))
for i in range(len(persp)):
persp[i].save("{}/{}_{}_persp_{}.jpg".format(save_dir, self._agent.name, date_time, i))
persp = eq.get_perspective(120, heading_direction, 0, 300, 2100)
return persp
except Exception as e:
print(f"Can't get streetview, error message: {e}")
Expand Down
25 changes: 13 additions & 12 deletions pycityagent/hubconnector/hubconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import Optional
import geojson
from geojson import Feature, FeatureCollection, Point, LineString
from pycitysim.apphub import AppHubClient, AgentMessage, UserMessage
from pycitysim.apphub import AppHubClient, AgentMessage, UserMessage, Waypoint
import PIL.Image as Image
import traceback
import time

class HubConnector:
"""
Expand Down Expand Up @@ -63,12 +63,11 @@ def InsertFuncAgent(self):
Insert the Func Agent to AppHub
"""
self._agent_id = self._client.bind_func(
self._agent._id,
self._agent._name,
Image.open(self._profile_img)
)

def Update(self, messages:Optional[list[AgentMessage]]=None, streetview:list[Image.Image]=None, longlat:list[float]=None, pop:str=None):
def Update(self, messages:Optional[list[AgentMessage]]=None, streetview:Image.Image=None, longlat:list[float]=None, pop:str=None):
"""
交互更新
FrontEnd Related Update
Expand Down Expand Up @@ -99,20 +98,22 @@ def Update(self, messages:Optional[list[AgentMessage]]=None, streetview:list[Ima
pop_ = self._agent.agent_name + ": " + pop
if longlat != None:
longlat_ = longlat
t_size = len(self._agent._history_trajectory)
self._agent._history_trajectory.append(Waypoint([longlat[0], longlat[1]], t_size*5000))
else:
longlat_ = [self._agent.motion['position']['longlat_position']['longitude'], self._agent.motion['position']['longlat_position']['latitude']]
if longlat != None:
self._agent._history_trajectory.append(longlat)
path = self._agent._history_trajectory
path_ls = LineString(path)
path_feature = Feature(id='history', geometry=path_ls)
fc = FeatureCollection([path_feature])

if 'direction' in self._agent.motion.keys():
direction = self._agent.motion['direction']
else:
direction = None
self._client.update_agent_map(
agent_id = self._agent_id,
lnglat = longlat_,
geojsons=fc,
direction=direction,
street_view=streetview,
popup=pop_
popup=pop_,
waypoints=self._agent._history_trajectory
)
if messages != None:
self.messageBuffer += messages
Expand Down
30 changes: 18 additions & 12 deletions pycityagent/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ async def GetCitizenAgent(self, name:str, id:int) -> CitizenAgent:
"""
await self.GetTime()
resp = await self._client.person_service.GetPerson({"person_id": id})
base = resp['base']
motion = resp['motion']
print(f"Agent {id}: {resp}")
base = resp['person']['base']
motion = resp['person']['motion']
agent = CitizenAgent(
name,
self.config['simulator']['server'],
Expand All @@ -152,7 +153,7 @@ async def GetCitizenAgent(self, name:str, id:int) -> CitizenAgent:
agent.set_streetview_config(self.config['streetview_request'])
return agent

async def GetFuncAgent(self, id:int, name:str) -> FuncAgent:
async def GetFuncAgent(self, name:str) -> FuncAgent:
"""
获取一个Func Agent模板
Expand All @@ -165,7 +166,6 @@ async def GetFuncAgent(self, id:int, name:str) -> FuncAgent:
"""
agent = FuncAgent(
name,
id+10000000,
self.config['simulator']['server'],
simulator=self
)
Expand All @@ -181,23 +181,29 @@ def InsertCitizenAgent(self, profile):
print("Not Implemented Yet")
pass

def set_poi_matrix(self, row_number:int=12, col_number:int=10, radius:int=10000):
def set_poi_matrix(self, map:dict=None, row_number:int=12, col_number:int=10, radius:int=10000):
"""
初始化pois_matrix
Args:
- map (dict): 地图参数
east, west, north, south
- row_number (int): 行数
- col_number (int): 列数
- radius (int): 搜索半径, 单位m
"""
if map == None:
self.matrix_map = self.map
else:
self.matrix_map = map
print(f"Building Poi searching matrix, Row_number: {row_number}, Col_number: {col_number}, Radius: {radius}m")
self.map_x_gap = (self.map.header['east'] - self.map.header['west']) / col_number
self.map_y_gap = (self.map.header['north'] - self.map.header['south']) / row_number
self.map_x_gap = (self.matrix_map.header['east'] - self.matrix_map.header['west']) / col_number
self.map_y_gap = (self.matrix_map.header['north'] - self.matrix_map.header['south']) / row_number
for i in range(row_number):
self.poi_matrix_centers.append([])
for j in range(col_number):
center_x = self.map.header['west'] + self.map_x_gap*j + self.map_x_gap/2
center_y = self.map.header['south'] + self.map_y_gap*i + self.map_y_gap/2
center_x = self.matrix_map.header['west'] + self.map_x_gap*j + self.map_x_gap/2
center_y = self.matrix_map.header['south'] + self.map_y_gap*i + self.map_y_gap/2
self.poi_matrix_centers[i].append((center_x, center_y))

for pre in self.poi_cate.keys():
Expand Down Expand Up @@ -225,13 +231,13 @@ def get_pois_from_matrix(self, center:Tuple[float, float], prefix:str):
elif prefix not in self.poi_cate.keys():
print(f"Wrong prefix, only {self.poi_cate.keys()} is usable")
return
elif center[0] > self.map.header['east'] or center[0] < self.map.header['west'] or center[1] > self.map.header['north'] or center[1] < self.map.header['south']:
elif center[0] > self.matrix_map.header['east'] or center[0] < self.matrix_map.header['west'] or center[1] > self.matrix_map.header['north'] or center[1] < self.matrix_map.header['south']:
print("Wrong center")
return

# 矩阵匹配
rows = int((center[1]-self.map.header['south'])/self.map_y_gap)
cols = int((center[0]-self.map.header['west'])/self.map_x_gap)
rows = int((center[1]-self.matrix_map.header['south'])/self.map_y_gap)
cols = int((center[0]-self.matrix_map.header['west'])/self.map_x_gap)
pois = self.pois_matrix[prefix][rows][cols]
return pois

Expand Down
2 changes: 1 addition & 1 deletion pycityagent/urbanllm/urbanllm.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def text_request(self, dialog:list[dict], temperature:float=1, max_tokens:int=No
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty
)
return response.choices[0].message.content
return response.choices[0].message.content, response.usage.total_tokens
elif self.config.text['request_type'] == 'qwen':
response = dashscope.Generation.call(
model=self.config.text['model'],
Expand Down

0 comments on commit 2c9a1d9

Please sign in to comment.