-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
299 lines (239 loc) · 9.43 KB
/
utils.py
File metadata and controls
299 lines (239 loc) · 9.43 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
import re
import json
import io
import sys
import logging
import gurobipy as gp
from gurobipy import GRB
from contextlib import redirect_stdout, redirect_stderr
class Agent:
def __init__(self, system_message, client, llm="gpt-3.5-turbo", dialog_round=20, **kwargs):
self.system_message = [
{"role": "system", "content": system_message}
]
self.dialog_round = dialog_round
self.messages = []
self.client = client
self.kwargs = kwargs
self.llm = llm
def generate_reply(self, prompt = None, tem = 0.) -> str:
self.messages.append({
"role": "user",
"content": prompt,
})
new_messages = []
new_messages.extend(self.system_message)
if len(self.messages) > self.dialog_round:
self.messages = self.messages[-self.dialog_round:]
new_messages.extend(self.messages)
#logging,info(new_messages)
completion = self.client.chat.completions.create(
model=self.llm,
messages=new_messages,
temperature=tem,
)
assistant_message = completion.choices[0].message.content
self.messages.append({"role": "system", "content": assistant_message})
return assistant_message
def clear(self):
self.messages = []
return
def system_update(self,system):
self.system_message = system
def get_model_stats_from_code(code):
"""
执行代码并从模型中获取统计信息
"""
try:
# 捕获输出,避免打印到控制台
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
# 创建全局命名空间
exec_globals = {
'gp': gp,
'GRB': GRB,
}
# 添加所有内置函数
exec_globals.update(__builtins__ if isinstance(__builtins__, dict) else {})
# # 创建本地命名空间
# exec_locals = {}
# 执行代码
partial_code = ""
last_line = ""
bogus_context = None
def code_split(code):
code_lines = code.split('\n')
code_list = []
i = 0
while i < len(code_lines):
if code_lines[i] == '' or (code_lines[i][0] != ' ' and code_lines[i][0] != ']' and code_lines[i][0] != ')' and code_lines[i][0] != '}' and code_lines[i][0:4] != 'elif' and code_lines[i][0:4] != 'else'):
code_list.append(code_lines[i])
else:
code_block = code_list.pop()
code_block += '\n' + code_lines[i]
code_list.append(code_block)
i += 1
# code_list.append('\n'.join(code_lines[-17:]))
return code_list
code_list = code_split(code)
for i in range(len(code_list)):
last_line = code_list[i]
partial_code += last_line + "\n"
bogus_context = last_line
exec(last_line, exec_globals, exec_globals)
# 从本地或全局命名空间中获取模型
model = exec_globals.get('model')
if model is None:
# 在全局命名空间中查找
for key, value in exec_globals.items():
if isinstance(value, gp.Model):
model = value
break
if model is None:
raise ValueError("未能找到Gurobi模型")
# 更新模型以计算统计信息
model.update()
# 获取统计信息
stats = {
'variable_number': int(model.NumVars),
'binary_variable_number': int(model.NumBinVars),
'integer_variable_number': int(model.NumIntVars - model.NumBinVars),
'constraint_number': int(model.NumConstrs),
'nonzero_number': int(model.NumNZs),
'runtime_seconds': float(model.Runtime)
}
# 清理模型
model.dispose()
return stats
except Exception as e:
return {
'variable_number': 0,
'binary_variable_number': 0,
'integer_variable_number': 0,
'constraint_number': 0,
'nonzero_number': 0,
'runtime_seconds': 0,
'error': f"{type(e).__name__}: {str(e)}"
}
def replace_fraction(match):
x, y = map(float, match.groups())
return str(x / y)
def extract_json(output):
if "```json" in output:
output = output[output.find("```json") + 7 :]
output = output[: output.rfind("```")]
# go back until the last character is a }
#print(output[-1])
while output[-1] != "}":
output = output[:-1]
# go forward until the first character is a {
while output[0] != "{":
output = output[1:]
# if there are '$' in the output, remove them
if "$" in output:
output = output.replace("$", "")
if "\text" in output:
output = output.replace("\text", "\\text")
output = output.replace("\\", "\\\\")
output = output.replace("\\", "\\\\")
output = output.replace("\\\\quad", "\\\\")
#output = output.replace("\n", "")
output = re.sub(r'(\d+)/(\d+)', replace_fraction, output)
#print(type(output))
return json.loads(output)
#return output
def extract_and_execute_code(text):
# Possible start and end markers
code_start_markers = ["```python", "```Python", "```"]
code_end_marker = "```"
# Find python part
code_start_index = -1
code_start_marker_used = None
for marker in code_start_markers:
code_start_index = text.lower().find(marker.lower())
if code_start_index != -1:
code_start_marker_used = marker
break
# If find code
if code_start_index != -1:
# Try to find the end point
code_end_index = text.find(code_end_marker, code_start_index + len(code_start_marker_used))
# If not, we assume the code is appended to the end of the text
if code_end_index == -1:
code_end_index = len(text)
# Extract the code
code_str = text[code_start_index + len(code_start_marker_used):code_end_index].strip()
# Clean up the code string
for marker in code_start_markers:
code_str = code_str.replace(marker, "")
code_str = code_str.replace(code_end_marker, "").strip()
# Create a stream
old_stdout = sys.stdout
new_stdout = io.StringIO()
sys.stdout = new_stdout
# Execute the code
try:
exec(code_str, globals())
except Exception as e:
# Primary output
sys.stdout = old_stdout
return f"An error occurred: {e}", code_str
# Extract the output
sys.stdout = old_stdout
return new_stdout.getvalue(), code_str
else:
return "No Python code found in the provided string.", None
def extract_single_objective(gurobi_output):
optimal_objective_match = re.search(r"Optimal objective\s+([\d\.e\+\-]+)", gurobi_output)
#print(optimal_objective_match)
if optimal_objective_match:
optimal_objective_value = optimal_objective_match.group(1)
return optimal_objective_value
else:
best_objective_match = re.search(r"Best objective ([\d\.e\+\-]+)", gurobi_output)
#print(best_objective_match)
if best_objective_match :
best_objective_value =best_objective_match.group(1) #float()
if best_objective_value != "-":
return best_objective_value
def extract_objective(gurobi_output):
match = extract_single_objective(gurobi_output)
if match:
value = float(match)
else:
value = 'infeasible'
return value
def bool_accuracy(value,truth):
acc = False
#print("value is")
#print(value)
#print("truth is")
#print(truth)
if value == truth:
acc = True
elif value!='infeasible' and truth !='infeasible' :
truth = float(truth)
if truth == 0:
acc = (abs(value-truth) <= 0.05)
elif (abs(value-truth)/truth)<= 0.05:
acc = True
return acc
def write_template(problem_type, template, template_path):
with open(template_path, "a") as file:
json_str = json.dumps({"problem": problem_type, "template": template})
file.write(json_str + '\n')
def correct_template(problem_type, template, template_path):
with open(template_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
for i, line in enumerate(lines):
line_data = json.loads(line)
if line_data['problem'] == problem_type:
line_data['template'] = template
lines[i] = json.dumps(line_data) + '\n'
with open(template_path, 'w', encoding='utf-8') as file:
file.writelines(lines)
if __name__ == '__main__':
with open("optmath_dataset/dataset_first_100_with_stats.json", 'r', encoding='utf-8') as file:
lines = json.load(file)
print(f"Total lines: {len(lines)}")