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
Related
Hi I would like to hatch turtles at random patches but confined within a specific area.
ask employees [
set shape "person"
set size 1.5
set color blue
setxy random-xcor random-ycor ]
How do I adjust the setxy line to force the turtles to all be confined to: [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
After looking through various bits of different code, what I've done is:
globals [office-space]
set office-space patches with [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
ask office-space [ set pcolor grey]
place-on-color-employees
to place-on-color-employees
let _patches (patches with [pcolor = grey])
ask employees [
move-to one-of (_patches with [not any? turtles-here])
]
end
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
I'm trying to create turtles in an office space area where designated grey patches represent their cubicles. However, when the turtles are created some overlap on top of each other within a cubicle.
How can I ensure only one turtle is created per cubicle? (find the code under ***)
This is the code I have:
breed [programmers programmer]
globals []
patches-own [
a-space
b-space
c-space
d-space
a-cubicle
b-cubicle
c-cubicle
d-cubicle
cubicle?]
programmers-own [
speed
team-name
motivation
myCubicle
teammates
projects
counter ]
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 ]
ask patches [
set a-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor > 0) and (pycor < 14)]
set b-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor > 0) and (pycor < 14)]
set c-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor < 0) and (pycor > -14)]
set d-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor < 0) and (pycor > -14)]
set a-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor <= 16) and (pxcor < 0) and (pycor > 0)]
set b-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor <= 16) and (pxcor > 0) and (pycor > 0)]
set c-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor >= -16) and (pxcor > 0) and (pycor < 0)]
set d-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor >= -16) and (pxcor < 0) and (pycor < 0)] ]
ask patches with [pcolor = grey] [set cubicle? true]
end
to setup-programmers
create-programmers Number_Of_Programmers [
set shape "person"
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "A"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "B"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "C"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "D"]
; *** WHAT I'M TRYING TO DO WOULD GO HERE ***
ask programmers with [team-name = "A"] [ set color blue] if other turtles-on patch-here [ move-to one-of a-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "B"] [ set color green move-to one-of b-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "C"] [ set color pink move-to one-of c-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "D"] [ set color yellow move-to one-of d-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers [set teammates programmers with [color = [color] of myself]]
set projects (list)
set motivation 100
set counter 0
]
end
to setup
clear-all
reset-ticks
setup-environment
setup-programmers
end
to go
lose-or-gain-motivation
socialize
complete-program
tick
end
There are three different ways to create turtle agents in NetLogo. If you want want no more than one turtle in a patch, the easiest way to do so is to select a subset of patches (using n-of XXX patches) and ask them to sprout turtles. In your case, something like this might work:
Replace create-programmers Number_Of_Programmers
with
ask n-of Number_Of_Programmers patches with [cubicle?]
[ sprout-programmers 1
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
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.)