Color a radius of patches Netlogo - netlogo

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.

Related

How to give property to agents Netlogo

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

How do you fix a problem with convert probability on a zombie apocalypse simulation on Netlogo [duplicate]

This question already has an answer here:
How do you go about making a convert probability chance for an agent in netlogo?
(1 answer)
Closed 1 year ago.
currently I'm working on a zombie apocalypse simulation on Netlogo, and I'm almost done except that the convertion function is not working. when the zombies collide with humans the humans they either die or convert the human to a zombie according to the global variable convert_probability. For example, if convert probability is set to 80 the human would have an 80% chance of winning the fight and killing the zombie and the zombie would have a 20% chance of converting the human to a zombie.
Also, zombies and humans are not allowed to pass over the brown patches and the function I wrote for that is change_direction, but had to comment those parts because I run to an error.
I have spent hours but couldnt come up with a solution so I would really appreciate any help;
breed [humans human]
humans-own [ humans_speed]
turtles-own [
infected?
]
patches-own [
block
]
breed [zombies zombie]
zombies-own [ zombies_speed]
globals [
student_id
username
nzomb
%infected
]
to setup_world
clear-all
reset-ticks
set student_id 19009670
ask patches [set pcolor green]
set-default-shape zombies "person"
set-default-shape humans "person"
create-zombies 5 [
set color red
set size 4
set zombies_speed 0.5
setxy random-pxcor random-pycor
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
create-humans 15 [
set color blue
set size 4
set humans_speed 1 + random-float 0.1
setxy random-pxcor random-pycor
]
ask humans[
move-to one-of patches with [pcolor = green]
]
set %infected (count turtles with [color = red ] / 20 ) * 100
set nzomb 0
draw_blocks
end
to draw_blocks
ask patches [
set block false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set block true
]
end
to change_direction
if[block] of patch-ahead 2 = true [
right 180
face one-of patches in-radius 2 with [not block]
]
end
to run_model
if ticks >= 10000 [ stop ]
tick
move-turtles
convert
if not any? humans [stop]
end
to move-turtles
ask humans [move-humans]
ask zombies [move-zombies]
end
to move-humans
ask humans [
if any? zombies in-radius (10 + random 20) [rt 180]
set infected? false
set heading (heading + 45 - (random 90))
forward 1 + random-float 1.1
zombiedeath
convert
;;change_direction
]
end
to move-zombies
ask zombies [
chasehuman
convert
zombiedeath
set %infected (count turtles with [color = red ] / 20 ) * 100
set infected? true
set heading (heading + 45 - (random 90))
forward 0.5
; change-direction
]
end
to chasehuman
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
; change_direction
end
to runaway
face min-one-of zombies [distance myself]
rt 180
;change_direction
end
to convert
ask turtles with [ breed = zombies ]
[
ask humans-on patch-here
[
if random 100 < convert_probability [set breed zombies]]
ask humans-on patch-here [if random 100 < convert_probability
[set color red]]
]
end
to zombiedeath
ask zombies-on patch-here [if random 100 > convert_probability
[die]]
end
Okay, the only procedure relevant to this question is this one (same code as above, formatted so you can see the structure logic}:
to convert
ask turtles with [ breed = zombies ]
[ ask humans-on patch-here
[ if random 100 < convert_probability
[ set breed zombies
]
]
ask humans-on patch-here
[ if random 100 < convert_probability
[ set color red
]
]
]
end
You haven't actually told us what the problem is - "convertion function is not working" does not say whether it is generating an error, or what behaviour it is doing compared to what is expected for example. But, look at what this code actually does. First it finds all the zombies then, for each zombie, it:
gets the humans on the same patch to convert to zombies with some probability
gets the humans on the same patch to change colour to red with some probability
Your first problem is that you haven't linked the turning red with becoming a zombie. Is that the problem you were trying to solve? If so, here is some cleaned up code that links zombie and red:
to convert
ask zombies ; note, you can just directly use the breed name
[ ask humans-here ; again, can use the breed name
[ if random 100 < convert_probability
[ set breed zombies
set color red
]
]
]
end

Netlogo assign group of patches to own variable of a breed

I'm very new to netlogo and am wondering how I can set a group of patches to be an own variable to a certain breed. For example, let's say I have:
breed [ buildings building ]
buildings-own [ my-patches ]
I want to be able to have a set of patches (let's say a rectangle, so constrained by some coordinates) that are assigned to each individual building's my-patches field. How would I do this?
First thing you need to know is that you can't have breeds of patches. While you don't say that's what you want, I just want to make sure you are aware of this.
Have a look at this code. It is a complete program that creates some turtles (called realtors) and assigns them each some patches. It then turns those patches the same colour as the realtor.
breed [realtors realtor]
realtors-own [my-patches]
to setup
clear-all
create-realtors 10
[ setxy random-xcor random-ycor
set size 2
set shape "circle"
set my-patches n-of 5 patches in-radius 3
]
ask realtors [ask my-patches [set pcolor [color] of myself ] ]
reset-ticks
end
You can run it by creating a button for 'setup' or simply typing setup in the command centre.
Welcome to Stack Overflow and Netlogo! Given your breed and buildings-own as above, you can simply use set to assign the patch-set that you want a building's my-patches variable to hold.
to setup
ca
ask patches with [ pxcor mod 10 = 0 and pycor mod 10 = 0 ] [
sprout-buildings 1 [
set shape "square"
set heading 0
set size 1.5
set my-patches patches with [
pxcor > [pxcor] of myself - 3 and
pxcor < [pxcor] of myself + 3 and
pycor > [pycor] of myself - 3 and
pycor < [pycor] of myself + 3
]
ask my-patches [
set pcolor [color] of myself - 2
]
]
]
reset-ticks
end

You can't use Migrate in a turtle context, because migrate is observer-only

I managed to build my small model, but i do get an error, but don't know why?
Ps: i'm a bloody beginner
The Error code is:
You can't use migrate in a turtle context, because migarte ia observer-only.
So what can I do?
Thank you for your answers.
breed [fish a-fish]
breed [boats boat]
boats-own [profit]
to setup
clear-all
ask patches [set pcolor blue]
set-default-shape fish "fish"
create-fish initial-number-fish
[
set color grey
set size 1.0
setxy random-xcor random-ycor
]
set-default-shape boats "boat"
create-boats initial-number-boats
[
set color black
set size 1.5
set profit random (1 * profit-per-fish)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask fish
[
move
fish-reproduce
]
ask boats
[
move-boats
catch-fish
death
reproduce-boats
migrate
]
tick
end
to move
rt random 50
lt random 50
fd 1
end
to fish-reproduce
if random-float 100 < fish-growth
[hatch 1 [rt random-float 360 fd 1]]
end
to move-boats
rt random 50
lt random 50
fd 1
set profit profit - 0.1
end
to catch-fish
let prey one-of fish-here
if prey != nobody
[ask prey [die]
set profit profit + profit-per-fish]
end
to death
if profit < 0 [die]
end
to reproduce-boats
if profit > 1
[
set profit (profit / 2)
hatch 1 [rt random-float 360 fd 1]]
end
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
There are 3 different primitives for creating new turtles in NetLogo:
create (observer), hatch (turtle) and sprout (patch).
Each one works only in a specific context.
This means you have to be aware of the context in which you are calling your procedure.
In your example, you are calling the create-turtles primitive inside of an ask turtles (specifically ask boats) context. Which means you are in a turtle context and not in an observer context and therefore create-turtles is not allowed to use.
To solve your problem, you just have to replace create-turtles by hatch (or hatch-<breed>, like hatch-fish if you want to spawn new agents in a specific breed).

Define home area-turtles?

I am very new to netlogo. I have searched every question here before I posted this.
I have the following code which sprouts a given number of horses:
ask n-of Number-horses patches with [grass? = "Yes"]
[sprout-horses 1 [set color 25 ]]
The person can change the number of horses using the slider but I would like each horse to have its own area/range/radius.
They can only move within this radius/area and they cannot meet each other.
From what I've read it's got something to do with the distance function?
You can find a similar problem here which has examples too :
Spacing agents in NetLogo based on territory size
There are several ways that you can assign a territory zone to each horse, but all methods that I know have two steps, first step is in order to make sure initial home area of horses are separated from each other , So we need to create horses only in patches which has a certain distance from another patch which has a horse on it,I did not follow your method that asked patches to sprout horses and instead I created them without asking patches.
I was not sure how you defined grass? Variable for each patch but I have assigned a number of patches with grass? = true and others false.
Second step is to set home-area property of each horse. If initially you moved them far away from each other they will have separate territories.
I have included a few examples here :
First to use in-radius for both steps:
Breed [Horses horse]
Horses-own [home-area]
patches-own [grass?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask patches
[
ifelse pxcor mod 5 = 3
[ set Grass? true ]
[ set Grass? false ]
]
create-horses Number-horses
[ Move-to one-of patches with [Grass? and not any? other horses in-radius (Zone-Radius + 1)]
set home-area patches in-radius Zone-Radius
set color 25
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area
[rt random 10 fd 1 ] ; move if next patch is in their zone
[rt random 180]
]
tick
end
In this example horses only move in the patches in their radius 2. But you can change that base on your model requirements.
In the second method you can use distance for the first step (finding empty patches with enough distance to current patch) and radius for second one (assigning home-area to each horse).
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches in-radius Zone-Radius
If you use higher distance for finding empty patches you will have completely seprated zones. Finally , you can use distance for both steps:
Move-to one-of patches with [Grass? and not any? other horses with [distance myself < (Zone-Radius + 1)]]
set home-area patches with [distance myself < Zone-Radius]
I just did it another way:
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
let i 0
while [i < Number-horses]
[
ask one-of patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
set i (i + 1)
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end
As you can see I used while and a condition to ask patches one by one, I might be mistaken but when I ask all the n-of Number-of-horses patches with [YourCondition][...] I get the wrong results and distance between horses is not effective, maybe they are created all at the same time and therefore upon creating a horse there was no horse nearby!? I am new to these concepts and might be wrong.
This is the code and view for the one which asks patches to create horses at once here :
Breed [Horses horse]
Horses-own [home-area]
patches-own [ concession? forest? parks?]
globals [Zone-Radius]
to setup
clear-all
reset-ticks
set Zone-Radius 2
ask n-of 500 patches [ set concession? "No" ]
ask n-of 500 patches[ set forest? "Yes" ]
ask n-of 500 patches[ set parks? "Yes"]
ask n-of number-horses patches with [(concession? = "No" or forest? = "YES" or parks? = "YES" ) and (not any? horses in-radius (Zone-Radius + 2) )]
[
sprout-horses 1 [
set home-area patches with [distance myself < Zone-Radius]
let w who
ask home-area [set pcolor red]
set color 25 ]
]
end
to go
ask horses [
ifelse member? patch-ahead 1 home-area [rt random 10 fd 1 ] [rt random 180]
]
tick
end