projects
/
picoclvr.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Update.
[picoclvr.git]
/
rpl.py
diff --git
a/rpl.py
b/rpl.py
index
7c1c96e
..
b51edef
100755
(executable)
--- a/
rpl.py
+++ b/
rpl.py
@@
-58,7
+58,9
@@
rpl_ops = ["add", "min", "max", "swp", "rep", "dup", "del"]
######################################################################
######################################################################
-def generate(nb_starting_values=3, max_input=9, prog_len=6, nb_runs=5):
+def generate(
+ nb_starting_values=3, nb_result_values_max=None, max_input=9, prog_len=6, nb_runs=5
+):
prog_len = (1 + torch.randint(2 * prog_len, (1,))).clamp(max=prog_len).item()
while True:
prog_len = (1 + torch.randint(2 * prog_len, (1,))).clamp(max=prog_len).item()
while True:
@@
-77,7
+79,10
@@
def generate(nb_starting_values=3, max_input=9, prog_len=6, nb_runs=5):
result = result + ["<prog>"] + prog
result = result + ["<end>"]
result = result + ["<prog>"] + prog
result = result + ["<end>"]
- if no_empty_stack:
+
+ if no_empty_stack and (
+ nb_result_values_max is None or len(result_stack) <= nb_result_values_max
+ ):
break
return result
break
return result
@@
-100,15
+105,21
@@
def decompose(seq):
k = 0
while seq[k] == "<input>":
o = next_marker(seq, ["<output>"], start=k + 1)
k = 0
while seq[k] == "<input>":
o = next_marker(seq, ["<output>"], start=k + 1)
+ if o is None:
+ raise ValueError("Missing output markers (should be correct in the prompt)")
e = next_marker(seq, ["<input>", "<prog>"], start=o)
e = next_marker(seq, ["<input>", "<prog>"], start=o)
- if o is None or e is None:
- raise ValueError("Invalid input/output")
+ if e is None:
+ raise ValueError(
+ "Missing input/output markers (should be correct in the prompt)"
+ )
try:
io.append(
([int(x) for x in seq[k + 1 : o]], [int(x) for x in seq[o + 1 : e]])
)
except ValueError:
try:
io.append(
([int(x) for x in seq[k + 1 : o]], [int(x) for x in seq[o + 1 : e]])
)
except ValueError:
- raise ValueError("Invalid input/output")
+ raise ValueError(
+ "Invalid input/output value (should be correct in the prompt)"
+ )
k = e
k = e
@@
-118,9
+129,18
@@
def decompose(seq):
prog = []
else:
prog = seq[k + 1 : e]
prog = []
else:
prog = seq[k + 1 : e]
+ else:
+ raise ValueError("Missing <prog> (it should be in the prompt)")
+
return prog, io
return prog, io
+def stack_distance(target_stack, result_stack):
+ return abs(len(result_stack) - len(target_stack)) + sum(
+ [0 if x == y else 1 for x, y in zip(result_stack, target_stack)]
+ )
+
+
def compute_nb_errors(seq):
prog, io = decompose(seq)
def compute_nb_errors(seq):
prog, io = decompose(seq)
@@
-140,9
+160,7
@@
def compute_nb_errors(seq):
for start_stack, target_stack in io:
result_stack = rpl_exec(prog, start_stack)
nb_total += len(target_stack)
for start_stack, target_stack in io:
result_stack = rpl_exec(prog, start_stack)
nb_total += len(target_stack)
- e = abs(len(result_stack) - len(target_stack)) + sum(
- [0 if x == y else 1 for x, y in zip(result_stack, target_stack)]
- )
+ e = stack_distance(target_stack, result_stack)
nb_errors += e
stacks.append((start_stack, target_stack, result_stack, e == 0))
nb_errors += e
stacks.append((start_stack, target_stack, result_stack, e == 0))