-
Notifications
You must be signed in to change notification settings - Fork 21
/
reevo.py
474 lines (403 loc) · 22.1 KB
/
reevo.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from typing import Optional
import logging
import subprocess
import numpy as np
import os
from omegaconf import DictConfig
from utils.utils import *
from utils.llm_client.base import BaseClient
class ReEvo:
def __init__(
self,
cfg: DictConfig,
root_dir: str,
generator_llm: BaseClient,
reflector_llm: Optional[BaseClient] = None,
) -> None:
self.cfg = cfg
self.generator_llm = generator_llm
self.reflector_llm = reflector_llm or generator_llm
self.root_dir = root_dir
self.mutation_rate = cfg.mutation_rate
self.iteration = 0
self.function_evals = 0
self.elitist = None
self.long_term_reflection_str = ""
self.best_obj_overall = None
self.best_code_overall = None
self.best_code_path_overall = None
self.init_prompt()
self.init_population()
def init_prompt(self) -> None:
self.problem = self.cfg.problem.problem_name
self.problem_desc = self.cfg.problem.description
self.problem_size = self.cfg.problem.problem_size
self.func_name = self.cfg.problem.func_name
self.obj_type = self.cfg.problem.obj_type
self.problem_type = self.cfg.problem.problem_type
logging.info("Problem: " + self.problem)
logging.info("Problem description: " + self.problem_desc)
logging.info("Function name: " + self.func_name)
self.prompt_dir = f"{self.root_dir}/prompts"
self.output_file = f"{self.root_dir}/problems/{self.problem}/gpt.py"
# Loading all text prompts
# Problem-specific prompt components
prompt_path_suffix = "_black_box" if self.problem_type == "black_box" else ""
problem_prompt_path = f'{self.prompt_dir}/{self.problem}{prompt_path_suffix}'
self.seed_func = file_to_string(f'{problem_prompt_path}/seed_func.txt')
self.func_signature = file_to_string(f'{problem_prompt_path}/func_signature.txt')
self.func_desc = file_to_string(f'{problem_prompt_path}/func_desc.txt')
if os.path.exists(f'{problem_prompt_path}/external_knowledge.txt'):
self.external_knowledge = file_to_string(f'{problem_prompt_path}/external_knowledge.txt')
self.long_term_reflection_str = self.external_knowledge
else:
self.external_knowledge = ""
# Common prompts
self.system_generator_prompt = file_to_string(f'{self.prompt_dir}/common/system_generator.txt')
self.system_reflector_prompt = file_to_string(f'{self.prompt_dir}/common/system_reflector.txt')
self.user_reflector_st_prompt = file_to_string(f'{self.prompt_dir}/common/user_reflector_st.txt') if self.problem_type != "black_box" else file_to_string(f'{self.prompt_dir}/common/user_reflector_st_black_box.txt') # shrot-term reflection
self.user_reflector_lt_prompt = file_to_string(f'{self.prompt_dir}/common/user_reflector_lt.txt') # long-term reflection
self.crossover_prompt = file_to_string(f'{self.prompt_dir}/common/crossover.txt')
self.mutataion_prompt = file_to_string(f'{self.prompt_dir}/common/mutation.txt')
self.user_generator_prompt = file_to_string(f'{self.prompt_dir}/common/user_generator.txt').format(
func_name=self.func_name,
problem_desc=self.problem_desc,
func_desc=self.func_desc,
)
self.seed_prompt = file_to_string(f'{self.prompt_dir}/common/seed.txt').format(
seed_func=self.seed_func,
func_name=self.func_name,
)
# Flag to print prompts
self.print_crossover_prompt = True # Print crossover prompt for the first iteration
self.print_mutate_prompt = True # Print mutate prompt for the first iteration
self.print_short_term_reflection_prompt = True # Print short-term reflection prompt for the first iteration
self.print_long_term_reflection_prompt = True # Print long-term reflection prompt for the first iteration
def init_population(self) -> None:
# Evaluate the seed function, and set it as Elite
logging.info("Evaluating seed function...")
code = extract_code_from_generator(self.seed_func).replace("v1", "v2")
logging.info("Seed function code: \n" + code)
seed_ind = {
"stdout_filepath": f"problem_iter{self.iteration}_stdout0.txt",
"code_path": f"problem_iter{self.iteration}_code0.py",
"code": code,
"response_id": 0,
}
self.seed_ind = seed_ind
self.population = self.evaluate_population([seed_ind])
# If seed function is invalid, stop
if not self.seed_ind["exec_success"]:
raise RuntimeError(f"Seed function is invalid. Please check the stdout file in {os.getcwd()}.")
self.update_iter()
# Generate responses
system = self.system_generator_prompt
user = self.user_generator_prompt + "\n" + self.seed_prompt + "\n" + self.long_term_reflection_str
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
logging.info("Initial Population Prompt: \nSystem Prompt: \n" + system + "\nUser Prompt: \n" + user)
responses = self.generator_llm.multi_chat_completion([messages], self.cfg.init_pop_size, temperature = self.generator_llm.temperature + 0.3) # Increase the temperature for diverse initial population
population = [self.response_to_individual(response, response_id) for response_id, response in enumerate(responses)]
# Run code and evaluate population
population = self.evaluate_population(population)
# Update iteration
self.population = population
self.update_iter()
def response_to_individual(self, response: str, response_id: int, file_name: str=None) -> dict:
"""
Convert response to individual
"""
# Write response to file
file_name = f"problem_iter{self.iteration}_response{response_id}.txt" if file_name is None else file_name + ".txt"
with open(file_name, 'w') as file:
file.writelines(response + '\n')
code = extract_code_from_generator(response)
# Extract code and description from response
std_out_filepath = f"problem_iter{self.iteration}_stdout{response_id}.txt" if file_name is None else file_name + "_stdout.txt"
individual = {
"stdout_filepath": std_out_filepath,
"code_path": f"problem_iter{self.iteration}_code{response_id}.py",
"code": code,
"response_id": response_id,
}
return individual
def mark_invalid_individual(self, individual: dict, traceback_msg: str) -> dict:
"""
Mark an individual as invalid.
"""
individual["exec_success"] = False
individual["obj"] = float("inf")
individual["traceback_msg"] = traceback_msg
return individual
def evaluate_population(self, population: list[dict]) -> list[float]:
"""
Evaluate population by running code in parallel and computing objective values.
"""
inner_runs = []
# Run code to evaluate
for response_id in range(len(population)):
self.function_evals += 1
# Skip if response is invalid
if population[response_id]["code"] is None:
population[response_id] = self.mark_invalid_individual(population[response_id], "Invalid response!")
inner_runs.append(None)
continue
logging.info(f"Iteration {self.iteration}: Running Code {response_id}")
try:
process = self._run_code(population[response_id], response_id)
inner_runs.append(process)
except Exception as e: # If code execution fails
logging.info(f"Error for response_id {response_id}: {e}")
population[response_id] = self.mark_invalid_individual(population[response_id], str(e))
inner_runs.append(None)
# Update population with objective values
for response_id, inner_run in enumerate(inner_runs):
if inner_run is None: # If code execution fails, skip
continue
try:
inner_run.communicate(timeout=self.cfg.timeout) # Wait for code execution to finish
except subprocess.TimeoutExpired as e:
logging.info(f"Error for response_id {response_id}: {e}")
population[response_id] = self.mark_invalid_individual(population[response_id], str(e))
inner_run.kill()
continue
individual = population[response_id]
stdout_filepath = individual["stdout_filepath"]
with open(stdout_filepath, 'r') as f: # read the stdout file
stdout_str = f.read()
traceback_msg = filter_traceback(stdout_str)
individual = population[response_id]
# Store objective value for each individual
if traceback_msg == '': # If execution has no error
try:
individual["obj"] = float(stdout_str.split('\n')[-2]) if self.obj_type == "min" else -float(stdout_str.split('\n')[-2])
individual["exec_success"] = True
except:
population[response_id] = self.mark_invalid_individual(population[response_id], "Invalid std out / objective value!")
else: # Otherwise, also provide execution traceback error feedback
population[response_id] = self.mark_invalid_individual(population[response_id], traceback_msg)
logging.info(f"Iteration {self.iteration}, response_id {response_id}: Objective value: {individual['obj']}")
return population
def _run_code(self, individual: dict, response_id) -> subprocess.Popen:
"""
Write code into a file and run eval script.
"""
logging.debug(f"Iteration {self.iteration}: Processing Code Run {response_id}")
with open(self.output_file, 'w') as file:
file.writelines(individual["code"] + '\n')
# Execute the python file with flags
with open(individual["stdout_filepath"], 'w') as f:
eval_file_path = f'{self.root_dir}/problems/{self.problem}/eval.py' if self.problem_type != "black_box" else f'{self.root_dir}/problems/{self.problem}/eval_black_box.py'
process = subprocess.Popen(['python', '-u', eval_file_path, f'{self.problem_size}', self.root_dir, "train"],
stdout=f, stderr=f)
block_until_running(individual["stdout_filepath"], log_status=True, iter_num=self.iteration, response_id=response_id)
return process
def update_iter(self) -> None:
"""
Update after each iteration
"""
population = self.population
objs = [individual["obj"] for individual in population]
best_obj, best_sample_idx = min(objs), np.argmin(np.array(objs))
# update best overall
if self.best_obj_overall is None or best_obj < self.best_obj_overall:
self.best_obj_overall = best_obj
self.best_code_overall = population[best_sample_idx]["code"]
self.best_code_path_overall = population[best_sample_idx]["code_path"]
# update elitist
if self.elitist is None or best_obj < self.elitist["obj"]:
self.elitist = population[best_sample_idx]
logging.info(f"Iteration {self.iteration}: Elitist: {self.elitist['obj']}")
logging.info(f"Iteration {self.iteration} finished...")
logging.info(f"Best obj: {self.best_obj_overall}, Best Code Path: {self.best_code_path_overall}")
logging.info(f"Function Evals: {self.function_evals}")
self.iteration += 1
def rank_select(self, population: list[dict]) -> list[dict]:
"""
Rank-based selection, select individuals with probability proportional to their rank.
"""
if self.problem_type == "black_box":
population = [individual for individual in population if individual["exec_success"] and individual["obj"] < self.seed_ind["obj"]]
else:
population = [individual for individual in population if individual["exec_success"]]
if len(population) < 2:
return None
# Sort population by objective value
population = sorted(population, key=lambda x: x["obj"])
ranks = [i for i in range(len(population))]
probs = [1 / (rank + 1 + len(population)) for rank in ranks]
# Normalize probabilities
probs = [prob / sum(probs) for prob in probs]
selected_population = []
trial = 0
while len(selected_population) < 2 * self.cfg.pop_size:
trial += 1
parents = np.random.choice(population, size=2, replace=False, p=probs)
if parents[0]["obj"] != parents[1]["obj"]:
selected_population.extend(parents)
if trial > 1000:
return None
return selected_population
def random_select(self, population: list[dict]) -> list[dict]:
"""
Random selection, select individuals with equal probability.
"""
selected_population = []
# Eliminate invalid individuals
if self.problem_type == "black_box":
population = [individual for individual in population if individual["exec_success"] and individual["obj"] < self.seed_ind["obj"]]
else:
population = [individual for individual in population if individual["exec_success"]]
if len(population) < 2:
return None
trial = 0
while len(selected_population) < 2 * self.cfg.pop_size:
trial += 1
parents = np.random.choice(population, size=2, replace=False)
# If two parents have the same objective value, consider them as identical; otherwise, add them to the selected population
if parents[0]["obj"] != parents[1]["obj"]:
selected_population.extend(parents)
if trial > 1000:
return None
return selected_population
def gen_short_term_reflection_prompt(self, ind1: dict, ind2: dict) -> tuple[list[dict], str, str]:
"""
Short-term reflection before crossovering two individuals.
"""
if ind1["obj"] == ind2["obj"]:
print(ind1["code"], ind2["code"])
raise ValueError("Two individuals to crossover have the same objective value!")
# Determine which individual is better or worse
if ind1["obj"] < ind2["obj"]:
better_ind, worse_ind = ind1, ind2
elif ind1["obj"] > ind2["obj"]:
better_ind, worse_ind = ind2, ind1
worse_code = filter_code(worse_ind["code"])
better_code = filter_code(better_ind["code"])
system = self.system_reflector_prompt
user = self.user_reflector_st_prompt.format(
func_name = self.func_name,
func_desc = self.func_desc,
problem_desc = self.problem_desc,
worse_code=worse_code,
better_code=better_code
)
message = [{"role": "system", "content": system}, {"role": "user", "content": user}]
# Print reflection prompt for the first iteration
if self.print_short_term_reflection_prompt:
logging.info("Short-term Reflection Prompt: \nSystem Prompt: \n" + system + "\nUser Prompt: \n" + user)
self.print_short_term_reflection_prompt = False
return message, worse_code, better_code
def short_term_reflection(self, population: list[dict]) -> tuple[list[list[dict]], list[str], list[str]]:
"""
Short-term reflection before crossovering two individuals.
"""
messages_lst = []
worse_code_lst = []
better_code_lst = []
for i in range(0, len(population), 2):
# Select two individuals
parent_1 = population[i]
parent_2 = population[i+1]
# Short-term reflection
messages, worse_code, better_code = self.gen_short_term_reflection_prompt(parent_1, parent_2)
messages_lst.append(messages)
worse_code_lst.append(worse_code)
better_code_lst.append(better_code)
# Asynchronously generate responses
response_lst = self.reflector_llm.multi_chat_completion(messages_lst)
return response_lst, worse_code_lst, better_code_lst
def long_term_reflection(self, short_term_reflections: list[str]) -> None:
"""
Long-term reflection before mutation.
"""
system = self.system_reflector_prompt
user = self.user_reflector_lt_prompt.format(
problem_desc = self.problem_desc,
prior_reflection = self.long_term_reflection_str,
new_reflection = "\n".join(short_term_reflections),
)
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
if self.print_long_term_reflection_prompt:
logging.info("Long-term Reflection Prompt: \nSystem Prompt: \n" + system + "\nUser Prompt: \n" + user)
self.print_long_term_reflection_prompt = False
self.long_term_reflection_str = self.reflector_llm.multi_chat_completion([messages])[0]
# Write reflections to file
file_name = f"problem_iter{self.iteration}_short_term_reflections.txt"
with open(file_name, 'w') as file:
file.writelines("\n".join(short_term_reflections) + '\n')
file_name = f"problem_iter{self.iteration}_long_term_reflection.txt"
with open(file_name, 'w') as file:
file.writelines(self.long_term_reflection_str + '\n')
def crossover(self, short_term_reflection_tuple: tuple[list[list[dict]], list[str], list[str]]) -> list[dict]:
reflection_content_lst, worse_code_lst, better_code_lst = short_term_reflection_tuple
messages_lst = []
for reflection, worse_code, better_code in zip(reflection_content_lst, worse_code_lst, better_code_lst):
# Crossover
system = self.system_generator_prompt
func_signature0 = self.func_signature.format(version=0)
func_signature1 = self.func_signature.format(version=1)
user = self.crossover_prompt.format(
user_generator = self.user_generator_prompt,
func_signature0 = func_signature0,
func_signature1 = func_signature1,
worse_code = worse_code,
better_code = better_code,
reflection = reflection,
func_name = self.func_name,
)
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
messages_lst.append(messages)
# Print crossover prompt for the first iteration
if self.print_crossover_prompt:
logging.info("Crossover Prompt: \nSystem Prompt: \n" + system + "\nUser Prompt: \n" + user)
self.print_crossover_prompt = False
# Asynchronously generate responses
response_lst = self.generator_llm.multi_chat_completion(messages_lst)
crossed_population = [self.response_to_individual(response, response_id) for response_id, response in enumerate(response_lst)]
assert len(crossed_population) == self.cfg.pop_size
return crossed_population
def mutate(self) -> list[dict]:
"""Elitist-based mutation. We only mutate the best individual to generate n_pop new individuals."""
system = self.system_generator_prompt
func_signature1 = self.func_signature.format(version=1)
user = self.mutataion_prompt.format(
user_generator = self.user_generator_prompt,
reflection = self.long_term_reflection_str + self.external_knowledge,
func_signature1 = func_signature1,
elitist_code = filter_code(self.elitist["code"]),
func_name = self.func_name,
)
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
if self.print_mutate_prompt:
logging.info("Mutation Prompt: \nSystem Prompt: \n" + system + "\nUser Prompt: \n" + user)
self.print_mutate_prompt = False
responses = self.generator_llm.multi_chat_completion([messages], int(self.cfg.pop_size * self.mutation_rate))
population = [self.response_to_individual(response, response_id) for response_id, response in enumerate(responses)]
return population
def evolve(self):
while self.function_evals < self.cfg.max_fe:
# If all individuals are invalid, stop
if all([not individual["exec_success"] for individual in self.population]):
raise RuntimeError(f"All individuals are invalid. Please check the stdout files in {os.getcwd()}.")
# Select
population_to_select = self.population if (self.elitist is None or self.elitist in self.population) else [self.elitist] + self.population # add elitist to population for selection
selected_population = self.random_select(population_to_select)
if selected_population is None:
raise RuntimeError("Selection failed. Please check the population.")
# Short-term reflection
short_term_reflection_tuple = self.short_term_reflection(selected_population) # (response_lst, worse_code_lst, better_code_lst)
# Crossover
crossed_population = self.crossover(short_term_reflection_tuple)
# Evaluate
self.population = self.evaluate_population(crossed_population)
# Update
self.update_iter()
# Long-term reflection
self.long_term_reflection([response for response in short_term_reflection_tuple[0]])
# Mutate
mutated_population = self.mutate()
# Evaluate
self.population.extend(self.evaluate_population(mutated_population))
# Update
self.update_iter()
return self.best_code_overall, self.best_code_path_overall