i have been working a basic over taking a code for a two lane scenario, i have attempted the coding below and i thought that it would work as it is, however cars still wont over take, im not sure if there are contradicting statements. so far this section of coding will accelerate the cars and stop them when a car is in front but for some reason will not move to the other lane. any ideas?
let in-lane (patch-set patch-ahead 2 patch-ahead 1.5 patch-ahead 1 ); patch-
left-and-ahead 26 1 patch-right-and-ahead 26 1)
let car01-ahead one-of cars01-on in-lane
let on-left (patch-set patch-left-and-ahead 90 1 );patch-left-and-ahead 45 1.5 patch-left-and-ahead 135 1.5)
let on-right (patch-set patch-right-and-ahead 90 1 );patch-right-and-ahead 45 1.5 patch-right-and-ahead 135 1.5)
; set meaning "road-all" = "road-right" "road-left"
ifelse car01-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed - deceleration]
]
[
ifelse [meaning] of patch-left-and-ahead 90 1 != "road-left"
[ifelse turtles-on on-left = nobody
[move-to patch-left-and-ahead 90 1]
[ifelse [speed] of car01-ahead = 0
[set speed 0] [
ifelse [speed] of car01-ahead >= maxSpeed
[ set speed maxSpeed
set speed speed - deceleration]
[ set speed maxSpeed
set speed speed - deceleration ]]]][
ifelse [meaning] of patch-right-and-ahead 90 1 != "road-left"
[ifelse turtles-on on-left = nobody
[move-to patch-right-and-ahead 90 1]
[ifelse [speed] of car01-ahead = 0
[set speed 0] [
ifelse [speed] of car01-ahead >= maxSpeed
[ set speed maxSpeed
set speed speed - deceleration]
[ set speed maxSpeed
set speed speed - deceleration ]]]][ set speed maxSpeed
set speed speed - deceleration ]
]]
turtles-on on-left = nobody is always false, because turtles-on always returns an agentset, and nobody is not an agentset. nobody occurs in contexts where a single agent was expected but none was available; that is separate from agentsets.
Instead, write turtles-on on-left = no-turtles or better yet not any? turtles-on on-left.
In this case, I was able to spot the problem (or at least, one problem) just by eyeballing the code. If I hadn't been able to do that, I would have suggested adding print statements to the code to check whether the values of variables are what you expected, and whether branches you expected to be taken were actually taken. That technique would have uncovered that turtles-on on-left = nobody is always false.
Related
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
In my project i am writing a semi-autonomous car simulation, as a beginner i have used a basic version of a driving simulation and aiming to expand it, whist expanding the coding i was trying to mimic lane centring, so have made each lane or a road three patches thick, in doing so i'm struggling to make cars overtake correctly now.
it was defined that the car should occupy all three of the patches as not to have two cars in one lane, but in coding this the car in the inner lane that should overtake now will only stay behind. i want the inner lane to overtake.
to control-speed01
let in-lane (patch-set patch-ahead 3 patch-left-and-ahead 26 2 patch-right-
and-ahead 26 2 )
let car-ahead one-of cars-on in-lane
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed -
deceleration]
]
[
ifelse [speed] of car-ahead = 0 [set speed 0] [
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
; try to overtake
ifelse [meaning] of patch-left-and-ahead 26 2 = meaning and not any?
turtles-on patch-left-and-ahead 26 2 and [meaning] of patch-left-and-ahead 90 1 != "crossroad"
and meaning != "crossing" and [meaning] of patch-left-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-left-and-ahead 169 3
and not any? turtles-on patch-left-and-ahead 45 1 and not any? turtles-on patch-left-and-ahead 135 1 and not any? turtles-on patch-left-and-ahead 23 2
and not any? turtles-on patch-left-and-ahead 157 2 and not any? turtles-on patch-left-and-ahead 12 3 and [meaning] of patch-ahead 1 != "crossing" [move-to patch-left-and-ahead 90 1] [
ifelse [meaning] of patch-right-and-ahead 90 1 = meaning and not any? turtles-on patch-right-and-ahead 90 14 and [meaning] of patch-right-and-ahead 90 1 != "crossroad"
and meaning != "crossing" and [meaning] of patch-right-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-right-and-ahead 12 3
and not any? turtles-on patch-right-and-ahead 45 1 and not any? turtles-on patch-right-and-ahead 135 1 and not any? turtles-on patch-right-and-ahead 23 2
and not any? turtles-on patch-right-and-ahead 157 2 and not any? turtles-on patch-right-and-ahead 169 3 and [meaning] of patch-ahead 1 != "crossing"[move-to patch-right-and-ahead 90 1] [
set speed [speed] of car-ahead
set speed speed - deceleration]
][enter image description here][1]
]
]
]
end
in the coding is a section about dealing with pedestrian crossings but for simplicity i have removed people for now.
i think i need to have different angles in the first line of "try to overtake". but i'm not sure. i stress that i am a beginner, if i can make the question clearer please say.
This is for a car to not pass a car in front or in front to the left and to the right. how can i use all commands, how do i link them?
to place-cars-level0 ;creating basic real life driver
;make a random placement of cars
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-up"] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set will-turn? "maybe"
set will-stop? "maybe"
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 0
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
] ; set the number of cars for the level, size, politness,speed
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-down" and count turtles-on neighbors = 0] [
if not any? cars-on patch pxcor (pycor + 1) and not any? cars-here and not any? cars-on patch pxcor (pycor - 1) and not any? patches with [meaning = "crossing"] in-radius 2 [
sprout-cars 1 [
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 180
set will-turn? "maybe"
set will-stop? "maybe"
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
ask n-of (num-of-cars-level0 / 3) patches with [meaning = "road-left" and
count turtles-on neighbors = 0] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 270
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
while [count cars < num-of-cars-level0] [
ask one-of patches with [meaning = "road-right"] [
if not any? cars-on patch (pxcor + 1) pycor and not any? cars-here and not
any? cars-on patch (pxcor - 1) pycor and not any? patches with [meaning =
"crossing"] in-radius 2 [
sprout-cars 1 [
set will-turn? "maybe"
set will-stop? "maybe"
set size 3
set shape "car top"
set politeness basic-politeness + random (101 - basic-politeness)
if random 100 > basic-politeness [set politeness random 21]
set heading 90
let s random 10
if s < 7 [set maxSpeed speed-limit - 15 + random 16]
if s = 7 [set maxSpeed speed-limit - 20 + random 6]
if s > 7 [set maxSpeed speed-limit + random 16]
set speed maxSpeed - random 20
]
]
]
]
end
in the section above is the code for writing the car placement and below is the speed and control
to control-speed
let car-ahead one-of cars-on patch-ahead 4
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed
- deceleration]
]
[
ifelse [speed] of car-ahead = 0 [set speed 0] [
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
;try to overtake
;ifelse [meaning] of patch-left-and-ahead 90 1 = meaning and not any? turtles-on patch-left-and-ahead 90 1 and [meaning] of patch-left-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-left-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-left-and-ahead 169 3
;and not any? turtles-on patch-left-and-ahead 45 1 and not any? turtles-on patch-left-and-ahead 135 1 and not any? turtles-on patch-left-and-ahead 23 2
; and not any? turtles-on patch-left-and-ahead 157 2 and not any? turtles-on patch-left-and-ahead 12 3 and [meaning] of patch-ahead 1 != "crossing" [move-to patch-left-and-ahead 90 1] [
; ifelse [meaning] of patch-right-and-ahead 90 1 = meaning and not any? turtles-on patch-right-and-ahead 90 14 and [meaning] of patch-right-and-ahead 90 1 != "crossroad"
;and meaning != "crossing" and [meaning] of patch-right-and-ahead 180 1.3 != "crossing" and not any? turtles-on patch-right-and-ahead 12 3
;and not any? turtles-on patch-right-and-ahead 45 1 and not any? turtles-on patch-right-and-ahead 135 1 and not any? turtles-on patch-right-and-ahead 23 2
;and not any? turtles-on patch-right-and-ahead 157 2 and not any? turtles-on patch-right-and-ahead 169 3 and [meaning] of patch-ahead 1 != "crossing"[move-to patch-right-and-ahead 90 1] [
;set speed [speed] of car-ahead
;set speed speed - deceleration]
]
]
]
i hope this is more usefulthe image shows the overall simulation
if you can see the image, my aim is to keep one car in each lane rather then three side by side per lane, this is to simulate semi autonomous vehicle lane centering
Okay, instead of doing
let car-ahead one-of cars-on patch-ahead 4
what you probably want to do is create a patch-set of all the three patches that a potential car could be occupying. That's a little difficult without knowing how you have created the lanes, and you need to be careful because the car doing the asking could be in any of three patches too. After creating that patchset, you can simply do what you did before but using the patch-set.
let in-lane (patch-set patch-ahead 4 <other patches you need go here>)
let car-ahead one-of cars-on in-lane
I'm modeling a critical situation that's happening in my city (ViƱa del Mar, Chile), where the "metro" (subway)can have serious problems if a tsunami occurs because the evacuation exits are not well prepared nor well managed.
This example is a simple model of the real one, but i was having really big problems trying to make the turtles move from the subway (blue patches), using the door (lime patch) and then jumping into the railways (gray patches). When i finally made them do those actions i found 2 problems: (1) the ticks aren't working the way i need them to work. I need it to run like it was a clock, counting seconds. (2) i create 3 actions called "check-vecinos-azul/lime/gray" where they need to check if there's any turtle arround them (neighbors). If there's an empty space arround a turtle it moves to that empty spot, otherwise it stands until it finds an empty one.
I'd be so glad if anyone of you could give me a help/tip with my model.
Thank you guys,
Kind regards,
Felipe.
breed [pasajeros pasajero]
turtles-own [
distancia
speed
speed-limit
speed-min
evacuacion
]
patches-own
[
pvacio
]
to setup
ca
setup-patches
setup-pasajeros
reset-ticks
end
to setup-patches
ask patches with [pycor <= 0]
[set pcolor blue - 1]
ask patches with [pycor > 0]
[set pcolor gray + 2]
ask patch 0 0 [set pcolor lime]
end
to setup-pasajeros
ask n-of num-pasajeros (patches with [pcolor = blue - 1])
[
sprout-pasajeros 1
[set size 1
set color orange
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0
set evacuacion 0
]
]
end
to go
ask pasajeros [
ifelse evacuar?
[
go-puerta
salir-puerta
move-chancado
]
[
stop
]
]
tick
end
to go-puerta
ask turtles with [pcolor = blue - 1]
[
set evacuacion 1
if evacuacion = 1
[
check-vecinos-azul
face min-one-of patches with [pcolor = lime][distance myself]
fd speed ;;
]
]
end
to salir-puerta
ask turtles with [pcolor = lime]
[
set evacuacion 1
if evacuacion = 1
[
check-vecinos-lime
face min-one-of patches with [pcolor = gray + 2][distance myself]
fd speed
]
]
end
to move-chancado
ask turtles with [pcolor = gray + 2]
[
set evacuacion 1
check-vecinos-gray
]
end
to check-vecinos-azul
ifelse not any? turtles-on one-of neighbors
[
wait 0
move-to min-one-of patches with [pcolor = blue - 1][distance myself]
fd speed
]
[
wait 0.5
]
end
to check-vecinos-lime
ifelse not any? turtles-on one-of neighbors
[
wait 0
face one-of patches with [pcolor = gray + 2]
fd speed
]
[
wait 0.5
]
end
to check-vecinos-gray
ifelse not any? turtles-on one-of neighbors
[
wait 0
face min-one-of patches with [pcolor = gray + 2][distance myself]
fd speed
]
[
wait 0.5
]
end
I'd like to use Netlogo Web to open my Netlogo (Desktop version) model, but it does not seem to work. I get a 'TO or TO-REPORT expected' error when I try to upload my model. Not sure what I'm doing wrong. I've inserted the code below. It is supposed to model a cellular process, in which cells divide, filaments of cells branch, and cells change shape as they grow older (procedures 'divide', 'branch', 'transform'). Help please?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-threads 1
ask turtles [
setxy random-xcor random-ycor
set shape "line"
set color 65
]
end
turtles-own [age]
to go
add-age
divide
branch
transform
tick
ask turtles [ set size 1 ]
end
breed [threads thread]
breed [cylinders cylinder]
breed [circles circle]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask turtles [
if breed = threads or breed = cylinders [
ifelse random 100 < 50
[if (not any? other turtles-on patch-ahead 1) and
(not any? other turtles-on patch-right-and-ahead 10 1) and
(not any? other turtles-on patch-left-and-ahead 10 1)
[hatch 1
[let turn-degree (random(20) - 10)
rt turn-degree
fd 1
set age age - 1
]
]
]
[if (not any? other turtles-on patch-right-and-ahead 180 1) and
(not any? other turtles-on patch-right-and-ahead 170 1) and
(not any? other turtles-on patch-right-and-ahead 190 1)
[hatch 1
[let turn-degree (170 + random(20))
rt turn-degree
fd 1
set age age - 1
]
]
]
]]
end
to branch
ask circles [
if random 1000 < 2[
ifelse random 100 < 50
[if not any? other turtles-on patch-right-and-ahead 90 1
[hatch-threads 1
[right 90
fd 1
set age 0
set shape "line"]
]
]
[if not any? other turtles-on patch-left-and-ahead 90 1
[hatch-threads 1
[left 90
fd 1
set age 0
set shape "line"]
]
]
]]
end
to transform
ask threads[
if (age > 50) and (random 100 < 50)[
set breed cylinders
set shape "cylinder1"
]
]
ask cylinders[
if (age > 100) and (random 100 < 50) [
set breed circles
set shape "circle"
]
]
end
NetLogo Web is stricter about order of declaration (and so will be future versions of NetLogo Desktop).
Just move your turtles-own and breed statements to the top of your code and you should be all right.