How to setup one patch with a particular colour in Netlogo? - netlogo

Why does this work:
to setup
clear-all
ask n-of 1 patches [set pcolor green]
end
But this doesn't:
to setup
clear-all
ask patch-at 1 1 [set pcolor green]
end
How do you have the patch at 1 1 change colour please?
Thank you.

You need to do the following:
to setup
clear-all
ask patch 1 1 [set pcolor green]
end
Your code is using patch-at which reports (ie tells the asking agent) the location or patch coordinates of the patch which is at a certain position (in this case, 1 up and 1 right) from the asker. That is, a relative position. Instead, you are wanting to ask a patch with specific coordinates or absolute position so you use the patch's name such as patch 1 1.

Related

Trouble specifying agent's location and sprout command

The objective of my submodel is to simulate how wolf agents avoid patches that have human densities above the wolves' tolerance threshold. When running my model, the sprout command is not generating the number of human agents in the urban patch like I would expect it to. The code for creating humans in the urban patch is:
ask patches [ if self = urban-patches [sprout-humans initial-number-humans]]
Here is the image of my Interface Tab:NetLogo space
The grey is coded as my urban-patches, brown is grass-patches, and green is forest-patches. Why are my human agents not appearing in the grey (urban) patches with the number of agents reflecting the initial-number-humans?
Here is the code for creating the human agents:code
I have specified the xy coordinates of the human agent to be located within the urban (grey) patch, but only one human agent appears when I run the model. How do I correctly code the initial-number-humans to connect with the sprout command?
As I think you have now discovered, the easiest way to distribute a set number of turtles randomly across a group of patches is to use create-turtles rather than sprout. This is because sprout creates the specified number of turtles on each patch that is sprouting them so you need to juggle the total to be created and the number of patches. But that option is useful if you want to achieve even distribution rather than random locations. Here is code that does both.
globals [urban-patches n-humans]
to setup
clear-all
set urban-patches n-of 20 patches
ask urban-patches [set pcolor gray]
set n-humans 100
make-humans-sprout
make-humans-create
end
to make-humans-sprout
ask urban-patches
[ sprout n-humans / count urban-patches
[ set color red
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
]
end
to make-humans-create
create-turtles n-humans
[ set color blue
move-to one-of urban-patches
set xcor xcor - 0.5 + random-float 1
set ycor ycor - 0.5 + random-float 1
]
end
Note that the adjustments to xcor and ycor are because sprout and move-to always place the turtle in the centre of the patch and there is no primitive for placing in a random place on a particular patch.

Netlogo add the average distance information on patches from the patch 3 -3

I would like to know how to design this.
All patches will compare their own distance from the patch [3 -3] with the average distance. So they realize that they are closer or far from the average distance.
globals [ref-patch av-dist]
to setup
ca
set av-dist mean [distance patch 3 -3] of patches
demo
end
to demo
ask patches [
if distance patch 3 -3 < av-dist [
set pcolor red
]
]
end
I think I would put everything in a patch reporter like this:
to-report closer-than-average?
report distance patch 3 -3 < mean [distance patch -3] of patches
end
Then you can
ask one-of patches [show closer-than-average?]
or
ask patches with [closer-than-average?] [set pcolor blue]
etc.

How can I move a turtle as close as possible to a certain patch?

I have a single blue patch and would like to move a turtle to the closest, empty patch to it. The only way I can think of doing this is using in-radius in a loop, increasing the radius size by one each time, but is there a better way?
globals [bluey]
to setup
ca
ask one-of patches [set pcolor blue set bluey self]
ask n-of 250 patches [sprout 1]
end
to-report nearest-empty [#patch]
report min-one-of
[other (patches with [not any? turtles-here])] of #patch
[distance #patch]
end
to test
setup
;the following fails if all patches occupied
;(can add a test for nobody)
ask nearest-empty bluey [set pcolor red]
end

NetLogo: Apply set [variable] from sub-procedure to global process?

I want to simulate reproduction process of turtles in time when single patch can by used only once. If the patch is red and ticks mod 50 = 0 then turtles-here (on this patch) hatch new 10 turtles. Every patch can be used only once during the whole simulation run.
Please, how can I include this condition into my code? I tried simple to change patch color to green with hope that the next hatching process will run only with red ones. However next time step NetLogo doesn't keep this patch green but changes it back to red. Thus my reproduction run from the same patch.
Any suggestions will be highly appreciated
The part of my code:
to go
if ticks mod 50 = 0 [ask patches with [pcolor= red] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[hatch 10 ;
die]
; set pcolor green - change infestlev from 2 to 5 only for specific tick, not for the rest of the simulation
end
The code you have should work fine. In your description, you state that the colour turns back to red - so that's why this code isn't working, somewhere else you have a colouring procedure. Alternatively, if you don't want to rely on colour (or if you want colours to mean something else), then you can add a variable to each patch to keep track of whether it has already reproduced.
patches-own [reproduced?]
to setup
...
ask patches [set reproduced? FALSE]
...
end
to go
if ticks mod 50 = 0 [ask patches with [not reproduced?] [reproduce] ]
end
to reproduce
ask one-of turtles-here
[ hatch 10
die ]
set reproduced? TRUE
end
Just as a general comment, it is a little odd to ask the patch to reproduce when what you are really trying to do is have the turtle on a patch reproduce. Logically you are saying that once one turtle on a patch has reproduced, then no other turtle on that patch can ever reproduce. If the reproduction is truly governed by the patch, it would be more usual to use sprout instead of hatch. That gets you code that looks like this:
to reproduce
sprout 10 [ any commands you want the new turtles to do ]
set reproduced? TRUE
end
My final working code with steps (available here: http://ulozto.cz/xRqtDDfV/timing-of-turtle-sprout-nlogo):
setup turtles
if turtle touch red patch, turn this patch blue
at the same time - tick 10 -> sprout from every blue patch 10 nwe turtles
every patch can be used only once during simulation run (turn red, assured by reproduced? variable)
enter code here
patches-own [reproduced?]
to setup
clear-all
setup-turtles
setup-patches
change-color
reset-ticks
end
to setup-patches
ask patches [set reproduced? FALSE]
ask patches [set pcolor green]
ask n-of 80 patches [set pcolor red] ; identify turles which could be a source for new turtles
end
to setup-turtles
crt 1
ask turtles [set color yellow]
end
to go
if ticks mod 10 = 0 [
ask patches with [(pcolor = blue) and not (reproduced?)]
[reproduce] ; set reproduction to every 10 ticks for all blue patches
]
move-turtles
change-color
tick
end
to move-turtles
ask turtles [fd 1]
end
to change-color ; if turtle touch red patch, red turns blue
ask turtles [if pcolor = red
[set pcolor blue]
]
end
to reproduce ; sprout 10 new turtles from blue patches at defined time step (multiply of 10 ticks)
sprout 10
set reproduced? TRUE
end

NetLogo - change color of patch when agent is on top

I am trying to make my turtle change the color of a patch when it comes into contact with it and have tried the following code:
to deesculateviolence
ask turtles [
if pcolor = red [set pcolor blue]
]
end
The code does not come up with any errors but when I play the model, the color of the patch does not change. I have tried similar codes from different models and still cannot get the patch to change color. If anyone knows where I am going wrong I'd really appreciate your help.
I think your code does the right thing :
to setup
clear-all
create-turtles 5 [
move-to patch random 20 random 20
]
ask n-of 25 patches [set pcolor red]
reset-ticks
end
to go
ask turtles [
rt random 10
fd 1
if pcolor = red [set pcolor blue]
]
tick
end
you can see the effect in following example better