Netlogo - Filling neighboring patches with specific colored turtles until full - netlogo

I'm trying to ask white turtles to create yellow turtles to one of the 8 empty neighboring spaces. If there is no free space the turtle should produce nothing.
Note: white turtles stay white and produce yellow turtles which are able to reproduce themselves as well as other yellow turtles. In summary, at the end I would like to fill up the black spaces with yellow turtles.
breed [ cells cell ]
cells-own [ n ]
to setup
clear-all
set-default-shape cells "square"
ask patches [ if pycor = min-pycor [sprout-cells 1]]
ask cells [ ifelse random 10 < 2 [set color white] [set color yellow]]
Thanks for the reply.
I'll explain what I'm trying to do:
The white squares represent "stem cells" which have the ability to reproduce themselves and produce another type of cell (e.g. TA cell), so that, initially each white cell will produce another cell above it or at one of its above corners.
In the second step, each TA cell produces other cells randomly in any empty space around it.
Third, stem cells (white squares) repeat step 1 and at the same time TA cells fill in one of the empty spaces around them. The cells (both stem cells and TA cells) stop producing new cells once they have no empty space around them (when the 8 neighbors are already filled in).
At the final stage all of the black space should be filled in with TA cells. The yellow squares at the last row basically do nothing.
Thanks again for your help.
reset-ticks
end
to go
ask cells
[ set n count neighbors with [pcolor = yellow] ]
ask cells
[ if n >= 1
[ set color yellow] ]
tick
;ask cells
;[if ticks = 10
;[set color yellow]]
end

What exactly you are trying to do is not entirely clear to me. I am going to go with a likely interpretation, but whether or not my interpretation is correct, you should try to clarify your question.
Trying to understand your code, it seems to me like there is a bit of confusion between patches and turtles. In your go procedure, you ask cells to turn yellow if they have a yellow neighbor. I think that what you want is for a yellow square to appear on patches that are neighbors of yellow squares. The empty patches you are trying to fill don't have any cells on them yet, so ask cells to turn yellow will not help you here.
If you really want to stick with cell agents, keeping your current setup procedure, you could do something like:
to go
ask patches with [not any? turtles-here] [
if any? neighbors with [any? turtles-here] [
sprout-cells 1 [
set color yellow
]
]
]
end
(If you want your cells to grow from the bottom up only, you should turn off wrapping in the view settings by right clicking on the view and choosing Edit...)
Now, a completely different approach, that may be simpler if all you want is some kind of cellular automaton, would be to ditch agents and work only with patches. That would give you something like:
to setup
clear-all
ask patches [
if pycor = min-pycor [
ifelse random 10 < 2
[set pcolor white]
[set pcolor yellow]
]
]
reset-ticks
end
to go
ask patches with [pcolor = black] [
if any? neighbors with [pcolor = yellow or pcolor = white] [
set pcolor yellow
]
]
tick
end
Both approaches are valid. The first one is a bit more visually pleasing. The second one is slightly simpler because you don't need turtles at all. It depends on what you ultimately want to do.

Related

Moving turtles towards two different colors of the patch in NetLogo

I have a basic NetLogo question.
I would like to encode the following:
The turtle asks what color the patch is in if it is green, it can walk on the green patches.
I know it is a basic question of NetLogo. But, I'm trying and some errors appear, such as: MOVE-TO expected input to be an agent but got NOBODY instead
Could someone help me understand what I am wrong with, or give any suggestions or even models that do this for me to explore? Every help is welcome. Thanks
globals [ edge-size ]
to setup
clear-all
set edge-size 10
set-patch-size 20
let pcolors []
set pcolors [135 55 105 85]
ask patches [
set pcolor item (random 4) colors
]
crt 1 [
set size 1
set color black
;pen-down
]
reset-ticks
end
to go
ask turtles [
if pcolor = green ;; if own pcolor patch = green
[ move-to one-of patches with [ pcolor = "green" ] ]
end
This code move-to [ veg ] of patch-under-me suggests you have several fundamental misunderstandings about how NetLogo thinks. I recommend that you do some more of the tutorials and look at the Model Library built in to NetLogo to find models that are similar to what you want to do. You also need to build in much smaller pieces and get a piece working properly before moving to the next piece.
What is wrong with move-to [ veg ] of patch-under-me?
[ veg ] of patch-under-me is a variable with values like pink or green. So you are saying 'move to green'
What's more, the colour that it gives back is the colour where the turtle is standing now, not where you want it to go to
You probably want something more like:
move-to one-of patches with [veg = "green"]
So go back to basics. Have just two colours (say red and blue) and move turtles around trying to get onto a red patch. Make that work before doing anything else. Now have only a few red patches, does your code still work? Then you can start adding a bunch of different conditions and colours

NetLogo: Having a turtle remember its starting location

I want to have my turtles move back and forth between a central area and their starting location. I have set the central area (patch 0 0, and its neighbouring patches). I have set these turtles to begin from random locations on setup.
Now I need them to move to the central area and be able to remember and return to their respective starting positions. Here is my attempt, but one that is not working.
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
create-turtles 10
[ set shape "car"
set size 1
set color white
setxy random-xcor random-ycor
if (patches != patches with [pcolor = green])
[ set start-position (random-xcor random-ycor)] ;; line with error
]
to go
ask turtles
[ set heading target-patch move-to target-patch
set hold-time hold-time + 5
]
ask turtles
[ if hold-time >= 10
[ set heading start-position move-to start-position]
]
end
There are several problems with your code. I strongly suggest that you code in smaller pieces. That is, add some code and make sure it works before writing the next piece. Making sure it works is not just making it through without error messages, it needs to do what you expect it to do.
On your specific question. The line if (patches != patches with [pcolor = green]) is causing an error. First, patches is the set of all patches, not just a particular patch. So you are (sort of) asking whether the set of all patches is not equal to the set of patches that are green. Is that really what you intended? If so, it is easier to simply ask whether there is any patch that is not green:
if any? patches with [pcolor != green]
or to check whether they are all green and continue if not:
if not all? patches [pcolor = green]
However, since you are asking about moving back and forth to and from the central green patches, I think you really want to have the turtle check whether the patch they happen to be located on is green. This code looks at the patch where the turtle is located (patch-here) and checks whether the color (pcolor) is green:
if [pcolor] of patch-here = green [ ]
However, one of the tricks of NetLogo is that turtles can access the variables of the patch they are on directly. Note that a patch cannot access a turtle's variables because there may be multiple turtles on the patch so the patch doesn't know which turtle you want. But a turtle can only ever be on one patch at once. So you could write:
if pcolor = green [ ]
You also need to rethink this code:
ask patches
[ set target-patch patch 0 0
ask target-patch
[ set pcolor green
ask neighbors [set pcolor green]
set hold-time 5
]
]
This suggests to me that you have misunderstood something very fundamental to NetLogo programming. You need to think from the perspective of an individual agent. Looking at this code, you first do ask turtles, so that is going to run through all the turtles in random order. Let's call them A, then B, then C and so on.
What is each turtle going to do? Everything in the [ ]. So, A sets the value of the global variable named "target-patch" to patch 0 0. Then A asks that patch to turn green, have the 8 surrounding patches to turn green, and to set the variable "hold-time" to the value 5.
So far, so good. But then turtle B does exactly the same thing - it assigns "target-patch", turns it and its neighbors green etc. Then turtle C. If you have 100 turtles, this block of code will run 100 times and do exactly the same thing each time.

How to get turtles run back and forth a coloured patch?

I have a simple image as a map. I would like the turtles to start from the lighter grey colour and run to the darker patches for resources. How can I do that?
My Code
to setup-patches
import-drawing "01.png"
import-pcolors "01.png"
ask patches [
setup-house
;setup-resource
]
end
to setup-house
create-turtles [setxy where pcolor = grey]
set house? where pcolor = grey
end
The image is at https://i.imgur.com/dmODyUW.png.
I can provide more details on request.
Okay, a patch is actually a NetLogo term for one of the grid cells in the World. Your image suggests that the grey areas will cover multiple NetLogo patches each. The following code creates some random grey multi-patch areas and a house at one of the patches in one of the grey areas.
breed [houses house]
to testme
clear-all
setup-patches
setup-houses
end
to setup-patches
ask n-of 3 patches
[ set pcolor gray
ask neighbors
[ set pcolor gray
ask neighbors
[ set pcolor gray
]
]
]
end
to setup-houses
ask one-of patches with [pcolor = gray]
[ sprout-houses 1
[ set color red
]
]
end
Your question is too vague to properly answer, but hopefully this will get you on the right track. I suggest you redo the NetLogo tutorials and look at some of the models in the library included in the software to find pieces of code that do tasks you are going to need.

Netlogo random patches setup without them touching at all

I am trying to set up random patches that have no corners touching at all, and the color of those patches are different colors of green (to visually represent each patch having a random quality value in my model). My conceptual idea of how to do this is to have the “false” part of my “ifelse” statement to basically tell the program to keep trying to assign the patch a different place if it has another patch touching any part of it, until all patches have a place with no other patches touching it all (including corners). I just have no idea if that is possible or what code could do that. Is there a netlogo primitive that is equivalent to "until" ? Any help or ideas would be appreciated!
Here is a part of my code:
patches-own [ quality ]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to setup-patches
ask n-of number-of-patches patches
[ ifelse sum [ pcolor ] of neighbors = 0
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10
]
[ set pcolor black ; this is the line I need to change
]
]
end
Have a look at the primitives while and neighbors in the NetLogo dictionary. I am not completely clear on the sequencing you want in your code so I can't provide a full answer. However, you want something like:
while any? neighbors [(however you define the ones you don't want to touch]
[(try again)
]
Note that patches always touch other patches because the world is constructed as a grid of patches. So you presumably want patches with specific conditions (such as high values of quality) to not touch each other.
As JenB says, patches are always touching other patches, so I'm assuming the question is "How can I generate number-of-patches green patches that aren't touching any other green patches?". Then I'd do this:
to setup-patches
while [ count patches with [ shade-of? green pcolor ] < number-of-patches ]
[ ask one-of patches with [ quality = 0 and count neighbors with [ shade-of? green pcolor ] = 0 ]
[ set quality random (2 + random 8)
set pcolor scale-color green quality 0 10 ] ]
end
Basically, while the number of green patches is less than the desired number of green patches, choose one of the patches that isn't green and doesn't have any green neighbors. Then turn it green.

Determining the radius of turtle clusters and number of turtles in them - postprocessing

If I have a situation in which about a 1000 black turtles disperse at random angles and steps throughout the netlogo world for a given duration of ticks. Each turtle is assigned a random probability at each timestep during dispersal, and if this number exceeds a given threshold for any given turtle it changes it's color to red and stops moving. Additionally, black turtles (still moving) that happen to move within a patch of red turtles (stopped/settled), change their color to grey and settle (stop moving) as well. Finally, other black turtles (still moving) that happen to be move within a patch of either grey or red turtles (stopped/settled), also change their color to grey and settle (stop moving)
My question is a post-processing question for when the simulation duration is reached. How do I determine the number of clusters of red-grey turtles in the sea of black turtles? Also, how do I determine the size (radial extent) of each cluster? And finally, how do I determine the number of turtles in each cluster?
Jen is right: you need a clear idea of what constitutes a cluster before being able to truly answer that question.
That being said, one possible option is to use a clustering algorithm. I'd suggest taking a look at Christopher Frantz's dbscan extension.
Here is a quickly thrown together example:
extensions [ dbscan ]
to setup
clear-all
ask patches [ set pcolor white ]
create-turtles 1000 [
set color black
set label-color blue
setxy random-xcor random-ycor
]
ask n-of 5 turtles [
ask turtles in-radius 3 [
set color one-of [red grey]
]
]
end
to find-clusters
let red-grey-turtles turtles with [ member? color [red grey] ]
let clusters dbscan:cluster-by-location red-grey-turtles 3 3
(foreach clusters range length clusters [ [c i] ->
foreach c [ t ->
ask t [ set label i ]
]
])
end
Sorry for the lack of further explanations: I have a plane to catch...