迷路解いた(A*で)

A*で解いてみた。距離はマンハッタン使った。

迷路やった

maze = ["**************************",
        "*S* *                    *",
        "* * *  *  *************  *",
        "* *   *    ************  *",
        "*    *                   *",
        "************** ***********",
        "*                        *",
        "** ***********************",
        "*      *              G  *",
        "*  *      *********** *  *",
        "*    *        ******* *  *",
        "*       *                *",
        "**************************"]
start    = ()
goal     = () 
max = len(maze) + len(maze[0])

for (h,line) in enumerate(maze):
    for (w,word) in enumerate(line):
        if   word == 'S': start = (h,w)
        elif word == 'G': goal  = (h,w)

checked = [start]
sol = [start]

def add_route(r):
    (h,w) = r[-1]
    cand = [c for c in [(h+1,w),(h-1,w),(h,w+1),(h,w-1)] if c not in checked and not_wall(c)]
    if len(cand) == 0:
        return r[:-1]
    else:
        sol = (); cost = max
        for c in cand:
            c_cost = calc_cost(c)
            if c_cost < cost:
                cost = c_cost
                sol = c
        r.append(sol)
        checked.append(sol)
        return r

def not_wall(p): (h,w) = p; return  maze[h][w] != '*'
def calc_cost(p): return (abs(goal[0]-p[0]) + abs(goal[1]-p[1]))
def check_goal(sol): return sol[-1] != goal

def draw_sol(sol):
    solved_maze = ''
    for (h,line) in enumerate(maze):
        for (w,word) in enumerate(line):
            if (h,w) in sol[1:-1]:
                solved_maze += '$'
            else:
                solved_maze += word
        solved_maze += '\n'
    return solved_maze

while check_goal(sol): sol = add_route(sol)
print draw_sol(sol)

実行結果

$ python maze_aster.py
**************************
*S* * $$$                *
*$* *$$*$ *************  *
*$* $$* $  ************  *
*$$$$*  $$$$$$$          *
**************$***********
* $$$$$$$$$$$$$          *
**$***********************
* $$$$$*$$$$$$$$$$$$$$G  *
*  *  $$$ *********** *  *
*    *        ******* *  *
*       *                *
**************************

迷路やった

この問題

maze = ["**************************",
        "*S* *                    *",
        "* * *  *  *************  *",
        "* *   *    ************  *",
        "*    *                   *",
        "************** ***********",
        "*                        *",
        "** ***********************",
        "*      *              G  *",
        "*  *      *********** *  *",
        "*    *        ******* *  *",
        "*       *                *",
        "**************************"]
solution = []
start    = ()
goal     = () 
max = (len(maze)-2) * (len(maze[0])-2)

for (h,line) in enumerate(maze):
    for (w,word) in enumerate(line):
        if   word == 'S': start = (h,w)
        elif word == 'G': goal  = (h,w)

checked = [start]

def add_route(sols):
    newroute = []
    for sol in sols:
        (h,w) = sol[-1]
        if ((h+1,w) not in checked) and maze[h+1][w] != '*':
            nr = list(sol)
            nr.append((h+1,w))
            checked.append((h+1,w))
            newroute.append(nr)
        if ((h-1,w) not in checked) and maze[h-1][w] != '*':
            nr = list(sol)
            nr.append((h-1,w))
            checked.append((h-1,w))
            newroute.append(nr)
        if ((h,w+1) not in checked) and maze[h][w+1] != '*':
            nr = list(sol)
            nr.append((h,w+1))
            checked.append((h,w+1))
            newroute.append(nr)
        if ((h,w-1) not in checked) and maze[h][w-1] != '*':
            nr = list(sol)
            nr.append((h,w-1))
            checked.append((h,w-1))
            newroute.append(nr)
    return newroute

def check_goal(sols):
    for sol in sols:
        if sol[-1] == goal:
            print draw_sol(sol)
            exit

def draw_sol(sol):
    solved_maze = ''
    for (h,line) in enumerate(maze):
        for (w,word) in enumerate(line):
            if (h,w) in sol[1:-1]:
                solved_maze += '$'
            else:
                solved_maze += word
        solved_maze += '\n'
    return solved_maze

sols = [[start]]

for i in range(max):
    sols = add_route(sols)
    check_goal(sols)

ちょっと長い

$ python maze.py
**************************
*S* *$$$$                *
*$* *$ *$ *************  *
*$*$$$* $  ************  *
*$$$ *  $$$$$$$          *
**************$***********
* $$$$$$$$$$$$$          *
**$***********************
* $$$  *$$$$$$$$$$$$$$G  *
*  *$$$$$ *********** *  *
*    *        ******* *  *
*       *                *
**************************

ダイクストラ法とA*アルゴリズムの違いをあとで読む

Real World Haskell 20章

20.5はパイプ

ProductName Real World Haskell―実戦で学ぶ関数型言語プログラミング
Bryan O'Sullivan,John Goerzen,Don Stewart
オライリージャパン / ¥ 3,990 ()
在庫あり。

理解はあやしいがとりあえず写経して動くとこまで。

*RunProcessSimple> runIO $ "ls /Users/kzfm/haskell" -|- "grep '.hs~'" -|- "tr a-z A-Z"
BARCODE.HS~
LOGGER.HS~
PNM.HS~
PARSE.HS~
RANDOM.HS~
RUNPROCESSSIMPLE.HS~

「アイデアスポッティング」を読んだ

散文的なのでサラッと読んだというか眺めたというか。

ProductName アイデア スポッティング
サム・ハリソン
二見書房 / ¥ 1,575 ()
在庫あり。

アイデアスポッティングとは、『探索+関連づけ』のことである。

  • わかりきったことや決まりきったことを超える
  • 探索 -> 自由 -> 小休止 -> 受容 -> 活用
  • 正しい展望とはエンドユーザーの感情的な受け止め方をとらえること
  • 本や雑誌を開けば心も開く

Evaluation of Drug Candidates for Preclinical Development

新しい本が出るらしい。

  • Cover drug transporters, cytochrome P-450 and drug-drug interactions, plasma protein binding, stability, drug formulation, preclinical safety assessment, toxicology, and toxicokinetics
  • Address developability issues that challenge pharma companies, moving beyond isolated experimental results
  • Reveal connections between the key scientific areas that are critical for successful drug discovery and development
  • Inspire forward-thinking strategies and decision-making processes in preclinical evaluation to maximize the potential of drug candidates to progress through development efficiently and meet the increasing demands of the marketplace

Evaluation of Drug Candidates for Preclinical Development serves as an introductory reference for those new to the pharmaceutical industry and drug discovery in particular. It is especially well suited for scientists and management teams in small- to mid-sized pharmaceutical companies, as well as academic researchers and graduate students concerned with the practical aspects related to the evaluation of drug developability.

A Face for Richie Hawtin

ruby-processingのところのGalleryにあったA Face for Richie Hawtinはマイクの音に反応する。

ffrh

椎名林檎の時は暴走するを流して適当にスクリーンショットを撮ってみた。

あとでコードをちゃんと見てみる。

Rubyで作る奇妙なプログラミング言語

予約した

ProductName Rubyで作る奇妙なプログラミング言語 ~Esoteric Language~
原 悠
毎日コミュニケーションズ / ¥ 2,814 ()
在庫あり。

Quine

Esotericを読み始め。

ProductName Rubyで作る奇妙なプログラミング言語 ~Esoteric Language~
原 悠
毎日コミュニケーションズ / ¥ 2,814 ()
在庫あり。

一章の練習問題にファイルのオープンを使わないでQuine

rubyだとかなり短く書けるのね。

printf a="printf a=%p,a",a

perlだとsigilが必要な分だけ難しいなぁ。

シングルクォートをq{}にして$_に入れてevalするとか。

HQ9+

HQ9+の実装終了。

ProductName Rubyで作る奇妙なプログラミング言語 ~Esoteric Language~
原 悠
毎日コミュニケーションズ / ¥ 2,814 ()
在庫あり。

HQ9+のQはQuineのQだったのね。

Brainf*ck

Brainf*ckの実装を通して、チューリングマシンに対する理解が深まった。

ProductName Rubyで作る奇妙なプログラミング言語 ~Esoteric Language~
原 悠
毎日コミュニケーションズ / ¥ 2,814 ()
在庫あり。

この本おもしろいですのう。