Drawing a two lane highway with a merging road - netlogo

I am trying to model autonomous vehicles merging into a two lane highway and I am having trouble drawing the merging road. I have the two lane highway from the model library,
to draw-road
ask patches [
set pcolor green
if ((pycor > -4) and (pycor < 4)) [ set pcolor gray ]
if ((pycor = 0) and ((pxcor mod 3) = 0)) [ set pcolor yellow ]
if ((pycor = 4) or (pycor = -4)) [ set pcolor black ]
]
end
but I can't get a road to merge from the bottom. The code above is just dealing with horizontal lines but I need to also add one with a slight slope. I have tried entering the equation of a line but I could not get it to work. Any help is appreciated.
Thanks

If you just want an angled single-lane road that joins the highway, you could try something like:
to draw-merge
ask patches [
if pycor < -3 [
if ( pxcor < pycor + 7 ) and (pxcor > pycor - 1 ) [
set pcolor black
]
]
if pycor < -2 [
if ( pxcor < pycor + 6 ) and (pxcor > pycor ) [
set pcolor gray
]
]
]
end

Related

Netlogo divide world into upper and lower

Good afternoon everyone:
Currently I am working in a program in Netlogo and I want to divide the world in upper-quadrant and lower-quadrant and ask turtles to move to the upper-quadrant. I have figured out how to divide the world in four quadrant from a previous question answered here, but I don't know how to divide it in two.
Thank you very much for your help
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor > 0]
[
set pcolor red
set quadrant 1
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor > 0]
[
set pcolor blue
set quadrant 2
]
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor < 0]
[
set pcolor green
set quadrant 3
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor < 0]
[
set pcolor yellow
set quadrant 4
]
Given that you are interested in a lower and an upper quadrant, you only need to look at y coordinates. The specific condition depends on where your world's origin (i.e. coordinates [0;0]) is.
If your world's origin is in the default position, which is the center, then do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > 0)
[set quadrant 1]
[set quadrant 2]
]
end
If your world's origin is in a corner (e.g. I assume bottom left corner in this case), just do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > max-pycor / 2)
[set quadrant 1]
[set quadrant 2]
]
end
If you don't know in advance where your world's origin will be, or if your world's origin is in a less common place than the two examples above, you can take a more generalised approach that fits any situation:
patches-own [
quadrant
]
to setup
clear-all
let y-extent (max-pycor - min-pycor + 1)
ask patches [
ifelse (pycor > y-extent / 2)
[set quadrant 1]
[set quadrant 2]
]
end

How can I solve the error (you can not use my_ area in a patch context, it is a turtle-only)?

I want the turtles to back again to their areas if batches which I had defined before. The problem is I got an error
you can not use my_ area in a patch context, it is a turtle-only
the button is
to back_from_event
ask turtles with [ area = 1 ] [ move-to one-of patches with [ (not any? other turtles-here) and ( area = my_area ) ] ]
end
the patches defined as below and the turtles were in areas (2,3,4,5) and moved to area 1 and I need them to back again to there areas:
to define_areas
ask patches with [ (pxcor > -3) and (pxcor < 3) and (pycor > -3) and (pycor < 3) ] [ set pcolor white set area 1 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 2 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 3 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 4 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 5 ]
end
Okay, this is the same problem as your previous question. Please try to understand the answer rather than simply copy the code. Otherwise, you will keep asking the same question.
You have 'area' as a patch variable and 'my_area' as a turtle variable.
What you need to realise is that turtle to patch is unique because a turtle can only be in one place at a time. Therefore, a turtle can access the variables owned by the patch that it is sitting on without having to specify the patch. So code like this is okay:
ask turtles with [area = 1] [ ]
This is because it is equivalent to:
ask turtles with [ [area] of patch-here = 1] [ ]
However, a patch cannot access a variable owned by a turtle because there may be multiple turtles on the same patch. For example, if you asked a patch to set its pcolor to color and you had red turtle and a blue turtle on the patch, how would it know which color to choose?
Your error says "you can not use my_area in a patch context, it is a turtle-only". That is telling you that you tried to have a patch use the my_area variable but that variable is owned by turtles. So you didn't tell it which turtle to get my_area from.
This is what you have:
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = my_area)]
]
end
I assume you want the area of the patch to be the same as the my_area of the turtle doing the asking. That is what myself is for.
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = [my_area] of myself)]
]
end

NetLogo set patches within a specific range

I'm new to stack overflow and netlogo, in fact this is my first question ever. I thank you all in advance.
In netlogo I have created 4 areas to represent 4 office spaces which coincide with quadrants I-IV:
to setup-environment
ask patches with [ pycor mod 2 = 0 and pxcor <= -16] [ set pcolor grey ]
ask patches with [ pycor mod 2 = 0 and pxcor >= 16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor <= -16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor >= 16] [ set pcolor grey ]
ask patches with [ pycor = 0] [ set pcolor red ]
ask patches with [ pxcor = 0] [ set pcolor red ]
; THIS PART IN PARTICULAR
ask patches [
set a-space patches with [(pxcor < 0) and (pycor > 0)]
set b-space patches with [(pxcor > 0) and (pycor > 0)]
set c-space patches with [(pxcor > 0) and (pycor < 0)]
set d-space patches with [(pxcor < 0) and (pycor < 0)]
]
This sets up, for example, a-space to be in quadrant II entirely, I need the patches in a-space to be within a certain range. I tried (-14 < pxcor < 0) and (14 > pycor > 0) so that the area is within x = (-14, 0) and y (16, 0), but got this error:
expected this input to be an agent or number or string, but got a
TRUE/FALSE instead
I understand that you can't set patches, but that is not what I'm trying to do here, I'm trying set an area with patches with the range I specify.
Welcome to StackOverflow (and NetLogo). For future questions, please show the specific code that generates the error as part of your example code. However, if I understand your question correctly, you had something like:
set a-space patches with [(-14 < pxcor < 0) and (14 > pycor > 0)]
You can't use compound comparisons such as this in NetLogo. The statement -14 < pxcor < 0 in mathematics is two separate logical statements: -14 < pxcor and pxcor < 0. You must construct them as two statements and use the logical operator and to join them.
Following is a complete model that I think does what you want. Note that as well as the logical structure, I removed your ask patches. The way you have your code set up, each patch sets the variables a-space etc. So, if you have 2500 patches, then those variables are set (to the same value) 2500 times.
globals [a-space b-space c-space d-space]
to setup-environment
ask patches with [ pycor mod 2 = 0 and pxcor <= -16] [ set pcolor grey ]
ask patches with [ pycor mod 2 = 0 and pxcor >= 16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor <= -16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor >= 16] [ set pcolor grey ]
ask patches with [ pycor = 0] [ set pcolor red ]
ask patches with [ pxcor = 0] [ set pcolor red ]
; THIS PART IN PARTICULAR
set a-space patches with [(-14 < pxcor) and (pxcor < 0) and (pycor > 0) and (pycor < 5)]
ask a-space [set pcolor blue]
end

How to make turtles move around patches with specific rules?

I am new to Netlogo, and have some questions. That would be great if you could help me.
I would like to create some fruit flies moving around a tree that is made up by green patches. Fruit flies are attracted to the tree. They will turn back to the tree if fruit flies move away certain distance (such as 5 grids) from the tree.
In the beginning, they will not stop on the green patches because they have enough energy. As time passed, they will loss their energy. They will find the closest green patch, and stay on it for certain time once their energy reaches 0. They gain energy after that, and they only can do short hops from the bottom branch to the top one (3 hops). Flies will move back to the bottom branch when they are on the top part of tree. I think I need to do a WHILE loop, but I have no idea how to do that. Please look at my codes.
breed [flies fly]
breed [suns sun]
turtles-own [energy]
flies-own [count-down]
to setup
clear-all
setup-suns
setup-flies
setup-patches
reset-ticks
end
to setup-suns
;; Create the sun
set-default-shape suns "sun"
create-suns 1 [
setxy max-pxcor - 3
max-pycor - 3
set color yellow
set size 7
]
end
to setup-flies
set-default-shape flies "bee 2"
create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]
end
to setup-patches
;; Create sky and grass
ask patches
[ set pcolor blue ]
ask patches with [pycor < min-pycor + 2]
[ set pcolor 66 ]
;; Create trunk and branches
ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]
ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]
ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ]
end
to go
move-flies
tick
end
to move-flies
ask flies [
set energy 6
ifelse energy > 10 [
;rt random 50 lt random 50 jump random-float 1 ;
let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.
if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
;set heading 45
;fd distance nearest-leaf
rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
]
[
ifelse pcolor != lime
[ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
continue]
[ stay ]
]
]
end
to continue
let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.
ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
fd random distance nearest-leaf
;ask patches in-radius 1 [set pcolor red]
;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
[
let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
;set heading 180
face turn-back
jump random-float 5 ]
move-up ;Flies tend to move up through all branches by short hops. Need a while loop.
; Move down if they reach the top of tree
let canopy patch-at-heading-and-distance 0 1
let canopy-left patch-left-and-ahead 45 1
let canopy-right patch-right-and-ahead 45 1
if canopy != lime or canopy-left != lime or canopy-right != lime
[
move-down
]
end
to move-up
ask flies [
set heading one-of [0 30 -30]
]
end
to move-down
ask flies [
set heading one-of [180 120 -120]
]
end
to stay
set count-down count-down - 1 ;;decrement timer
set label count-down
if count-down = 0
[
rt random 50 lt random 50 jump random-float 1
set label ""
reset-count-down ;;it's another procedure
]
end
to reset-count-down
set count-down 30
end
I am sorry if you are confused by my codes. I appreciate your help. Thanks.
Kind Regards,
Ming
From your description, I think you almost certainly don't want a while loop. In a NetLogo model's go procedure, you're only specifying what happens in a single tick. So you would only use while if need it to express something that happens all in a single instant, not a process that unfolds over multiple ticks.
(Agree with Frank this is too much code to post to have a good chance of getting help. It would take quite a while for me to read and study this much code, let alone try to help you with it. In this answer I've tried to extract and address a single aspect.)

netlogo ask pathces color in limited coordinates

I want to learn whether there is any brown patch in this limited coordinates. But this doesn't work. How can I query it?
if not any? pcolor = brown with [ (pxcor >= max-pxcor - boundary-width) or (pxcor <= min-pxcor + boundary-width)][fd 2]
You were almost there, I think. Something like this should work:
if not any? patches with
[ pcolor = brown and
( pxcor >= max-pxcor - boundary-width)
or (pxcor <= min-pxcor + boundary-width) ]
[ fd 2 ]