How to get turtles to move to the target? - netlogo

I'm stumped with an issue in my model. I have a model that looks to simulate an office environment, where there are two breeds: employees and citizens. The employees stay in the office, denoted by grey coloured patches, and the citizensstay in the outside world denoted by black colour patches.
In the middle of the world patch 0 0 there is an office, where employees go to pick up money. There are 4 service-desks where both employees and citizens are to meet for a transfer of money to occur. Here is the full code:
globals [ office-space ]
breed [ offices office ]
breed [ service-desks service-desk ]
breed [ employees employee ]
breed [ citizens citizen ]
offices-own [ money ]
employees-own [ money-held ]
citizens-own [ money-received ]
to setup
clear-all
create-offices 1
ask offices [
set shape "building institution"
set size 4
set color yellow
set money num-of-money ]
create-employees num-of-employees
ask employees [
set shape "person"
set size 1.5
set color blue
setxy random-xcor random-ycor ]
create-citizens num-of-citizens
ask citizens [
set shape "person"
set size 1.5
set color white
setxy random-xcor random-ycor ]
;; create four service desks
ask patch 0 8 [
sprout 1 [
set breed service-desks
set shape "building institution"
set color pink
set size 3 ]
]
ask patch 0 -8 [
sprout 1 [
set breed service-desks
set shape "building institution"
set color pink
set size 3 ]
]
ask patch -8 0 [
sprout 1 [
set breed service-desks
set shape "building institution"
set color pink
set size 3 ]
]
ask patch 8 0 [
sprout 1 [
set breed service-desks
set shape "building institution"
set color pink
set size 3 ]
]
;; create office space
set office-space patches with [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
ask office-space [ set pcolor grey]
place-on-color-employees ;; sets all employees randomly within the grey square
place-on-color-citizens ;; sets citizens randomly outside of the grey box
reset-ticks
end
to place-on-color-employees
let _patches (patches with [pcolor = grey])
ask employees [
move-to one-of (_patches with [not any? turtles-here])
]
end
to place-on-color-citizens
let _patches (patches with [pcolor = black])
ask citizens [
move-to one-of (_patches with [not any? turtles-here])
]
end
to go
employee-movement
employee-take-money
citizen-movement
tick
end
to employee-movement
ask employees [
ifelse [pcolor] of patch-ahead 1 = black
[ rt random-float 360]
[ forward 1 ]
let target one-of citizens
if money-held > 0 [
set heading (towards target)
]
]
end
to citizen-movement
;; citizens walk only in the black patches, they do not go into the office area
ask citizens [
ifelse [pcolor] of patch-ahead 1 = grey
[ rt random-float 360]
[ forward 1 ]
;; if they have no money, then the citizens will walk to one of the service-desks
let target one-of service-desks
if money-received = 0 [
set heading (towards target )
]
]
end
to employee-take-money ;; asks employees to go to the main office and get money
ask employees [
if any? turtles-here with [ shape = "building institution" and color = yellow] [
set money-held money-held + 1
set color green ]
]
end
The issue I have is that within the employee-movement function, the employees will not move towards their targets. Whereas, the citizens will move to their targets. The code for both breeds is basically the same. The employees tend to congregate around the central patch, rather than moving to the targets.

Edited after receiving the full code:
The problem lies with how you define target as a local variable (let target one-of service-desks) each time you call the code. However, since there is more than one service-desk, nothing is forcing them to choose the same service-desk each time. This means that the employees, who are standing in the middle of the four service-desks, are basically taking a step in a random direction at each tick. The citizens on the other hand stand outside of the square on which the service-desks are located. Because of that, all of the service-desks are roughly in the same direction for them, so their general movement also goes in that direction.
I see two different options for working around this. The first one is to not use let target one-of service-desks but use let target min-one-of service-desks [distance myself]. This moves them to the closest service-desk rather than to a random service desk.
The other option would be to set target as a turtles-own variable, and not letting them choose a new one all the time.
Finally I streamlined your setup a little bit, reducing the amount of code you needed.
globals [
office-space
num-of-money
num-of-employees
num-of-citizens
]
breed [ offices office ]
breed [ service-desks service-desk ]
breed [ employees employee ]
breed [ citizens citizen ]
offices-own [ money ]
employees-own [ money-held ]
citizens-own [ money-received ]
to setup
clear-all
set num-of-money 100
set num-of-employees 5
set num-of-citizens 5
create-offices 1 [
set shape "building institution"
set size 4
set color yellow
set money num-of-money
]
create-employees num-of-employees [
set shape "person"
set size 1.5
set color blue
setxy random-xcor random-ycor
]
create-citizens num-of-citizens [
set shape "person"
set size 1.5
set color white
setxy random-xcor random-ycor
]
;; create four service desks
let service-desk-patches (patch-set patch 0 8 patch 8 0 patch 0 -8 patch -8 0)
ask service-desk-patches [
sprout-service-desks 1 [
set shape "building institution"
set color pink
set size 3
]
]
;; create office space
set office-space patches with [ pxcor <= 8 and pxcor >= -8 and pycor <= 8 and pycor >= -8 ]
ask office-space [ set pcolor grey]
place-on-color-employees ;; sets all employees randomly within the grey square
place-on-color-citizens ;; sets citizens randomly outside of the grey box
reset-ticks
end
to place-on-color-employees
let _patches (patches with [pcolor = grey])
ask employees [
move-to one-of (_patches with [not any? turtles-here])
]
end
to place-on-color-citizens
let _patches (patches with [pcolor = black])
ask citizens [
move-to one-of (_patches with [not any? turtles-here])
]
end
to go
employee-movement
employee-take-money
citizen-movement
tick
end
to employee-movement
ask employees [
ifelse [pcolor] of patch-ahead 1 = black
[ rt random-float 360]
[ forward 1 ]
let target min-one-of citizens [distance myself]
if money-held > 0 [
set heading (towards target)
]
]
end
to citizen-movement
;; citizens walk only in the black patches, they do not go into the office area
ask citizens [
ifelse [pcolor] of patch-ahead 1 = grey
[ rt random-float 360]
[ forward 1 ]
;; if they have no money, then the citizens will walk to one of the service-desks
let target min-one-of service-desks [distance myself]
if money-received = 0 [
set heading (towards target )
]
]
end
to employee-take-money ;; asks employees to go to the main office and get money
ask employees [
if any? offices-here [
set money-held money-held + 1
set color green ]
]
end

Related

Is there a way to create impassable barriers in NetLogo?

I am attempting to code a path-finding behavior wherein agents will locate an optimal patch in the environment and navigate their way around fences to reach said patch. I've created a patch variable 'f', which is set to 1 to indicate fences and 0 for any other patch.
I want to make these fences impassable (i.e. I want them to be patches the agents will not use for movement), but agents still seem to be able to travel on them to some extent and in some cases are even able to fully cross them.
Here is a picture of an agent crossing a barrier I don't want it to cross
Relevant decision-making code for the agents is as follows:
{let moveset patches in-radius 30 with [f = 0 and n > 0]
let target max-one-of moveset [n]
ifelse patch-here != target
[
set heading towards target
]
[]
let chance random-float 10
if chance >= 5 [let pick -145]
if chance < 5 [let pick 145]
ask patches in-radius 1
[if f = 1
[ask myself
[set heading towards min-one-of patches [distance myself] + 180 - random 10 + random 10 ]
]
]
fd 1}
For clarity, 'n' is simply a variable to denote the patch I want my agent to locate and venture to.
Is anyone aware of a simple way in NetLogo to exclude certain patches as viable zones for movement in the decision making process (i.e. hard barriers)?
If you haven't yet, have a look at the "Look Ahead" example in the Models Library- it's a simple demonstration of using patch color to control turtle movement. Some code based on that model is below. With this setup:
breed [ seekers seeker ]
breed [ goals goal ]
patches-own [ steps-from-goal ]
to setup
ca
ask patches [
set steps-from-goal 999
]
ask patches with [ pxcor mod 10 = 0 ] [
set pcolor red
]
ask patches with [ pycor mod 10 = 0 ] [
set pcolor black
]
ask one-of patches with [ pcolor = black ] [
sprout-seekers 1 [
set color blue
pd
]
]
ask one-of patches with [ pcolor = black ] [
sprout-goals 1 [
set color white
set shape "circle"
]
]
reset-ticks
end
You can have the seekers breed wander around the black squares until they share a patch with a goal turtle:
to random-wander
ask seekers [
if any? goals-here [
stop
]
rt random 61 - 30
ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [
fd 1
] [
rt one-of [ 90 -90 ]
]
]
tick
end
However, note that turtles can still 'jump' corners of patches using this method, because they are able to assess the patch-ahead 1 at any angle- so, a patch one space ahead of a turtle may be assessed across the corner of another patch. The turtle should never actually land on the forbidden patch, but you may notice that their path can cross those blocked patches.
Edit:
See simplified code that "traps" a turtle in a square cage:
to setup
ca
crt 1 [
setxy 5 5
set heading 180
repeat 4 [
repeat 10 [
ask patch-here [ set pcolor red ]
fd 1
]
rt 90
]
die
]
crt 1 [ pd ]
reset-ticks
end
to go
ask turtles [
rt random 61 - 30
ifelse can-move? 1 and [pcolor] of patch-ahead 1 = black [
fd 1
] [
rt one-of [ 90 -90 ]
]
]
tick
end
After 1100 ticks:
After 13300 ticks:

How to use to in radius command for turtles to seek food and how to regenerate food over time in Netlogo?

I am trying to do the following in NetLogo:
Get turtles (Elephants) to seek food
Ask plants to reproduce slowly over time on each side, one side before the other
Keep turtles (Elephants) to stay within world boundary
Keep turtles (Elephants) upright
What I would basically like to do is have our turtles (elephants) eat food on one side and seek food to cross to the other side. They will die if they get hit by a car. We want them to cross back and forth between sides so that they all die over time. We have tried to use the seek food primitive but is does not work for our simulation. We have also had the turtles stay within the world using the bounce primitive but with this current code they tend to move everywhere once again. As for the food regeneration, we have tried to use the hatch function but that also does not work.
Your help is very much appreciated.
Here is our code for the simulation:
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own [
speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of number-of-elephants (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "elephant"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of number-of-cars (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
]
]
end
to setup-plants
ask n-of number-of-plants (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end
to go
ask elephants [
bounce forward 1
]
ask cars [
set xcor random-xcor
set heading 90
forward 1
move-elephants
move-cars
eat-plants
kill-elephants
]
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants [
right random 360
forward 1
]
end
to move-cars
set speed 0.1
set speed-limit 0.1
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
There are several problems with this code so I am going to try and get rid of the more obvious logical issues and see if that allows you to focus on a specific question. Note that you should really be building your code more gradually - add one behaviour (eg move elephants, move cars, eat food or whatever) and make sure it works before adding the next behaviour.
Your go procedure doesn't have a tick for time passage
Your go procedure has each car randomly move all the elephants, so they are moving multiple times
Your car speeds and speed limits are being set to the same value each tick and never changed
You have nested ask cars [ ask elephants [ <do stuff> ] ] for eating plants and killing elephants, which will make these happen many times each tick
Fixing just those problems, gets this (note that I replaced slider inputs with numbers so you will have to change them back). This should fix the things you mentioned in your comments. You will have to ask a specific question about whatever else it is you are trying to fix.
breed [ elephants elephant ]
breed [ cars car ]
breed [ plants plant ]
turtles-own
[ speed
speed-limit
speed-min
]
to setup
clear-all
setup-patches
setup-elephants
setup-cars
setup-plants
reset-ticks
end
to go
ask elephants
[ bounce
forward 1
]
ask cars [ forward 1 ]
move-elephants
eat-plants
kill-elephants
tick
end
to bounce
if abs pxcor = max-pxcor
[ set heading ( - heading ) ]
if abs pycor = max-pycor
[ set heading ( 180 - heading ) ]
end
to move-elephants
ask elephants
[ right random 360
forward 1
]
end
to eat-plants
ask elephants
[ let prey one-of plants-here
if prey != nobody [ask prey [die]]
]
end
to kill-elephants
ask cars
[ let prey one-of elephants-here
if prey != nobody [ask prey [die]]
]
end
to setup-patches
ask patches [
ifelse (pycor > -2) and (pycor < 2)
[ set pcolor black ]
[ set pcolor green ]
]
end
to setup-elephants
ask n-of 20 (patches with [ pycor < -4 ])
[ sprout-elephants 1
[ set shape "wolf"
set color 4
set size 4
]
]
end
to setup-cars
ask n-of 20 (patches with [ pcolor = black ])
[ sprout-cars 1
[ set shape "car"
set color 105
set size 2
set heading 90
set speed 0.1
set speed-limit 0.1
]
]
end
to setup-plants
ask n-of 50 (patches with [ pcolor = green ])
[ sprout-plants 1
[ set shape "plant"
set color 62
set size 1
]
]
end

How can I create territories for several groups of agents in netlogo?

I'm very new to Netlogo, and this is my very first post in a forum.
I need to create animal agent groups which move in habitats or territories. The territories can overlap. More precisely I need male agents that move in different territories and several groups of female agents which move in other territories.
This is what I did so far. I created a territory just for one group of females and one group of males. Thats not exactly what I need.
globals [fragments]
breed [preys prey]
breed [femaletigers femaletiger]
breed [kids kid]
breed [maletigers maletiger]
turtles-own
[
energy
age
gender
territory
]
to setup
clear-all
setup-fragments
;setup-habitats
setup-turtles
reset-ticks
end
to setup-fragments
ask patches[set pcolor 67]
repeat 50
[
ask one-of patches
[
set pcolor brown
repeat 30
[
let a random 360
let b random 5
ask patch-at-heading-and-distance a b
[
ask neighbors [ set pcolor brown]
set pcolor brown
]
]
]
]
end
to setup-turtles
set-default-shape femaletigers "default" ; default shape (dreieck)
create-femaletigers 10
[
set color red
set size 1.5
set energy 100
set age random 20
set gender "female"
set territory patches-in-territory patch 10 6 15
move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas
]
set-default-shape maletigers "default" ; default shape (dreieck)
create-maletigers 10
[
set color blue
set size 1.5
set energy random 100
set age random 20
set gender "male"
set territory patches-in-territory patch 40 15 10
move-to one-of territory with [pcolor = 67 ] ; tigers start in territory but not on fragmented areas
]
set-default-shape preys "circle"
create-preys 100
[
move-to one-of patches with [pcolor = 67] ;preys don't start in fragmented areas
set color 114
set size 0.75
]
end
to-report patches-in-territory [Center rd]
let ptr []
ask Center [set ptr patches in-radius 20]
report ptr
end
I'm thankful for any help.
Maria
welcome to Stack Overflow. When posting on here, in general you will be better served by restricting your code to just the bare minimum needed to demonstrate your issue (see the MCVE guidelines here); for example you could remove your to-report, prey breeds, etc just to make it very obvious what you're trying to solve.
I'm not following your code exactly as I'm not sure of some of your overall goal- instead I give an alternative example that will hopefully illustrate one way to accomplish what you're after. I'll show an example of territories that can overlap for females in this example and of territories that cannot overlap, for males in this example. Using these variables and setup:
breed [ femaletigers femaletiger ]
breed [ maletigers maletiger ]
turtles-own [ territory ]
patches-own [ maleclaimed? ]
to setup
ca
ask patches [
set maleclaimed? false
]
create-maletigers 3 [
set shape "triangle"
set size 1.5
move-to one-of patches with [ maleclaimed? = false ]
pd
set territory patches in-radius 5 with [ maleclaimed? = false ]
ask territory [
set maleclaimed? true
]
]
create-femaletigers 3 [
set shape "square"
setxy random-xcor random-ycor
pd
set territory patches in-radius 3
hatch 1 + random 3 [
rt random 360
fd 1
]
]
reset-ticks
end
In the setup, the patches-own boolean indicates if a patch has been claimed by a male- set it to false to start so that a male can check, as it sets up its territory, whether a patch is already claimed or not. Tigers then set up their territories similar to how you did, except males will not select territory from another male. Females set up their territory then hatch a few more females that will share the territory of the "mother".
To move within their territory, just restrict turtles such that they can only move to one of their territory patches- here is one way to do that:
to go
ask turtles [
let target one-of territory in-radius 1.5
if target != nobody [
face target
fd 1
]
]
tick
end
If you want to confirm that the turtles stay within a territory, run something like:
to territory-check
ask turtles [
let col color + 2 + random 3
ask territory [
set pcolor col
]
]
end
Thanks again. What is when the tigers reproduce and I want their kids to create a new territoy and the territories should not overlap?
to move-male
ask maletigers [
let target1 one-of territory in-radius 2
if target1 != nobody [
face target1
fd 1]]
end
to reproduce-tiger
if age >= 48 and any? maletigers-on patch-here and pcolor = 67 [
hatch random 1 + random (5 - 1) [
set gender one-of [ "male" "female" ]
set breed kids
fd random-float 2 ]]
end
to search-territory-male
ask maletigers with [ territory = nobody ] [
if maleclaimed? = false [
set territory patches in-radius 18 with [ maleclaimed? = false ]
ask territory [ set maleclaimed? true ]]]
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

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