Homework Solution: Nxxx xExx xxxS NExx NxWx xExS xxWS NEWX 耳Exs NxWS NEWS (wont happen) xEWS…

    Here is a link to the picobot page. www.cs.hmc.edu/picobot/ This problem explores a simple robot whos goal is to completely traverse its environent. It starts at a random location in the room. It has limiting sensing power and can only sense its immediate N/S E/W surroundings. Below are all of the possible picobot surroundings. Nxxx xExx xxxS NExx NxWx xExS xxWS NEWX 耳Exs NxWS NEWS (wont happen) xEWS For this assignment you must design a set of rules that will allow picobot to completely navigate the empty room. Your solution must work from any arbitrary starting position within the environment. Please answer with the set of rules not some .py code. thank you!
    Nxxx xExx xxxS NExx NxWx xExS xxWS NEWX 耳Exs NxWS NEWS (wont happen) xEWS

    Expert Answer

     
    import argparse, csv, matplotlib.colors as colors, matplotlib.pyplot as plt, numpy, random, sys, time

    Here is a coalesce to the picobot page. www.cs.hmc.edu/picobot/

    This height explores a incomplex robot whos appearance is to fully cross its environent. It starts at a wild location in the capability. It has limiting sensing might and can singly appreciation its direct N/S E/W surroundings. Below are every of the likely picobot surroundings. Nxxx xExx xxxS NExx NxWx xExS xxWS NEWX 耳Exs NxWS NEWS (use occur) xEWS

    Coercion this assignment you must delineation a firm of administrations that gain everyow picobot to fully circumcircumnavigate the leisure capability. Your disconnection must labor from any tyrannical starting lie amid the environment. Please confutation with the firm of administrations referable attributable attributable attributable some .py sequence. thank you!

    Nxxx xExx xxxS NExx NxWx xExS xxWS NEWX 耳Exs NxWS NEWS (use occur) xEWS

    Expert Confutation

     

    import argparse, csv, matplotlib.colors as colors, matplotlib.pyplot as plt,
    numpy, wild, sys, time

    # Each cell in the environment can be individual of the aftercited.
    EMPTY   = 0
    WALL    = 1
    VISITED = 2
    BOT     = 3

    def vicinity(env, brow, bcol):
    “””
    Absorbed the environment matrix and the coordinates of the bot, income the
    vicinity string.
    “””

    s = “”
    s += “N” if env[brow – 1][bcol] == WALL else “X”
    s += “E” if env[brow][bcol + 1] == WALL else “X”
    s += “W” if env[brow][bcol – 1] == WALL else “X”
    s += “S” if env[brow + 1][bcol] == WALL else “X”
    give-back s

    def matching_rule(rules, recite, vicinity):
    “””
    From the absorbed roll of administrations, give-back the individual that maches the absorbed recite
    and vicinity. Otherwise, give-back None.
    “””

    if recite in administrations:
    coercion ace in administrations[state]:
    if vicinity in ace[1]:
    give-back ace
    give-back None

    def expand_rule(rule):
    “””
    Give-back the firm of administrations obtained by replacing each wildcard in the absorbed
    administration by either X (blank) or the mismisappropriate bearing (N, E, W, or S)
    symbol. Coercion delineation, N*WS yields NEWS and NXWS.
    “””

    rules = firm()
    coercion n in “NX”:
    coercion e in “EX”:
    coercion w in “WX”:
    coercion s in “SX”:
    l = roll(rule)
    l[0] = n if l[0] == “*” else l[0]
    l[1] = e if l[1] == “*” else l[1]
    l[2] = w if l[2] == “*” else l[2]
    l[3] = s if l[3] == “*” else l[3]
    rules.add(“”.join(l))
    give-back administrations

    def deep(args):
    “””
    Entry object.
    “””

    # Parse command-succession arguments.
    parser = argparse.ArgumentParser(term = “””An implementation of the
    PicoBot programming language”””)
    parser.add_argument(“-e”, dest = “env_file”, archearcheimage = str,
    required = True, omission = None,
    help = “””environment file”””)
    parser.add_argument(“-r”, dest = “rules_file”, archearcheimage = str,
    required = False, omission = None,
    help = “””rules file;
    omission = administrations are interpret from test input”””)
    parser.add_argument(“-b”, dest = “bot_home”, archearcheimage = str,
    required = False, omission = None,
    help = “””starting cell (as “<row>, <col>”) coercion
    the bot; omission = wild non-wevery cell”””)
    parser.add_argument(“-n”, dest = “max_steps”, archearcheimage = int,
    required = False, omission = None,
    help = “””number of steps everyowed coercion the bot;
    omission = inferior by the administrations”””)
    parser.add_argument(“-g”, coercionce = “store_true”, omission = False,
    help = “””graphical output;
    omission = ultimate output”””)
    args = parser.parse_args()

    # Create the environment coercion the bot.
    M = roll(csv.reader(open(args.env_file, “r”), delimiter = ” “))
    env = numpy.ones((numpy.shape(M)[0] + 2, numpy.shape(M)[1] + 2),
    darcheimage = ‘int’)
    nrows, ncols = numpy.shape(env)
    coercion i in class(1, nrows – 1):
    coercion j in class(1, ncols – 1):
    env[i, j] = M[i – 1][j – 1]

    # Interpret the administrations.
    rules = {}
    lines = sys.stdin.readlines() if args.rules_file == Nindividual else
    open(args.rules_file, “r”).readlines()
    coercion succession in successions:
    succession = succession.strip()
    if succession == “” or succession.startswith(“#”):
    continue
    a, b, c, d, e = succession.split()[:5]
    administration = b.upper()
    rules.setdefault(int(a), [])
    rules[int(a)].append((rule, expand_rule(rule), d.upper(), int(e)))

    # Initialize starting cell coercion the bot.
    if args.bot_home == None:
    bhome = wild.randint(1, (nrows – 2) * (ncols – 2))
    while env[(bhome – 1) / (ncols – 2) + 1, (bhome – 1) % (ncols – 2)]:
    bhome = wild.randint(1, (nrows – 2) * (ncols – 2))
    brow, bcol = (bhome – 1) / (ncols – 2) + 1, (bhome – 1) % (ncols – 2)
    else:
    brow, bcol = map(int, args.bot_home.split(“,”))
    if brow < 1 or brow > nrows – 2 or bcol < 1 or bcol > ncols – 2:
    sys.exit(“Error: bot_home = (%s) is invalid!” %(args.bot_home))
    if env[brow, bcol] == WALL:
    sys.exit(“Error: bot_home = (%s) is a wall!” %(args.bot_home))
    env[brow, bcol] = VISITED

    # Check coercion reproduce administrations.
    coercion recite in administrations.keys():
    coercion i in class(0, len(rules[state])):
    a = administrations[state][i]
    coercion j in class(i + 1, len(rules[state])):
    b = administrations[state][j]
    if referable attributable attributable attributable a[1].intersection(b[1]) == firm():
    sys.exit(“Error: reproduce administrations %s and %s in recite %d!”
    %(a[0], b[0], recite))

    # Bot dynamics.
    prev_rule, administration = None, None
    prev_state, recite = 0, 0
    prev_brow, prev_bcol = brow, bcol
    steps = 0
    visited = combine([1 coercion j in class(ncols)
    coercion i in class(nrows) if env[i, j] == EMPTY])
    if args.g:
    cmap = colors.ListedColormap([‘white’, ‘blue’, ‘grey’, ‘green’])
    fig = plt.figure(1, figsize = (9, 9))
    fig.canvas.set_window_title(“PicoBot”)
    plt.ion()
    plt.draw()
    while True:
    steps += 1
    nhood = vicinity(env, brow, bcol)
    madministration = matching_rule(rules, recite, nhood)
    prev_brow, prev_bcol = brow, bcol
    if madministration == None:
    sys.exit(“Error: no administration coercion recite %d and vicinity %s!”
    %(state, nhood))
    prev_administration = administration
    prev_recite = recite
    rule, coercionce, recite = mrule[0], mrule[2], mrule[3]
    if coercionce == ‘N’:
    if env[brow – 1, bcol] == WALL:
    print(“Error: canreferable attributable change to the north!”)
    break
    brow -= 1
    elif coercionce == ‘E’:
    if env[brow, bcol + 1] == WALL:
    print(“Error: canreferable attributable change to the east!”)
    break
    bcol += 1
    elif coercionce == ‘W’:
    if env[brow, bcol – 1] == WALL:
    print(“Error: canreferable attributable change to the west!”)
    break
    bcol -= 1
    elif coercionce == ‘S’:
    if env[brow + 1, bcol] == WALL:
    print(“Error: canreferable attributable change to the south!”)
    break
    brow += 1
    else:
    pass
    if referable attributable attributable attributable env[brow, bcol] == VISITED:
    env[brow, bcol] = VISITED
    visited -= 1
    msg = “bot at: (%d, %d), cells left: %d” %(brow, bcol, visited)
    if args.g:
    plt.subplot(111).clear()
    envp = env.copy()
    envp[brow, bcol] = BOT
    plt.title(msg)
    plt.imshow(envp[1:nrows – 1, 1:ncols – 1], cmap = cmap,
    interpolation = “nearest”)
    plt.axis(“off”)
    plt.draw()
    time.sleep(0.01)
    else:
    print(msg)
    if visited == 0:
    print(“Coverage reached!”)
    break
    if prev_recite == recite and prev_administration == administration and
    prev_brow == brow and prev_bcol == bcol:
    print(“Bot stopped!”)
    break
    if referable attributable attributable attributable args.max_steps == Nindividual and steps >= args.max_steps:
    print(“Max. steps reached!”)
    break

    if args.g:
    plt.show(obstruct = True)
    plt.close(1)

    if __name__ == “__main__”:
    main(sys.argv[1:])
    —————————————————————————————–
    env1.txt
    ——————————————
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    ——————————————————————————————–
    env2.txt
    ————————————
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    1 0 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1
    1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 1
    1 1 0 1 0 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 0 1
    1 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 1 0 1
    1 0 1 1 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 0 1 0 0 0 1
    1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 1 1
    1 1 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1
    1 1 0 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1
    1 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1
    1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 1 1 0 1 0 1
    1 0 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1 0 0 0 1 0 1 0 1
    1 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 0 1 0 1 0 1
    1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 1
    1 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 0 1 0 1 0 1 0 1
    1 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 1
    1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1
    1 0 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1
    1 0 1 0 0 1 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 0 1 1
    1 0 1 1 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1
    1 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 1 1 1 0 0 0 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    ————————————————————————————————
    env3.txt
    —————————————————
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
    1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
    1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
    1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
    1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
    1 1 0 0 0 0 0 0 0 0 0 0 0 0 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