Identifying the location of the patch with the sprouting turtle - netlogo

I am using the following condition to sprout a new turtle and it works.
ask one-of patches with [distancexy 0 0 < 30]
[ if (random-float 1 < (beta * pm * 1000)) [sprout-parties 1 [initialize-party] ]]
What i want is to also record the patch co-ordinates on which the new turtle is created.

Suppose each turtle owns an origin attribute. Then you can just include in intialize-party the code set origin (list xcor ycor).

Related

NetLogo let target min-one-of neighbors math operation produced a non-number

I am new to NetLogo and I am trying to reproduce https://github.com/YangZhouCSS/Pedestrian_Model_Krasnow. When I open, it says that it was produced in NetLogo 5.2 and I am running NetLogo 6.3 so I may need to update code. When I try to run I get a math operation produced a non-number on the following line in the to go section:
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
This is the complete to go section:
to go
if count turtles > 0 [set move-speed count turtles with [moved? = true] / count turtles]
if count turtles = 0 [stop]
ask patches with [exit = 1] [ask turtles-here[die]]
ask turtles [
set moved? false
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
if [elevation + (count turtles-here * 9999999)] of target < [elevation] of patch-here
[ face target
move-to target
set moved? true
ask target [set path path + 1]]
]
if Show_path? [ask patches with [elevation < 9999999][let thecolor (9.9 - (path * 0.15)) if thecolor < 0.001 [set thecolor 0.001] set pcolor thecolor]]
tick
end
And the complete code is here: https://github.com/YangZhouCSS/Pedestrian_Model_Krasnow/blob/master/pedestrian_floor-2exits.nlogo
Thank you in advance for any guidance
Many of the patches have invalid (NaN) values for their elevation, which then causes the error you are getting when you try to use them in an arithmetic operation. I'm not conversant in the gis extension, so I can't tell you why it seems to be assigning invalid elevations to these patches, but if you run setup and then enter in the command center
ask patches [show elevation]
you will see which ones are invalid and may then be able to trace how they came to be that way. Perhaps the gis extension has changed the way it works with the data files since NetLogo v5?
Charles
Not exactly sure why it worked but I replaced
let target min-one-of neighbors [ elevation + ( count turtles-here * 9999999) ]
With
let target min-one-of neighbors [ elevation ]
And the simulation ran

Netlogo Help - neighbors function

I am a beginner with Netlogo and I am attempting to make a simple model so that when an individual is created, it must be placed in a patch that is neighboring the parent (in one of the 8 spaces). I think I need to use the one-of neighbours command and sprout but I am not sure how to do this.
Currently, I have something this in my code:
to birth-death
set npop count turtles
ask turtles [
if random-float 1.0 < dt * r [
set i random-pxcor
set j random-pycor
ask patch i j [set lpop count turtles-here]
if lpop = 0 [
hatch 1 [
set color green
set xcor i
set ycor j
]
]
]
if random-float 1.0 < dt [ die ]
]
end
Which sets a turtle at a random location, but I am not sure what to write so that when an individual is born it knows to select one of the eight neighbors of the parent site to add a new turtle.
You are close. When a turtle is born (created with the hatch command) it is created at the same patch as the parent. So you just need to move it to one of the neighbouring patches from where it already is. Instead of:
hatch 1
[ set color green
set xcor i
set ycor j
]
Use:
hatch 1
[ set color green
move-to one-of neighbors
]

Netlogo Creating a Link with a turtle when they both hit the same patch

Maybe i worded the question wrong but what i want to do in the code is when another turtle meets another turtle they create links with each other
I know its
to go
tick
make-circle
move
if ticks >= timer1 [stop]
end
to move
ask turtles
[
create-links-with other turtles ;here is where i want to put the code
set heading random 360 fd 1]
create-network
end
to create-network
ask links [
set thickness 0.01 * counter
if [patch-here] of end1 = [patch-here] of end2
[set counter (counter + 1)]
]
end
but im not sure how to word it correctly to link when they meet how do i do that
Establish a breed variable for your counts. Then create a link between all turtles that are on the same patch with other turtles-here. Then increment that count variable for when the others turtles have met the original calling turtle. I'll note that I increment the count variable by .5 because each turtle in the link will increment it (there's 2 turtles, so .5 * 2 = 1).
links-own [meets]
to setup
clear-all
crt 100 [setxy random-xcor random-ycor ]
end
to go
ask turtles [fd 1]
ask turtles [rt random 90]
ask turtles [ create-links-with other turtles-here]
ask turtles [ ask other turtles-here [ask link-with myself [ set meets meets + .5]]]
end

Order of the agents in agentset don't match

I have a doubt and apologize if the answer is very obvious. I created the following code. Pretty simple & straightforward:
patches-own [ tl ls ls2 ls3 ls4 tsl]
turtles-own [mysize]
to setup
clear-all
reset-ticks
crt 5
ask turtles [ set heading random 360 jump random 20 set mysize random-float 1]
asd
inspect patch 0 0
end
to asd
let old sum [mysize] of turtles
ask patches [ set tl other turtles
set tsl [self] of tl
set ls [distance myself] of tl
set ls2 [distance myself ^ 2] of tl
set ls3 [(mysize) / old] of tl
]
;print tl
end
to initial
set heading random 360 jump random 20 set mysize 1
end
to go
inspect patch 0 0
ask turtles [ fd 1 set mysize mysize + random-float 1]
let qwe random-float 1
print qwe
if qwe < 0.2 and count turtles > 2 [ask one-of turtles [die]]
if qwe > 0.8 [ ask one-of patches [sprout 1 [initial]]]
asd
tick
end
As you can see, i have a inspect function in the code and below is the snapshot:
My question is why is the ls and ls2 agents out of order. Agentset TSL shows the order of the turtles, so shouldn't the other agentsets created based on that follow the same order.
Unless you sort them somehow, Netlogo will query agents in an agentset in a random order. Functionally, ask tl follows the same logic as ask turtles, and the same goes for retrieving variables from an agentset. For example, if you use the command center to try the code below several times (after running your setup and asd), you will note that the order of agents queried is not the same every time.
ask patch 0 0 [ print [distance myself] of tl ]
All this to say that the creation of a patch's "tsl" list is independent of the creation of your other lists. It is not an ordered list, it is a list of randomly called turtles from the agentset "tl". One way to get consistent ordering of an agentset is to use one of the sort primitives.

why plabel replace previous plabel in this netlogo model for displaying patch and turtle coordination?

with the help of #JenB I wrote a code to help understand forward 1, patch and turtle coordination. The code is attached at the end.
However, I noticed an error: I built two buttons setup and test, after click setup and then click test a few times, we will notice some of turtle 1's plabels will replace previous ones, which is not at all intended and should not happen; whereas, turtle 0 seems to have no such problem.
to setup
clear-all
create-ordered-turtles 2
[
ifelse (who mod 2) = 0
[
set heading heading + 15
]
[
set heading 135
]
pen-down
; forward 1
]
end
to test
ask turtles [ ;; for each turtle
fd 1
;; get patch corrdination
let patch-cor (word "px" pxcor ":" "py" pycor)
;; get turtle corrdination
let turtle-cor (word "x" (precision xcor 2) ":" "y" (precision ycor 2))
;; get distance between current and previous turtle corrdinations
;; get distance between current and origin turtle corrdinations
let distanceTurtle precision (distancexy 0 0) 1
let patch-color color ;; get turtle's color
set label patch-cor ;; let this turtle's label show current patch-corrdination
set label-color patch-color
ask patch-at -1 0 ;; focus on patch 1 unit left to where this turtle is on
[
set plabel patch-cor ;; let this patch label show turtle's patch corrdination
set plabel-color patch-color
]
ask patch-at 4 0 ;; focus on patch 4 unit right to where this turtle is on
[
set plabel turtle-cor ;; let this patch label show turtle's own corrdination
set plabel-color patch-color
]
ask patch-at 5 0 ;; focus on patch 5 unit right to where this turtle is on
[
set plabel distanceTurtle ;; let this patch label show distance between current turtle and origin
set plabel-color patch-color
]
]
end