[[チュートリアル>http://www13.atwiki.jp/netlogo/pages/17.html]] > モデルの作成 ここでは、前ページ([[プログラミング>http://www13.atwiki.jp/netlogo/pages/20.html]]で作成したプログラムを発展させて、生態系モデルを作成する。 #Contents ---- *生態系の作成 生態系として、草(パッチ)と動物(タートル)の関係を考える。 &italic(){ 草: 動物に食べられる。一定確率で再び生える。} &italic(){ 動物:草を食べることでエネルギーを得る。歩くことでエネルギーを消費し、エネルギーが無くなったら死ぬ。} これを実際にプログラミングしてみると以下のようになる。 **草(パッチ)の状態を制御する 1. &bold(){go} を以下のように書き換える。 #blockquote(){ to go move-turtles &bold(){ eatgrass} end } 2. 次に &bold(){eatgrass} を定義する。以下の処理を書き加える。 #blockquote(){&bold(){ to eat-grass} &bold(){ ask turtles [} &bold(){ if pcolor = green [} &bold(){ set pcolor black} &bold(){ set energy (energy + 10)} &bold(){ ]} &bold(){ ]} &bold(){ end} } NetLogoの文法では、C言語などとほぼ同じ判定文が使える。 この場合は&bold(){ if文 }を用いて、 &italic(){ もし自分のいる場所が緑だったら黒にセット、エネルギーを10得る} という処理を全てのタートルに行わせている。 値や色を代入する際には &bold(){ set 代入先 代入する値や色} を用いる。()は使っても使わなくても可。 3. また、以下のように書き加えることで食べられた草が復活するようになる。 #blockquote(){ to go move-turtles eat-grass &bold(){ regrow-grass} end } #blockquote(){&bold(){ to regrow-grass} &bold(){ ask patches [} &bold(){ if random 100 < 3 [ set pcolor green ]} &bold(){ ]} &bold(){ end} } ここでは乱数を利用している。 &bold(){ random 数} で、0から数未満の乱数を発生させることができる。この場合は0から99までの値がランダムに選ばれており、 その値が3以下の場合、ふたたびパッチを緑にする(=草が生える)よう処理を行っている。 **タートルのエネルギーを制御する 各タートルはそれぞれエネルギーを持ち、そのエネルギー量に従って行動する。 ここではそのエネルギーの管理、制御処理を記述する。 3. プログラムの最初に以下のように書き加える。 #blockquote(){&bold(){ tturtles-own [energy]}} 4. &bold(){move-turtle} を以下のように書き換える。 #blockquote(){ to move-turtles ask turtles [ right random 360 forward 1 &bold(){ set energy energy - 1} ] end } これで、&bold(){go} が行われるたびに(=1歩進むごとに)エネルギーが1ずつ減少するようになる。 5. 次に、 ・エネルギーが0になったら死ぬ ・エネルギーが50以上になったら子供を産む という処理を付け加えてみる。 &bold(){go }に以下の処理を追加する。 #blockquote(){&bold(){ reproduce} &bold(){ check-death} } &bold(){ reproduce }が子供を産む処理、&bold(){check-death }が生死を判定する処理を表す。 これらの処理の中身を記述する。 #blockquote(){&bold(){ to check-death} &bold(){ ask turtles [} &bold(){ if energy <= 0 [ die ]} &bold(){ ]} &bold(){ end} } #blockquote(){&bold(){ to reproduce} &bold(){ ask turtles [} &bold(){ if energy > 50 [} &bold(){ set energy energy - 50} &bold(){ hatch 1 [ set energy 50 ]} &bold(){ ]} &bold(){ ]} &bold(){ end} } &bold(){ check-death} で、エネルギーが0以下になったらタートルが死ぬ、 &bold(){ reproduce} で、エネルギーが50以上の場合にエネルギーを50減らし、新たなタートルを作るよう記述している。 &bold(){ hatch &italic(){ 数 } [ コマンド ]} で、数の分だけ新しいタートルを作り出し、コマンドを実行することができる。 ここまで記述したらInterfaceタブに切り替えてsetup、goを押す。 するとパッチが消えたりタートルが増えたりするのが確認できる。 *拡張 **数の変更をGUIで行う ここではタートルの数やパッチの復活率などを任意に決められるよう拡張する。 1. Interfaceタブから「スライダー」を選択し、配置したい場所をクリックする。 2. 設置したスライダーを右クリック、Editを選択。 &bold(){Grobal Variable}に &bold(){number-of-turtles} と書いてOKを押す(下図参照) これで、グローバル変数 "number-of-turtles" が定義される。 &ref(pic14.png) 3. Procedureタブを押し、&bold(){to setup} 部分を参照。 #blockquote(){ create-turtles 100 を create-turtles &bold(){number-of-turtles}} と書き換える。 これにより、setup を行ったときに number-of-turtles 分だけタートルが生成されるようになる。 4. 同様に、パッチの復活率も可変にする。 新たにスライダーを作成し、右クリック、Editを選択。 Grobal Variable のところに "%-of-regrow" と記述する。 5. Procedreタブを押し、&bold(){to regrow-grass} 部分を参照、以下のように書き換える。 #blockquote(){ to regrow-grass ask patches [ &bold(){ if random 100 < %-of-regrow [ set pcolor green ]} ] end } これで、乱数が "%-of-regrow" 以上のときにパッチが復活するようになる。 6. Interfaceタブに戻り、setup、goを押して処理を確認する。 **タートルの観測 ここではモニターやプロットなどを新たに作り、タートルの数やパッチの数などを観測する。 *** モニターの追加 Interfaceタブから「モニター」を選択し、配置したい場所をクリックする(下図参照) &ref(pic10.png) 作ったモニターを右クリック、Editを選択。 &bold(){Reporter} 部分に以下のように書いてOKを押す。 &bold(){ count turtles} これで、setup、goを押すとタートルの数がリアルタイムで表示される。 同様に、緑のパッチの数を数えたいときはもう1つモニターを作成し、&bold(){reporter} 部分に &bold(){ count patches with [ pcolor = green ] } と記述することで緑のパッチの数が表示される。 *** プロットの追加 1. プロットを選択し、配置したい場所をクリックする。 作ったプロットを右クリック、Editを選択。下図のように入力する。 &ref(pic11.png) 2. &bold(){Rename }を押し、出てきたウインドウに &bold(){turtles }と入力。 &bold(){Create }を押し、&bold(){grass }と入力。&bold(){color }を &bold(){green }へ変更し、OKを押す。 3. Procedureタブに切り替え、&bold(){setup} と&bold(){ go }それぞれに以下の処理を付け加える。 #blockquote(){&bold(){ do-plots}} 4. do-plot を定義する。 #blockquote(){&bold(){ to do-plots} &bold(){ set-current-plot "Totals"} &bold(){ set-current-plot-pen "turtles"} &bold(){ plot count turtles} &bold(){ set-current-plot-pen "grass"} &bold(){ plot count patches with [pcolor = green]} &bold(){ end} } ここでは、プロットの名前を "Total" に、 "turtles" と "grass" の数をプロットするよう処理している。 *** カウンターの追加 NetLogoには tick という標準関数が用意されている。これを用いてカウンターを作成する。 1. &bold(){go} を以下のように書き換える。 #blockquote(){ to go &bold(){ if ticks >= 500 [ stop ]} move-turtles eat-grass reproduce check-death regrow-grass &bold(){ tick} do-plots end } &bold(){tick} を加えることで、go が一回行われると ticks が1増えるようになる。 この場合は最初の&bold(){ if文 }で、 ticks が500以上になったら動作を止める よう定義している。 **サンプルコード 以下に出来上がったプログラムの全文を掲載する。公式ページより引用(一部改変) ---- #blockquote(){ turtles-own [energy] ;; for keeping track of when the turtle is ready ;; to reproduce and when it will die to setup clear-all setup-patches setup-turtles do-plots end to setup-patches ask patches [ set pcolor green ] end to setup-turtles create-turtles number-of-turtles ;; uses the value of the number slider to create turtles ask turtles [ setxy random-xcor random-ycor ] end to go if ticks >= 500 [ stop ] ;; stop after 500 ticks move-turtles eat-grass reproduce check-death regrow-grass tick ;; increase the tick counter by 1 each time through do-plots end to move-turtles ask turtles [ right random 360 forward 1 set energy energy - 1 ;; when the turtle moves it looses one unit of energy ] end to eat-grass ask turtles [ if pcolor = green [ set pcolor black ;; the value of energy-from-grass slider is added to energy set energy (energy + energy-from-grass) ] ifelse show-energy? [ set label energy ] ;; the label is set to be the value of the energy [ set label "" ] ;; the label is set to an empty text value ] end to reproduce ask turtles [ if energy > birth-energy [ set energy energy - birth-energy ;; take away birth-energy to give birth hatch 1 [ set energy birth-energy ] ;; give this birth-energy to the offspring ] ] end to check-death ask turtles [ if energy <= 0 [ die ] ;; removes the turtle if it has no energy left ] end to regrow-grass ask patches [ ;; 3 out of 100 times, the patch color is set to green if random 100 < 3 [ set pcolor green ] ] end to do-plots set-current-plot "Totals" ;; which plot we want to use next set-current-plot-pen "turtles" ;; which pen we want to use next plot count turtles ;; what will be plotted by the current pen set-current-plot-pen "grass" ;; which pen we want to use next plot count patches with [pcolor = green] ;; what will be plotted by the current pen end } ----
下から選んでください: