I'm having major trouble trying to direct turtle movement toward an amount of patch-variable in-cone 20 45 10, and I think it might be because I am trying to modify uphill-chemical to fit the new reporter. For instance, with the code below, the ants were jumping across the world from one side to another. I just don't know how to clearly direct turtle movement toward the total-chemical-in-cone, as designated by the reporter below, instead of just uphill-chemical of the surrounding eight patches. Is the uphill primitive interfering with my goal, which is to direct movement toward the chemical within the entire cone, and if so, what could I use, since fd 1 is not directing movement properly?
to uphill-total-chemical-scent-in-cone
let scent-ahead total-chemical-scent-in-cone 20 45 10
let scent-right chemical-scent-at-angle 90
let scent-left chemical-scent-at-angle -90
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
if scent-ahead > scent-left or scent-ahead > scent-right
[fd 1]
to-report total-chemical-scent-in-cone [cone-distance angle angle-width ] ; ant procedure - reports the mean amount of pheromone in cone
rt 45
let p patches in-cone 20 10 ;; cone distance 20 patches angle 45 angle width 10
lt 45
if p = nobody [ report 0 ]
;ask p [ show chemical ]
report (sum [chemical] of p)
end
Related
How do you direct turtle movement into a cone? I want turtles to move into a cone of highest chemical. I tried face cone, but it triggered the error that
nothing named cone has been defined.
I am aware that uphill-chemical is an existing way to direct turtle movement, but it is not working in my model, since the turtles wander in and out of the trail, instead of clearly following it towards one direction.
My proposed code is:
to uphill-total-chemical-scent-in-cone
let gradient-color red ;; gradients are a temporary debugging feature
let scent-ahead total-chemical-scent-in-cone 20 0 10
let scent-ahead-2 total-chemical-scent-in-cone 20 -45 10
let scent-ahead-3 total-chemical-scent-in-cone 20 45 10
ifelse (scent-ahead-2 > scent-ahead) or (scent-ahead-3 > scent-ahead)
[ ifelse scent-ahead-3 > scent-ahead-2
[ face cone 20 45 10 hatch-gradients 1 [set color gradient-color ]] ;;
not sure how to direct movement into the cone, but could try face or some variation/ask for help
[ face cone 21 -45 10 hatch-gradients 1 [set color gradient-color ]]]
[face cone 20 0 10 hatch-gradients 1 [ set color gradient-color fd 1]]
end
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 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 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.
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
]
]
]