【发布时间】:2026-02-13 03:10:01
【问题描述】:
先生,我正在做一个项目,有时我会在这里发布与该问题相关的问题。我创建了一个模拟场景,其中海龟(机器人)在空间中四处移动,并且每个机器人都跟踪其访问过的补丁。在移动中,如果访问了patch-ahead 1 补丁,那么我需要将机器人向左转 45 度并再次检查patch-ahead,直到它检查所有 8 个邻居,如果这 8 个邻居中的任何一个未被访问,那么它应该移动到那个修补并继续探索。但是如果所有 8 个都被访问,那么它应该在检查所有 8 个邻居之后移动到当前标题前面的补丁,无论它是否被访问。
这是我正在使用的一段代码。
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 : 我正在使用一个名为“memory”的列表来存储访问过的位置。
-
顺便说一句,您是否知道如果您一遍又一遍地向它添加已经访问过的补丁,
memory可能会永远增长?这可能不是问题,但如果成为问题,您可能需要考虑从列表切换到patch-set,或者至少偶尔在您的列表中调用remove-duplicates...
标签: netlogo