2016-10-18 7 views
2

Railsback & Grimm 2012の書籍を使用して、NetlogoでABMを作成する方法を教えています。「バーチャル」回廊に続く1冊の本の演習に問題があります。基本的な考え方は、蝶は、ガイドとしての高さの違いを使って交配するために上り坂に行くということです。私は、蝶が最初のパッチから最終パッチまで飛ぶ平均距離を超えて、蝶によって使用されたパッチの数を分割する回廊の幅を計算する必要があります。私は が、私はこのようにコーディングしています。この廊下の幅を、プロットに苦しんでいます:Netlogo:開始パッチと終了パッチ間の平均距離を測定する

to-report corridor-width 

    let patches-visited count patches with [used?] 
    let mean-distance mean [distance start-patch] of turtles 
    report patches-visited/mean-distance 

私は、コマンドでインターフェイスのプロットを作成しました:

plot corridor-width 

私が取得エラーメッセージが読み取ります。

ゼロ除算。エラーオブザーバが実行中に/ボタン「セットアップ」私は信じて

によって呼び出さプロット「回廊幅」ペン「デフォルト」の更新手続きSETUPによって呼び出さ コードによって呼び出される手続き CORRIDOR-WIDTHによって呼び出されると間違って何かがあります私は符号化していますdistance start-patchしかし、私はウェブをサーフィンし、いくつかのコードを見て、私は間違いを見つけられません。私のコード全体は次のようになります:

globals [ q ]     ;; q is the probability that butterfly moves directly to highest patch 
turtles-own [ start-patch ] 
patches-own [ elevation used? ]  ;; patches property of elevation and whether the patch has been used by butterfly or not. 

to setup 
    ca 

    ;; Let's create patches and asign them an elevation and color by using ask patches statement 

    ask patches 

    [ 
    ;; Elevation decreases linearly with distance from the center of hills. Hills are at (30,30) and 
    ;; (120,120) coordinates. The first hill is 100 units high whereas the second one is 50 
    let elev1 100 - distancexy 30 30 
    let elev2 50 - distancexy 120 100 

    ifelse elev1 > elev2 
    [ set elevation elev1 ] 
    [ set elevation elev2 ] 

    set pcolor scale-color green elevation 0 100 

    set used? false 
    ] 

    ;; Create 50 butterflies 

    crt 50 

    ask turtles [ 

    set size 6 

    ;; set their initial location as their initial patch 

    setxy random-pxcor random-pycor 

    set start-patch patch-here 


    ;; have the butterfly draw its path with the pen-down statement 
    pen-down 
] 

    reset-ticks 

    ;; Initialize the q parameter 
    set q 0.4 

end 

;; The master schedule 

to go 

    ask turtles [ move ] 
    plot corridor-width 
    tick 
    if ticks >= 1000 

    [ 
    let final-corridor-width corridor-width 
    write "Corridor width: " print final-corridor-width 
    ;export-plot "Corridor width" (word "Corridor-width-output-for-q-" q ".csv") 
    stop 

    ] 

end 

;; let's code the butterfly procedure of movement 

to move 

    if elevation >= 
    [ elevation ] of max-one-of neighbors [ elevation ] 
    [ stop ] 

    ifelse random-float 1 < q    ;; Decide whether to move to the highest sorrounding 
              ;; patch with p=q 

    [ uphill elevation ]      ;; move deterministically uphill 
    [ move-to one-of neighbors ]    ;; or move randomly 

    set used? true 

end 

to-report corridor-width 

    let patches-visited count patches with [used?] 
    let mean-distance mean [distance start-patch] of turtles 
    report patches-visited/mean-distance 

end 

答えて

1

平均距離が0の場合はどうなりますか?

let mean-distance mean [distance start-patch] of turtles 

基本的に、セットアップでは、すべてのタートルのスタートパッチを現在のパッチに設定します。あなたが最初のパッチからどのくらい離れているかをあなたがすべてのカメに尋ねれば、彼らはすべてあなたに0ユニット離れたことを伝えます。

したがって、[distance start-patch] of turtlesはすべての0のリストで満たされます。

したがって、0のすべてのリストの平均は0であり、0で除算するエラーが発生します。

はたぶん、このような状況で、あなたは私のミスが何であったかを説明するためにあなたに感謝@matttsapので

ifelse mean-distance = 0 
[ report 0] 
[report patches-visited/mean-distance] 
+0

...代わりに0を報告したいです。私のカメは実際に動いているので、開始パッチと終了パッチの間の距離は0にならないはずです。私がコードしようとしていたのは、それぞれの実行後に私のカメの開始パッチと終了パッチの間の平均距離を得ることでした。この目的のために私のコードをどのように変更すべきですか? – AnnK

+0

まあ、ポイントは、あなたが問題が発生している最初の部分を含め、すべてのティックで平均距離を計算しているということです。単一の点だけが必要な場合は、目盛りごとに平均距離をプロットしないでください。代わりに、シミュレーションの停止基準を定義し、停止基準[show corridor-width]または何かこれに似ています。 – mattsap

関連する問題