UPDATE
Thanks Luke C & JenB. Here's my revised code. I just converted the females to "killer" by turning her territory to a different color (green to blue) if a male wanders into her territory. If any male happens to be on a blue colored patch, he dies. It seems that the male who initially triggers the conversion does not get eaten immediately, which is what I want. He can be eaten if he doesn't get out of her blue territory on the next tick. Then, any subsequent males are eaten immediately.
Also, I am exploring some cool things with the BehaviorSpace tool, like how many ticks it takes for all the males to be eaten in a number of runs.
The only loose ends are creating a graph window that displays the number of males over time (ticks) and a report window(s) with the number of nonkiller/killer females.
ORIGINAL POST
I would like to model a behavioral system of wolf spiders with the following rules:
Females are stationary
Males move around
If any male encroaches within a certain radius of a female, that female becomes a “killer” two ticks later
Any males within a “killer” female’s territory is eaten and taken out of the population
The simulation ends when all males are eaten
All females are “non-killers” at setup
End goals of the model:
How many ticks does it take for all of the males to be eaten?
Are there any ‘non-killer’ females in the population at the end of the simulation?
Here is what I have so far. The parts I am struggling with are setting up the female territories, converting non-killer females to killer females when a male enters her territory, accounting for/taking out eaten males, reporting the duration of the simulation in ticks and if any non-killer females remain at the end.
breed [females female]
breed [males male]
to setup
clear-all
reset-ticks
ask patches [set pcolor white]
setup-females
setup-males
end
to setup-females
create-females 15 [
set shape "spider"
set color 15
set size 25
setxy random-xcor random-ycor]
ask females [move-to one-of patches with [not any? females in-radius 40]
; prevents overlapping
ask patches in-radius 20 [set pcolor green]
]
end
to setup-males
create-males 30 [
set shape "spider"
set color black
set size 20
setxy random-xcor random-ycor]
ask males [move-to one-of patches with [not any? males in-radius 10]
; prevents overlapping
]
end
to go
move-males
tick
if count males = 0 [stop]
end
to move-males
ask males [
right random 360
if random 100 > 49 [forward 1 + random (15 - 1)]
if pcolor = blue [die]
;moves male 1 to 15 units per tick if condition satisfied
score-females
]
end
to score-females
ask females in-radius 20 [
if count males > 0 [ask patches in-radius 20 [set pcolor blue]]
]
end
Related
I have added 4 different user input sliders into net-logo. Yellow, red and green should represent percentages of the total patches grid, whilst the blue one is on a set amount of 0-10 (non-percentage). The % sliders vary in value for both green red and yellow. When I'm setting up the grid with the following code, only the blue patches are being inserted in the grid. I'm also looking to continuously have the same % of each colored patch throughout the simulation. Any help is appreciated as I've been trying this one for a good couple hours without any luck googling.
let total 961
let p-yellow yellow-patches-amount-% * 100 / total
let p-green (green-patches-amount-% * 100) / total
let p-red (red-patches-amount-% * 100) / total
let p-blue blue-patches-amount
let patch-yellow p-yellow
ask patches with [pcolor = black] [
if count patches with [pcolor = blue] < p-blue [
ask n-of p-blue patches [set pcolor blue]
]
]
ask patches with [pcolor = black] [
if count patches with [pcolor = yellow] < p-yellow [
ask n-of p-yellow patches [set pcolor yellow]
]
]
end
Your problem is how you define p-yellow, and how you use your percentage.
Let's say yellow-patches-amount-% is 50. Multiply that by 100 and you get 5000. divide that by 941 and you get 5.3 yellow patches. That number is then rounded down by n-of.
The correct way of using percentages here is:
let p-yellow yellow-patches-amount-% / 100 * total
Then there are a few problems with how you recolor your patches. I will address them one by one.
In your code, you have a ask patches [ ask patches [...]] construction. This is almost always a horrible idea, since it means that every patch can be asked to do something patch number of times, instead of just once. Letting one agent ask all agents to do something can be okay. Letting all agents ask one agent to do something can be okay. But letting all agents ask all agents to do something will almost always lead to code not doing what you want. I simplified the code you were using to illustrate this.
to setup
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
ask patches with [pcolor = black] [
if count patches with [pcolor = yellow] < p-yellow [
ask n-of p-yellow patches [set pcolor yellow]
show count patches with [pcolor = yellow] ;first 54, next 108 or a bit lower
]
]
end
With this setup, you would want 54 patches to become yellow. The first black patch that you call upon will now count all yellow patches, which is 0. Because of that, it now asks p-yellow, rounded down, random patches to turn yellow. Now the second black patch comes up and counts all yellow patches. It comes up with 54, which is lower than 54.45, so again it asks 54 random patches to turn yellow. By now you have more than 54.45 yellow patches so all subsequent patches count them but no longer ask any patches to change color. But that still means you turned a patch yellow 108 times instead of 54 times.
Even though you turned a patch yellow 108 times, you didn't necessarily turn 108 different patches yellow. If you ask n-of p-yellow patches [set pcolor yellow] once, then each patch can only be turned yellow once. This breaks down if you do it multiple times, since now ask n-of p-yellow patches [set pcolor yellow] can also ask a patch that you previously turned yellow to turn yellow again. This can be avoided by using ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow]. It is especially important since you are working with multiple colors.
Finally, the if count patches with [pcolor = yellow] < p-yellow construction makes sense if you are turning patches yellow one by one until you have >= p-yellow patches. The moment you turn more than 1 patch yellow at the same time, this sort of construction can easily let you overshoot your target.
With that in mind, I have two different suggestions for you
to setup-1
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
ask n-of p-yellow patches with [pcolor = black] [set pcolor yellow] ;54 in this case
end
to setup-2
ca
let total count patches ;1089 in this case
let p-yellow 5 * total / 100 ;54.45 in this case
while [count patches with [pcolor = yellow] < p-yellow] [
ask one-of patches with [pcolor = black] [set pcolor yellow]
] ;55 in this case
end
setup-1 simply turns the required number of patches yellow all at once and is done with it.
setup-2 turns black patches yellow one by one, until the count of yellow patches exceeds p-yellow. Note that this will get you 55 yellow patches instead of 54, since p-yellow > 54. If you would want it to be rounded down instead, you could use floor, which rounds a value down to the nearest integer:
[count patches with [pcolor = yellow] < floor p-yellow]
I´m setting up some turtles randomly on the the map, then they have to change the color of the patches to show that they cultivated however some times they overlap and change the color of other turtles. To solve this i asked not any? other turtles in-radius 6 to force them to be apart, but this is not elegant nor efficient. What would be a better way to give each turtle their own patch to make a more populated world.
to setup
ca
resize-world 0 100 0 100
create-turtles 50
[ set size 1
set color 135
setxy random-xcor random-ycor
move-to one-of patches with [not any? other turtles in-radius 6]
ask patches in-radius (2 + random 2) [set pcolor 35]
]
end
to go
ask turtles[ ask patches in-radius 4 with [pcolor = 35]; falta representar las cosechas 4-2-3-3
[ set pcolor 42]]
end
The go is using the fact that there are no turtles in a radius of 6, but if i wanted to get them closer or change the color on different ticks this would be bad.
Thanks. If more is need please let me know.
You can give the turtles a new turtles-variable that you can assign a patch-set to. This is done by using turtles-own at the start of your program.
The turtle then remembers which patches were assigned to this variable and can access them again at a later time. This way, when it comes time to change patch colors, they only change the colors of patches within their original radius and not of the patches in the extended radius.
turtles-own [territory]
to setup
ca
resize-world 0 100 0 100
create-turtles 50
[ set size 1
set color 135
setxy random-xcor random-ycor
set territory patches in-radius (2 + random 2)
ask territory [set pcolor 35]
]
end
to go
ask turtles [ ask territory ; falta representar las cosechas 4-2-3-3
[ set pcolor 42]]
end
As an alternative solution, here it is not the turtles that remember which patch they own, but the patches that remember which turtle owns them. In the case I use here, patches can only be owned by a single turtle.
Here, I give each patch a patch-variable using patches-own. I then let the turtles tell the patches within their radius to choose that turtle as their owner ask patches ... [set owner myself]. Myself is a reporter that refers not to the patch carrying out the command, but to the turtle that asked the patch to carry out the command.
patches-own [owner]
to setup
ca
resize-world 0 100 0 100
create-turtles 50 [
set size 1
set color 135
setxy random-xcor random-ycor
ask patches in-radius (2 + random 2) [
set owner myself
set pcolor 35
]
]
end
to go
ask turtles [ ask patches in-radius 4 with [owner = myself] ; falta representar las cosechas 4-2-3-3
[ set pcolor 42]
]
end
If multiple turtles try to be the owner of the same patch, the last one becomes the owner. This is because each turtle completely overwrites the previous owner value.
If you still want an option for multiple owners, you can work with a turtle-set instead. This requires a few different primitives.
In setup, you have to define owners as an empty turtle-set in order for the rest to work, using set owners (turtle-set).
To then assign a turtle to owners, you use set owners (turtle-set owners myself). This changes the owners turtle-set to add the turtle calling the patch to the set.
Finally, you can no longer ask patches to with [owner = myself] to change color since owners is now a turtle-set, not a turtle. Instead, you use the member? primitive that looks if a turtle is part of a turtle-set.
patches-own [owners]
to setup
ca
resize-world 0 100 0 100
ask patches [set owners (turtle-set)]
create-turtles 50 [
set size 1
set color 135
setxy random-xcor random-ycor
ask patches in-radius (2 + random 2) [
set owners (turtle-set owners myself)
set pcolor 35
]
]
end
to go
ask turtles [
ask patches in-radius 4 with [member? myself owners] ; falta representar las cosechas 4-2-3-3
[ set pcolor 42]
]
end
im trying to show a cultivation process. At setup im creating the farmers and giving them a random farm size (with the splotch) then, at go, im telling them that if they have enought money all patches near them that are the splotch turn into green, representing cultivation. But its just changing one pixel and not all round it. It most be someting small but i cant see it. Thanks in advance for the help
.
breed [cercas cerca]
breed [medios medio]
breed [lejos lejo]
patches-own[calidad
cercanialago
cultivado
]
turtles-own [ingresos
gastos]
create-cercas 10 + random 10
[ set size 1 ;; easier to see
set color 135
setxy random xcor random ycor
move-to one-of patches with [not any? other turtles in-radius 3 and pcolor = 57]
set heading random 45 + 45
set ingresos 1000000 + random 6000000
]
ask turtles
[ ask patches in-radius (1 + random 3)
[ set pcolor 35 ] ]
to go
ask cercas [
ifelse ingresos > 2000000 [if any? patches in-radius 4 with [pcolor = 35] [if ticks mod 3 = 0 [set pcolor 62] ]]
[]
] ```
Your cercas are a breed of turtles.
A useful feature of NetLogo is that any turtle can directly read and modify the variables of the patch it is on (i.e. without the need to invoke such patch).
So when you ask a turtle to set pcolor 62, it will automatically refer to pcolor of the patch it is on.
If we eliminate all of the conditions from your last block of commands, we have:
ask cercas [set pcolor 62]. This is what you are asking cercas to do: simply changing the pcolor of the patch they are on.
The fact that you use patches in-radius 4 in the condition for the first if statement does not influence the ask cercas [set pcolor 62] part. The condition is one thing, the command to be executed if the condition holds true is a separate thing.
Therefore you should make cercas ask patches in-radius 4 to change their pcolors.
I'm trying to have a car visit 50 different cities in the shortest route possible, and every time he visits a place he changes the colour of the city form blue to yellow and moves on, trouble is I'm finding errors trying to find a way to implement this. I'm getting many errors in the last line of code, where it says CAR expected 1 input, a number. Any help would be appreciated.
breed [cities city]
breed [cars car]
cars-own [history travelled-distance]
to setup
clear-all
reset-ticks
setup-patches
setup-turtles
end
to setup-turtles
ask n-of 50 patches with [pcolor = 55 and not any? other turtles-here][sprout-cities 1 [set color blue set size 2 set shape "square"]]
create-cars 1[
setxy -90 -90
set color red
set size 5
]
end
to setup-patches
ask patches [set pcolor green]
ask n-of 100 patches [set pcolor brown ask neighbors [set pcolor brown]]
end
to go
ask cars [
pendown
if history <= 50
[ ;set heading towards city in-radius 10]]
move-to min-one-of cities in-radius 360 [distance myself]
]
if car in-radius 5 = true [set color = yellow]]
end
set heading towards one-of cities in-radius 10
I have a slider that controls population, set to the max value of 100, which creates 100 turtles in the nest. However, the number of turtles within a breed is independent of the total population, so instead of having 100 total turtles, I get 100 + #breed1 + #breed2. Additionally, I am hatching new turtles of breed [followers] and [foragers] during the course of the model. How do I get the turtles to die each time a new breed member is hatched?
I know this is not a code problem, but ideally, I would like the new foragers to be the turtles that are in the nest, not just new turtles.
to setup
clear-all
set-default-shape turtles "bug"
create-turtles population
create-foragers 10
[set color yellow]
end
to go ;; forever button
ask leaders
[wiggle
fd 1
return-to-nest]
ask followers
[if any? leaders
[uphill-chemical
fd 3
pickup-food]
uphill-food
fd 1
if distancexy nest-x nest-y < 3 and color = violet
[hatch-foragers 1
[set color yellow
uphill-chemical]]
tick
end
to return-to-nest ;; turtle procedure
ifelse nest?
[if count followers < 5
[hatch-followers 1 [set color brown - 1]]
facexy food-x food-y ;; drop food and head out again
move-to patch food-x food-y]
[else commands]
end
````````
The easiest way to maintain a population is simply to ask a newly born turtle to kill a randomly selected turtle. So instead of:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
]
]
you could have:
[ if count followers < 5
[ hatch-followers 1
[ set color brown - 1
ask one-of turtles [die]
]
]