forked from raindeer/seq2seq_experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_generator.py
218 lines (170 loc) · 6.87 KB
/
program_generator.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
import string
import io
#from contextlib import redirect_stdout
from random import choice, random, randint
from data import encode_sequences, dense_to_one_hot
GO_SYMBOL = 'G'
PAD_SYMBOL = '_'
LETTERS = string.digits + string.ascii_lowercase + ' +-=():<>\n'
SYMBOLS = [GO_SYMBOL, PAD_SYMBOL] + list(LETTERS)
#SYMBOLS = [PAD_SYMBOL, GO_SYMBOL] + list(LETTERS)
SYMBOL_TO_IDX = dict((l, i) for i, l in enumerate(SYMBOLS))
INPUT_SEQ_LEN = 70
OUTPUT_SEQ_LEN = 6
MAX_NUM_LENGTH = 2
MAX_PROGRAM_LENGTH = 3
COMPARATORS = ('<', '>')
OPERATORS = ('+', '-')
VARIABLE_NAMES = list('abcdefgh')
# https://stackoverflow.com/questions/3906232/python-get-the-print-output-in-an-exec-statement
import sys
#import StringIO
import contextlib
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = io.BytesIO()#io.StringIO()
#stdout = StringIO.StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
def execute(program):
with stdoutIO() as ss:# buf, redirect_stdout(buf):
#print (10)
exec(program)
result = ss.getvalue()
print (result)
return result
def if_operation(variables, nesting, difficulty):
compare_variable = choice(list(variables))
comparator = choice(COMPARATORS)
compare_value = random_digit(difficulty)
code = 'if {0}{1}{2}:'.format(compare_variable,
comparator,
compare_value)
nesting += 1
return code, nesting
def assign_operation(variables, nesting, num_len):
variable = choice(VARIABLE_NAMES)
variables.add(variable)
value = random_digit(num_len)
code = '{0}={1}'.format(variable, value)
return code, nesting
def add_or_sub_operation(variables, nesting, num_len):
variable = choice(list(variables))
operator = choice(OPERATORS)
value = random_digit(num_len)
if random() < 0.5:
code = '{0}{1}={2}'.format(variable, operator, value)
else:
variable2 = choice(list(variables))
code = '{0}={1}{2}{3}'.format(variable, variable2, operator, value)
return code, nesting
def print_operation(variables, nesting, num_len):
operator = choice(OPERATORS)
code = 'print({0})'.format(operator.join(list(variables)))
return code, nesting
OPERATIONS = (add_or_sub_operation, if_operation, assign_operation)
def generate_program(num_len, length):
variables = set()
nesting = 0
lines = []
lines.append(assign_operation(variables, nesting, num_len)[0])
if length > 0:
num_lines = randint(1, length)
for i in range(num_lines):
if num_lines <= 1:
operation = add_or_sub_operation
elif nesting == 0:
operation = choice(OPERATIONS)
else:
operation = choice((add_or_sub_operation, if_operation))
code, new_nesting = operation(variables, nesting, num_len)
lines.append(''.join([' '] * nesting) + code)
if nesting == new_nesting and random() < 0.5:
nesting -= 1
nesting = new_nesting
if nesting > 0:
code, new_nesting = add_or_sub_operation(variables, nesting, num_len)
lines.append(''.join([' '] * nesting) + code)
lines.append(print_operation(variables, nesting, num_len)[0])
return '\n'.join(lines)
def random_digit(difficulty):
size = 10 ** randint(1, difficulty)
if difficulty > 1:
return randint(-size, size)
else:
return randint(0, size)
class ProgramGenerator():
def __init__(self, batch_size, program_length=1, num_len=1):
self.program_length = program_length
self.num_len = num_len
self.num_len_was_last_inc = False
self.batch_size = batch_size
def generate_program(self, hash_mod=None):
if hash_mod is None:
program = generate_program(num_len=self.num_len,
length=self.program_length)
else:
program_hash = None
while program_hash != hash_mod:
program = generate_program(num_len=self.num_len,
length=self.program_length)
program_hash = hash(program) % 2
return program
def increase_difficulty(self):
if self.has_max_difficulty():
return
# Alternate between increasing program length and number length
if self.num_len_was_last_inc:
if self.num_len < MAX_PROGRAM_LENGTH:
self.program_length += 1
self.num_len_was_last_inc = False
else:
if self.num_len < MAX_NUM_LENGTH:
self.num_len += 1
self.num_len_was_last_inc = True
print("Difficulty:", self.num_len, self.program_length)
def has_max_difficulty(self):
return self.num_len >= MAX_NUM_LENGTH and \
self.program_length >= MAX_PROGRAM_LENGTH
def difficulty(self):
return (self.num_len, self.program_length)
def next_batch(self, validation=False):
programs = [self.generate_program(hash_mod=0 if validation else 1)
for _ in range(self.batch_size)]
results = []
# Execute the programs to get the targets
for program in programs:
result = execute(program)
results.append(result)
with open('programs.txt', 'w') as f:
for i,program in enumerate(programs):
f.write('PROGRAM:\n')
f.write(program)
f.write('\n')
f.write('RESULT:\n')
f.write(results[i])
f.write('\n\n')
input_sequences = encode_sequences(programs,
symbol_to_idx=SYMBOL_TO_IDX,
sequence_len=INPUT_SEQ_LEN,
#go_symbol=GO_SYMBOL,
pad_symbol=PAD_SYMBOL,
pad_beginning=True,
reverse=True)
input_sequences = dense_to_one_hot(input_sequences,
num_classes=len(SYMBOL_TO_IDX))
target_sequences = encode_sequences(results,
symbol_to_idx=SYMBOL_TO_IDX,
sequence_len=OUTPUT_SEQ_LEN,
go_symbol=None,
pad_beginning=False,
pad_symbol=PAD_SYMBOL)
target_sequences = dense_to_one_hot(target_sequences,
num_classes=len(SYMBOL_TO_IDX))
return input_sequences, target_sequences
if 1:
p = ProgramGenerator(batch_size=25, program_length=1, num_len=1)
p.next_batch()