Question. I have agents moving in the environment. I would like them to record at the beginning and at the end of a step:
in a radius of one patch how many other agents (neighbors) there are (minus themselves) and their characteristics. They should put this in a list or so that gets added.
Is this possible? I'm struggling.
Edit 1: changed tick to step and added (neighbors)
You can keep a breed-variable that keeps this information.
You can just append (using lput) the count the turtles in the radius (using the in-radius)
turtles-own [beginning-step end-step]
to go
ask turtles [set beginning-step lput (count other (turtles in-radius 1)) beginning-step]
ask turtles [ move]
ask turtles [set end-step lput (count other (turtles in-radius 1)) end-step]
end
Related
I want to move turtles to one of patches not fully occupied (n-jobs-to-fill != 0). The code is
ask turtles [move-to one-of patches with [n-jobs-to-fill != 0]
After each allocation turtle-patch, n-jobs-to-fill (that works like a counter) counts one place less
ask patches [set n-jobs-to-fill n-jobs-to-fill - 1]
Can you help me to do that iteratively (for each tick) and to test it by printing each movement turlte-patch in the observer line?
Thank you
The part where you ask patches to update their variable should not be done with a general ask patches, as this will target all patches and either be called too seldom (if you do it just once per iteration of go) or too often (if you do it every time a specific patch receives a turtle).
To easily target the specific patch that just received a turtle, and exactly the amount of times that it receives a turtle, make the moving turtle ask its new patch to perform the update. Or even better: given that turtles can directly access the patches-own variables of the patch they are standing on, you can do:
patches-own [
n-jobs-to-fill
]
to setup
clear-all
reset-ticks
ask patches [
set n-jobs-to-fill random 10
]
create-turtles 10
end
to go
if (not any? patches with [n-jobs-to-fill != 0]) [stop]
ask turtles [
if (any? patches with [n-jobs-to-fill != 0]) [
move-to one-of patches with [n-jobs-to-fill != 0]
set n-jobs-to-fill n-jobs-to-fill - 1
show (word "I moved to " patch-here ", which now has " n-jobs-to-fill " jobs to fill")
]
]
tick
end
I want to compare the patches in a certain radius regarding the amount of a certain class of agents on them. The agents should move to the patch where the most agents (in this case humans) are. If they are already on the patch with the most humans then they must not move. I coded it and the humans group but most of them don't stay and run around in lines (one behind the other). Would be great if anyone of you could have a quick look at my code. Thanks
if Strategy = "Gathering-Simple" [
if ((count(humans-on max-one-of patches in-radius rad [count(humans-here)] )) ) >= count(humans-here) [
if count(humans-on patches in-radius rad) - count(humans-here) > 0 [
face max-one-of patches in-radius rad [count(humans-here)]
fd 1
]]
]
This is a complete working example that uses your code. Is this displaying the behaviour you mean? It does have turtles chasing each other.
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch >= count turtles-here ; comment 1
[ if count turtles-on patches in-radius rad > count turtles-here ; comment 2
[ face target-patch
forward 1
]
]
]
tick
end
If so, have a look at the two lines I have comments on.
Comment 1: The >= means that, even if the turtles are already on the highest count patch, this condition will be satisfied because count turtles-here will be equal to the count of the turtles on the highest count patch (this patch).
Comment 2: This line means that as long as there are any turtles on any patch within the radius but not on the particular patch where the asking turtle is located, then the turtle will move forward.
If you want to only have turtles move if not on a maximum count patch, try this instead:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
reset-ticks
end
to go
let rad 5
ask turtles
[ let target-patch max-one-of patches in-radius rad [count turtles-here]
if count turtles-on target-patch > count turtles-here
[ face target-patch
forward 1
]
]
tick
end
I took out the = in the comment 1 line and removed the second condition entirely so now the turtles move if their current patch has fewer (strictly, not <=) turtles than the patch they've spotted.
I agree with the previous post, but have some additional information.
If you want to move entirely to the target patch on each iteration, instead of moving just one step towards the target patch, in the above answer you could replace the code which produces one step of motion
[ face target-patch
forward 1
]
with
[
move-to target-patch
]
I confirmed by experimentation that the results of the two methods of moving will produce similar but somewhat different results.
We try to show a simple infection via Netlogo. For our purpose we need to start the infection with the same turtle for several times.
But right now with every setup another turtle begins with the infection. We already tried to work with the Node ID, but unfortunately the ID of the different turtles changes with every setup, too. We are out of ideas but
maybe there is a way to sove this problem I am happy for any answers :)
This is our Code so far:
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "JKnachnamennetlogo.graphml"
end
to spread
if (count turtles with [informed? = true] > .7 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.01)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you a lot.
I am a little unclear so am giving bits of different answers for different situations.
If the turtles are different each time, what do you mean by 'the same turtle'. For example, do you mean the turtle in a particular position? If so, you could select the turtle on the appropriate patch.
If it doesn't matter which particular turtle it is (just that it's the same turtle), then the simplest approach is to set the random-seed. Then every time you run any random process (including choosing one-of the turtles to select the starting infection, or ask turtles to do something), NetLogo will use the same chain of random numbers. Of course, if you are still building your model, then adding new pieces of code that change how many calls are made to the random number generator will lead to a different chain, but rerunning with the same code will give the identical run.
You may need to use with-local-randomness and random-seed new-seed if you want to have some parts actually change.
The problem is that nw does not store the WHO variable this is to avoid conflict with already existing turtles in a model.
A work-around would be assigning each turtle a separate id variable and setting that to who.
turtles-own [informed? id]
in turtles creation asign them each the id thus
set id who
you may want to write a conversion procedure like this
to convert
nw:load-graphml "JK_nachnamen.graphml"
ask turtles [set id who]
nw:save-graphml file-name "JK_nachnamen(id).graphml"
end
and use the copy. Of course you would not use
turtle 74
but
one-of turtles with [id = 74]
I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.
I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.
EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.
In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.
The code I tried to implement is as follows,
breed [h2o-molecules h2o]
to setup
clear-all
reset-ticks
create-h2o-molecules h2o-num [
set color 105
set sIze .5
set shape "circle"
setxy random-xcor random-ycor
set pen-mode "up"
]
end
to setup-patches
ask patches [set pcolor 0]
show count turtles
end
to set-label
ask patches [
ifelse count turtles-here > 0
[set plabel count turtles-here]
[set plabel ""]
]
end
to move-h2o-molecules
ask h2o-molecules [
let dice random 1000
let change (dice - 1)
forward 2
set HEADING (HEADING + change * 2)
]
end
to go
setup-patches
move-h2o-molecules
ask turtles [rt random 1
fd 0.3]
set-label
tick
end
Thanks for your time and patience. Regards,
Using turtles-here
You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.
Combine procedure
If you declare
a turtle variable after your breed declaration:
h2o-molecules-own [
turtles-inside
]
(set the variable value inside your create-h2o-molecules)
and your combination limit max-inside as a global variable (use slider widget with 5 as default value)
then the combine procedure can look like:
to combine ;; turtle procedure
; take one turtle from the same patch as a target
; which has turtles-inside low enough to combine with
let target one-of other h2o-molecules-here with
[turtles-inside <= max-inside - [turtles-inside] of myself]
if target != nobody
[
set turtles-inside turtles-inside +
[turtles-inside] of target ;; increase turtles-inside
ask target [ die ] ;; kill the target
set size sqrt turtles-inside ;; increase size
]
end
Stop
You can stop the simulation by
if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]
Comment
The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).
I really need some advice on this, I try to create few leaders and some amount of followers, however it seems that followers do not follow any leader, how do I make a follower follow the nearest leader to them.thanks
turtles-own
[
is-leader?
follower
]
to setup
clear-all
reset-ticks
ask n-of 50 patches
[sprout 1
[set color blue
set follower self]]
choose-leaders
end
to choose-leaders
ask max-n-of 5 turtles [count turtles in-radius 3]
[
set is-leader? true
set color white
]
end
to go
ask turtles [follow_leader]
tick
end
to follow_leader
ask follower
if any? is-leader? in-radius 30
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
end
It's a bit hard to make sense of what you are trying to do with the follower turtle variable. With the code you posted, all turtles have themselves as follower, which I am almost certain is not right. In other words, currently the variable doesn't do anything, and you can delete it unless you want to do something else with it.
Regardless, the problem with your code is in your follow_leader procedure. This will work - I added comments so you can see what
to follow_leader
ask follower
;; since all turtles are asked to run this procedure, and all turtles have themselves as
;;follower, this asks turtles to ask themselves to do something.
if any? is-leader? in-radius 30
;; this is incorrect syntax.
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
;; 'self' refers to the turtle itself, so this will always return 0, and turtles will face themselves
end
Given these errors, this is probably what you want:
to go
ask turtles with [not leader?] [follow_leader];; we only want to ask turtles that are not leaders
tick
end
to follow-leader ;; changed just for NetLogo convention
let nearby-leaders turtles with [leader? and distance myself < 30] ;; find nearby leaders
if any? nearby-leaders [ ;; to avoid 'nobody'-error, check if there are any first
face min-one-of nearby-leaders [distance myself] ;; then face the one closest to myself
fd 1
]
end
Make sure to initialize the leader? boolean in all your turtles. Add set leader? false in the command block that you send to turtles that you sprout in your to setup procedure.