How to force turtles to move only in a half of the world? - netlogo

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end

Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

Related

NetLogo Trouble detecting turtles in radius

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

How to make turtles move in one color in Netlogo

I start developping with Netlogo and I face the problem that I want to make all Turtles moving in one way which has the Black Color. How can I do that ? I tried with patch-ahead but I didn't success.
Anyone have a solution ? I will be grateful.
to setup
clear-all
import-drawing "patch.png"
create-turtles 10 [set xcor -10 set ycor -13]
ask turtles [set color white]
ask turtles [set shape "bug"]
reset-ticks
end
to bouge
ask turtles[
fd 1
]
end
to go
bouge
ask turtles [if [pcolor] of patch-ahead 6 != black [set heading heading - 100] ]
end
enter image description here
You should use something like these:
set black-patches patches with [pcolor = black]
ask turtles [
face one-of black-patches
]
Bests, Ervin

How to stop turtles when all patches have been colored

When the turtles have covered the world in patches, I would like the turtles to stop on the last one so that I can record the amount of ticks it took.
Here is my code so far:
globals [marked-patches angle nextangle]
to setup ca ask patches [ set pcolor black ] crt turtle_amount
[set color red
set size 1
setxy (random 20) (random 20)] reset-ticks
end
to go ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [set pcolor yellow] ]
tick end
In go, specifically in the turtle command, you can add:
to go
ask turtles [
fd 1
rt random trt_ang
lt random trt_ang
if pcolor = black [
set pcolor yellow
if count patches with [pcolor = black] = 0 [
stop
]
]
]
tick
end

How to ask turtles to avoid a patch with specific color at patch-ahead 1 but turtles move forward by bigger number (not fd 1) in one step in NetLogo

I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.
to setup
clear-all
ask patches [set pcolor green ]
ask patches with [pycor = 3] [set pcolor red]
create-turtles 40
[
set color blue
set xcor random-pxcor
set ycor random-pycor
set heading random 360
set size 1
set speed 1 + random-float 2.0
]
end
to go
ask turtles [
fd speed
avoid-walls
]
end
to avoid-walls
if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end
Try using in-cone instead of patch-ahead
to avoid-walls
let front-patches patches in-cone 2 75
if pcolor of one-of front-patches = red [set heading heading - 180]
end

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