Netlogo divide world into upper and lower - netlogo

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

Related

Turtles randomly walking in specific area

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

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 move turtles on a game board simulation?

I'm creating a board game simulation similar to monopoly, where pieces are moving around the board. My turtle acts as the user's game piece and moves around the border of the world. The border is set up in alternating colors. I have a button that generates the number of steps the turtle moves. Those steps are stored as dice-num, a global variable. However, when the turtle lands on the patches before the patch (16 -16) (the bottom right-hand corner of the board) and the number of steps it needs to move exceeds 1, I can't turn the turtle's heading halfway to move up the board. As a result it only moves back to the beginning of the board.
I've tried to treat each case separately:
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
;dice-num refers to number of steps the turtle moves
Heres my code so far:
to setup
board
end
to go
dice-roll
end
to board
ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min-
pxcor or pycor = min-pycor
[set pcolor blue]]
ask patches [if pycor = max-pycor or pycor = min-pycor
[if pxcor mod 2 = 0
[set pcolor orange]]]
ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
[if pycor mod 2 = 0
[set pcolor orange]]]
ask patch min-pxcor min-pycor [set pcolor green]
end
to dice-roll
set dice [1 2 3 4 5 6]
set dice-num one-of dice
user-message (word "You rolled: " dice-num)
ask turtle 0 [
fd dice-num
]
;allows the turtle to turn if it lands on a corner
ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
if xcor = max-pxcor and ycor = max-pycor [set heading 270]
if xcor = min-pxcor and ycor = max-pycor [set heading 180]
;add a statement to end game once player rereaches the green patch
]
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
end
I expect the output to be the turtle to start moving up the right hand side of the board once it lands on the patch (15 -16) and recieves a dice-num greater than 1. However, when the turtle does land on the patch (15 -16) and the dice-num is greater than 1 it simply moves back to the beginning of the board.
Okay, this was simply too long for comments so I had to make some guesses. I expect the problem was that you were forwarding the full amount of the die roll after checking location. This would mean you have to test all the potential patches that could reach the corner. Instead, it is easier to move one patch and test location before moving the next patch. This is a cleaned up version of your code that implements this move and check process.
to setup
clear-all
board
ask one-of patches with [pcolor = green]
[ sprout 1
[ set color white
set heading 90
forward 1
]
]
end
to go
let die-num dice-roll
ask one-of turtles [ move die-num ]
end
to board
ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
[ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
ask patches with [ pycor = max-pycor or pycor = min-pycor ]
[ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
ask patch min-pxcor min-pycor [set pcolor green]
end
to-report dice-roll
let dice-num one-of [1 2 3 4 5 6]
user-message (word "You rolled: " dice-num)
report dice-num
end
to move [#roll]
while [ #roll > 0 ]
[ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
if pxcor = min-pxcor and pycor = min-pycor [ stop ]
fd 1
set #roll #roll - 1
]
end
The alternative is simply to add the die roll to the current location and see if it goes past. If it does, calculate where you want to get to.

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.)