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
Related
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
I am very new to NetLogo and now I try to understand the logic behind NetLogo. I am building my first model and can not figure out how the procedures have to be ordered to do what I would like them to.
What my model should do:
In the “morning” (beginning of the simulation) spread out my agents randomly
Then let them move “uphill” with a little bit of randomness to go to the places with the highest insolation in their surroundings.
Stay there for a number of ticks (between 25 and 34)
After my timer has expired they should start foraging – so far just moving around randomly – the timer should not start again and also the “uphill” behaviour should not be executed again
What the model does:
The agents spread randomly
They move to the place with the highest insolation in their surrounding (uphill)
The timer starts and counts down
The agents start moving again– but the I am trapped in the uphill – stay put till the timer expires “loop” again
I guess that it is a real beginner-mistake… Could you please explain to me how to get this right?
to go
move-turtles
tick
end
to move-turtles
ask turtles
[ifelse insolation <= max [insolation] of neighbors
[continue]
[bask]
pen-down
]
end
to continue ;; a turtle procedure
ifelse random-float 1.0 < 0.7
[uphill insolation]
[move-to one-of neighbors]
end
to bask ;; a turtle procedure
set count-down count-down - 1
set label count-down
if count-down = 0
[
forage
set label ""
]
end
to forage ;; turtle procedure
set heading (random-normal 180 30)
fd random-normal 3 2
end
If I understand your problem correctly, you only want them to do the uphill / bask once per day but they do it repeatedly. Imagine a turtle has done their basking, the forage procedure sends them in a random direction once. Now, the next tick, they are faced with:
ifelse insolation <= max [insolation] of neighbors
[ continue ]
[ bask ]
So they look around and compare the insolation values and then select the 'continue' or 'bask' procedure depending on the comparison. If they get the continue procedure, they move to one of the neighbouring patches, either to the one with the largest insolation value or randomly. If they get the bask procedure, nothing happens except that the label gets turned on. This is because the value of count-down is 0 from the last time they basked, so it is -1 by the time the code checks whether the value is 0.
Once nothing happens, they are faced with the same choice next tick and will have the same outcome. So once the turtle is at the insolation peak, it will stay there indefinitely.
If you want something to only occur once, the easiest thing to do is to add a variable that remembers whether it has happened yet. NetLogo convention has true/false (yes/no) variables with a question mark at the end. So you can do something like (assuming you have added 'bask-over?' to the turtles-own and initialised it as false):
to bask ;; a turtle procedure
set count-down count-down - 1
set label count-down
if count-down = 0
[ forage
set label ""
set bask-over? true
]
end
And you then bring that variable into the test for whether to continue or bask:
to move-turtles
ask turtles
[ ifelse bask-over? or insolation <= max [insolation] of neighbors
[ continue ]
[ bask ]
pen-down
]
end
Note that if bask-over? is the same as if bask-over? = true. Similarly you can use if not bask-over? instead of if bask-over? = false.
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.
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).
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.)