Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed MNTM's implementation of read_input_as_ntm #239

Merged

Conversation

CamiloMartinezM
Copy link
Contributor

Completed also test_read_input_as_ntm() to be able to detect whether the tapes are different after running an MNTM normally and as an NTM (which would have detected that both mntm.accepts_input(input_str)) and mntm.read_input_stepwise(input_str) weren't working in unison.

Now, the following code does work:

from automata.tm.mntm import MNTM

# MNTM which accepts all strings of the form ww, where w is in {0, 1}*
mntm = MNTM(
    states={"q0", "q1", "q2", "q3", "q4"},
    input_symbols={"0", "1"},
    tape_symbols={"0", "1", "$", "#"},
    n_tapes=3,
    transitions={
        "q0": {
            ("0", "#", "#"): [("q1", (("0", "N"), ("$", "R"), ("$", "R")))],
            ("1", "#", "#"): [("q1", (("1", "N"), ("$", "R"), ("$", "R")))],
        },
        "q1": {
            ("0", "#", "#"): [
                ("q1", (("0", "R"), ("0", "R"), ("#", "N"))),
                ("q2", (("0", "R"), ("#", "N"), ("0", "R"))),
            ],
            ("1", "#", "#"): [
                ("q1", (("1", "R"), ("1", "R"), ("#", "N"))),
                ("q2", (("1", "R"), ("#", "N"), ("1", "R"))),
            ],
        },
        "q2": {
            ("0", "#", "#"): [("q2", (("0", "R"), ("#", "N"), ("0", "R")))],
            ("1", "#", "#"): [("q2", (("1", "R"), ("#", "N"), ("1", "R")))],
            ("#", "#", "#"): [("q3", (("#", "N"), ("#", "L"), ("#", "L")))],
        },
        "q3": {
            ("#", "0", "0"): [("q3", (("#", "N"), ("0", "L"), ("0", "L")))],
            ("#", "1", "1"): [("q3", (("#", "N"), ("1", "L"), ("1", "L")))],
            ("#", "$", "$"): [("q4", (("#", "N"), ("$", "N"), ("$", "N")))],
        },
    },
    initial_state="q0",
    blank_symbol="#",
    final_states={"q4"},
)

print(mntm.accepts_input("0101"))  # This correctly returns True
for conf in mntm.read_input_as_ntm("0101"):  # This previously threw the exception
    final_config = conf.pop()
    print(final_config)

The final NTM output is the following: TMConfiguration('q4', TMTape('0101#^_$^01#_$^01#_', '#', 18))
Whereas the final MNTM output is: MTMConfiguration('q4', (TMTape('0101#', '#', 4), TMTape('$01#', '#', 0), TMTape('$01#', '#', 0))).

If we concatenate either of them, we get: 0101#^_$^01#_$^01#_ (which is what test_read_input_as_ntm() now tests).

@coveralls
Copy link

coveralls commented Nov 8, 2024

Coverage Status

coverage: 99.613% (+0.001%) from 99.612%
when pulling 281d495 on CamiloMartinezM:fixed-mntm-read_input_as_ntm
into 9993dd1 on caleb531:develop.

@coveralls
Copy link

Coverage Status

coverage: 99.613%. remained the same
when pulling 4042a47 on CamiloMartinezM:fixed-mntm-read_input_as_ntm
into 9ab1a1c on caleb531:main.

@CamiloMartinezM
Copy link
Contributor Author

The linting errors are caused by doing: configs[-1].pop(), saying "AbstractSet[TMConfiguration]" has no attribute "pop"mypy[attr-defined](https://mypy.readthedocs.io/en/stable/_refs.html#code-attr-defined). Any advice on how to do this properly? @eliotwrobson
Also, can you link the PR to the #237 issue? :)

@caleb531 caleb531 changed the base branch from main to develop November 12, 2024 03:57
@eliotwrobson eliotwrobson linked an issue Nov 26, 2024 that may be closed by this pull request
@eliotwrobson
Copy link
Collaborator

@CamiloMartinezM sorry for the delay! It's been a busy time of year for me. I've fixed the linting issue by adding a cast, and it looks like everything is green now. If the current changeset looks good to you still, please comment below and I can go ahead and merge this.

@CamiloMartinezM
Copy link
Contributor Author

@eliotwrobson LGTM!

@eliotwrobson eliotwrobson merged commit 6359d33 into caleb531:develop Nov 26, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug in read_input_as_ntm() in MNTM implementation
3 participants