【发布时间】:2015-07-20 19:02:45
【问题描述】:
我已经创建了人们(乌龟)随机移动的道路(灰色区域)。在交叉点(白色斑块),我让它们(海龟)可能会或可能不会转向他们的航向。以我编码的方式,如果他们的标题是 90 和 270,它工作正常。但是,如果航向为 0,则几乎 95% 的时间他们会返回 (180) 而不是向左 (270) 或向右 (90)。代码中没有错误。我只是想知道,我们能否以其他方式编码,使 0 航向的海龟也可以像其他两个航向一样在所有三个转弯中都有 50% 的机会。
以下是我的代码:
breed [people person]
globals [ streets intersections head]
people-own [ speed direction]
patches-own [ intersection? ]
to setup
clear-all
ask patches [set pcolor green
set intersection? false]
setup-street
setup-people
end
to setup-street
;set streets patches with [ (( pxcor < max-pxcor and pxcor > min-pxcor ) ;and (pycor = 4) or (pxcor = 0 and ( pycor > min-pycor and pycor < 4))]
set streets patches with [ (( pxcor < max-pxcor and pxcor > min-pxcor ) and (pycor = 4 or pycor = 5)) or ((pxcor = 0 or pxcor = 1) and ( pycor > min-pycor and pycor < 4))]
set intersections patches with [ (pxcor = 0 or pxcor = 1) and (pycor = 4 or pycor = 5)]
ask streets [set pcolor gray]
ask intersections [ set pcolor white]
setup-intersection
end
to setup-intersection
ask intersections [ set intersection? true]
end
to setup-people
ask n-of No-of-person patches with [ pcolor = gray or pcolor = white] [
sprout-people 1 [
set color brown
set size 1
set shape "person"
set speed 0.01 + random-float 0.9
set direction random 2
set-default-headings direction
]
]
end
to set-default-headings [ direct]
if direct = 0 [
;ask people with [ ( pxcor < max-pxcor and pxcor > min-pxcor ) and pycor = 4 ]
ask people with [ (pxcor < max-pxcor and pxcor > min-pxcor ) and (pycor = 4 or pycor = 5)]
[ set head random 2
if head = 0 [ set heading 90]
if head = 1 [ set heading 270]
]
]
if direct = 1 [
;ask people with [ pxcor = 0 and ( pycor > min-pycor and pycor < 4) ]
ask people with [(pxcor = 0 or pxcor = 1) and ( pycor > min-pycor and pycor < 4)]
[ set head random 2
if head = 0 [ set heading 0]
if head = 1 [ set heading 180]
]
]
end
to go
ask people [ fd speed
check-intersection
check-end]
end
to check-intersection
if intersection? [
if heading = 90 [if random 20 < 10 [ set heading 180] ]
if heading = 270 [if random 20 < 10 [ set heading 180] ]
if heading = 0 [ let new-heading random 2
if (new-heading = 0) [rt 90 ]
if (new-heading = 1) [lt 90 ] ]
]
end
to check-end
ask people [ if [pcolor] of patch-ahead 1 = green [set heading [heading] of self - 180 ] ]
end
【问题讨论】:
-
请提出一些想法。我会很感激的。
标签: netlogo