-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.py
64 lines (54 loc) · 2.57 KB
/
main.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
import hydra
import logging
import os
from pathlib import Path
import subprocess
from utils.utils import init_client
ROOT_DIR = os.getcwd()
logging.basicConfig(level=logging.INFO)
@hydra.main(version_base=None, config_path="cfg", config_name="config")
def main(cfg):
workspace_dir = Path.cwd()
# Set logging level
logging.info(f"Workspace: {workspace_dir}")
logging.info(f"Project Root: {ROOT_DIR}")
logging.info(f"Using LLM: {cfg.get('model', cfg.llm_client.model)}")
logging.info(f"Using Algorithm: {cfg.algorithm}")
client = init_client(cfg)
# optional clients for operators (ReEvo)
long_ref_llm = hydra.utils.instantiate(cfg.llm_long_ref) if cfg.get("llm_long_ref") else None
short_ref_llm = hydra.utils.instantiate(cfg.llm_short_ref) if cfg.get("llm_short_ref") else None
crossover_llm = hydra.utils.instantiate(cfg.llm_crossover) if cfg.get("llm_crossover") else None
mutation_llm = hydra.utils.instantiate(cfg.llm_mutation) if cfg.get("llm_mutation") else None
if cfg.algorithm == "reevo":
from reevo import ReEvo as LHH
elif cfg.algorithm == "ael":
from baselines.ael.ga import AEL as LHH
elif cfg.algorithm == "eoh":
from baselines.eoh import EoH as LHH
else:
raise NotImplementedError
# Main algorithm
if cfg.algorithm != "reevo":
lhh = LHH(cfg, ROOT_DIR, client)
else:
lhh = LHH(cfg, ROOT_DIR, client, long_reflector_llm=long_ref_llm, short_reflector_llm=short_ref_llm,
crossover_llm=crossover_llm, mutation_llm=mutation_llm)
best_code_overall, best_code_path_overall = lhh.evolve()
logging.info(f"Best Code Overall: {best_code_overall}")
logging.info(f"Best Code Path Overall: {best_code_path_overall}")
# Run validation and redirect stdout to a file "best_code_overall_stdout.txt"
with open(f"{ROOT_DIR}/problems/{cfg.problem.problem_name}/gpt.py", 'w') as file:
file.writelines(best_code_overall + '\n')
test_script = f"{ROOT_DIR}/problems/{cfg.problem.problem_name}/eval.py"
test_script_stdout = "best_code_overall_val_stdout.txt"
logging.info(f"Running validation script...: {test_script}")
with open(test_script_stdout, 'w') as stdout:
subprocess.run(["python", test_script, "-1", ROOT_DIR, "val"], stdout=stdout)
logging.info(f"Validation script finished. Results are saved in {test_script_stdout}.")
# Print the results
with open(test_script_stdout, 'r') as file:
for line in file.readlines():
logging.info(line.strip())
if __name__ == "__main__":
main()