モデルの作成

チュートリアル > モデルの作成

ここでは、前ページ (プログラミング) で作成したプログラムを発展させて、生態系モデルを作成する。





生態系の作成


 生態系として、草(パッチ)と動物(タートル)の関係を考える。

  草: 動物に食べられる。一定確率で再び生える。
  動物:草を食べることでエネルギーを得る。歩くことでエネルギーを消費し、
     エネルギーが無くなったら死ぬ。

 これを実際にプログラミングしてみると以下のようになる。

草(パッチ)の状態を制御する


 1. go を以下のように書き換える。

   to go
    move-turtles
    eatgrass
   end

 2. 次に eatgrass を定義する。以下の処理を書き加える。

   to eat-grass
    ask turtles [
     if pcolor = green [
      set pcolor black
      set energy (energy + 10)
     ]
    ]
   end


   NetLogoの文法では、C言語などとほぼ同じ判定文が使える。
   この場合は if文を用いて、

   もし自分のいる場所が緑だったら黒にセット、エネルギーを10得る

   という処理を全てのタートルに行わせている。
   値や色を代入する際には

   set 代入先 代入する値や色

   を用いる。()は使っても使わなくても可。

 3. また、以下のように書き加えることで食べられた草が復活するようになる。

   to go
    move-turtles
    eat-grass
    regrow-grass
   end

   to regrow-grass
    ask patches [
    if random 100 < 3 [ set pcolor green ]
    ]
   end

   ここでは乱数を利用している。
    random 数
   で、0から数未満の乱数を発生させることができる。
   この場合は0から99までの値がランダムに選ばれており、その値が3以下の場合、
   ふたたびパッチを緑にする(=草が生える)よう処理を行っている。


タートルのエネルギーを制御する


 各タートルはそれぞれエネルギーを持ち、そのエネルギー量に従って行動する。
 ここではそのエネルギーの管理、制御処理を記述する。

 3. プログラムの最初に以下のように書き加える。

   tturtles-own [energy]

 4. move-turtle を以下のように書き換える。

   to move-turtles
    ask turtles [
     right random 360
     forward 1
     set energy energy - 1
    ]
   end

 これで、go が行われるたびに(=1歩進むごとに)エネルギーが1ずつ減少するようになる。

 5. 次に、
   ・エネルギーが0になったら死ぬ
   ・エネルギーが50以上になったら子供を産む
   という処理を付け加えてみる。

 goに以下の処理を追加する。

 reproduce
 check-death

  reproduceが子供を産む処理、check-deathが生死を判定する処理を表す。
  これらの処理の中身を記述する。

   to check-death
    ask turtles [
     if energy <= 0 [ die ]
    ]
   end

   to reproduce
    ask turtles [
     if energy > 50 [
      set energy energy - 50
      hatch 1 [ set energy 50 ]
     ]
    ]
   end

  check-death で、エネルギーが0以下になったらタートルが死ぬ、
  reproduce で、エネルギーが50以上の場合にエネルギーを50減らし、
  新たなタートルを作るよう記述している。

   hatch [ コマンド ]

  で、数の分だけ新しいタートルを作り出し、コマンドを実行することができる。

  ここまで記述したらInterfaceタブに切り替えてsetup、goを押す。
  するとパッチが消えたりタートルが増えたりするのが確認できる。


拡張


数の変更をGUIで行う


 ここではタートルの数やパッチの復活率などを任意に決められるよう拡張する。

 1. Interfaceタブから「スライダー」を選択し、配置したい場所をクリックする。

 2. 設置したスライダーを右クリック、Editを選択。
  &bold(){Grobal Variable}に &bold(){number-of-turtles} と書いてOKを押す(下図参照)
   これで、グローバル変数 "number-of-turtles" が定義される。


 3. Procedureタブを押し、to setup 部分を参照。

   create-turtles 100  を   create-turtles number-of-turtles

   と書き換える。
   これにより、setup を行うと number-of-turtles 分だけタートルが生成されるようになる。

 4. 同様に、パッチの復活率も可変にする。
   新たにスライダーを作成し、右クリック、Editを選択。
   Grobal Variable のところに "%-of-regrow" と記述する。

 5. Procedreタブを押し、to regrow-grass 部分を参照、以下のように書き換える。

   to regrow-grass
    ask patches [
    if random 100 < %-of-regrow [ set pcolor green ]
    ]
   end

   これで、乱数が "%-of-regrow" 以上のときにパッチが復活するようになる。

 6. Interfaceタブに戻り、setup、goを押して処理を確認する。


タートルの観測

  ここではモニターやプロットなどを新たに作り、タートルの数やパッチの数などを観測する。

モニターの追加


  Interfaceタブから「モニター」を選択し、配置したい場所をクリックする(下図参照)


  作ったモニターを右クリック、Editを選択。
  Reporter 部分に以下のように書いてOKを押す。

   count turtles

  これで、setup、goを押すとタートルの数がリアルタイムで表示される。
  同様に、緑のパッチの数を数えたいときはもう1つモニターを作成し、reporter 部分に

   count patches with [ pcolor = green ]

 と記述することで緑のパッチの数が表示される。

プロットの追加


  1. プロットを選択し、配置したい場所をクリックする。
    作ったプロットを右クリック、Editを選択。下図のように入力する。


  2. Renameを押し、出てきたウインドウに turtlesと入力。
    Createを押し、grassと入力。colorgreenへ変更し、OKを押す。

 3. Procedureタブに切り替え、setup と go それぞれに以下の処理を付け加える。

   do-plots

  4. do-plot を定義する。

   to do-plots
    set-current-plot "Totals"
    set-current-plot-pen "turtles"
    plot count turtles
    set-current-plot-pen "grass"
    plot count patches with [pcolor = green]
   end

   ここでは、プロットの名前を "Total" に、
   "turtles" と "grass" の数をプロットするよう処理している。


カウンターの追加


 NetLogoには tick という標準関数が用意されている。これを用いてカウンターを作成する。

 1. go を以下のように書き換える。

   to go
    if ticks >= 500 [ stop ]
    move-turtles
    eat-grass
    reproduce
    check-death
    regrow-grass
    tick
    do-plots
   end

   tick を加えることで、go が一回行われると ticks が1増えるようになる。
   この場合は最初の if文で、
    ticks が500以上になったら動作を止める
   よう定義している。



サンプルコード


 以下に出来上がったプログラムの全文を掲載する。公式ページより引用(一部改変)




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



タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2009年05月22日 00:50
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。
添付ファイル