Netlogo: How to iterate over agentset and set variable speed - loops

I am attempting to iterate over a set of turtles and assign each of them a different, random speed. When I attempt to use:foreach turtles [ ... ] I get an error message stating "cannot iterate over agentset". I know I can use ask for setting all turtles the same, but I want to have turtles moving at different speeds from one another.

ask can do this job just fine:
ask turtles [
set speed random 10
]
this will give each turtle its own different, random speed.

Related

Set Turtle to Specific Grid Location - NetLogo

I have seen similar questions but haven't found what I needed yet...
Was wondering if anyone knew how to set specific locations for turtles at NetLogo Setup?
Currently tried:
to setup
create-turtles 5
set turtles at-points [[-12 20 ] [-11 19] [-12 18 ] [-18 18 ] [-11 17]]
but they're all showing up at [0 0]
Thanks!
This can be done in many ways:
You can use setxy in the create-turtles command block. In most models you will see this is used in combination with random-xcor and random-ycor to give each turtle a random location, but you can also use it in any other way.
You can use move-to to ask a specific turtle or group of turtles to go to a specific agent (e.g. to a specific patch).
You can use sprout to directly ask a specific patch or group of patches to create turtles there.
In any case, I suggest you take a look at the NetLogo Programming Guide and the NetLogo Dictionary

Netlogo while loop only one time

I have a small problem in Netlogo, which I began to work on only a few days ago, I try to make a maze with two adventurers, and I make them go from two different starting locations, to one final location. All the beginning of my code works fine, to draw my maze, but when I want to make the adventurers go, only one of them goes in the right direction and find the exit, and the second doesn't even go in the asked direction (East).
I think the problem is in my GO procedure, but I can't reach to find a solution...
Here is my code, I work on Netlogo 5.2
to createaventurier
create-aventuriers pointsdepart
[set shape "person"
set color pink
set size 1
move-to one-of patches with [pcolor = green]
ask patch-here
[set pcolor blue]
set beta ycor
]
show count aventuriers
end
Here the program does what it's supposed to do.
to go
set i 0
createaventurier
while [i < pointsdepart]
[show count aventuriers
ask one-of aventuriers
[set heading 90
execute]
set i i + 1
]
show count pas
end
And it's here that the program return that there are no adventurers (no agents or agentsets) while the observer returns me that there are two of them (when I want two adventurers). I Breed-ed them at the beginning of the code, and I used a lot of while loops in other procedures, which worked perfectly.
I'm not at ease with the software, I'm just looking for a simple explanation, (I'm not so good in english too).
If you need some other parts of my program I can post it, but I don't think they'll be needed. If you need more informations I can also post it, but I hope I have been clear enough.
I thank you in advance.
Here is a simplified version of your code. I have changed adventurers into turtles so I didn't need breeds and hard-coded the number 2 for your variable pointsdepart. It works fine, in the sense that there are always 2 turtles.
to setup
clear-all
ask n-of 20 patches [set pcolor green]
reset-ticks
end
to make-agents
create-turtles 2
[ set shape "person"
set color pink
set size 1
move-to one-of patches with [pcolor = green]
ask patch-here [ set pcolor blue]
]
show count turtles
end
to go
let i 0
make-agents
while [ i < 2 ]
[ show count turtles
ask one-of turtles
[ set heading 90
forward 1
]
set i i + 1
]
show count turtles
end
This suggests that the problem is in your execute function (which I replaced with forward 1).
Running my code will demonstrate a logical problem. You are looping through (twice in this example) and running ask one-of in each loop. one-of selects a random turtle, so you might get them each to run your execute code once, or you might have the same turtle chosen each time. It is very likely you want code that looks more like this:
to go
make-agents ; note - should really be in setup, not go
ask turtles
[ set heading 90
forward 1
]
show count turtles
end
Also, you would generally have a tick command at the end of the go procedure to advance the clock and then the go procedure is run again so the turtles continue to move etc. This is why I have commented that the call to create the adventurers should really be in the setup procedure, otherwise another 2 adventurers will be created each time the clock advances.
The setup procedure is for everything that needs to be in place for the beginning of the simulation (eg creating adventurers, setting up your maze, giving initial resources to adventurers). The go procedure is for the actual process that is being simulated (eg moving, obtaining resources from the environment, depleting energy).

How to randomly generate turtles in netlogo that do not overlap/touch

I am building a parking model and am having trouble when I set up my model. Users are allowed to select the number of parked cars and I currently have the program randomly generating these turtles on rounded random x-coordinates (ensuring all turtles are on only one space/patch). The problem is multiple turtles can be located on the same patch. Is there a way to create these turtles so that they do not lay on the same patch, while randomizing the x-coordinate? If not, I was envisioning a do-until type of loop to keep moving turtles forward until the patch the turtle was on had no other turtles on it.
If the loop is the clear solution, I have never used a loop in this program, and would really benefit from seeing an example, but if that is too much to ask I'm sure I could find one posted online somewhere.
to setup-turtles
ask n-of 10 patches with [pycor = -1] [
sprout 1
]
print max [count turtles-here] of patches
ask turtles with [who > 0]
[set color blue
set shape "car"
set heading 90
set xcor round random-xcor
set ycor -1 ;; this ycor indicates that it is in the parking lane
set pcolor red
]
Another solution approach would be to use n-of, so e.g. to make 20 turtles,
ask n-of 20 patches with [pycor = 0] [
sprout 1
]
So for example if we test it with:
to test
clear-all
ask n-of 20 patches with [pycor = 0] [
sprout 1
]
print max [count turtles-here] of patches
end
the result printed is always 1.
Your best bet would be to use the turtles-here primitive. Check out the following which will only add turtles to patches on pycor 1 if there's no other turtles on the patch:
let new-patch one-of patches with [ (pycor = 1) and not any? turtles-here ]
if new-patch != nobody [
ask new-patch [ sprout 1 ]
]
See http://ccl.northwestern.edu/netlogo/docs/dictionary.html#turtles-here

NetLogo - How to see the number of patches that contain turtles

This seems like it should be very simple and have a primitive command for, however I cannot seem to find one in the netlogo dictionary.
I just want to report back a number, which is the number of patches which have one or more turtles on.
I thought about using the "turtles-here" command, but that returns an agentset rather than something I could use such as a boolean or value.
Anyone know a way of doing this?
Simply:
count patches with [ any? turtles-here ]

netlogo: how to make turtles stop for a set number of ticks then continue

I'm trying to create a model where turtles walk randomly (but with a tendency for forward movement) until they land on a yellow coloured patch which represents a baited object.
When a turtle lands on one of the yellow patches, I'd like it to stop on that patch and stay there for 15 ticks whilst it 'investigates' the bait.
After 15 ticks have elapsed I want the turtles to continue moving as usual until they encounter another yellow patch.
I've attempted to modify parts of this parked card model in the netlogo modelling commons but couldn't really make sense of it (I'm new to netlogo)
http://modelingcommons.org/browse/one_model/3205#model_tabs_browse_procedures
I've also tried implementing a count-down timer as described in this thread
How can one create a countdown timer in NetLogo?
However I receive a runtime error 'Only the observer can ASK the set of all turtles' when I try to run the simulation. Can anyone tell me where I'm going wrong? Probably several places! Thanks.
Here's the code that's causing the runtime error:
turtles-own [count-down]
to setup
clear-all
ask patches with [count neighbors != 8]
[set pcolor blue]
create-turtles 20
ask turtles
[setxy random-xcor random-ycor
pen-down]
ask n-of 20 patches
[ set pcolor yellow ]
reset-ticks
end
to go
move-turtles
tick
if ticks >= 720 [stop]
end
to move-turtles
ask turtles
[ ifelse pcolor != yellow
[continue]
[stay]
]
end
to continue
ask turtles
[rt -90 + random 181]
ask turtles
[ifelse [pcolor] of patch-ahead 1 = blue [ lt random-float 360 ]
[fd 1]
]
end
to stay
ask turtles
[
setup-timer
decrement-timer
if timer-expired? [continue]
]
end
to setup-timer
set count-down 15
end
to decrement-timer
set count-down count-down - 1
end
to-report timer-expired?
report ( count-down <= 0 )
end
This is just one example, How many ticks they should stay in the yellow area? i Assumed 15 ticks and I ask turtles to print their tick number on their label too, if it runs too fast you might miss their stay so adjust running speed for your model to see when they stay and when they move. You can have different methods to continue , in this one they just move 1 patch forward.
turtles-own [count-down]
to setup
clear-all
ask patches with [count neighbors != 8]
[set pcolor blue]
create-turtles 20
ask turtles
[setxy random-xcor random-ycor
pen-down
set count-down 15
]
ask n-of 20 patches
[ set pcolor yellow ]
reset-ticks
end
to go
move-turtles
tick
if ticks >= 720 [stop]
end
to move-turtles
ask turtles
[ ifelse pcolor != yellow
[continue]
[stay]
]
end
To continue
rt random 10
fd 1
end
to stay
set count-down count-down - 1 ;decrement-timer
set label count-down
if count-down = 0
[
Continue
set label ""
reset-count-down
]
end
to reset-count-down
set count-down 15
end
To answer just the part about "Only the observer can ASK the set of all turtles", that error message happens if you do:
ask turtles [
ask turtles [
do-something
]
]
This isn't allowed in NetLogo because it's almost always accidental rather than intentional. You probably just wanted each turtle to "do-something" once; you probably didn't mean for each turtle to "do-something" for every possible pair of two turtles.
It's less obvious that you're having all turtles ask all turtles if it's split across procedures. So for example if you write:
to go
ask turtles [ my-procedure ]
end
to my-procedure
ask turtles [ do-something ]
end
It's still wrong for the same reason, but it isn't as easy to see that just from glancing at it.
Your code follows this latter pattern. You have:
to move-turtles
ask turtles [
...
continue
...
]
end
to continue
ask turtles [
rt -90
...
]
end
I don't think you want to do ask turtles in the continue procedure. Since you call the procedure inside ask turtles, it's already a turtle procedure. I'd suggest writing it as:
to continue ;; turtle procedure
rt -90
...
end
The comment reminds you that it's intended to be run by turtles. (We follow this style in all of the models in the Models Library.)

Resources