NetLogo 6.1.1에는 녹색 패치 (부시)에서 다른 품종 (올무)을 찾는 한 품종 (레인저)이 있습니다.
내가 얻을 수없는 행동은 다음과 같습니다.
스네어가 발견되지 않으면 (20 % 확률) 동일한 절차 (핫스팟 검색, 주석 처리 된 섹션-> 무한 루프 발생)를 5 번 반복합니다.
이 5 회 동안 스네어가 제거되지 않으면 정상적인 동작을 재개하십시오 (무작위 우회전 및 전진 1).
어떻게 할 수 있습니까?
;; globals =========================
breed [rangers ranger]
breed [snares snare]
rangers-own [snare-found?]
;; initialise ========================
to setup
resize-world -4 4 -4 4
clear-all
create-rangers 1 [set color blue]
ask patches [
if random 100 < 20 [set pcolor green]]
ask patches with [pcolor = green] [
sprout-snares random 5]
ask snares [
set size 0.5
set shape "dot"
fd 0.25
]
reset-ticks
end
;; main ==================================
to go
ask rangers [
rt random 181 - 90
fd 1
if any? snares-here [hotspot-search]
hotspot-search
]
end
;; functions ===============================
to hotspot-search
move-to patch-here
let target one-of snares-here
if (random 100 < 20) and (target != nobody) [
ask target [die]
set snare-found? true
hotspot-search
]
;;if snare-found? = true [
;; repeat 5 [hotspot-search]
;;]
end
한 가지 관찰은 성공 확률 20 %에서 레인저가 5 번 검색하기를 원하지만 시간이나 에너지 손실과 같은 검색 비용이 들지 않으면 한 번에 한 번만 확인하면됩니다. 67 % 성공 확률 (1.00 - 0.80^5) = 0.67
.
그러나 비용이 필요하거나 검색 횟수 등을 추적해야하는 경우 코드를 작성하는 방법은 다음과 같습니다. 업데이트하는 대신 처음부터 작성해서 죄송합니다. 가능한 한 간단하게 유지하려고 노력했습니다.
breed [ rangers ranger ]
breed [ snares snare ]
to setup
create-snares 100 [
set shape "circle"
set color blue
set size 0.5
move-to one-of patches
]
create-rangers 10 [
set color red
move-to one-of patches
]
end
to go
ask rangers [
move-to one-of neighbors4
; call our procedure with the starting limit of 5
hunt-snares 5
]
end
to hunt-snares [ search-limit ]
; the recursive base case is search-limit of 0
if ( search-limit > 0 ) [
; can only find a snare if one exists here
ifelse random 100 < 20 and any? snares-here [
; found one
ifelse random 100 < 80 [
; disabled
ask one-of snares-here [ die ]
; restart the search
hunt-snares 5
] [
; we found a snare but failed to disable it, unclear what to do?
hunt-snares 5
]
] [
; failed to find one, move towards the base case
hunt-snares (search-limit - 1)
]
]
end
추가 할 편집 : 죄송합니다. 원래 코드가 더 복잡하다고 생각했습니다. 실제로는 비교적 간단 해 보이며 방금 버전을 수정했을 것입니다. 당신의 이유 hotspot-search
때문에 절차가 무한 루프에 가고는 snare-found?
설정되지 않습니다 false
코드에서 아무 곳. 재귀를 처리하는 일반적인 방법은 마지막 재귀 호출이 종료 될 수 있고 전체 호출 체인도 종료되는 기본 케이스를 향해 재귀 프로 시저의 하나의 매개 변수가 진행되도록하는 것입니다.
이 경우 두 가지 문제가 있습니다. 1) 매개 변수가 아닌 암시 적 기본 케이스-무장 해제를 시도 할 패치에 여전히 스네어가 있다는 것, 2) 기본 케이스에서 더 멀리 매개 변수를 재설정 할 수 있다는 것, 항목 1을 아는 것은 반복되지 않는다는 것을 의미합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다