-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesizer.py
100 lines (81 loc) · 3.52 KB
/
synthesizer.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
# To activate env run: $ source activate cs6172_env
# To deactivate env run $ conda deactivate
import openai
from decouple import config
from readability import *
from inputs import inputs
from verifier import verify
from data.features import *
from data.costFuncWithFeatures import featureBasedCost
openai.api_key = config('OPENAI_TOKEN')
# details of the problem we want to synthesize
problem = inputs[1]
def clear_file():
file = open("codeTestOutput.txt", "r+")
file.truncate(0)
file.close()
if __name__ == "__main__":
valid_programs = []
correct_program_count = 0
wrong_program_count = 0
while correct_program_count < 4:
# Clear file as precaution
clear_file()
response = openai.Completion.create(
engine="davinci",
prompt=problem.get("query") + " :\n\npublic static " + problem.get("type"),
temperature=1,
max_tokens=100,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n"]
)
code = response.choices[0].text
passes_tests = verify(code, problem)
if(passes_tests):
correct_program_count += 1
classification = classifier(code)
classification_cost = cost(classification)
feature_cost = featureBasedCost(code)
valid_programs.append((code, classification_cost, feature_cost))
print("CORRECT")
print("\tCode: ", code, "\n\tScore: ", classification_cost, ", ", feature_cost)
else:
wrong_program_count += 1
print("INCORRECT")
print("\tCode: ", code)
print("Correct programs generated: ", correct_program_count)
print("Incorrect programs generated: ", wrong_program_count)
print("===================================================================")
valid_programs.sort(key=lambda a: a[1])
print("Valid Synthesized Programs:")
for program in valid_programs:
code = program[0]
print("\tCode: ", code)
print("\tClassification Score: ", program[1])
print("\Feature Score: ", program[2])
print("\tNumber of brackets: ", numBrackets(code))
print("\tNumber of characters: ", len(code))
print("\tTime complexity: ", timeComplexity(code))
final_classification_cost = 500
final_feature_cost = 500
final_code = ""
final_file = open("synthesizedProgram.java", "w")
for code, classification_cost, feature_cost in valid_programs:
final_file.write("//Program: \n")
final_file.write(code.strip() + "\n")
final_file.write("//Classification Cost: " + str(classification_cost) + "\n")
final_file.write("//Feature Cost: " + str(feature_cost) + "\n")
final_file.write("//===================================================\n")
if (classification_cost < final_classification_cost - 5 or
(classification_cost > final_classification_cost - 5 and
classification_cost < final_classification_cost)):
if feature_cost < final_feature_cost:
final_classification_cost = classification_cost
final_feature_cost = feature_cost
final_code = code
final_file.write("\n\n//Synthesized Program:\n")
final_file.write(final_code.strip() + "\n")
final_file.write("//Program classification cost: " + str(final_classification_cost) + "\n")
final_file.write("//Program feature cost: " + str(final_feature_cost))
final_file.close()