Nested if queries in patch context NetLogo - netlogo

I want to change color of patches to green under these conditions:
-IF there are any two blue patches on same column and their distance is smaller than 25 AND
-IF there are any yellow patches on same column and between these selected blue patches
-Then change color of all patches satisfying these conditions to green.
I am struggling to make it in NetLogo, tried using nested loop but couldn't find a way. Thank you for any help. And I have added a sample image which I want to achieve and marked example blue patches.
As addition, to show what I want to do in code (sorry about code):
if any? patches with [pcolor = blue and
(if any? patches with [pcolor = blue and pycor = ?(selected_first_blue's_pycor)
if any? patches with [pcolor = yellow and pycor < ??(selected_first_blue's_pycor) and pycor > ?(selected_second_blue's_pycor)))
[ask patches [set pcolor green]]

The way you are approaching it, you need fairly convoluted statements like:
let upper-blues patches with [color = blue and
any? other patches with [color = blue and pxcor = [pxcor] of myself
and pycor < [pycor] of myself and pycor > [pycor] of myself - 25]
I believe this would be much easier to take the perspective of the patch you want to potentially turn green. If I have interpreted your conditions correctly, that patch needs to work out the closest yellow patch above/below/at and then check if there are two blue patches bracketing both the yellow and itself with the blue patches sufficiently close to each other. I assume you have wrapping turned off.
Here is a complete model that puts in a red turtle instead of turning the patch green so you can see whether it is identifying the correct patches.
to setup
clear-all
ask patches [set pcolor white]
ask n-of 100 patches [set pcolor blue]
ask n-of 100 patches [set pcolor yellow]
end
to convert-to-green
let turn-green nobody
ask patches
[ let my-column patches with [pxcor = [pxcor] of myself]
let above-yellow min-one-of my-column with [pcolor = yellow and pycor >= [pycor] of myself][pycor]
let above-blue ifelse-value (above-yellow != nobody) [min-one-of my-column with [pcolor = blue and pycor > [pycor] of above-yellow][pycor]][nobody]
let below-yellow max-one-of my-column with [pcolor = yellow and pycor <= [pycor] of myself][pycor]
let below-blue ifelse-value (below-yellow != nobody) [max-one-of my-column with [pcolor = blue and pycor < [pycor] of below-yellow][pycor]][nobody]
if above-blue != nobody and below-blue != nobody and ([pycor] of above-blue - [pycor] of below-blue < 25)
[ set turn-green (patch-set self turn-green)
]
]
ask turn-green [sprout 1 [set color red]]
end
Once you are satisfied it is working correctly, change ask turn-green [sprout 1 [set color red]] to ask turn-green [set pcolor green].
This code checks each patch in random order and adds it to the set of patches (called turn-green) if the conditions are satisfied. Once all patches have been tested, the set of selected patches then change their colour. This avoids issues of yellow or blue patches turning green and not being available for later patches to check against.

Related

How to force turtles to move only in a half of the world?

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

Spatial Autocorrelation in NetLogo

Is there any straight-forward way to adjust spatial autocorrelation for three different patch colors? I am trying to control both the number of red patches and how spatially autocorrelated (how close same colored patches are to each other). I can control the proportion of red patches, but don't know how to setup the autocorrelation.
Here is my code so far:
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [
set pcolor one-of [ green brown ]
]
ask patches [
let close-patches patches with [pcolor != red]
ask n-of ((proportion-red-plants * count patches) - count patches with [pcolor = red]) close-patches
[set pcolor red]
]
end
proportion-red-plants is a slider in the interface
If you know that AC of 0 means pick a patch that has no red neighbours, and that AC of 1 means pick a neighbour of any red patch, then all that is required is to choose AC=1 method with the given probability and the AC=0 method otherwise. This is what I meant by a design issue, you need to work out the steps required before trying to code those steps.
Here is an almost solution. I haven't bothered to do things like make sure the patches being turned red aren't already red so the counts will be incorrect.
to setup
clear-all
let prop-red 0.1
let AC 0
ask one-of patches [set pcolor red]
ask n-of (prop-red * count patches) patches
[ ifelse random-float 1 < AC
[ ask one-of patches with [ pcolor = red ]
[ ask one-of neighbors [ set pcolor red ]
]
]
[ let candidates patches with [not any? neighbors with [pcolor = red] ]
if any? candidates
[ ask one-of candidates [ set pcolor red ]
]
]
]
end
Thank you JenB again for helping. This is the code I ended up using which proved to get the job done (while keeping counts of red patches correct)
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [set pcolor one-of [green brown]]
let first-patch one-of patches
ask first-patch [set pcolor red]
repeat (proportion-red-plants * count patches - 1) [ask one-of patches [assign]]
end
to assign
ifelse random-float 1 < AC
[let candds patches with [any? (neighbors with [pcolor = red])]
ask one-of candds [set pcolor red]]
[ask one-of patches [set pcolor red]]
end

Netlogo: agentset of patches to single agent

I have a list of turtles (carriers) that i'm trying to narrow down in my go method.
Here is my program:
globals [
metal-sheets
cut-sheets
standard-skeleton
finished-standard-skeleton
prestige-skeleton
finished-prestige-skeleton
]
breed[carriers carrier]
turtles-own [
on-job?
]
patches-own [
processing-time
machine-type ;;cutter, standard-welder, prestige-welder, riveter
status ;;import, export, pending
]
to setup
clear-all
set-default-shape carriers "circle"
create-carriers number-of-carriers ;;number-of-carriers is a slider
[set color grey
set on-job? false
setxy random-xcor random-ycor]
setup-patches
reset-ticks
end
to setup-patches
ask patches [
if pxcor = 1 and pycor = 1 [set machine-type "cutter"]
if pxcor = 1 and pycor = 5 [set machine-type "standard-welder"]
if pxcor = 5 and pycor = 1 [set machine-type "prestige-welder"]
if pxcor = 5 and pycor = 5 [set machine-type "riveter"]
if machine-type = "cutter"
[set pcolor red
set status "import"]
if machine-type = "standard-welder"
[set pcolor green
set status "import"]
if machine-type = "prestige-welder"
[set pcolor blue
set status "import"]
if machine-type = "riveter"
[set pcolor yellow
set status "import"]
]
end
to Go
let cutter patches with [machine-type = "cutter"]
let standard-welder patches with [machine-type = "standard-welder"]
let prestige-welder patches with [machine-type = "prestige-welder"]
let riveter patches with [machine-type = "riveter"]
let free-carriers carriers with [on-job? = false]
let closest-carrier min-one-of free-carriers [distance cutter] ;;Distance expects agent, got agentset
ask closest-carrier [
set color green
]
end
I'm getting an error on the line let closest-carrier min-one-of free-carriers [distance cutter] saying that distance expected a agent but got a agentset although there only is one agent in the set.
The full error message is:
DISTANCE expected input to be an agent but got the agentset (agentset, 1 patch) instead.
How do narrow down the patches to one patch?
It doesn't matter if there happens to be only one agent in the agentset, it is still an agentset. Put a one-of in front, which selects one agent from the agentset and therefore changes the way the code interprets the code

coloring patches with specific in a domain

I want to have pcolor of my outer-ring patches as green
I have written following command
To setup
ask patch 0 0 [ set pcolor red
ask neighbors
[ set pcolor blue]
ask patches with [pxcor > 1 and pxcor < -1 and pycor > 1 and pycor < -1]
[set pcolor green ]
]
end
I get center and neighbors with required color but out ring of patches remained black.
plz help.
The problem is that you provide a condition that no patch can satisfy. (E.g., it cannot be both to the left of your colored patches and to the right, but you use and.) Does the following meet your needs?
to colorPatches
ask patch 0 0 [
set pcolor red
ask neighbors [ set pcolor blue]
]
ask patches with [pcolor = black] [
set pcolor green
]
end

netlogo patch set as chess board style

how can I set al of patches like chess board. one white than black?
ask patches [if ...... [set pcolor white]
if .......[set pcolor black]]
could you say is it the right way or?
assuming your world is already 8x8 patches and the patches are default black:
ask patches [if pxcor mod 2 = pycor mod 2 [set pcolor white]]
Assuming you have an 8x8 world
to setup
ask patches [
ifelse pxcor mod 2 = pycor mod 2
[set pcolor white]
[set pcolor black]
]
end