forked from tsinghua-fib-lab/AgentMove
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
333 lines (287 loc) · 14.6 KB
/
agent.py
File metadata and controls
333 lines (287 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os
import json
import time
import tqdm
import random
import argparse
import multiprocessing
from datetime import datetime
from processing.data import Dataset
from models.llm_api import LLMWrapper
from models.world_model import SpatialWorld, SocialWorld
from models.personal_memory import Memory
from models.prompts import prompt_generator
from utils import create_dir, extract_json, haversine_distance
from config import PROXY, PROCESSED_DIR
random.seed(100)
class Agent:
def __init__(self, city_name, platform, model_name, spatial_world: SpatialWorld, social_world: SocialWorld, memory_unit: Memory, prompt_type, save_dir, use_int_venue, social_info_type):
self.city_name = city_name
self.platform = platform
self.model_name = model_name
self.llm_model = LLMWrapper(model_name, platform)
self.spatial_world = spatial_world
self.social_world = social_world
self.memory_unit = memory_unit
self.prompt_type = prompt_type
self.save_dir = save_dir
self.use_int_venue = use_int_venue
self.social_info_type = social_info_type
def predict(self, user_id, traj_id, traj_seqs, target_stay, true_value, stay_points):
# spatial world model info
spatial_world_info = self.spatial_world.get_world_info()
# personal memory
memory_info = self.memory_unit.read_memory(user_id, target_stay)
# social world mdoel
last_venue_id = traj_seqs["context_stays"][-1][3]
self_history_points = [x[3] for x in traj_seqs["context_stays"]]
social_world_info = self.social_world.get_world_info(last_venue_id, self_history_points, self.social_info_type)
# prepare candiates fro baseline llmmove
candidate_poi_info = dict()
output = dict()
if self.prompt_type == "llmmove":
all_pois = list(stay_points.keys())
tar_poi = int(true_value['ground_stay'])
# 5 is the limit for candidate POIs within a 5km range of the correct option, and 99 ensures that each prediction matches the original paper's setting, which is 100 options
candidates_within_xkm = 5
for poi in all_pois:
if haversine_distance(stay_points[poi]['pos'][1],stay_points[poi]['pos'][0],traj_seqs['context_pos'][-1][1],traj_seqs['context_pos'][-1][0]) < 5:
candidate_poi_info.update({poi: stay_points[poi]})
if len(candidate_poi_info) > 99:
break
# print(len(candidate_poi_info))
# final prompt
prompt_text = prompt_generator(traj_seqs, self.prompt_type, spatial_world_info, memory_info, social_world_info, candidate_poi_info)
pre_text = self.llm_model.get_response(prompt_text=prompt_text)
# prediction results extraction
if self.prompt_type == "llmmove":
output_json, prediction, reason = extract_json(pre_text, prediction_key="recommendation")
true_venue = tar_poi
else:
output_json, prediction, reason = extract_json(pre_text, prediction_key="prediction")
# true_addr = true_value["ground_addr"]
true_venue = true_value["ground_stay"]
predictions = {
'input': prompt_text,
'output': output_json,
'prediction': prediction,
'reason': reason,
'true': true_venue
}
# Construct the filename with model type and save to file
filename = f"{self.llm_model.model_name}_{self.prompt_type}_{user_id}_{traj_id}_{self.use_int_venue}.json"
file_path = os.path.join(self.save_dir, filename)
with open(file_path, 'w') as f:
json.dump(predictions, f, indent=4)
class Agents:
def __init__(self, platform, model_name, prompt_type, city_name, prompt_num, use_int_venue, dataset: Dataset, workers=1, exp_name="",
traj_min_len=3, traj_max_len=100, sample_one_traj_of_user=False, social_world: SocialWorld=None, social_info_type='address', memory_lens=15,
skip_existing_is_on=False, max_explore_places=5, max_sample_trajectories=1):
self.city_name = city_name
self.platform = platform
self.model_name = model_name
self.prompt_type = prompt_type
self.prompt_num = prompt_num
self.exp_name = exp_name
self.traj_min_len = traj_min_len
self.traj_max_len = traj_max_len
self.sample_one_traj_of_user = sample_one_traj_of_user
self.social_world = social_world
self.social_info_type = social_info_type
self.memory_lens = memory_lens
self.skip_existing_is_on = skip_existing_is_on
self.max_explore_places = max_explore_places
self.max_sample_trajectories = max_sample_trajectories
# test_dictionary, true_locations
test_dataset, self.ground_data = dataset.get_generated_datasets()
self.trajectories = []
self.trajectory_groups = []
self.known_stays = {}
self.use_int_venue = use_int_venue
self.workers = workers
self.save_dir = os.path.join("results/", self.exp_name, self.city_name, "agentmove/", self.model_name, self.prompt_type)
create_dir(self.save_dir)
self.trajs_sampling(test_dataset)
def trajs_sampling(self, test_dataset):
counter = 0
user_list = [str(y) for y in sorted([int(x) for x in list(test_dataset.keys())])]
for user_id in user_list:
v = test_dataset[user_id]
traj_ids = [str(y) for y in sorted([int(x) for x in list(v.keys())])]
if self.city_name in ["Shanghai"]:
if len(traj_ids)==0:
continue
else:
if len(traj_ids) < self.traj_min_len:
continue
if len(traj_ids) > self.traj_max_len:
continue
traj_list = []
traj_count=0
for traj_id in traj_ids:
self.trajectories.append((user_id, traj_id, v[traj_id]))
traj_list.append((user_id, traj_id, v[traj_id]))
counter += 1
traj_count += 1
if self.sample_one_traj_of_user:
break
else:
if traj_count>self.max_sample_trajectories:
break
self.trajectory_groups.append(tuple(traj_list))
self.known_stays[user_id] = v[traj_ids[0]]["historical_stays_long"]
if counter >=self.prompt_num:
print("Data is prepared, Except:{} Real:{} Users:{}".format(self.prompt_num, counter, len(self.trajectory_groups)))
break
if counter < self.prompt_num:
print("Data is not enough, Except:{} Real:{} Users:{}".format(self.prompt_num, counter, len(self.trajectory_groups)))
def skip_existing_file(self, user_id, traj_id, ):
filename = f"{self.model_name}_{self.prompt_type}_{user_id}_{traj_id}_{self.use_int_venue}.json"
file_path = os.path.join(self.save_dir, filename)
return os.path.exists(file_path)
def get_predictions(self):
stay_points = {}
if self.prompt_type == "llmmove":
for traj in tqdm.tqdm(self.trajectories):
for idx, point in enumerate(traj[2]['historical_stays']):
stay_points[int(point[3])] = {'poi': int(point[3]), 'cat': point[2], 'pos': traj[2]['historical_pos'][idx]}
for idx, point in enumerate(traj[2]['context_stays']):
stay_points[int(point[3])] = {'poi': int(point[3]), 'cat': point[2], 'pos': traj[2]['context_pos'][idx]}
for user, trajs in self.ground_data.items():
for traj,info in trajs.items():
if int(info['ground_stay']) not in stay_points:
stay_points[int(info['ground_stay'])] = {'poi': int(info['ground_stay']), 'cat': None, 'pos': info['ground_pos']}
if self.workers==1:
for traj in tqdm.tqdm(self.trajectories):
user_id, cur_context_stays = self.single_prediction(traj, stay_points, None)
self.known_stays[user_id].extend(cur_context_stays)
elif args.sample_one_traj_of_user:
with multiprocessing.Pool(self.workers) as pool:
res = pool.starmap(self.single_prediction, [(traj, stay_points, None) for traj in self.trajectories])
else:
# BUG: Use multiprocessing to speed up the prediction process
raise NotImplementedError("Multiprocessing is not implemented yet, please use single process for now.")
manager = multiprocessing.Manager()
know_stays_parallel = manager.dict()
for u in self.known_stays:
know_stays_parallel[u] = self.known_stays[u]
shared_dict = manager.dict()
shared_dict['counter'] = manager.list([0])
shared_dict['data'] = know_stays_parallel
shared_dict['lock'] = manager.Lock()
with multiprocessing.Pool(min(self.workers, len(self.trajectory_groups))) as pool:
res = pool.starmap(self.single_prediction_group, [(traj_groups, stay_points, shared_dict) for traj_groups in self.trajectory_groups])
def single_prediction_group(self, trajs, shared_dict):
for traj in trajs:
user_id, cur_context_stays = self.single_prediction(traj, shared_dict)
with shared_dict["lock"]:
shared_dict['counter'][0] += 1
shared_dict['data'][user_id].extend(cur_context_stays)
# self.known_stays[user_id].extend(cur_context_stays)
def single_prediction(self, traj, stay_points, shared_dict=None):
user_id, traj_id, traj_seqs = traj
if self.skip_existing_is_on and self.skip_existing_file(user_id=user_id, traj_id=traj_id):
return (user_id, traj_seqs.get('context_stays', []))
# spatial world model
spaital_world = SpatialWorld(
model_name=self.model_name,
platform=self.platform,
city_name=self.city_name,
traj_seqs=traj_seqs,
explore_num=self.max_explore_places
)
# personal memory
cur_context_stays = traj_seqs.get('context_stays', [])
target_stay = traj_seqs.get('target_stay', [])
if self.workers==1 or self.sample_one_traj_of_user:
cur_know_stays = self.known_stays[user_id]
else:
with shared_dict["lock"]:
cur_know_stays = shared_dict['data'][user_id]
memory_unit = Memory(
know_stays=cur_know_stays,
context_stays=cur_context_stays,
memory_lens=self.memory_lens
)
# agent
agent = Agent(
city_name = self.city_name,
platform=self.platform,
model_name=self.model_name,
spatial_world=spaital_world,
social_world=self.social_world,
memory_unit=memory_unit,
prompt_type=self.prompt_type,
save_dir=self.save_dir,
use_int_venue=self.use_int_venue,
social_info_type=self.social_info_type
)
# predict
true_value = self.ground_data[user_id][traj_id]
agent.predict(user_id, traj_id, traj_seqs, target_stay, true_value, stay_points)
return (user_id, cur_context_stays)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--city_name', type=str, default="Shanghai")
parser.add_argument('--model_name', type=str, default="qwen2.5-7b")
parser.add_argument('--platform', type=str, default="SiliconFlow", choices=["SiliconFlow", "OpenAI", "DeepInfra", "vllm", "OpenRouter"])
parser.add_argument('--trajectory_mode', type=str, default="trajectory_split", choices=["trajectory_split"])
parser.add_argument("--historical_stays", type=int, default=15)
parser.add_argument('--context_stays', type=int, default=6)
parser.add_argument('--traj_min_len', type=int, default=3)
parser.add_argument('--traj_max_len', type=int, default=10)
parser.add_argument('--prompt_num', type=int, default=5)
parser.add_argument('--sample_one_traj_of_user', action='store_true',)
parser.add_argument('--max_sample_trajectories', type=int, default=100)
parser.add_argument('--use_int_venue', action='store_true', help='Use int Venue ID')
parser.add_argument('--prompt_type', type=str, default="agent_move_v6", choices=["agent_move_v6", "origin", "llmmob", "llmzs", "llmmove"])
parser.add_argument('--workers', type=int, default=1)
parser.add_argument('--exp_name', type=str, default="")
parser.add_argument('--social_info_type', type=str, default="address")
parser.add_argument('--memory_lens', type=int, default=15)
parser.add_argument('--skip_existing_prediction', action='store_true')
parser.add_argument('--max_neighbors', type=int, default=10)
parser.add_argument('--max_explore_places', type=int, default=5)
args = parser.parse_args()
print("INFO START TIME:{}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print("args:{}".format(args.__dict__))
print("runnning experiment in city@{} model@{} samples:{} type:{}".format(args.city_name, args.model_name, args.prompt_num, args.prompt_type))
start_time = time.time()
dataset = Dataset(
dataset_name=args.city_name,
traj_min_len=2 if args.city_name in ["Shanghai"] else 3,
trajectory_mode=args.trajectory_mode,
historical_stays=args.historical_stays,
context_stays=args.context_stays,
save_dir=PROCESSED_DIR,
use_int_venue=args.use_int_venue,
)
social_world = SocialWorld(
traj_dataset=dataset,
save_dir=PROCESSED_DIR,
city_name=args.city_name,
khop=1,
max_neighbors=args.max_neighbors
)
agents = Agents(
city_name=args.city_name,
platform=args.platform,
model_name=args.model_name,
prompt_type=args.prompt_type,
prompt_num=args.prompt_num,
use_int_venue=args.use_int_venue,
dataset=dataset,
workers=args.workers,
exp_name=args.exp_name,
traj_max_len=args.traj_max_len,
traj_min_len=args.traj_min_len,
sample_one_traj_of_user=args.sample_one_traj_of_user,
social_world=social_world,
social_info_type=args.social_info_type,
memory_lens=args.memory_lens,
skip_existing_is_on=args.skip_existing_prediction,
max_explore_places=args.max_explore_places,
max_sample_trajectories=args.max_sample_trajectories
)
agents.get_predictions()
print("runnning experiment within {} seconds".format(int(time.time()-start_time)))