-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Self, TYPE_CHECKING | ||
|
||
from tm.lin_rec import BeepHistory, HeadTape, LinRecMachine | ||
|
||
if TYPE_CHECKING: | ||
from tm.lin_rec import State, Slot, Tapes | ||
|
||
|
||
class LinRecSampler(LinRecMachine): | ||
history: BeepHistory | ||
|
||
def run( | ||
self, | ||
sim_lim: int, | ||
samples: Tapes, | ||
) -> Self: | ||
self.blanks = {} | ||
|
||
comp = self.comp | ||
|
||
self.tape = tape = HeadTape.init() | ||
|
||
self.history = BeepHistory(tapes = samples) | ||
|
||
step: int = 0 | ||
state: State = 0 | ||
|
||
for cycle in range(sim_lim or 1_000_000): | ||
slot: Slot = state, tape.scan | ||
|
||
if step in self.history.tapes: | ||
self.history.add_state_at_step(step, state) | ||
self.history.add_tape_at_step(step, tape) | ||
|
||
if (instr := comp[slot]) is None: | ||
self.undfnd = step, slot | ||
break | ||
|
||
color, shift, next_state = instr | ||
|
||
step += tape.step(shift, color, False) | ||
|
||
if (state := next_state) == -1: | ||
self.halted = step | ||
break | ||
|
||
if not color and tape.blank and state not in self.blanks: | ||
self.blanks[state] = step | ||
|
||
else: | ||
self.xlimit = step | ||
|
||
self.cycles = cycle | ||
|
||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Self, TYPE_CHECKING | ||
|
||
from tm.lin_rec import BeepHistory, HeadTape, LinRecMachine | ||
|
||
if TYPE_CHECKING: | ||
from tm.lin_rec import State, Slot, Tapes | ||
|
||
######################################## | ||
|
||
def read_progs(name: str) -> set[str]: | ||
with open(f'test/data/{name}.prog') as holdouts: | ||
return set( | ||
prog.strip() | ||
for prog in holdouts.readlines() | ||
) | ||
|
||
######################################## | ||
|
||
class LinRecSampler(LinRecMachine): | ||
history: BeepHistory | ||
|
||
def run( | ||
self, | ||
sim_lim: int, | ||
samples: Tapes, | ||
) -> Self: | ||
self.blanks = {} | ||
|
||
comp = self.comp | ||
|
||
self.tape = tape = HeadTape.init() | ||
|
||
self.history = BeepHistory(tapes = samples) | ||
|
||
step: int = 0 | ||
state: State = 0 | ||
|
||
for cycle in range(sim_lim or 1_000_000): | ||
slot: Slot = state, tape.scan | ||
|
||
if step in self.history.tapes: | ||
self.history.add_state_at_step(step, state) | ||
self.history.add_tape_at_step(step, tape) | ||
|
||
if (instr := comp[slot]) is None: | ||
self.undfnd = step, slot | ||
break | ||
|
||
color, shift, next_state = instr | ||
|
||
step += tape.step(shift, color, False) | ||
|
||
if (state := next_state) == -1: | ||
self.halted = step | ||
break | ||
|
||
if not color and tape.blank and state not in self.blanks: | ||
self.blanks[state] = step | ||
|
||
else: | ||
self.xlimit = step | ||
|
||
self.cycles = cycle | ||
|
||
return self |