I write algorithms for red cars turn right with direction from right to drive (heading = 90) towards heading = 180.
but its car turn right, go along too. how it came to a certain degree, it is first turned the corner to go towards heading 180
I tried some code without the right result?
[
ifelse heading = 90
[
if distancexy 0 0 < 30
[
rt 22
fd speed
if xcor = -2
[
set heading 180
fd speed
]
]
]
[
fd speed
]
or I write code like this?
ifelse heading = 90
[
if distancexy 0 0 < 30
[
rt 22
fd speed
if ycor = -30
[
set heading 180
fd speed
]
]
]
[
fd speed
]
You are using exact conditions like xcor = -2 and ycor = -30, but these conditions are probably never true.
NetLogo turtles don't move smoothly. They jump from point to point. Imagine the turtle disappearing from its old location and reappearing in its new location. The turtle doesn't pass through all the points in between.
For example, suppose your turtle has an xcor of -2.2 and is facing east. If the turtle does fd 1, its new xcor will be -1.2. But that doesn't mean code that says xcor = -2 will run. The turtle's xcor was never -2. At one instant, it was -2.2. At the next instant, it was -1.2. So xcor = -2 was never true.
Instead of conditions like xcor = -2, you probably want to be using conditions like pxcor = -2 (true anywhere in a whole patch) or xcor > -3 and xcor < -2 — that kind of thing.
JenB already told you this once a week ago at https://stackoverflow.com/a/34313275/86485 . I am just saying the same thing again. It's crucial you understand this.
Related
I'm new to NetLogo and I'm unsure of how to place 300 spartans in a narrow row. Let's say an area of 2x5 patches, turtles overlapping one another. I have tried using sprout, this achieved the specific coordinate requirements but the turtles are only one per patch.. Here is some code I have.
ask patches with [pxcor > 0 and pycor > -2 and pycor < 2]
[ sprout 1 [ set color red ] ]
or
to setup-spartans
create-spartans 300
set-default-shape turtles "person"
ask spartans
[ setxy random-xcor -3 ;; makes only a single row and goes across entire screen
;; (I need it to be in a specific area)
set heading 180
set color red ]
end
How about something like this?
create-spartans 300 [
set xcor -2 + random-float 5
set ycor -1 + random-float 2
]
I want my turtles to hatch (= make one more turtle) when they cross a specific line. I have tried the command ifelse?, and I can get it to work on a simple model, when my turtles randomly wanders: If they move to a patch on the left side (xcor < 0) they die, if they make a move to a patch with xcor > 0 they hatch 1.
But I want the proces to be linked to witch patch they come from. If they stand on a patch with xcor < 0 and moves to another patch with xcor < 0 they shall die - but if they change xcor from negative to positive - they should multiply (= hatch 1).
My problem is: Is it possible to write a code-string that "remembers" the turtles position one tick before and use it to either let the turtle die og multiply?
{
to setup
clear-all
create-turtles 20
ask turtles [set size 2
set heading random 45 ;; turtle is now facing northeast
setxy random-xcor random-ycor
set color white
set shape "person"]
end
to go
ask turtles
[ rt random 360 ; turns in a random direction
fd 4 ;; all turtles move forward one step
rt random 180 ;; ...and turn a random amount
fd 4
lt random 180
]
ask turtles
[ifelse pxcor > 0
[hatch random 2]
[die]]
end }
You can use a turtle variable to give each turtle a memory.
turtles-own
[
old-xcor
]
Just before the turtle moves, assign ‘xcor‘ to that variable.
To move-turtle
Set old-xcor xcor
< Your movement code >
if old-xcor < 0
[ Ifelse xcor < 0
[ Die ]
[ Hatch 1 ]
]
End
I want to ask to turtles who on the cells except the origin cell (x, y) = (0, 0) in one-dimensional cells spaces.
The following is sample code, however, this syntax includes the origin cell (x, y) = (0, 0).
ask turtles with [xcor < max-pxcor]
Please advice here. Thank you.
Do I have it right that you want to ask turtles that are not on patch 0 0? If so, maybe this will work for you:
to setup
ca
crt 100 [ move-to one-of patches ]
reset-ticks
end
to go
ask turtles with [ patch-here != patch 0 0 ] [
rt random 61 - 30
fd 1
]
tick
end
Edit: If you want to ask turtles not on the origin patch (in this example, 0 0) and turtles not on a patch with max-pxcor:
to go-2
ask turtles with [ floor xcor > 0 and xcor < max-pxcor ] [
rt random 61 - 30
fd 1
]
tick
end
I am currently building a model for people walking on a certain path (road patches, color = white). The agents' movement will only be limited on the white patch and when they encounter black patch they will rotate 180 deg.
The problem I found was when I tried to set the heading of the agents. I want the heading to walk to the nearest red patch (destination), but when I use:
1
set heading towards min-one-of patches with [pcolor = red ] [ distance myself ] <
the agents wont move from their patch. It turned out that the "heading" generated by the above code has the value of -360 to 360 with decimals in it. The walking procedure that I have built only allows heading with multiplies of 90 (-360, -180 ... 90, 180, 270, 360). This is the full code for my walking procedure:
2
globals [ flagl flagr ]
ask agents [
;reset flag
set flagr 0
set flagl 0
; check and note if there is a path on the left or the right
ask patch-left-and-ahead 90 1 [if (pcolor = white ) [set flagl 1]]
ask patch-right-and-ahead 90 1 [if (pcolor = white ) [set flagr 1]
]
;in T-junction, decide to turn left or right (random)
if((flagl = 1) and (flagr = 1))
[
ifelse random 100 > 50
[set heading heading - 90]
[set heading heading + 90]
]
;if it's only applicable to turn right, then turn right
if((flagl = 0) and (flagr = 1)) [set heading heading + 90]
;if it's only applicable to turn left, then turn right
if((flagl = 1) and (flagr = 0)) [set heading heading - 90]
;return if there's no mor path
if [pcolor] of patch-at-heading-and-distance heading 1 = black [rt 180]
;agent movement
**;face min-one-of patches with [pcolor = red ] [ distance myself ]**
fd 1
;stopping procedure
if [pcolor] of patch-here = red [fd 0]
]]
I tried to round the heading generated by #1 like this:
let direction heading towards min-one-of patches with [pcolor = red ] [ distance myself ]
if direction != mod 90
set direction 90 * round(arah/ 90)
However it gives error notice since "let" command cannot take that kind of input.
This is the screenshot of the map I have built:
Map
Any help to solve this problem is hugely appreciated. Thank you!
This might get you started- with minimal checking it seems to do the trick, but you will probably want to check for exceptions.
The cardinal-4 reporter takes whatever input is given and checks if the modulo is equal to 0. If not, it will check if modulo is less than/equal to 45- if so, subtracts that value from the direction (dir). If modulo is greater than 45, the subtract the modulo from the direction and add 90.
The check procedure just prints the reporter output for the values listed.
to setup
ca
reset-ticks
end
to-report cardinal-4 [ heading-input ]
let dir heading-input
let remain dir mod 90
if remain != 0 [
ifelse remain <= 45 [
set dir dir - remain
]
[
set dir dir - remain + 90
]
]
report dir
end
Edit: improved check procedure:
to check
let testVals [ -361 -359 -271 -269 -181 -179 -91 -89 -1 0 1 89 91 179 181 269 271 359 360 361 ]
print map [ i -> ( word i " becomes " cardinal-4 i "\n" ) ] testVals
end
I want to write a NetLogo program to merge automobile lanes. Vehicles are in 4 lanes, separated by 3.5 meters (with each patch representing 1m). The center coordinates of each lane are at ycor values of -3.75, -7.25, -10.75 and -14.25.
Vehicles have random xcor values with ycor values at the center of one of the lanes, and are heading to the right. I want the traffic to merge so that cars driving toward the center of the map (distancexy 0 0 <50) all move to the same lane at ycor = -14.25 as pictured. So the car already in that lane continues forward, but the cars in other lanes turn right 45 degrees to switch lanes and then turn left by 45 degrees when they reach the pycor = -14.25 lane.
The cars turn right. However, the conditions I have set to turn the car left again when it reaches ycor = -14.25 are not working. Instead, the car continues straight ahead, crossing the lane as in the next figure.
My code is:
ifelse ycor = -14.25
[ fd speed ]
[ rt 45
fd speed
ifelse ycor = -14.25
[ lt 45
fd speed ]
[ fd speed ]
]
]
You wrote:
if ycor = -10.75
[
rt 45
fd speed
;;;fd 5.1
ifelse ycor = -14.25
[
lt 45
fd speed
]
[
fd speed
]
]
If I leave out some things that don't matter, that's:
if ycor = -10.75
[
...
ifelse ycor = -14.25
[
...
The ifelse is inside the if, so it only runs if ycor is -10.75. But how can ycor be equal to -10.75, and equal to -14.25? It can't, so the second condition never triggers.
Perhaps the structure you intended is:
ifelse ycor = -10.75
[
...
]
[
ifelse ycor = -14.25
[
...
this is how you express "if ycor is -10.75, do this; but if ycor is -14.25, do that instead".
I think your problem is that ycor never exactly equals -14.25 unless it starts at -14.25. This is because the car moves forward and only checks its position after the movement, so it might move to -14.5 or -14.0 or some other value that is not -14.25. In that case, you want it turn left whenever it gets close to the -14.25 lane. Try something like this:
ifelse ycor = -14.25
[ fd speed ]
[ if heading = 90 [ rt 45 ]
fd speed
if ycor <= -12.5
[ set heading
set ycor -14.25
]
]
ifelse ycor = -14.25 [
fd speed
]
[
rt 45
fd speed
ifelse ycor = -14.25
[
lt 45
fd speed
]
[
fd speed
]
]
]