-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathReflexion.py
128 lines (98 loc) · 4.3 KB
/
Reflexion.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
import openai
from utils import get_response
import os
import json
import traceback
import subprocess
prompt_template = """
You are an expert operations research analyst. Your task is to generate Gurobi code to solve the following optimization problem:
{problem_description}
The code should save the final optimal value in a file named 'ref_optimal_value.txt'.
First, reason about the problem and model it. Then, generate gurobipy code to solve it. Put the code between two '=====' lines, like this:
=====
import ...
...
=====
- The code should save the final optimal value in a file named 'ref_optimal_value.txt'.
- Generate the complete code, including the model definition, variables, constraints, objective function, and optimization. It must be runnable.
- Do not generate anything after the second '====='.
- Take a deep breath and think step by step.
"""
reflection_template = """
You are an expert operations research analyst. You have been given the task to generate Gurobi code to solve an optimization problem. You have generated the following Gurobi code:
{generated_code}
You have been updating the code for these errors (the last one is the most recent one):
{feedback}
Based on this feedback, suggest improvements to the Gurobi code.
First, reason about the problem and model it. Then, generate gurobipy code to solve it. Put the code between two '=====' lines, like this:
=====
import ...
...
=====
- The code should save the final optimal value in a file named 'ref_optimal_value.txt'.
- Generate the complete code, including the model definition, variables, constraints, objective function, and optimization. It must be runnable.
- Do not generate anything after the second '====='.
- Take a deep breath and think step by step.
"""
def extract_code(text):
ind1 = text.find("=====")
ind2 = text.find("=====", ind1 + 5)
code = text[ind1 + 5 : ind2].strip()
code = code.replace("```python", "").replace("```", "").strip()
return code
def execute_code(file_path):
try:
# Using Python's subprocess to execute the code as a separate process
result = subprocess.run(
["python", file_path], capture_output=True, text=True, check=True
)
# save result in a file
with open(
os.path.join(os.path.dirname(file_path), "ref_optimal_value.txt"), "w"
) as f:
f.write(f"Optimal Revenue: {result.stdout}\n")
return result.stdout, "Success"
except subprocess.CalledProcessError as e:
return e.stderr, "Error"
def main(problem_description, dir, max_iter=3):
feedback = ""
current_prompt = prompt_template.format(problem_description=problem_description)
print(current_prompt)
print("====================\n\n\n\n")
for iteration in range(max_iter):
response = get_response(current_prompt, model="llama3-70b-8192")
code = extract_code(response)
# Save the code to a file
code_filename = f"generated_code_{iteration}.py"
code_file_path = os.path.join(dir, "ref_codes", code_filename)
with open(code_file_path, "w") as f:
f.write(code)
# Execute the code
output, status = execute_code(code_file_path)
# Save error output (if any)
error_filename = f"error_{iteration}.txt"
if status == "Error":
with open(os.path.join(dir, "ref_codes", error_filename), "w") as f:
f.write(output)
# Print status and update the prompt if needed
if status == "Success":
print("Code executed successfully. Output:\n", output)
break
else:
feedback += "\n" + output
current_prompt = reflection_template.format(
generated_code=code, feedback=feedback
)
print(f"Iteration {iteration + 1}: Error encountered. Debugging...")
else:
print("Max iterations reached with errors remaining.")
if __name__ == "__main__":
dir = "data/nlp4lp/train-dev/1"
if not os.path.exists(os.path.join(dir, "ref_codes")):
os.makedirs(os.path.join(dir, "ref_codes"))
with open(os.path.join(dir, "desc.txt"), "r") as f:
desc = f.read()
with open(os.path.join(dir, "data.json"), "r") as f:
data = json.load(f)
desc = desc + "\n" + json.dumps(data, indent=4)
main(desc, dir, max_iter=3)