Netlogo ERROR - DISTANCE expected input to be an agent but got the number 0 instead - netlogo

I am trying to create home ranges for 3 types of hosts for a model that aims to simulate the movement of ticks across a landscape based on the presence of three host-types.
Using some code that was described in a previous post I created the following code. When I run the model I eventually get the following error message:
DISTANCE expected input to be an agent but got the number 0 instead.
Any help with the issue is greatly appreciated.
to-go
if ((week-id = 13) or (week-id = 25) or (week-id = 33))
[ask patches
[sprout-mice 2
[set color gray
set shape "mouse side"
set size 0.5
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-deer count deer
ask n-of (total-deer / 2) patches
[sprout-deer 1
[set color brown
set shape "deer"
set size 1
setxy random-xcor random-ycor]
]
]
if week-id = 17[
let total-raccoons count raccoons
ask n-of (total-raccoons) patches
[sprout-raccoons 1
[set color black
set shape "wolf 2"
set size 0.5
setxy random-xcor random-ycor]
]
]
mice-mortality
;print (count mice)
if (week-id = 40)
[deer-mortality]
;print(count deer)
if (week-id = 45)
[raccoon-mortality]
print (count raccoons)
ask deer
[deer-move]
ask raccoons
[raccoons-move]
ask mice
[mice-move]
the submodels:
to deer-move
right random 50
left random 50
forward 1
end
to raccoons-move
ifelse distance home-patch > 10
[face home-patch]
[right random 90
left random 90]
forward 1
end
to mice-move
ifelse distance home-patch > 2
[face home-patch]
[right random 90
left random 90]
forward 1
end

Related

How to force turtles to move only in a half of the world?

I created a world divided in two parts with the command ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green] .
In one of this there're 100 turtles, who 5 are infected and the same in the other part.
My problem is to force the turtles to move only in their part of the world. So I want that turtles with pcolor = blue move randomly in the second and third quadrant (pxcor <0) and turtles with pcolor = green in the first and fourth quadrant (pxcor> 0). how do I do?
This is the code:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
Your movement procedure looks like:
to move
right random-float 360
forward 1
end
If you want them to just stay where they are if moving would take them into the wrong half, then you can use patch-ahead to test the patch they'd be moving to. I think what you want is that they don't go to a different coloured patch. One way is:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1 returns the colour of the patch that is one distance unit ahead, so where the turtle is trying to move to. pcolor is the colour of the patch that the turtle is currently standing on.

NetLogo Battle Simulation: Placing soldiers in between specific coordinates?

I'm new to NetLogo and I'm unsure of how to place 300 spartans in a narrow row. Let's say an area of 2x5 patches, turtles overlapping one another. I have tried using sprout, this achieved the specific coordinate requirements but the turtles are only one per patch.. Here is some code I have.
ask patches with [pxcor > 0 and pycor > -2 and pycor < 2]
[ sprout 1 [ set color red ] ]
or
to setup-spartans
create-spartans 300
set-default-shape turtles "person"
ask spartans
[ setxy random-xcor -3 ;; makes only a single row and goes across entire screen
;; (I need it to be in a specific area)
set heading 180
set color red ]
end
How about something like this?
create-spartans 300 [
set xcor -2 + random-float 5
set ycor -1 + random-float 2
]

Imported Patch colors not working correctly in NetLogo

I've been trying to get my turtles to 'bounce' off a wall using Netlogo.
I've imported a png file that has the colors of the different surfaces (walls, interface, liquid, heater eventually there will be a few more). I used MSPaint and the colormap from netlogo color chart to create my shapes (a purple square, with brown on the side borders, red border on the bottom and blue border on the top)
What I'm trying to do is have the turtles start on the liquid patches and move in straight lines until they bump into a surface (different colored wall). If they bump into a wall, they should 'bounce' off in a random direction, if they hit the heater, their temperature should go increase and they should also bounce off of the heater surface. If they bump into the interface and their temperature is above 100, they should move through the interface and then move around above the interface (basically teleport up a few pixels).
The issue I'm having is that the turtles move around and they appear to be bouncing off the walls just fine, but they seem to be oblivious to the colors of the heater and the interface. I'm sure I'm missing something basic or obvious, but I've been struggling for three days on this same quirk. Any help would be greatly appreciated.
Here is the code I have right now:
(in this code I have it set to just 'teleport' through the interface regardless of the temperature of the water)
globals[ liquid-color heater-color wall-color reflection-color air-color interface-color
liquid heater wall reflection air interface]
breed [h2o water]
to setup
clear-turtles
reset-ticks
clear-all-plots
import-pcolors "boilermap.png"
set liquid-color 115
set heater-color 19
set wall-color 35
set interface-color 105
setup-patches
create-molecules
end
to setup-patches
set heater patches with [pcolor = heater-color]
set heater-color 19
ask heater [set pcolor 19]
set interface patches with [pcolor = interface-color]
set interface-color 105
ask interface [set pcolor 105]
set liquid patches with [pcolor = liquid-color]
set liquid-color 115
ask liquid [set pcolor 115]
set wall patches with [pcolor = wall-color]
set wall-color 35
ask wall [set pcolor 35]
end
to create-molecules
create-h2o (totalmoles * h20number / 100)[
set shape "circle"
set color black
set size 2
set temperature 20
setxy random xcor random ycor
move-to one-of patches with [pcolor = liquid-color]
]
to go
ask h2o [
(ifelse
pcolor = liquid-color[fd 1 ];ifblock
pcolor = heater-color [set temperature temperature + 5]
pcolor = interface-color [set ycor ycor + 100] ;just trying to get them to jump here, regardless of their temperature
;elseblock
[ bk 1
rt random 180]
)]
end
Diesel, you have a great start on this! I put some code below that answers your questions.
By the way, the title on your question doesn't really match what your actual problem was: imported patch colors not working correctly
I suspect the reason your code ignores the heater and interface is that they imported as pcolors that don't match the ones you expected. I added a verify-colors command that checks to see if the imported colors are correct. I also added an option to generate a new boiler image you can work with in case importing still causes more problems.
Also, I notice you are adding 100 to ycor when your molecule hits the interface. It's possible this moves the molecule entirely up out of view, which could be confusing, as the default view is only 32 units high. I changed the 100 to 5 to keep the molecule in view.
I cleaned up a few more things and confirmed the model ran successfully. The handling of correct bouncing off walls and off the heater was not addressed -- that's your job!
Here's the things I tweaked in your code:
;;
;; added h2o-own section so molecules each have a temperature
;; added a user-choice of whether to import the boilermap.png or create a new one here
;; if importing, verify the colors exactly match the valid colors
;; added "make-drawing" and "verify-colors" sections at the end
;; moved reset-ticks to the end of setup, its usual location
;; added ticks to the end of the go section !!
;; added set-aircolor grey ( it wasn't defined )
;; cleaned out unneeded commands from the setup-patches section. You already have the
pcolors imported, so you don't need to set them again.
;; added "set label temperature" to the create-molecules section as well as to the place
;; where temperature is increased at the heater, so each molecule shows its current temperature
I tested it with interface variables h2o-number =100 and total-moles either 1 or 5 and speed set to very slow. It works!
globals[ liquid-color heater-color wall-color reflection-color air-color interface-color
liquid heater wall reflection air interface]
breed [h2o water]
h2o-own [
temperature
]
to setup
clear-turtles
clear-all-plots
set liquid-color 115
set heater-color 19
set wall-color 35
set interface-color 105
set air-color grey
if-else user-yes-or-no? "import boilermap.png?"
[
import-pcolors "boilermap.png"
verify-colors ;; check the command section for counts of pcolors
]
[
make-drawing ;; just make a new image of a boiler
]
setup-patches
create-molecules
reset-ticks ;; usually, do this as the last step in setup
end
to setup-patches
set heater patches with [pcolor = heater-color]
set interface patches with [pcolor = interface-color]
set liquid patches with [pcolor = liquid-color]
set wall patches with [pcolor = wall-color]
end
to create-molecules
no-display
create-h2o (totalmoles * h20number / 100)[
set shape "circle"
set color black
set size 2
set temperature 20
setxy random xcor random ycor
move-to one-of patches with [pcolor = liquid-color]
set label temperature
]
display
end
to go
ask h2o [
(ifelse
pcolor = liquid-color[fd 1 ]
pcolor = heater-color [ set temperature temperature + 5 set label temperature set heading 0 forward 1]
pcolor = interface-color [set ycor ycor + 5 forward 1 ]
pcolor = wall-color and pxcor < 0 [set heading 135 forward 1]
pcolor = wall-color and pxcor > 0 [set heading 220 forward 1]
[ bk 1
rt random 180]
)]
tick ; DONT FORGET THIS !!
end
;; ========================== new utility functions ===
to make-drawing
no-display
ask patches with [ pycor > 1] [ set pcolor air-color]
ask patches with [ pycor < 1] [set pcolor liquid-color]
ask patches with [ abs pycor <= 1] [ set pcolor interface-color]
ask patches with [ pycor < (min-pycor + 4)] [ set pcolor heater-color]
ask patches with [ pxcor > (max-pxcor - 2)] [ set pcolor wall-color]
ask patches with [ pxcor < (min-pxcor + 2)] [ set pcolor wall-color]
ask patches with [ pycor > (max-pycor - 2)] [ set pcolor wall-color]
ask patches with [ pycor < (min-pycor + 2)] [ set pcolor wall-color]
display
end
to verify-colors ;; see what we imported
let good-colors (list
liquid-color
heater-color
wall-color
interface-color
air-color )
let good-count count patches with [member? pcolor good-colors]
let bad-count count patches - good-count
type "good patch count " type good-count type ", bad patch count: " print bad-count
if bad-count > 0 [ if "no" = user-yes-or-no? " bad patch colors, should I continue?" [stop] ]
end

Create a gradient for a turtle swarm

I created a rectangular turtle grid using a total of 1000 turtles.
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
Now I need to create a sort of gradient, that will give a sense of distance to my turtle swarm. Given a "seed" with value 0, it emits a message with its value 0 in a certain talk_radius.
The turtles inside this talk_radius compute their distance to the seed. If the distance is less than a fixed value (called gradient_distance) the turtle will assume 1 as gradient_value, emitting a message with its value.
The other turtles do the same. So everyone will take the value x + 1, where x is the lowest value of the turtle within the gradient_distance, as shown in the picture
This is the relative algorithm in pseudo code:
loop
if gradient seed = TRUE then // check if robot is designated as gradient source
gradient value(self) = 0
else
gradient value(self) = GRADIENT MAX
for all neighbors n do
if measured distance(n) < G then // G represents the gradient-distance and GRADIENT MAX is infinity
if gradient value(n) < gradient value(self) then
gradient value(self) = gradient value(n)
gradient value(self) = gradient value(self) + 1
transmit gradient value(self)
And that's my implementation in netlogo:
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed gradient_value]
to setup
ca
resize-world -60 60 -20 20
crt 1000
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
ask turtles
[set shape "circle"]
ask turtles-on patch -50 5
[set gradient_seed true]
end
to gradient-formation
while [true]
[
ask turtles
[
ifelse (gradient_seed = true)
[
set gradient_value 0
]
[
set gradient_value gradient_max
set color scale-color green gradient_value 0 120
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance] ;; i consider all the turtle in talk_radius having the right gradient_distance
[
let a ([gradient_value] of self) ;; "of self" is not necessary but helped me for a better comprehension
if (a < ([gradient_value] of myself))
[
ask myself [set gradient_value a]
]
]
set gradient_value (gradient_value + 1)
]
set color scale-color green gradient_value 0 120
]
]
end
I used a scale-color in order to have a feedback of what i have done, as you can see in the image.
And now the problem: instead of let a ([gradient_value] of self), i tried set a ([gradient_value] of self) adding a to the turtle variable (I added a in the turtle-own list on top).
I thought the result would have been the same, but instead i got a constantly increasing gradient_value for every turtle as you can see in the image(the color white denotes a very high gradient_value).
Why this difference? Thank you in advance and sorry for the long question.
EDITED EXTENSIVELY in response to discussion that refined the problem
First, I would like to start with a simpler version of the code. I believe this is exactly the same as yours without the while[true]. I removed the extra 1000 turtles you are creating, and separated the ifelse on whether a seed into two separate ask statements, for clarity. I also moved the colouring until after the value calculation is complete.
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]
to setup
clear-all
resize-world -60 60 -20 20
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape
[ sprout 1
[ set shape "circle"
set gradient_seed? false
]
]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
repeat 10 [ gradient-formation ]
end
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value gradient_max
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
[ let my-gradval ([gradient_value] of self)
if my-gradval < [gradient_value] of myself
[ ask myself [set gradient_value my-gradval]
]
]
set gradient_value (gradient_value + 1)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
There is a conceptual issue here. Until a turtle has its gradient_value calculated, it is 0. This means a large number of turtles will have a 0 turtle nearby and then have their own gradient_value as 1. It does not produce a colour gradient. To get around this, you need to run the gradient-formation several times. The approach in your code of while [true] introduces an infinite loop. Instead, you can repeat an arbitrary number of times (10 in the code above).
The problem with let versus set + turtles-won is that the set with turtles-own creates 1000 copies of gradient_value - one for each turtle. The let version creates a (temporary) global variable that all turtles access. So when you use set, you are setting it for that turtle, not as a general access number. I think what is happening is that the line set gradient_value my-gradval is accessing the wrong turtle's copy of my-gradval.
But, from the discussion, the purpose of the code that is causing the problem is to find a local minimum. There is a much more direct way of doing that.
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value 1 + min [gradient_value] of
other turtles in-radius min (list talk_radius gradient_distance)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
ADDED a minimum working example to show the differences.
This is the let (global variable) version
turtles-own [testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
let my-testval [testval] of self ;; creates a temp global variable
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies the global variable value
]
]
]
]
end
This is the set (turtle attribute) version
turtles-own [testval my-testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
set my-testval [testval] of self
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies value from one attribute to other
]
]
]
]
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