Merge traffic lanes in NetLogo simulation - netlogo

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
]
]
]

Related

to turn turtle 90 degrees

The orientation (heading) of turtles must be set randomly when wandering but must be limited to a range of 40 degrees (20 to the left and 20 to the right) per step taken, the only exception to this is when avoiding other agents, in this instance a maximum turn of 90 degrees is permitted.
I am new to netlogo. I want these movements with my turtles.
to move-turtles
ask turtles[
if who = ticks[
stop]
let agent-ahead one-of turtles-on patch-ahead 1
ifelse agent-ahead != nobody [
??????
][
rt random 20
lt random 20
]
fd 0.2
]
As stated in the comments you can just use rt and lt with random 45 to achieve what you want.
to setup
clear-all
reset-ticks
crt 5 [set color red]
end
to go
ask turtles[
if who = ticks[
stop]
fd 1
let agent-ahead one-of turtles-on patch-ahead 1
ifelse agent-ahead != nobody [
set color blue
ifelse random 2 = 1 [rt random 45] [lt random 45]
][set color red
rt random 20
lt random 20
]
fd 0.2
]
tick
end

Netlogo random ycor

How i can make a random ycor higher/lower than a value?I have traffic 2 sample and i added some sheeps on the grass but i want them to respawn and only on the grass randomly not on the road.And what should i change so that sheeps can`t never reach the streets?
breed [sheeps sheep]
to setup
clear-all
draw-road
setup-vans
create-or-remove-cars
set selected-car one-of turtles
ask selected-car [ set color red ]
set selected-van one-of turtles
ask selected-van [ set color red ]
reset-ticks
set-default-shape turtles "sheep"
create-sheeps number-of-sheeps [
set color white
set xcor random-pxcor
set ycor -4
set energy random 10
]
end
to-report free [ road-patches ] ; turtle procedure
let this-car self
report road-patches with [
not any? turtles-here with [ self != this-car ]
]
end
to go
ask turtles [ move-forward ]
ask turtles with [ ycor != target-lane ] [ move-to-target-lane ]
ask sheeps
[
move
forward 1
]
tick
end
to move
rt random 100
lt random 100
fd 20
set energy energy - 0.5
end
You have two questions here. Let me answer the first.
set ycor some-value + random-float (max-pycor + 0.5 - some-value)
where some-value is the value you want pycor to be greater than. Similarly,
set ycor some-value - random-float (some-value - min-pycor - 0.5)
where some-value is the value you want pycor to be less than.
max-pycor + 0.5 is the maximum possible value of ycor and min-pycor - 0.5 is the minimum possible value.

How to use to in radius command for turtles to seek food and how to regenerate food over time in Netlogo?

I am trying to do the following in NetLogo:
Get turtles (Elephants) to seek food
Ask plants to reproduce slowly over time on each side, one side before the other
Keep turtles (Elephants) to stay within world boundary
Keep turtles (Elephants) upright
What I would basically like to do is have our turtles (elephants) eat food on one side and seek food to cross to the other side. They will die if they get hit by a car. We want them to cross back and forth between sides so that they all die over time. We have tried to use the seek food primitive but is does not work for our simulation. We have also had the turtles stay within the world using the bounce primitive but with this current code they tend to move everywhere once again. As for the food regeneration, we have tried to use the hatch function but that also does not work.
Your help is very much appreciated.
Here is our code for the simulation:
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own [
speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of number-of-elephants (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "elephant"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of number-of-cars (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
]
]
end
to setup-plants
ask n-of number-of-plants (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end
to go
ask elephants [
bounce forward 1
]
ask cars [
set xcor random-xcor
set heading 90
forward 1
move-elephants
move-cars
eat-plants
kill-elephants
]
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants [
right random 360
forward 1
]
end
to move-cars
set speed 0.1
set speed-limit 0.1
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
There are several problems with this code so I am going to try and get rid of the more obvious logical issues and see if that allows you to focus on a specific question. Note that you should really be building your code more gradually - add one behaviour (eg move elephants, move cars, eat food or whatever) and make sure it works before adding the next behaviour.
Your go procedure doesn't have a tick for time passage
Your go procedure has each car randomly move all the elephants, so they are moving multiple times
Your car speeds and speed limits are being set to the same value each tick and never changed
You have nested ask cars [ ask elephants [ <do stuff> ] ] for eating plants and killing elephants, which will make these happen many times each tick
Fixing just those problems, gets this (note that I replaced slider inputs with numbers so you will have to change them back). This should fix the things you mentioned in your comments. You will have to ask a specific question about whatever else it is you are trying to fix.
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own
[ speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to go
ask elephants
[ bounce
forward 1
]
ask cars [ forward 1 ]
move-elephants
eat-plants
kill-elephants
tick
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants
[ right random 360
forward 1
]
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of 20 (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "wolf"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of 20 (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
set speed 0.1
set speed-limit 0.1
]
]
end
to setup-plants
ask n-of 50 (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end

How to make two agents fight

I need to make two teams of agents fight if they see each other.
Each team has a 50% chance to win.
If an agent has less energy than initially configured, it loses 50% of its energy.
How do I do that? My code seems wrong. Thank you.
to move-teamA
ask teamA
[
ifelse any? teamB-on patch-right-and-ahead -90 1 or any? teamB-on patch-
ahead 1
[
fd 1
if energy < advance_energy [set energy (energy / 2)]
let x random 1
if x = 0 [fd 1]
if x = 1 [die]]
end
Have a look at this approach- I think it's a simpler version of what you're trying to do. I ignored your energy component as I'm not sure how you set that up, but you can put that in as you like.
breed [ teamA one-teamA ]
breed [ teamB one-teamb ]
to setup
ca
create-teamA 10 [
setxy random-xcor random-ycor
set color red
]
create-teamB 10 [
setxy random-xcor random-ycor
set color blue
]
reset-ticks
end
to go
ask turtles [
let enemy one-of turtles in-cone 1 90 with [ breed != [breed] of myself ]
ifelse enemy != nobody [
ifelse random 2 = 1 [ ; if visible enemy exists, flip coin
ask enemy [ die ] ; if 1, enemy dies, I move forward
fd 1
] [
die
] ; if 0, I die
] [
fd 1
] ; if no enemy, move forward
]
tick
end

simulate traffic in roundabouts

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.