Netlogo-Why does storing data from turtles into the global variable matrix crash - netlogo

My program has an array for each turtle, and when I want to store the data in this array to the global variable matrix, the program does not work properly
I want to be able to implement an array for each turtle that stores data into a global variable matrix................................................................................................
The problem arises in:
``
let n 0 while [n < 40][ let tmp_num1 (array:item get_k n) matrix:set col_k CatNum n tmp_num
Here is my full code
`extensions [
array
matrix]
globals[
i
v
col_k
]
breed[cats cat]
breed[rats rat]
cats-own [
CatNum
data_from_rat
a_array
GetRatNum
get_k
k ;;
]
rats-own[
kg
num
]
to setup
clear-all
reset-ticks
set i 0
set v 0
creat-animals
creat-world
set col_k matrix:make-constant 40 40 0
ask cats[
set k 0
set a_array array:from-list n-values 20 [0]
set get_k array:from-list n-values 20 [0]
]
end
to go
ask n-of 20 rats[ ;;
if i < 20 [
set num i
set i i + 1
]
]
ask rats[
update
creat-ratnum
fd 3
rt random 90
]
ask n-of 40 cats[
if v < 40 [
set CatNum v
set v v + 1
]
]
ask cats[
let agsets other turtles-here with [breed = rats]
if count agsets >= 1
[
interaction(one-of agsets)
]
fd 3
lt random 90
;;print_array
]
tick
end
to creat-animals
create-cats 40[
set shape "cat"
set size 2
set color orange
setxy random-xcor random-ycor
]
create-rats 20[
set shape "mouse side"
set size 2
set color pink
setxy random-xcor random-ycor
]
end
to creat-world
ask patches[
set pcolor white
]
end
to creat-ratnum
loop[
if i >= 20 [stop]
set num i
set i i + 1
]
end
to update
set kg random-float 1
end
to interaction[provider]
set data_from_rat([kg] of provider)
set GetRatNum ([num] of provider)
let tmp_num (array:item get_k GetRatNum) ;;
ifelse tmp_num >= 1 [
set tmp_num tmp_num + 1
array:set get_k GetRatNum tmp_num ;;
;print (word "this is:" tmp_num)
][
set tmp_num tmp_num + 1
array:set get_k GetRatNum tmp_num
]
array:set a_array GetRatNum data_from_rat ;;
;;
let n 0
while [n < 40][
let tmp_num1 (array:item get_k n)
matrix:set col_k CatNum n tmp_num
]
end`
``

Related

Calculate index of dissimilarity in NetLogo

I'm want to calculate the index of dissimilarity in NetLogo. I have a world divided into different regions and want to examine how evenly species are distributed around the world.
Consider this example: A world is divided into 16 different regions. The world is populated with two types of ants, red and blue. It looks like this:
The world in the picture is produced with the following code:
globals[indexdissimilarity] ; where I want the index of dissimilarity to be stored.
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.50) turtles [set color blue]
dissimilarity
end
; Here is where I want to calculate the index of dissimilarity.
to dissimilarity
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
; set indexdissimilarity
end
My main issue is how to iterate parts of the calculations over each neighborhood.
Thanks!
I think I managed to solve it. Please, let me know if it looks correct. Here is the full updated code.
globals[indexdissimilarity
dis
]
patches-own [reg]
to setup
ca
;Setting world.
resize-world 0 19 0 19
set-patch-size 15
;Creating regions.
let x 5
let y 5
let col 45
while [y <= max-pycor + 1 ][
while [x <= max-pxcor + 1][
ask patches with [pxcor < x and pxcor >= x - 5 and pycor < y and pycor >= y - 5][
set pcolor col
]
set x x + 5
set col col + 3
]
set x 5
set y y + 5
]
ask patches [set reg [pcolor] of self]
ask n-of (count patches * 0.85) patches[sprout 1[
set shape "bug"
set color red]]
ask n-of (count turtles * 0.7) turtles [set color blue]
update
end
to update
;Dissimilarity index.
let tot_red (count turtles with [color = red])
let tot_blue (count turtles with [color = blue])
let neighb1 [reg] of turtles
let neighb remove-duplicates neighb1
set dis []
foreach neighb [i -> set dis lput abs((count turtles with [reg = i and color = red] / tot_red) - (count turtles with [reg = i and color = blue] / tot_blue)) dis]
set indexdissimilarity sum(dis) / 2
print(indexdissimilarity)
end

expected input to be a number but got the TRUE/FALSE true instead

I want to write an evacuation with leader code in net-logo but i have this error (AND expected input to be a TRUE/FALSE but got the number 0 instead.)the error came from function (follow_leader). my project is that i evacuate an environment and evacuate it with the nearest leader from the crowd . the code is not perfect that because iam knew to netlogo .
this is my code
globals[ goal-x goal-y n number leader
percent_of_leader wall
window
door largecircle]
patches-own [path?
obstacle
goal]
turtles-own [direction
fast?
fear?
leader?
is-leader?
follower
other-nearby]
to setup
clear-all
set-default-shape turtles "person"
;create-turtles 50 [ set color yellow ]
;ask turtles [fd random 13 ]
drwa-walls
ask n-of population patches with [ pcolor = black]
[sprout 1
[ set color white
set size 0.9
set shape "person"
set leader? false
;set follower self
if xcor < 9 and ycor > 9 [set heading 0]
if xcor >= 9 and ycor >= -10[set heading -90]
if xcor > -12 and ycor < -10 [set heading 90]
if xcor <= -8 and ycor > -10 and ycor < 0 [set heading 180]
]]
choose-leaders
end
to choose-leaders
ask turtle 6
[
set leader? true
set color yellow
set size 1
set shape "default"
set leader self
set xcor 3
set ycor 10
set heading 0
]
end
to go
move-for
ask turtle 6[move-to-exit1
fd 1 ]
ask turtles with [shape = "person"][
ifelse (xcor < 9 and ycor > 9 ) [follow_leader] [move-to-exit1]
if xcor >= 9 and ycor >= -10 [move-to-exit2]
if xcor > -12 and ycor < -10 [move-to-exit3]
if xcor < -12 and ycor < -10 [move-to-exit3]
if xcor <= -8 and ycor > -10 and ycor < 0 [move-to-exit4]
;avoid obstcao
; avoid_obstacles
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ] ;; Otherwise, it is safe to move forward.
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;blue or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;blue or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;blue
;[lt random-float 360 ]
;[fd 1]
; while [patch-ahead 1 = nobody]
;[
;lt random-float 360
;]
;ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
;[ fd 1 ]
; ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;white or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;white or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;white
; [lt random-float 360 ]
; [fd 1]
;ifelse not is-patch? patch-ahead 3 or [ pcolor ] of patch-ahead 3 =
;green or not is-patch? patch-ahead 2 or [ pcolor ] of patch-ahead 2 =
;green or not is-patch? patch-ahead 1 or [ pcolor ] of patch-ahead 1 =
;green
;[lt random-float 360]
; [fd 1]
]
reset-ticks
tick
end
to rt-random
while [patch-ahead 3 = nobody]
[
rt random 360
]
end
to avoide
ask turtles [
if [pcolor] of patch-ahead 1 = green
[lt random 360 fd 1]
]
end
to avoid_obstacles ;; all obstacles set as green patches
let i 1
while [[pcolor] of patch-ahead i != blue ]
[set i (i + 1) ]
if ([pcolor] of patch-ahead i = blue)
[
ifelse [pcolor] of patch-at-heading-and-distance (heading - 20) i + 1 = blue
[
ifelse [pcolor] of patch-at-heading-and-distance (heading + 20) i + 1 = blue
[
ifelse random 1 = 0
[ rt 360 ]
[ lt 360]
]
[ rt 360 ]
]
[lt 360]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;
to bounce
if [pcolor] of patch-at dx 8 = blue [
set heading (90)
]
if [pcolor] of patch-at 0 dy = blue [
set heading (-90)
]
end
;;;;;;;;;;;;;;;;;;;;;;;
to move-for
ask turtles with [shape = "person"][fd 1]
end
to drwa-walls
draw-exit1
draw-exit2
draw-exit3
draw-exit4
;rows x
ask patches with [pxcor <= 30 and pxcor >= 0 and pycor = -10]
[set pcolor blue]
ask patches with [pxcor >= -30 and pxcor <= 0 and pycor = 9]
[set pcolor blue]
;coilmn
;ask patches with [pycor <= 9 and pycor >= -10 and pxcor = 0]
;[set pcolor green]
;ask patches with [pycor <= 4 and pycor >= 0 and pxcor = 0]
;[set pcolor green]
;circle shape
ask patch -14 -9 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor green
]
ask patch 16 16 [
set largecircle patches in-radius (2)
]
;set color of largecircle patches green
ask largecircle [
set pcolor white
]
end
to draw-exit1
;exits at top of concourse area, where turtles will leave
set goal-x -1
set goal-y -30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit2
set goal-x -1
set goal-y 30
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit3
set goal-x -30
set goal-y -6
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
to draw-exit4
set goal-x 30
set goal-y 4
ask patch goal-x goal-y [
sprout 1 [ set pcolor red
set shape "square"
]
]
end
;;;;;;;;;;;;;;;;;;;;;;exit goals;;
to move-to-exit1
facexy -1 30
end
to move-to-exit2
facexy 30 4
end
to move-to-exit3
facexy -1 -30
end
to move-to-exit4
facexy -30 -6
end
;;;;;;;;;;;;;;;;;;;;;;;;;;
;to follow-leader
;if not leader? ;; we only want to ask non-leaders
;[let nearby-leaders turtles with [leader? and distance myself < 3] ;; find nearby leaders
;if any? nearby-leaders ;; to avoid 'nobody'-error, check if there are any first
; [ set heading (towards min-one-of nearby-leaders [distance myself]) ]] ;; then face the one closest to myself
; end
;;;;;;;;;;;;;;;;;;;;;;;;;;
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10] ;; nearby leaders
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
In the future, please only provide relevant code. For NetLogo, that is usually the procedure that throws the error (or doesn't work correctly etc) and whichever procedure calls it.
In your case, this is the procedure you mentioned:
to follow_leader
let nearby-leaders turtles with [is-leader? and distance myself < 10]
if any? nearby-leaders[
face min-one-of nearby-leaders [distance myself]
fd 0.5]
end
So the and that is throwing the error must be [is-leader? and distance myself < 10] (also, please state which line if you know it). My guess would be that you haven't initialised the variable is-leader? so it is 0 (the default value) instead of either true or false.
If this is the problem, wherever you creates turtles, initialise with set is-leader? false

Check turtle's neighborhood over some patches

I am struggling with coding identifying an expanding neighborhood based on an attribute of a patch. I need to check if there are a wall in my turtle's vision, if it is the case, my turtle should not be able to see through this wall.
Currently my recursive code works only with a vision's distance of 1 (that corresponds to neighbors), but over of 2 I get this error : A patch can't access a turtle or link variable without specifying which agent.
I don't know how it is possible to do this with an agent, do someone have an idea to do this?
breed[robots robot]
robots-own[step]
globals [ max-dist]
patches-own [ dist ]
to setup
ca
init-environement
create-robots 1 [init-robots]
end
to init-robots
set shape "person"
set size 4
move-to one-of patches with [no-wall? and (not any? turtles-here)]
set step 0
end
to init-environement
ask patches with [ (abs pxcor = max-pxcor) or (abs pycor = max-pycor) ]
[ set pcolor brown ]
ask patches with [ (abs pxcor = 20 and abs pycor > 15)
or (abs pycor = 10 and pxcor > 25)
or (pycor = 0 and pxcor < 1)][ set pcolor brown ]
ask n-of nbObstacles patches [ask patches in-radius random-float 2 [ set
pcolor brown ]]
end
to move-robot
let k 0
let v (neighbors with [no-wall? and (not any? turtles-here)])
if (any? v)[ move-to min-one-of v [dist] paint-agents k neighbors]
set step (step + 1)
output-show step
end
to paint-agents [k case]
let w ([neighbors] of case with [no-wall? and (not any? turtles-here)])
if (k < radius) [
set k k + 1
foreach w [
[x] ->
ask neighbors with [pcolor != brown][ set pcolor [color] of myself - 2
paint-agents k x]
]
]
end
to go
propagate
if any? patches with [pcolor = black] [ clear-output ask robots [move-robot] ]
end
to propagate
ask patches with [ no-wall? ][ set dist -1]
let p (patch-set patches with [pcolor = black])
let d 0
while [ any? p ]
[ ask p [ set dist d ]
set d d + 1
set p (patch-set [ neighbors with [no-wall? and ((dist = -1) or (dist > d))]] of p)
]
set max-dist max [ dist ] of patches
if (max-dist < 0) [ set max-dist 0 ]
ifelse (show-labels?)
[ ask patches with [no-wall?]
[ set plabel-color white
set plabel dist]
]
[]
end
to-report no-wall?
report pcolor != brown
end
There, my function which contains this problem is "paint-agents"
One way would be to get rid of patches in the sight that intersect a wall. You can imagine if you draw a line between each patch and if the wall lies on the drawn line, then that patch should be removed from things in vision.
This link may be useful on an implementation: Not see through the walls

"You can't use TICK in a turtle context, because TICK is observer-only." - Don't know how to fix that error

I'm trying to create an own ecosystem and have already written about 100 lines of code. But I'm currently getting an annoying error message [Title] I cannot get rid of unless I delete the line with the «Tick» command.
That's how the Interface looks like at the moment
That's how the Interface looked like after the latest successful simulation
That's the defective Code:
turtles-own [saturation age gender]
breed [sheep a-sheep]
sheep-own [love-cooldown-sheep-f love-cooldown-sheep-m age%-sheep]
globals [tick-stopper]
patches-own [Next-to]
to setup
clear-all
setup-patches
setup-sheep
set tick-stopper 0
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-sheep
create-sheep number_of_sheep [set saturation max-saturation-sheep set gender random 2 set shape "sheep" set age 0]
if setup-sheep-random-age? [
ask turtles with [shape = "sheep"] [set age random life-exp.-sheep]
]
ask turtles with [shape = "sheep"] [
setxy random-xcor random-ycor
if gender = 0 [
set color 118
]
if gender = 1 [
set color 98
]
]
end
to go
if tick-stopper >= stop-at-tick and stop-at-tick? [
set tick-stopper 0
stop
]
move-sheep
sheep-eat-grass
make-love-sheep
check-death-sheep
regrow-grass
set tick-stopper tick-stopper + 1
tick
end
to move-sheep
ask turtles with [shape = "sheep"] [
right random 60
left random 60
forward 1
set saturation saturation - 1
set age age + (((random life-exp.-sheep) + 1) / ((life-exp.-sheep + 1)/ 2))
if gender = 0 and love-cooldown-sheep-f > 0 [
set love-cooldown-sheep-f love-cooldown-sheep-f - 1
set saturation (saturation - (saturation-loss-til-birth-sheep / contribution-period-sheep))
]
]
if gender = 1 [
if love-cooldown-sheep-m > 0 [
set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
to sheep-eat-grass
ask turtles with [shape = "sheep"] [
if pcolor = green and saturation < max-saturation-sheep [
set pcolor brown
set saturation saturation + saturation-from-grass
]
ifelse show-saturation?
[ set label saturation ]
[ set label "" ]
]
end
to make-love-sheep
ask turtles with [shape = "sheep"] [
if gender = 0 and love-cooldown-sheep-f = 1 [hatch 1 [set saturation birth-saturation-sheep set gender random 2 set age 0
if gender = 0 [set color 118]
if gender = 1 [set color 98]
if gender = 1 and love-cooldown-sheep-m = 0 and saturation > saturation-for-love-sheep and 1 = count turtles with [shape = "sheep" and gender = 0 and love-cooldown-sheep-f = 0 and saturation > saturation-for-birth-sheep] in-radius 1 [
set saturation saturation - saturation-loss-at-love-sheep
set love-cooldown-sheep-m cooldown-for-love-sheep
ask turtles with [shape = "sheep"] in-radius 1 [
if gender = 0 and saturation > saturation-for-birth-sheep and love-cooldown-sheep-f = 0 [set love-cooldown-sheep-f contribution-period-sheep]
]
]
]
]
]
end
to check-death-sheep
ask turtles with [shape = "sheep"] [
set age%-sheep (100 * (age / life-exp.-sheep))
if saturation <= 0 [die]
if age%-sheep < 22.5039287 [
if (10 ^(0.00984 *(age%-sheep ^ 2) - 0.25918 * age%-sheep + 2.922))/ life-exp.-sheep > random 10000 [die]
]
if age%-sheep > 156.24733 [
if 10000 / (life-exp.-sheep / 100) > random 10000 [die]
]
if age%-sheep > 22.5039287 and age%-sheep < 156.24733 [
if (10 ^(1.899801588 -(((16 / 75 * (age%-sheep ^ 3)) - (50 * (age%-sheep ^ 2))) / 131250)))/ life-exp.-sheep > (random 10000) [die]
]
]
end
to regrow-grass
ask patches with [pcolor = brown] [
set next-to count neighbors4 with [pcolor = green]
if (random 1000000 / 10000) <= (grass-growth% * count neighbors4 with [pcolor = green]) [set pcolor green]
]
end
Thanks for your help <3
This is my first project with NetLogo 6.0.4 and it isn't planned to start any other projects after this one.
You have a bracketing problem in your move-sheep procedure. You have a structure like this:
to move-sheep
ask turtles with [shape = "sheep"]
[ right random 60
...
if gender = 0 and love-cooldown-sheep-f > 0
[
]
]
if gender = 1
[ if love-cooldown-sheep-m > 0
[ set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
So you accidentally closed the ask turtles before dealing with the if gender = 1. As soon as you have if gender = 1 then it switches to turtle context.

Netlogo Sprouting turtles at regular intervals

I want to place turtles on each of the black patches(below Figure) at a specified step size:
Therefore if step size less more turtles will be created/sprouted and more step size will result in less turtles.
Code I use right now:
ask patches with [pcolor = black][sprout-dead-turtles wall-agents [set color red]]
This gives the following result:
Previous question asked on same lines:Netlogo Sprouting turtles spaced at less than one patch
Here:
to fill-wall [ d ]
set d precision d 1 ; make sure d is a multiple of 0.1
let n precision (d / 0.1) 0 ; interval at which to hatch
ask one-of possible-next-patches [
sprout 1 [
hatch 1
let i 0
let next-patch my-next-patch
while [ next-patch != nobody ] [
face next-patch
while [ patch-ahead 0.55 != nobody and [ pcolor ] of patch-ahead 0.55 = black ] [
fd 0.1
setxy precision xcor 1 precision ycor 1 ; avoid floating point imprecisions
set i i + 1
if i mod n = 0 [ hatch 1 ]
]
set next-patch my-next-patch
]
die
]
]
end
to-report possible-next-patches
let empty-black-patches patches with [ pcolor = black and not any? turtles-here ]
report empty-black-patches with [
count neighbors4 with [ member? self empty-black-patches ] = 1
]
end
to-report my-next-patch
report one-of possible-next-patches with [ member? self [ neighbors4 ] of myself ]
end
Here is how you would use it:
to setup
ca
; draw the background:
ask patches with [ abs pxcor != max-pxcor and abs pycor != max-pycor ] [ set pcolor grey ]
ask patches with [ pycor = max-pycor and abs pxcor <= 1 ] [ set pcolor white ]
set-default-shape turtles "circle 2"
fill-wall 0.3
end
Constraints:
d has to be a multiple of 0.1
world wrapping needs to be turned off