When I run the following two lines I get different answers. Does anybody know why? The first gives the answer I want:
ask turtles[
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = tempcol]]
ask turtles[
set nextcolor [color] of self
let tempcol [color] of self
show count (turtles-on neighbors4) with [color = [color] of self]]
You're right in that the issue is with the use of self- from the dictionary entry for that primitive:
"self" is simple; it means "me". "myself" means "the agent who asked
me to do what I'm doing right now.
In short, you want myselfin the second example. Currently, your second example is saying something like, "turtles, show the count of neighbor turtles whose color is the color of themselves" where you really want to say "turtles, show the count of neighbor turtles whose color is the color of myself." For a potentially clearer example, check out this setup:
to setup
ca
crt 10 [
set color red
setxy random-xcor random-ycor
]
ask n-of 3 turtles [
set color blue
]
reset-ticks
end
This creates 7 red turtles and 3 blue turtles. Now, if you ask one of the blue turtles to show the count of of turtles with the same color as itself, we should expect it to return a value of 3. If you run that code using self, however, the value returned is 10- because all turtles have a color that is equal to the color of themselves:
to self-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of self' example:"
show count turtles with [ color = [color] of self ]
]
end
If you run the exact same code but use myself, it returns the answer we would expect:
to myself-compare
ask one-of turtles with [ color = blue ] [
print "'[color] of myself' example:"
show count turtles with [ color = [color] of myself ]
]
end
I would also point out that almost all of your of self statements are redundant-you should be able to take them all out (except for [color = [color] of self]] that you will be changing to a myself statement anyway) and have your code run as before.
Related
I have a set of positive COVID-19 cases by district in a state. I want to use agent based modelling to make the infected cases move around the state and count the number of infected, number of healthy and number of immune. I have a set of code which the number of population is random and not based on the location. Tried to watch youtube for the tutorial but could not find one which really helps. the code is as below. I want to insert the map of the state, number of population for each district and number of infected in Netlogo. The codes in Netlogo are as below.
turtles-own [illness]
to setup
clear-all
reset-ticks
create-turtles 200 [
setxy random-xcor random-ycor
set shape "person"
set color pink
set size 1.0
set illness 0
]
ask n-of starting-number-infected turtles [
set color yellow
set shape "X"
set size 1.0
]
end
to go
tick
move
infect
recover
lose-immunity
end
to move
ask turtles [
rt random 360
fd movement
]
end
to infect
ask turtles [
if color = yellow [
ask turtles in-radius infect-distance [
if random-float 200 < infect-chance [
if color = pink [
set color yellow
set shape "X"
]
]
]
]
]
end
to recover
ask turtles [
if color = yellow [
set illness illness + 1
if illness > infectious-period [
set color white
set shape "circle"
set size 1.0
set illness 0
]
]
]
end
to lose-immunity
ask turtles [
if color = white and waning-immunity < random-float 100 [
set color pink
set shape "person"
]
]
end
Our trout model provides an example of using the GIS extension to import a set of polygons that represent the places where agents live. In our case, the polygons are habitat "cells", and the agents are fish; but you could use the same approach to import districts and keep track of which people are in which district.
Our model does not represent where a fish is within a polygon, but only which polygon they are in. We use a separate breed of agent called "cells" that contain the variables of the polygon. Each NetLogo patch has a variable "patches-cell" that says which cell it belongs to.
The model is here: https://ecomodel.humboldt.edu/instream-7-and-insalmo-7
(look for inSTREAM7.3). Look at the procedure build-cells.
Steve R.
I'm quite new to NetLogo and I want to use the below code to create a world of green and red circles, but the below code doesn't work with the color as it is only grey? any advice?
to create_turtles
ca
ask patches [ sprout 1 ]
ask turtles [ set shape "circle" set color green]
end
I just tried your code and it works just fine except it just create all green turtles (circles). If you want it red and green, I suggest you add some piece of code in the ask turtles command and may I suggest you to use the indentation style as well (usually NetLogo will do it automatically):
to create_turtles
ca
ask patches [ sprout 1 ]
ask turtles
[
set shape "circle"
set color green
let chooser random 2
ifelse chooser = 0
[ set color green ]
[ set color red ]
]
The let procedure is a local variable assigner and we let the value a random number of 0 and 1 (two numbers, hence the random 2, and primitive random always include 0 as first number).
In that example we ask the circles to randomly choose a number between 0 and 1. If it chooses 0 then it will set it color to green, otherwise red.
You can explore more about those primitives in the NetLogo Dictionary.
I'm very new at Netlogo and programming in general.
I want to create a netlogo model, with female and male turtles. Both populations move through the world via random walk. The female population should find a partner and has the property 'radius'. Her own radius should expand if she has not found a partner until the moment she finds one. How can I program a radius around the female turtles, that expand after each time step if she has not found a partner?
Thanks for your help!
First, you need a turtle attribute that stores the value for each turtle. The way to do that is with a turtles-own statement. Then you simply change the value as required. The primitive in-radius looks at everything within the specified distance, and then you can set a condition by whether there are any? suitable mates. Your code would look something like (this is a complete model):
turtles-own
[ search-radius
mate
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set color blue
set search-radius 1
]
reset-ticks
end
to go
check-for-mate
tick
end
to check-for-mate
ask turtles with [color = blue]
[ let candidates other turtles in-radius search-radius
ifelse any? candidates
[ set mate one-of candidates
set color red
]
[ set search-radius search-radius + 0.5 + random-float 1
]
]
end
I need to "do-something-special" if around yellow turtle there is at least 3 blue turtles . Is the code bellow correct?
I tried
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[do-something-special]
]
do-something-special should remove (disappear) 3 of blue turtles and current yellow one
Did I do the location of relevant turtles correctly and how do I kill them after I find them?
Hannah's answer is good but the linked example won't fully fix your problem. Since you will be using the set of close agents twice (once to count and once to potentially remove some), you should also create an agentset for efficiency reasons (you don't want to create it twice). Here is a full solution.
ask turtles with [color = yellow]
[ let near-blue turtles in-radius 1 with [color = blue]
if count near-blue >= 3
[ ask n-of 3 near-blue [die]
die
]
]
Also, if you don't care about the exactness of the radius, an alternative to turtles in-radius 1 would be turtles-on (patch-set neighbors patch-here), which is all the turtles on the neighbouring and same patches to wherever your asker turtle is sitting.
At the moment your code counts the amount of turtles that are blue in the radius of one patch around the yellow turtle. If the amount of blue turtles is bigger/equal 3 the yellow turtles die if you use the "die" command instead of "do-something-special". So it looks as follows.
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[die]
]
Maybe you can merge the code with the following example and then kill the neighbors.
I want to hatch a turtle after two different colors of turtles overlap.
to interact
if any? other turtles-here
[
birth
]
;detect interaction
end
to birth
ask turtles
[
hatch random 5 [ fd 1 ]
]
end
I would like to hatch a turtle that was an average color of the two parent turtles that interacted.
something like.
to birth
ask turtles
[ hatch random 5
[ let color be sum of previous turtles color sum / 2
fd 1 ] ]
end
also any tips on what I might be misunderstanding about netlogo syntax might be appreciated.
This might not be exactly what you're looking for, but if the parents are the only ones on that patch when they give birth then this block should do the trick.
to birth
let Q mean [color] of turtles-here
ask one-of turtles-here
[hatch random 5
[
set color Q
fd 1
]
]
end
I'm not sure if you'd need to make the offspring it's own breed though to tell them to change their color and move, or if this will work... If this doesn't work then:
breed[offsprings offspring]
breed[parents parent]
to birth
let Q mean [color] of parents-here
ask one-of parents-here
[hatch-offsprings random 5 ]
ask offsprings-here
[
set color Q
fd 1
]
end