ฉันกำลังสอนตัวเองถึงวิธีสร้าง ABM ใน Netlogo โดยใช้หนังสือ Railsback & Grimm 2012 ฉันประสบปัญหากับแบบฝึกหัดหนังสือเล่มหนึ่งซึ่งอยู่บนผีเสื้อตามทางเดิน "เสมือน" แนวคิดพื้นฐานคือให้ผีเสื้อขึ้นเนินเพื่อผสมพันธุ์โดยใช้ส่วนสูงที่ต่างกันเป็นแนวทาง ฉันจำเป็นต้องคำนวณความกว้างของทางเดินโดยแบ่งจำนวนแผ่นที่ผีเสื้อใช้ตลอดระยะทางเฉลี่ยที่ผีเสื้อบินจากจุดเริ่มต้นไปยังจุดสิ้นสุด ฉันกำลังดิ้นรนกับการวางแผนความกว้างของทางเดินนี้ ซึ่งฉันกำลังเขียนโค้ดดังนี้:
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
ข้อความแสดงข้อผิดพลาดที่ฉันได้รับอ่าน:
การหารด้วยศูนย์. ข้อผิดพลาดในขณะที่ผู้สังเกตการณ์ทำงาน / เรียกตามขั้นตอน CORRIDOR-WIDTH เรียกโดยพล็อต 'ความกว้างของทางเดิน' ปากกา 'ค่าเริ่มต้น' รหัสอัปเดตที่เรียกโดยขั้นตอน SETUP เรียกโดยปุ่ม 'ตั้งค่า'
ฉันเชื่อว่ามีบางอย่างผิดปกติกับวิธีที่ฉันเขียนโค้ด 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