質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1874閲覧

このコードは何を意味しているのでしょうか?

Surface-Yuki

総合スコア34

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/02/12 14:21

編集2017/02/12 14:23

このコマンド何を意味しているのでしょうか?
Picobotという簡単なプログラムを作成して、それを提出するとう課題なのですが、picobot自体は簡単なのですが、その後がわかりません。

課題画面のスクショとpicobot.pyが入っているフォルダのスクショを貼っておきます。
課題画面がそもそも何を意味するのかがわかりません。(英語の意味はわかります。)

以下のコマンドを打つと、ターミナル上で起動するはずなのですが、
うまく行きません。
python.py 以降のコマンドが何を指しているのかが全くわかりません。

"$ python picobot .py -e env1 . txt -b "10 , 10" < north_south .pb"

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

t_obara

2017/02/12 15:57

課題なのであれば、その課題を提示している方にお聞きになれば良いのではないでしょうか?それができない理由はなんなのですか?
Surface-Yuki

2017/02/12 16:27

すみません。意味不明な質問をしてしまい、申し訳ございませんでした。先生に聞いて解決しました。削除依頼をteratailへ送っておきました。
Zuishin

2017/02/23 04:34

解決したのなら経緯を書いた自己回答を投稿して自己解決にするか、現在の回答にベストアンサーをつけてください。
guest

回答1

0

ベストアンサー

コマンドラインのオプションですが、

sample.py

1python picobot.py -e env1.txt -b "10 , 10" < north_south.pb"

このコマンドラインのオプションを知るには、「picobot.py」のソースコードを読む必要があります。
中を読むと、-eのオプションの意味などがわかります。

もし宜しければ、picobot.pyのコードを貼り付けていただけると追加でお答え出来るかと思います。

投稿2017/02/14 16:48

K_T_T_K

総合スコア231

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Surface-Yuki

2017/02/14 17:09

``` import argparse, csv, matplotlib.colors as colors, matplotlib.pyplot as plt, \ numpy, random, sys, warnings # Each cell in the environment can be one of the following. EMPTY = 0 WALL = 1 VISITED = 2 BOT = 3 def neighborhood(env, brow, bcol): """ Given the environment matrix and the coordinates of the bot, returns the neighborhood 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" return s def matching_rule(rules, state, neighborhood): """ From the given list of rules, return the one that maches the given state and neighborhood. Otherwise, return None. """ if state in rules: for item in rules[state]: if neighborhood in item[1]: return item return None def expand_rule(rule): """ Return the set of rules obtained by replacing each wildcard in the given rule by either X (blank) or the appropriate direction (N, E, W, or S) symbol. For example, N*WS yields NEWS and NXWS. """ rules = set() for n in "NX": for e in "EX": for w in "WX": for s in "SX": l = list(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)) return rules def main(args): """ Entry point. """ # Parse command-line arguments. parser = argparse.ArgumentParser(description = """An implementation of the PicoBot programming language""") parser.add_argument("-e", dest = "env_file", type = str, required = True, default = None, help = """environment file""") parser.add_argument("-r", dest = "rules_file", type = str, required = False, default = None, help = """rules file; default = rules are read from standard input""") parser.add_argument("-b", dest = "bot_home", type = str, required = False, default = None, help = """starting cell (as "<row>, <col>") for the bot; default = random non-wall cell""") parser.add_argument("-n", dest = "max_steps", type = int, required = False, default = None, help = """number of steps allowed for the bot; default = governed by the rules""") parser.add_argument("-g", action = "store_true", default = False, help = """graphical output; default = terminal output""") args = parser.parse_args() # Create the environment for the bot. M = list(csv.reader(open(args.env_file, "r"), delimiter = " ")) env = numpy.ones((numpy.shape(M)[0] + 2, numpy.shape(M)[1] + 2), dtype = 'int') nrows, ncols = numpy.shape(env) for i in range(1, nrows - 1): for j in range(1, ncols - 1): env[i, j] = M[i - 1][j - 1] # Read the rules. rules = {} lines = sys.stdin.readlines() if args.rules_file == None else \ open(args.rules_file, "r").readlines() for line in lines: line = line.strip() if line == "" or line.startswith("#"): continue a, b, c, d, e = line.split()[:5] rule = b.upper() rules.setdefault(int(a), []) rules[int(a)].append((rule, expand_rule(rule), d.upper(), int(e))) # Initialize starting cell for the bot. if args.bot_home == None: bhome = random.randint(1, (nrows - 2) * (ncols - 2)) while env[(bhome - 1) / (ncols - 2) + 1, (bhome - 1) % (ncols - 2)]: bhome = random.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 for repeat rules. for state in rules.keys(): for i in range(0, len(rules[state])): a = rules[state][i] for j in range(i + 1, len(rules[state])): b = rules[state][j] if not a[1].intersection(b[1]) == set(): sys.exit("Error: repeat rules %s and %s in state %d!" \ %(a[0], b[0], state)) # Bot dynamics. prev_rule, rule = None, None prev_state, state = 0, 0 prev_brow, prev_bcol = brow, bcol steps = 0 visited = sum([1 for j in range(ncols) for i in range(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") while True: steps += 1 nhood = neighborhood(env, brow, bcol) mrule = matching_rule(rules, state, nhood) prev_brow, prev_bcol = brow, bcol if mrule == None: sys.exit("Error: no rule for state %d and neighborhood %s!" \ %(state, nhood)) prev_rule = rule prev_state = state rule, action, state = mrule[0], mrule[2], mrule[3] if action == 'N': if env[brow - 1, bcol] == WALL: print("Error: cannot move to the north!") break brow -= 1 elif action == 'E': if env[brow, bcol + 1] == WALL: print("Error: cannot move to the east!") break bcol += 1 elif action == 'W': if env[brow, bcol - 1] == WALL: print("Error: cannot move to the west!") break bcol -= 1 elif action == 'S': if env[brow + 1, bcol] == WALL: print("Error: cannot move to the south!") break brow += 1 else: pass if not env[brow, bcol] == VISITED: env[brow, bcol] = VISITED visited -= 1 msg = "bot at: (%d, %d), cells left: %d" %(brow, bcol, visited) if args.g: envp = env.copy() envp[brow, bcol] = BOT plt.subplot(111).clear() plt.title(msg) plt.imshow(envp[1:nrows - 1, 1:ncols - 1], cmap = cmap, interpolation = "nearest") plt.axis("off") plt.pause(0.01) else: print(msg) if visited == 0: print("Coverage reached!") break if prev_state == state and prev_rule == rule and \ prev_brow == brow and prev_bcol == bcol: print("Bot stopped!") break if not args.max_steps == None and steps >= args.max_steps: print("Max. steps reached!") break if args.g: plt.show(block = True) plt.close(1) if __name__ == "__main__": warnings.filterwarnings("ignore") main(sys.argv[1:]) ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問