how to move the turtles together free neighbors without other turtles occupy them - netlogo

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

Related

Is there a possibility to have an ifelse statement with two conditions (ticks mod and probability)?

I'm fairly new to Netlogo but I want to build a model where an agent (a car driver) will leave his home at a certain hour with a certain probability. Let's say he leaves Monday morning at 1 am (if have linked the ticks to the time so one tick is one hour).
I tried to work with ifelse-statements combined with a second statement which has to be verified in order for the entire statement to become true. In the example below the car / agent should leave with a probability of 7.7% its house and drive to a patch called underway-patches. Since one week has 168 hours, I tried to link the hour via the mod ticks (hence, mod ticks = 1 is equal to 1 am on a Monday morning).
This alone works:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
This works fine. So I have always about 7 out of 100 turtles moving to the underway-patch.
But If I now add the second hour, so 2 am, the first function does not work anymore (there are no turtles moving at all at 1 am - only at 2 am). I expect about 7 out of 100 turtles to move at 1 am to the underway-patch and then I expect about 5 out of 100 turtles to move at 2 am to the underway-patch (and the other 7 of the first hour should go back to the home-patches).
This does not work anymore:
; Monday, 1 am
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
; Monday, 2 am
ifelse ticks mod 168 = 2 and random-float 100.0 < 5.1
[ ask turtles [ move-to one-of underway-patches] ]
[ ask turtles [move-to one-of home-patches] ]
I appreciate every help! Thanks in advance.
First, congratulations on an extremely clear question despite your newness to the site.
The problem is not that you have multiple ifelse statements, that is fine. The issue is that your ifelse statement is applying a single test to all turtles. Just look at the first example with one statement:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
Imagine that it is tick number 1. The computer runs the random number generator and gets 2. Great, the condition is true so the first block gets run. That will have ALL turtles move to the underway-patches. Similarly, if the random number generator returns 10, then the condition is false and ALL turtles move to home-patches.
You probably want something more like (you don't have to do the brackets on multiple lines, I did it so you can see the logical blocks of the structure):
ifelse ticks mod 168 = 1
[ ask turtles-on home-patches
[ if random-float 100.0 < 7.7
[ move-to one-of underway-patches
]
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Or if you want exactly the correct proportion of turtles to move:
ifelse ticks mod 168 = 1
[ let num-to-move 0.077 * count turtles-on home-patches
ask n-of num-to-move turtles-on home-patches
[ move-to one-of underway-patches
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Just a general observation, if you are going to code this for every possible starting time, you are going to have a lot of code that is identical except for the tick and the proportion. You state that you are new to NetLogo, so I don't want to jump too quickly to more advanced concepts, but come back when you're a little further along with having thought through your model and we can probably help you create a procedure that reduces the need to duplicate code.
UPDATE: One approach to reusing the code
This isn't quite right because I'm not really clear what sort of movement you want, but here is a complete model where the proportions are stored in a list and ticks is used to identify the correct item in the list. That proportion is then passed to a piece of code that moves turtles.
globals
[ home-patches
underway-patches
proportions
]
to setup
clear-all
set proportions [0 0.077 0.05 0.15]
set home-patches patches with [abs pxcor <= 3 and abs pycor <= 3]
ask home-patches [ set pcolor white ]
set underway-patches patches with [not member? self home-patches]
ask underway-patches [ set pcolor yellow ]
create-turtles 100
[ set color red
]
reset-ticks
end
to go
move-turtles item (1 + ticks mod 3) proportions
tick
end
to move-turtles [#prop]
print #prop
ask turtles
[ ifelse member? patch-here home-patches
[ if random-float 1 < #prop
[ move-to one-of underway-patches
]
]
[ move-to one-of home-patches
]
]
end

Netlogo: find next target discounting previous targets

I'm trying to get my agents to calculate the next patch target once they have reached their first target. A target is defined as the (highest patch influence value / distance to agent). The next target needs to be calculated using this same procedure, but also discounting any previous targets from the equation.
I have attempted to create a patch-set to include all targets, although I'm not sure if it works.
My issue is how to create a to-report function that calculates the next highest-influence-value. I've gotten completely stuck with this final part of my procedure.
My initial reporter function is:
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
But I'm not sure how to have the next reporter function use the same parameters while also discounting any previous targets
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
Any help would be greatly appreciated. I've provided the full code.
breed [walkers walker]
walkers-own [traveled? ;; parameter to keep track of if a walker has traveled to a target yet or not
target ]
patches-own [influence influence-set] ;highest-influence
to setup
clear-all
reset-ticks
define-patches
create-walkers num-walkers
ask walkers [
setxy 0 0
set heading 90
let streets neighbors with [pcolor = black]
ifelse any? streets in-cone 1 25
[fd 1]
[move-to one-of streets in-radius 1]
set traveled? false ;; so that walkers know they have not yet moved to a target
]
ask walkers [
define-target ]
end
to define-patches
ask n-of 100 patches [
set pcolor white
]
ask n-of 40 patches with [pcolor = white] [set influence random 5 set pcolor blue set plabel influence]
ask patches [set influence-set patches with [pcolor = green] ]
end
to define-target ;; this defines the initial destination of walkers
if traveled? = false [
set target highest-influence
ask target [set pcolor green]
face target ]
end
to new-target ;; this defines subsequent desinations after walkers have reached inital target
if traveled? = true [
set target next-highest-influence
ask target [set pcolor green + 2]
face target ]
end
;;;;;;;;;;;;run-time procedure;;;;;;;;;;
to go
ask walkers [
if distance target > 1
[ travel-walkers
leave-a-trail
set traveled? true ]
if distance target = 1
[ stop
new-target
travel-walkers
leave-a-trail
]
]
tick
end
to travel-walkers
ask walkers [
move-towards-target
]
end
to move-towards-target
ask walkers [
ifelse [pcolor] of patch-ahead 1 != white or any? other turtles-here
[ Move-Function ]
[ Avoid-Function ]
]
end
to Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Avoid-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to leave-a-trail
ask patch-here [set pcolor grey]
end
;;;;;; reporter calculations ;;;;;;
to-report highest-influence
let available-target patches with [influence > 0] ;and not any? patches with [pcolor = green]
report max-one-of available-target [influence / distance myself]
end
to-report next-highest-influence
; patches with [influence > 0] and not part of patch set
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != white and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
You can make use of the member? primitive to do that. Check out the simplified toy model below (I didn't use your code as I'm not sure how you'd want to implement and there are some interface pieces i would need to set up to run it. Check out the MCVE guidelines).
turtles-own [ visited-list ]
patches-own [ influence ]
to setup-member?
ca
crt 1 [ set visited-list [] ]
ask patches [ set influence 0 ]
ask n-of 5 patches [ set influence 5 + random 50 ]
reset-ticks
end
to go-member?
ask turtles [
let target highest-influence
ifelse target != nobody [
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ask target [ set pcolor red]
set visited-list lput target visited-list
]
] [ print "No targets remaining." ]
]
tick
end
to-report highest-influence
let to-visit patches with [
influence > 0 and
not member? self [visited-list] of myself ]
report max-one-of to-visit [ influence / ( distance myself )]
end
The reported returns the patch with the highest influence/distance value that is not a member of the turtle's visited-list. Initially, no patches are a member of the list, but as the turtle visits each target, it adds the target to the visited-list so that patch is no longer considered.

Netlogo: simulation of cellular differentiation pattern?

I am trying to simulate the differentiation pattern of a simple organism. This is how I'd like my breeds and variables to work:
Breeds: vegetatives and heterocysts. Vegetatives can divide, heterocysts can't. Vegetatives can become heterocysts. Ideally, once a heterocyst is formed, the closer a vegetative is to it, the less likely it is for it to become a heterocyst in turn.
Variables:
age: + 1 per tick, - 1 for newly-hatched turtles
bump: A means by which to displace all the turtles located 'ahead' of a newly-hatched turtle, so they don't overlap. I imagined the system a bit like a Newton's Cradle (http://s.hswstatic.com/gif/newtons-cradle-1.jpg)
pro: promoter. Accumulates partially randomly. Once it reaches a certain value ('concentration'), a vegetative would change breed to become a heterocyst. Value decreased by inh.
proL: label for pro, with rounded values.
inh: inhibitor. Ideally this value should form a 'gradient' (highest in turtles near heterocysts, lowest further away).
The obvious problem that I can see is that I get a lot of contiguous heterocysts. Which is sort of what I've been trying to avoid. But I can't see what went wrong...Please help?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-vegetatives 1
ask turtles [
setxy random-xcor random-ycor
set shape "circle"
set color 65]
end
to go
divide
add-age
move
differentiate
tick
end
turtles-own [age
bump
inh
pro
proL]
breed [vegetatives vegetative]
breed [heterocysts heterocyst]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask vegetatives [
if random 100 < 2 [
hatch 1[
set bump 1
set age age - 1
set inh 0
]
]]
end
;;Trying to get only one turtle per patch, making the others move
to move
ask turtles[
while [bump = 1] [
ifelse not any? turtles-on patch-right-and-ahead 180 1[
rt 180
fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here
[set bump 1]
]
]
[fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here[
set bump 1]
]
]
]]
end
to differentiate
ask turtles[
set pro (pro - inh + (random 2))
set proL round pro
ifelse show-proL?
[ set label proL ]
[ set label "" ]
create-links-with other turtles-on patch-ahead 1
create-links-with other turtles-on patch-right-and-ahead 180 1
if breed = vegetatives [
if any? link-neighbors[
ifelse any? link-neighbors with [breed = heterocysts]
[]
[set inh mean [inh] of link-neighbors]
]
if any? vegetatives with [pro > 50]
[ask vegetatives with [pro > 50]
[set breed heterocysts
set color brown
set shape "circle"
if any? link-neighbors[
ask link-neighbors with [breed != heterocysts]
[set inh 2]]
]]
]]
clear-links
end

How to move turtles between neighbors on a hex grid?

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

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