Reputation: 205
I have created roads (gray patches) on which people (turtles) are randomly moving. At the intersection point (white patches) I am making them (turtles) to may or may not turn their headings. In the way I coded, if their headings are 90 and 270 it is working fine. But if heading is 0 almost 95% of the times they turn back (180) instead of turning to left(270) or right(90). There is no error in the code. I just simply want to know, can we code in some other way so that 0 heading turtles can also have 50% of chances for all the three turns like the other two headings.
Following is my code:
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
Upvotes: 2
Views: 615
Reputation: 17678
I ran your code with the speed slowed down and a single person created. I don't think the problem is your random direction creation - it is that the speed means that the person spends multiple ticks on the intersection and only leaves when the random direction generated moves them down again. For example, the agent enters the intersection with a heading of 0 and gets random heading 90 so moves to the right by some distance < 1 (from speed) and IS STILL IN THE INTERSECTION, so gets a new random heading say 90, still in intersection, then gets 270 so still in intersection and then gets 180 so leaves.
One way to fix this is to create a counter / flag. It gets set on reaching the intersection and the intersection is not checked if the flag is still set.
to go
ask people [ fd speed
set just-turned max (list 0 just-turned - 1)
check-intersection
check-end]
end
to check-intersection
if intersection? and just-turned = 0 [
set just-turned 1 / speed + 1
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
By the way, when I did this testing, I also noticed that at least some (and possibly all) agents created in the base of your T shaped streets have a heading of 45.
Also, some code suggestions, instead of things like
set head random 2
if head = 0 [ set heading 0]
if head = 1 [ set heading 180]
1/ you could use (so it doesn't check every if statement)
set head random 2
ifelse head = 0 [ set heading 0] [ set heading 180]
2/ or (so you don't have to create the local variable for the random number)
set heading ifelse-value (random 2 = 0) [0] [180]
3/ or (so you can have different commands for different cases)
ifelse random 2 = 0 [ set heading 0] [ set heading 180]
4/ or (because the mathematical relationship exists in this case)
set heading random 2 * 180
Number 4 would be best in this case, but number 3 would also work for sections where you have different commands (like [lt 90][rt 90])
Upvotes: 2