Im looking to modify this code so that the turtle will only move into the cell that is forward 1 if there isn't already a turtle in there.
ask turtles
[ let close-friend min-n-of 1 turtles with [my-group = [my-group] of myself] [distance myself]
ask close-friend
[ face myself
forward 1
]
]
I looked at adding adding the number of turtles from forward 1 to a variable and then a IF statement but i couldn't get it to work.
Any reply's will be greatly appreciated.
Check if there's any turtle's on the patch ahead:
if not any? other turtles-on patch-ahead 1 [forward 1]
You may want to check if the turtle can move forward first because patch-ahead may report nobody.
if can-move? 1 and not any? other turtles-on patch-ahead 1 [forward 1]
Related
I am trying to write some code in Netlogo to ask my turtles to go to the second destination after reaching the first destination. Here's my code and hope it will help explain my question.
SETUP:
First, the patch will generate turtles, and I will give each turtle a variable called my-patch:
ask n-of 40 patches with [ pxcor > -23 and pxcor < -5 and pycor < 21 and pycor > -6]
[
set quad-patch 1
sprout 1 [
set origin-turtle 1
set color yellow
set my-patches patch-here
set goal one-of patches with [pxcor = 0 and pycor = 12]
set speed 0.1 + random-float 0.9
set size 0.5
set shape "person"]
]
Secondly I will ask these turtles to move to the road which is outside the building:
set road patches with [
(pxcor > 45 and pycor <= 8 and pycor >= -18)
]
ask turtles [
move-to one-of road with [not any? other turtles]
]
GO:
This is the tricky part. Now I have set up a path to ask my turtles walk along that path and go into the building. So first I set a goal which is like a rest point in the building. All the turtles will head off to that rest point first and then they will start to find their origins (which I saved as a variable as mentioned before called my-patches).
to go
ask turtles with [should-stay = false] [
move
]
tick
end
to move
face best-way-to goal
avoid-walls
ifelse patch-here != goal
[fd 1]
[find-origin]
end
to avoid-walls
if [wall] of patch-ahead 1 = 1
[set heading heading - 180]
end
to find-origin
face my-patches
fd 1
end
to-report best-way-to [ destination ]
; of all the visible route patches, select the ones
; that would take me closer to my destination
let visible-routes patches with [ path = 1 ]
set routes-that-take-me-closer visible-routes with [
distance destination < [ distance destination - 1 ] of myself
]
ifelse any? routes-that-take-me-closer [
; from those route patches, choose the one that is the closest to me
report min-one-of routes-that-take-me-closer [ distance myself ]
] [
; if there are no nearby routes to my destination
report destination
]
end
So my problem is after turtles move along the walking path that I have set, they seem to stop at the rest point instead of moving on to find their origins (my-patch). I know this is because the code I set here:
ifelse patch-here != goal
[fd 1]
[find-origin]
But I am not sure how to ask these turtles go to find their origins next after they already reached at this rest point? Any help is really appreciated. Thanks!
ifelse any? turtles-on patch-ahead 1 with isgeneral = 1 or any? turtles-on patch-right-and-ahead 90 1 with isgeneral = 1
[
fd -2
set energy energy -2
]
[
fight
]
This is the idea of what I want to do but have not been able to , it gives me
WITH expected this input to be an agentset, but got a number instead
and highlights the 1 from 'patch-ahead 1'.
isgeneral is a turtles-own variable how would I go about doing this ?
In case it wasn't clear I want to check if there's a turtle ahead or to the right and if that turtle has 'isgeneral' variable set to 1
A couple things. It's parsing:
any? turtles-on patch-ahead 1 with isgeneral = 1
as
any? turtles-on patch-ahead (1 with isgeneral = 1)
You need to add parentheses to make it have the with act on the turtles. Next, the isgeneral = 1 is something you're having the turtles check (as you point out). In technical terms, with takes a reporter block that it then passes to the agents in the agentset for evaluation. Blocks are surrounded by []. So the corrected code is:
any? (turtles-on patch-ahead 1) with [ isgeneral = 1 ]
The part after the or needs analagous changes.
Hope that helps!
I want to move turtles with a "security distance" from other turtles, for example one patch.
this is my code:
ask turtles [
let q neighbors with [
(not any? turtles-here) and (not any? other turtles-on neighbors)
]
if any? q [
face one-of q
move-to patch-ahead 1
]
]
if any? turtles-on neighbors [ ????? ]
the start condition is each turtles not have turtles-on neighbors.
Also i wish turtles find a new patch to go if turtles-on q or any turtles-on q neighbors, but i have no idea..
i try also this:
to move
ask turtles [
set ahead patches at-points [[2 1] [2 0] [2 -1]]
set neighbors-e patch-at 1 0
ask turtles with [not any? turtles-on ahead] [
let d distance exit ;exit is patch "target"
if (distance [exit] of neighbors-e < d) [
fd 1
]
]
]
end
but it shows the error : A patch can't access a turtle variable without specifying which turtle. (refer to neighbors-e)
The idea is:
look 3 patches ahead at distance 2
if any? turtles-on this
control if the distance of exit where you will go (neighbors-e) is lesser than actual position (to verify if direction to exit is correctly)
fd 1
I want to ask how to move turtles to their neighbors. ( the houses are hex cells)
I tried to make it but give me an error.
;;;;;;;;;;;;;to make houses or the hex cells
to setup-cells
set-default-shape cells "hex"
ask patches
[ sprout-cells 1
[ set color grey
set size 1.2
;; shift even columns down
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ] ] ]
ask cells
[ ifelse pxcor mod 2 = 0
[ create-links-with cells-on patches at-points [[0 1] [1 0] [1 -1]] ]
[ create-links-with cells-on patches at-points [[0 1] [1 1] [1 0]] ] ]
ask links [ hide-link ]
end
;;;;; I add turtles to their houses
to setup-population
ask patches [
sprout 2
if pxcor mod 2 = 0
[ set ycor ycor - 0.5 ]]
end
my question is how to move this turtle to the link neighbors
I tried to do this
to move
ask turtles[
if ( random-float 1) < 0.03 [
move-to one-of link-neighbors
fd 1 ]]
end
but this always give me an error
move-to expected input to be agent but got nobody instead
Great question. It's too bad this isn't covered in Hex Cells Example (the code example you seem to be working from). Or maybe we should have a separate Hex Cells With Turtles Example.
First, you should make a separate breed for the turtles that live in the houses, so you can refer to them separately from the cell turtles. Suppose you call them people:
breed [people person]
then you should change sprout 2 to sprout-people 2.
As for implementing movement, the code you tried doesn't work because people don't have link-neighbors. It's the cells that have link-neighbors.
So instead of:
move-to one-of link-neighbors
you must write:
move-to [one-of link-neighbors] of one-of cells-here
If you want the people to face in the direction of movement, then it's:
let target [one-of link-neighbors] of one-of cells-here
face target
move-to target
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