grammar.enumerate_at_depth seems to have bug
HuangHam opened this issue · comments
I tried this: a simple right branching grammar for binary sequences
grammar = Grammar(start='S')
# basic symbols
grammar.add_rule('S', '"0"', None, 0.5)
grammar.add_rule('S', '"1"', None, 0.5)
# concatenation
grammar.add_rule('S', 'concat_', ['"0"', 'S'], 1)
grammar.add_rule('S', 'concat_', ['"1"', 'S'], 1)
grammar.add_rule('S', 'concat_', ['"101"', 'S'], 1)
trees = grammar.enumerate_at_depth(1)
for t in trees:
print(t)
get error
`RuntimeError Traceback (most recent call last)
/Users/hh7792/Desktop/Knitting/Knitting.ipynb Cell 6 line 1
16 trees = grammar.enumerate_at_depth(1)
18 # t = eval(str(formula))
---> 19 for t in trees:
20 print(t)
File ~/Desktop/Knitting/LOTlib3/Grammar.py:297, in Grammar.enumerate_at_depth(self, d, nt, leaves)
294 # BVRuleContextManager here makes us remove the rule BEFORE yielding,
295 # or else this will be incorrect. Wasteful but necessary.
296 with BVRuleContextManager(self, fn, recurse_up=False):
--> 297 yieldfn.args = next(myiter)
298 for a in yieldfn.argFunctionNodes():
299 # Update parents
...
--> 316 state[idx] = next(iterators[idx])
317 break # break the idx loop (which would process "carries")
318 except StopIteration:
RuntimeError: generator raised StopIteration`
Oh weird, apparently they changed how StopIteration works. I will need to check the rest of the library, but the current fix should fix enumeration.
Hi Steve, unfortunately, I think there is another issue with it. Here is my code that got the error:
grammar = Grammar(start='S')
grammar.add_rule('R', 'rep_', ['T', 'N'], 1) #repeat or alternate One bit
grammar.add_rule('R', 'alt_', ['T', 'N'], 1)
grammar.add_rule('T', '"0"', None, 1)
grammar.add_rule('T', '"1"', None, 1)
grammar.add_rule('N', '0', None, .1) #start with 0 meaning empty string
grammar.add_rule('N', '%s+1', ['N'], 1)
grammar.add_rule('S', 'concat_', ['R', 'R'], 1)
trees = grammar.enumerate_at_depth(6)
for item in trees:
print(item)
StopIteration Traceback (most recent call last)
File ~/Desktop/Knitting/Analysis/LOTlib3/Miscellaneous.py:308, in lazyproduct(iterators, restart_ith)
307 # initialize the state
--> 308 state = [next(it) for it in iterators]
310 yield state
File ~/Desktop/Knitting/Analysis/LOTlib3/Miscellaneous.py:308, in (.0)
307 # initialize the state
--> 308 state = [next(it) for it in iterators]
310 yield state
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
Cell In[59], line 57
50 # formula = grammar.generate()
51 # formula.fullprint()
52 # print(formula.depth())
53 # eval(str(formula))
55 trees = grammar.enumerate_at_depth(6)
---> 57 for item in trees:
58 print(item)
...
298 for a in yieldfn.argFunctionNodes():
299 # Update parents
300 a.parent = yieldfn
RuntimeError: generator raised StopIteration