NETLOGO: Count patches outside of certain radius - netlogo

I want to count patches of a specific color starting at a patch radius end and outward. There is the in-radius command, but it considers the space between a patch center and its radius, but I want this space to be excluded and start counting where the radius ends. Any help is appreciated.
Javier

How about a reporter that uses member? to return only a patch-set consisting only of patches that are not in-radius?
to setup
ca
ask n-of 10 patches [ set pcolor white ]
crt 1 [
setxy random-pxcor random-pycor
set color red
set size 2
]
reset-ticks
end
to go
ask turtles [
ask patches in-radius 5 with [ pcolor = black ] [ set pcolor black + 2 ]
print count ( patches-outside-radius 8 ) with [ pcolor = white ]
]
end
to-report patches-outside-radius [ radius ]
let inrad patches in-radius radius
report patches with [ not member? self inrad ]
end

Related

How to get turtles to move to the target?

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

Netlogo - Ordered Movement

I have the following error in Netlogo and I'm unsure why. I am trying to make the turtles move around in a ordered fashion but I keep getting an error when I change the d angle: "FACE expected input to be an agent but got NOBODY instead".
Any help would be appreciated.
globals [Angles]
to setup
clear-all
create-turtles amount [ setxy random-xcor random-ycor ]
ask turtles [
set Angles (random 360)
]
reset-ticks
end
to monitor
show count patches
show ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
face min-one-of patches with [ pcolor = black ] [ distance myself ]
;; This line of code tells the turtle to head towards the nearest patch containing the colour of black.
set Angle d Angle * 1 - Angle
rightt Angle
forwardd 1
ifelse show-travel-line? [pen-down][pen-up]
set color red
if pcolor = black [
set pcolor yellow
]
]
tick
end
You can unveil the problem but running this test:
to test
ca
crt 1
let x -10E307 * 10
show x
ask turtle 0 [rt x]
inspect turtle 0
end
You will see that the heading is NaN because you gave it a turn of -Infinity. Now if you move the turtle, the xcor and ycor will become Nan.
To avoid this problem, you need to limit the values taken by angle. For example,
globals [turn angle]
to setup
clear-all
set turn random-float 1
create-turtles 10 [
setxy random-xcor random-ycor
set color red
pen-down
]
reset-ticks
end
to go
if (all? patches [pcolor = yellow]) [stop]
ask turtles [
part1
part2
if pcolor = black [
set pcolor yellow
]
]
tick
end
to part1
let _patches (patches with [ pcolor = black ])
face min-one-of _patches [ distance myself ]
end
to part2
set turn (4 * turn * (1 - turn))
set angle turn * 360
rt angle
fd 1
end

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

Initial proportion of three different patches with only two sliders

i am trying to create a predation model in netlogo with foxes and rabbits. foxes eat rabbits and waste (left from people) und rabbits eat grass in a special pleasure ground.
the initial proportion of patches fixed in the setup should be: 65% green patches (grass), 35 % brown patches (eaten grass), 10 % margenta patches (waste from people). they can be distributed randomly.
at the moment it looks like that :
globals [grass waste]
breed [rabbits rabbit]
breed [foxes fox]
turtles-own [energy]
patches-own [countdown]
to setup
clear-all
ask patches [ set pcolor green ]
if grass? [
ask patches [
set pcolor one-of [green brown]
if-else pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ]
]
]
ask patches [ set pcolor magenta ]
if waste? [
ask patches [
set pcolor one-of [ green brown magenta ]
if-else pcolor = [ green brown ]
[ set countdown waste-regeneration-time ]
[ set countdown random waste-regeneration-time ]
]
]
set-default-shape rabbits "rabbit"
create-rabbits initial-number-rabbits
[
set color white
set size 2.2
set label-color gray + 1
set energy random ( 2 * rabbit-gain-from-food)
setxy random-xcor random-ycor
]
set-default-shape foxes "fox"
create-foxes initial-number-foxes
[
set color red
set label-color gray + 1
set size 2.2
set energy random ( fox-gain-from-food )
setxy random-xcor random-ycor
]
display-labels
set grass count patches with [pcolor = green]
set waste count patches with [pcolor = magenta]
reset-ticks
end

Selecting the first 20 turtles out of 1000 turtles

turtles-own [wages]
to setup
clear-all
setup-patches
setup-turtles
reset-ticks
end
to go
move-turtles
get-employed
tick
end
to move-turtles
ask turtles [
right random 360
forward 1
]
end
to get-employed
ask turtles [
if pcolor = blue [
set color green
set wages wages + 10
]
ifelse show-wages?
[ set label wages ]
[ set label " " ]
]
end
to setup-patches
ask patches [set pcolor pink ]
patch 0 0 [ set pcolor blue ]
end
to setup-turtles
create-turtles 80
ask turtles [ setxy random-xcor random-ycor ]
ask turtles [ set color red]
end
I want to add the code to select the first 20 percent of the 80 turtles who comes in contact with the patch having color blue.
In your question is not clear how you will use the first 20% of turtles who get the blue patch so i assume you just want to store them in order to use this information later.
I would add a turtles-own called is-first-20-percent? set to false for every turtle.
Then, at the end of the go procedure, before the tick i would execute the check-20 procedure as following:
to check-20
if count turtles with [color = green] = (count turtles * 20 / 100) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end
In every moment you can retrieve the first 20% of turtles who reached the blu zone with the command:
ask turtles with [is-first-20-percent?] [ ... do something ... ]
This code is working because 20% of 80 is an integer number (16) but if you plan to modify the starting number of turtles i suggest to modify the check-20 procedure as following:
to check-20
if (count turtles with [color = green] >= (count turtles * 20 / 100)
and count turtles with [is-first-20-percent?] = 0) [
ask turtles with [color = green] [set is-first-20-percent? true]
]
end