Netlogo Cars Merging - netlogo

I am working on a simulation of a one lane road merging with a two lane road, all going the same direction. The cars that are not merging (cars1) check for merging cars within a radius of 2 to see if they need to change into the left lane and if they do, the car checks the left lane to see if it is safe.
ask cars1[
if (not any? turtles-at -2 4) and (not any? turtles-at -1 4)
and (not any? turtles-at 0 4) and (not any? turtles-at 1 4)
and (not any? turtles-at 2 4) and (not any? turtles-at 3 4)
and (any? merging-cars in-radius 3)
[ set ycor 2]]
I am trying to make the merging cars stop if it is not safe for the cars1 in the closest lane to change to the left lane.
ask merging-cars[
loop[
if[any? cars1 in-radius 2]
[stop]
]]
That is the code that is not working. I haven't figured out a way to make the merging cars stop if it is unsafe and go when it is safe.
Thanks

To get you on the right direction have a look at the not any? documentation and make a note that you can incorporate the angle of which to check as well as right or left with a command such as :
not any? turtles-on patch-right-and-ahead 60 2
this is saying.... check the angle of 60 degrees to the right 2 patch ahead of this turtle.

Related

Two Unrelated Conditions - Expected Command Error

I am trying to get the turtle breed foragers to reduce the food source by a small amount and become green if there is a leader, but to become orange and reduce the food by 1 unit if not, which allows the first turtle to reach the food patch to become the leader.
To the best of my knowledge, the two conditions linked by and should work, but for some reason I get an "expected command" error. Could you help resolve this?
to look-for-food ;; turtle procedure
if food > 0 and not any? leaders
[ set color orange + 1 ;; pick up food
set food food - 1 ;; and reduce the food source
rt 180
stop]
[set color green + 1
set food food - .5
rt 180
stop]
end
Also, how do I incorporate food quality and chemical, which was originally an if procedure that was part of look-for-food? Or do I need to write a new procedure for that?
if (chemical >= 0.05) and (chemical < 2)
[ uphill-chemical ]
if food-quality > 0
[uphill food]
You need ifelse not if. The if tests runs the code block if the condition is true but does nothing (just skips it) if it is false.

How to specifically change a variable of an agent in the agentset?

I am trying to change a variable (score) of a particular agent in the agent set if it meets the specific condition of a patch. This is called by an another agent. To be more clear. My idea is for instance if there is a breed (horse) and it sees the patch (grass) and it is standing on another breed (vertices - since horse move along a path connected by nodes represented by vertices) - a score variable to added to vertices-own where if the grass quality <=3, it would add a score to the vertex on which it stands.
ask horses[
ask patches in-cone 50 60 [
if grass-quality <= 3 ask vertices with [min-one-of vertices in-radius 0 [distance myself] [set vertex-score vertex-score + 1 ]]]]
I know something is wrong with this code logic. I am trying to convert my mentioned thought into codes. Kindly suggest me.
Thank you all.
Regards,
Heng wah
NetLogo agent (turtle) positions are continuous numbers so it is generally wrong to try and say something like 'if another turtle is where I am'. While you may have got there using move-to, it's probably safer to have the horse identify a vertex that is very close to it rather than in the exact position. You have used radius 0 but I'm going to change that to 0.001 to allow for potential errors in position.
ask horses
[ if any? patches in-cone 50 60 with [ grass-quality <= 3 ]
[ let my-vertex min-one-of vertices in-radius 0.001 [distance myself]
ask my-vertex
[ set vertex-score vertex-score + 1 ]
]
]
]
This is not tested, but I have simply reorganised your code. You had some bracketing issues and you were also asking vertices to find the closest vertex (which would have been itself), rather than having the horse find the closest vertex.
It's also not necessary to separate the let and the ask but I thought that would be easier for you to see how it works.

How to set the target of the near turtle and follow it when world is not allowed to wrap (without going througt the walls)

I have a problem with following the nearest candida (breed) by another turtle - neutrophil (breed). Candidas can't move and neutrophil looks for the nearest candida in the area, face to him and go forward. Everything is fine when world is allowed to wrap. Then neutrophils go througt the "walls" and chase candida.
BUT the point is that world can't wrap.
So I created condidion with help another model, which bounce the neutrophil, when they reach one of the max coordinates. But now they keep bouncing from the walls and don't looking for another target.
I will be very thankful for help.
to lookfor ;;if meet candida in near area - chase, if not - continue looking for
ifelse any? candidas [ chase ] [ move ]
end
to chase ;;set the target on the nearest candida, face to them and move one step to him
set nearest-candidas min-one-of candidas [ distance myself ]
face nearest-candidas
bounce fd 1
end
to move
bounce
fd 2
end
to bounce
if abs pxcor = max-pxcor [ set heading (- heading) ] ;; bounce off top and bottom walls
if abs pycor = max-pycor [ set heading (180 - heading) ]
if abs pzcor = max-pzcor [ set heading (- heading) ]
set nearest-candidas max-one-of candidas [ distance myself ]
end
edit: Oh, you are in NetLogo 3D. That changes everything, unfortunately. Without being able to prevent the world from wrapping, there is no easy way of doing this. If you're feeling adventurous, you could try to find a way of ensuring that the nearest turtle is on the "right side of the wall" by triangulating with other turtles in the world. It wouldn't be easy though, but I could see that working. Good luck.
Outdated response:
I think the problem might be that in your bounce procedure, you have
set nearest-candidas max-one-of candidas [ distance myself ]
Using max-one-of sets the candidas to the one furthest AWAY from the neutrophil. You have it correctly in your chase procedure in which you set it to min-one-of (that is min one of, not max one of). Does changing that give you the behavior you were expecting?

Move turtles to other turtles

I created a model where birds shall fly on a certain route. I created this route by placing 10 turtles I called "rasts" (resting place) on patches an linking them with each other. Now, I want to make the birds fly on these routes and when a resting place disappears (by using an "on/off" switch), they shall take another route.
The birds don't fly on the route I created at the moment.
My question is:
Is it possible to make turtles (birds) follow a route an change it, when there's another turtle (rast) in there way?
My code with the "move" command:
breed [rasts rast]
breed [birds bird]
to setup
setup-birds
setup-rasts
set-links
end
to setup-rasts
set-default-shape rasts "circle"
create-rasts 1 [setxy -12 36 ]
to set-links
ask rast (number-of-birds + 0) [
create-link-with rast (number-of-birds + 1)
]
end
to move-on-links
stop move-birds
ask birds
at-points [[-12 36]]
(facexy -17 16) and (fd 1)
end

Move turtles to vacant patch ahead in NetLogo

I'm struggling to work out how to tell my turtles to move forward 1 onto a vacant patch, once they have already been turned around in a procedure called "turn-turtle".
let ahead patch-ahead 1
let vacant-ahead ahead with [not any? turtles-here ] ;;this line needs fixing
if any? turtles
[turn-turtle if vacant-ahead [fd 1]]
It produces this error, which I understand, but can't work out how to fix.
WITH expected input to be an agentset but got the patch (patch 1 -2) instead.
error while solute 2 running WITH
called by procedure MOVE-TURTLE
called by procedure GO
called by Button 'go'
Just replying to some comments in a more readable fashion:
Sorry I don't think I explained it very well, as I've just taken a tiny bit of my code out. Maybe this makes more sense.
to go
ask turtles
[move-turtle]
end
to move-turtle
turn-turtle
if (not any? turtles-on patch-ahead 1)
[fd 1]
end
So I just want this code to move the turtles that have been turned with "turn-turtle" to an empty patch ahead 1, preferably taking up the entire patch, like if they were "sprouted". Thanks!
Sounds like you want to do something a bit different, if you really want ahead to be a single patch. So perhaps
to move
ifelse (any? turtles-on patch-ahead 1) [
turn-turtle
][
fd 1
]
end