선생님 나는 프로젝트에서 일하고 있으며 그 문제와 관련하여 여기에 질문을 게시 할 때가 있습니다. 거북이 (로봇)가 공간에서 움직이는 시뮬레이션 시나리오를 만들었고 각 로봇이 방문한 패치를 추적합니다. 이동시에 patch-ahead 1
패치를 방문하면 로봇을 45도 왼쪽으로 돌리고 patch-ahead
을 다시 확인해야합니다. 8 명이 하나라도 유엔 방문하면 8 이웃을 모두 확인한 다음 그 패치로 이동해야합니다. 탐구. 그러나 8 명이 모두 방문하면 방문한 모든 8 명의 이웃을 확인한 후 현재 제목 앞에있는 패치로 이동해야합니다. 여기에 제가 사용하고있는 코드 조각이 있습니다. 방문한 패치 피하기 netlogo
breed [robots robot ]
robots-own[ state memory ]
patches-own [ is-obstacle? ]
to setup
__clear-all-and-reset-ticks
create-robots num [
set memory (list patch-here)
]
draw-obstacles
ask patches [if pxcor = 0 and pycor = 0 [ set pcolor black ]]
end
to draw-obstacles
ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [set pcolor red set is-obstacle? true]
; set pcolor red
ask patches [ ifelse pcolor = red [ set is-obstacle? true ][ set is-obstacle? false ] ]
ask patches with [ count neighbors != 8 ] [ set pcolor red set is-obstacle? true ]
end
to make-obstacle
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor red]]
]
end
to remove-obs
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor black]]
]
end
to go
ask patches [if (pcolor = red)[set is-obstacle? true]]
ask patches [if (pcolor = black)[set is-obstacle? false]]
ask-concurrent robots ; wanderers instructions
[
rt random-float rate-of-random-turn
lt (rate-of-random-turn/2)
set-state
move-robots
]
tick
end
to move-robots ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "Unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 1) [
set state "disperse"
] [
set state "explore"
]
end
to disperse ;;turtle proc
avoid-obstacle
move1
; move-to one-of patch-set [neighbors] of neighbors
end
to explore
move
;search-open-room
avoid-obstacle
;move-to one-of neighbors
end
to move
fd speed
set memory lput patch-here memory
if ((member? patch-ahead 1 memory) or ([is-obstacle?] of patch-ahead 1))
[ lt random 45
]
end
to move1
fd speed
end
to avoid-obstacle
set memory lput patch-here memory
if ([is-obstacle?] of patch-ahead 1)
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading - 5) 1
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading + 5) 1
[
ifelse random 1 = 0
[ rt 40 ]
[ lt 40 ]
]
[ rt 60 ]
]
[lt 60]
]
end
to search-open-room
ask robots[
ifelse ([is-obstacle?] of patches in-cone 2 150)
[ rt 45 ] [ move ]
]
end
그러나 move
절차
lt random 45
단지 수 있어요. 위에서 언급 한 시나리오에 따라 변경하는 방법. 나는
while
루프와
repeat
문장으로 많은 시도를했지만 코드가 작동하지 않는 것 같습니다.
패치가 방문되었는지 정확히 어떻게 알 수 있습니까? –
David Merinos : "메모리"라는 목록을 사용하여 방문한 위치를 저장하고 있습니다. – Ashfaq
그런데 이미 방문한 패치를 계속 추가하면 '메모리'가 잠재적으로 영원히 커질 수 있다는 것을 알고 계셨습니까? 이것은 문제가되지는 않지만, 하나가된다면 목록에서 ['patch-set'] (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#)로 바꾸는 것이 좋습니다. 패치 세트) 또는 적어도''remove-duplicates' (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#remove-duplicates)를 목록에서 잠시 동안 호출하십시오 ... –