Reputation: 1
I am in the process of creating a netlogo model to demonstrate the spread of a plant via epizoochory (when seeds attach to clothing or shoes and then drop off somewhere else).
Summary: I have 2 breeds: 1. hikers and 2. seedlings. Each hiker hatches one seedlings every tick. Each patch has a random value between zero and 1. Hikers spawn seedling agents and then the seedlings are asked to die at a probability equal to the patch value.
My current objective: Once 'seedlings' breed is hatched from the 'hiker' breeds I need seedlings to have the chance to die based on the value of the patch. In other words, I'll ask the seedlings to die depending on the value of the patch it hatches on.
Scenario example: 'seedling' breed is hatched from 'hiker' breed on patch with value = 0.7. Therefor, seedling has 70% chance of surviving and 30% chance of dying.
The following is an elementary 'scenario' that can be used to help solve the problem. Note that I need help with 'ask seedlings [chance-to-die]'
Thank you so much!
patches-own [value]
breed [hikers hikes]
breed [seedlings seedling]
hikers-own [hiker-seed]
to setup
clear-all
ask N-of 500 patches [set pcolor red]
ask N-of 500 patches [set pcolor blue]
ask patches [
if pcolor = blue [set value 0.5]
if pcolor = red [set value 0.9]
if pcolor = black [set value 0.1]
]
create-hikers 10 [
setxy random-ycor random-xcor
set size 2
set color yellow
set shape "person"
set hiker-seed 10
]
reset-ticks
end
to go
ask hikers [drop-seeds]
ask seedlings [chance-to-die]
end
to drop-seeds
ask hikers [hatch-seedlings 1 [set pcolor white set size 2 set shape "plant"]]
end
to chance-to-die
*here is where the code would go that is something like:
ask seedlings [have the probability of death = patch-value]
end
Upvotes: 0
Views: 167
Reputation: 2926
You only need to do:
to chance-to-die
if (random-float 1 < value) [die]
end
This is the case because every turtle can directly read (and modify) the patches-own variables of the patch it is standing on.
In your case, this means that each seedling is able to read value
of the patch it is on without having to specify that patch.
Anyway note that what you said is different from what your code is doing. You said that you want each new seedling, hatched by a hiker, to die or remain according to patches-own value
. However in your code to chance-to-die
is called as part of to go
, where you say ask seedlings [chance-to-die]
. This means that, at every iteration of to go
, you are asking all seedlings (including those that have not just been hatched) to perform to chance-to-die
. The code that would resemble your words better would be something like:
to go
ask hikers [
drop-seeds
]
end
to drop-seeds
hatch-seedlings 1 [
chance-to-die
set pcolor white
set size 2
set shape "plant"
]
end
to chance-to-die
if (random-float 1 < value) [die]
end
Now, with this different arrangement, it is only the new seedlings performing to chance-to-die
, and not all of them (for example: not the seedlings that survived from previous iterations).
Note that in any case you should not include ask seedlings
in to chance-to-die
, because this procedure is called from the hatch-seedlings
command block. This means that each new seedling will automatically execute to chance-to-die
as soon as it is created. Including ask seedlings
in to chance-to-die
would result in each new seedling asking every other seedling to perform the action.
(A similar and equivalent reasoning would apply also to your initial example where you had ask seedlings [chance-to-die]
. In general, always make sure that you don't perform double-asking!)
Final suggestion: in terms of style etiquette, it is preferable that all command procedures (i.e. those starting with to
; as opposed to reporter procedures, that are those starting with to-report
) are named as verbs (for example: to check-death-probability
).
This might be excessive, but it came to my mind and I cannot help but post it.
If you are an efficiency fanatic, then doing as below will be more efficient:
to go
ask hikers [
if (random-float 1 >= value) [
drop-seeds
]
]
end
to drop-seeds
hatch-seedlings 1 [
set pcolor white
set size 2
set shape "plant"
]
end
Note that there is no to chance-to-die
procedure in this arrangement, but the check is performed upstream, i.e. before hikers hatch.
This will be more efficient because, on those occasions when the seedling does not have to survive, NetLogo won't have to create the seedling (through hatch-seedling 1
) only to make it die one second after. On the contrary, on those occasions the condition (random-float 1 >= value)
will evaluate as FALSE
(note the change of direction in the inequality, from the usual <
to >=
) and the hiker will just skip the command block that contains drop-seeds
.
I'm not claiming that this efficiency gain is going to make any practical difference (unless you have a huge model with an immense number of calls to drop-seeds
), but just thought to point it out for two reasons:
Upvotes: 3