I'm having a hard time in making the turtles die after certain ticks.
What's a procedure that I could use to let the turtles die after 10 ticks?
It depends. Do you want them to die after 10 ticks of their (individual) life or 10 ticks of simulation?
a) Let them live for 10 ticks
1. Declare the birthday (or birthtick) variable at the beginning of program:
turtles-own [
birth-tick
]
; Note: you can define variables for specific breed if you are using breeds
2. Set their birth-tick when you create them:
create-turtles 1 [ set birth-tick ticks ]
Note: make sure the reset-ticks is called before using ticks reporter
3. Ask them to die
Somewhere (usually in a go procedure) call:
ask turtles[ if ticks - birth-tick > 10 [die] ]
Note: make sure you call ticks in your go procedure
b) The end of the living world after 10 ticks
If you want to stop the simulation after 10 ticks call
if ticks > 10 [stop]
Related
I have a simulation which I like to start displaying after 20 ticks, while the program run in the previous ticks but without display.
Thank you
This is possible, you just have to use the no-display primitive and set your model view updates to be on ticks instead of continuous.
Below is a sample that will have some turtles move forward in the 20 ticks, then spin in place once their actions become visible. Setup a normal setup button and go forever button, then when you hit go you will not see the turtles move forward. Once they're moved, I use the display primitive to have the rest of the actions show as normal.
to setup
clear-all
create-turtles 100
reset-ticks
end
to go
ifelse ticks < 20 [
no-display
ask turtles [ fd 0.2 ]
] [
display
]
ask turtles [ lt (10 - random 20) ]
tick
end
A side note is that this technique will not work in NetLogo Web, since the display and no-display primitives do not work there yet. An alternative if you need to run in NLW in this way is to use repeat instead. This is not how I'd recommend building a model in general, but in this case it gets the job done:
to go-repeat
ifelse ticks = 0 [
no-display
repeat 20 [
ask turtles [ fd 0.2 ]
tick
]
] [
display
ask turtles [ lt (10 - random 20) ]
tick
]
end
I'm rather new to NetLogo but I want to simulate the collective charging behaviour of drivers. For now, I want to implement a schedule (e.g. that the drivers are on the road between 8am and 5pm and therefore are eligible to charge before and after these hours). In order to do so, I anchored the time to the ticks. But now the implementation of the "to setup-schedule" does not work. I already looked at the collinsheppard/time example: "Discrete Event Scheduling" but that does not run either on my computer (an internal error is announced). I would appreciate your help a lot!
extensions time]
globals [
cars-charging ;; number of cars charging
blackout-patch ;; patch showing the blackout label
home-patches ;; agentset of green patches representing BEVs being at home
charge-patches ;; agentset of blue patches representing the charging stations
work-patches ;; agentset of grey patches representing BEVs not being at home
energy-demanded ;; kW requested by BEVs in charging events
q ;; probability of driving somewhere (i.e. work)
tick-datetime ;; time in the model
; blackout-threshold
]
turtles-own [
battery-level
battery-level-minimum
charge?
]
to setup
__clear-all-and-reset-ticks
;; set the start date to January 1, 2020 and link the ticks to one hour
time:anchor-schedule time:create "2020-01-01 00:00" 1 "hour"
set tick-datetime time:anchor-to-ticks (time:create "2020-01-01 00:00") 1 "hour"
setup-world
setup-turtles
setup-schedule
end
to setup-world
;; Creation of Work
set work-patches patches with [pxcor > 1 and pycor < 1]
ask work-patches [set pcolor 37
]
;; Creation of Home
set home-patches patches with [pxcor < 1]
ask home-patches [set pcolor 57 ]
;; Creation of Charging Stations
set charge-patches patches with [pxcor > 1 and pycor > 1]
ask charge-patches [ set pcolor 98 ]
;; use one of the patch labels to visually indicate whether or not the electricity grid is overloaded
ask patch (1 * max-pxcor) (0.33 * max-pycor) [
set blackout-patch self
set plabel-color red
]
end
to setup-turtles
crt 10
ask turtles [
set size 1
set shape "car"
set color 105
move-to-empty-one-of home-patches ;; start at without charging and at home
]
end
to setup-schedule
;; schedule all of the turtles to perform the "move-to-empty-one-of" work-patches procedure at 12am
time:schedule-event turtles [ [] -> move-to-empty-one-of work-patches ] time:create "2020-01-01 12:00"
;; See what's on the schedule
;print time:show-schedule
end
to go
;; stop when there is a blackout
if energy-demanded > blackout-threshold [stop]
;; update the global variables
ask blackout-patch [ set plabel "" ]
;; advance the clock
tick
end
to move-to-empty-one-of [locations] ;; turtle procedure of not using the same patch = same charging station
move-to one-of locations
while [any? other turtles-here]
[move-to one-of locations]
end
I am afraid that the discrete event scheduling part of the time extension is not working with the current version of NetLogo. (The rest of the extension still does, even though you may get a warning when starting a model with NetLogo 6.1.)
The NetLogo development team started to fix the time extension for packaging with NetLogo but the work seems to have slowed down. (The original programmer of the extension is no longer available to support it.) I will ask them to give it more priority but anyone else wanting to do discrete event simulation (in addition to the extension's other features, such as linking ticks to specific amounts of time) might also want to let the developers know.
If you have a tick representing one hour and you want all your turtles to go to do something at a particular time each day, then just use the mod function. Something like:
if ticks mod 24 = 9 [ask turtles [go-to-work] ]
I am trying to create a network that moves through the environment as a "static" unit, i.e. nothing in the simulation changes except the position and orientation of the whole, the position and orientation of the individual turtles relative to one another are fixed by their links. Turtles are connected via undirected links, which are tied and set to tie-mode "fixed".
The problem is that in certain situations the links fail to remain fixed and link-lengths begin to change. Initially I noticed that, where the average network degree is relatively low or the network is a complete graph, the tie primitive works. However, when links were created to produce a graph that is moderately connected the link-lengths between the turtles begins to change. Upon further experimentation I can create a network with the same number of links and turtles but with different configurations i.e. the network structure is different, that sometimes maintain the positions and link-lengths but in other situations fail to do so.
How do I get the network to move as a unit no matter how connected the network is or what the configuration of the network is? See example code below, I have added code at the end where you can run multiple configurations of a network with 6 turtles and 6 links to see the issue for yourself, try running at least a half dozen iterations. Thanks!
this produces a network that moves as a unit
to setup
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
This produces a network whose links are still tied and set to tie-mode "fixed", but change their link-lengths. The more links that are asked to die, the more the link-lengths change.
to setup
clear-all
create-turtles 10
ask turtles [fd 2]
ask turtles [create-links-with other turtles [tie] ]
ask links [set tie-mode "fixed"]
ask one-of links [die]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
Here is additional code showing a specific instance of link-length change. Please input the seed 659269695 when prompted by the button "use-seed-from-user". Apologies if the code is clunky, first time using random-seed. "Print-lengths" button is to confirm that lengths change.
;USE seed: 659269695
to use-new-seed
let my-seed new-seed ;; generate a new seed
output-print word "Generated seed: " my-seed ;; print it out
random-seed my-seed ;; use the new seed
reset-ticks
end
;; Use a seed entered by the user
to use-seed-from-user
loop [
let my-seed user-input "Enter a random seed (an integer):"
carefully [ set my-seed read-from-string my-seed ] [ ]
ifelse is-number? my-seed and round my-seed = my-seed [
random-seed my-seed ;; use the new seed
output-print word "User-entered seed: " my-seed ;; print it out
reset-ticks
stop
] [
user-message "Please enter an integer."
]
]
end
to setup
clear-all
create-turtles 6
ask turtles [
fd 5
set shape "circle"
set size 1
set color yellow
if count links < 7 [ask one-of turtles [create-link-with one-of other turtles
[tie]]]]
reset-ticks
end
to go
ask turtles [lt 1 fd 1]
end
to print-lengths
print sort-by < [precision link-length 2] of links
end
I slightly revised your code so that the go procedure includes breaking a link. I also got rid of the explicit setting of tie-mode since that is done by setting the link to tie and added a tick so I could plot. So the code looks like this:
to setup
clear-all
create-turtles 10 [fd 2]
ask turtles [create-links-with other turtles [tie] ]
reset-ticks
end
to go
ask one-of links [die]
ask turtles [lt 1 fd 1]
tick
end
As far as I can see, the turtles move as a unit until it fragments with the loss of links.
I added a monitor for mean [link-length] of links, which is what I think you are asking about and also a plot of the same calculation. Yes, it is true that the average link length changes, but remember that the links are not all the same length. If a longer one dies, then the average length will reduce, and if a shorter one dies then the average will increase. The plot wanders a little, but it is basically flat until fragmentation.
If i add if to my go command i get the error message
you cant use tick in a turtle context because tick is observer only
here is my go commands. search eat go-home and den are all defined in my commands.
energy is also defined as a global variable that turtles own
to go
if ticks = day-length [set day day + 1 create-next-day]
ask adults [search eat]
if energy < 20000 [ask adults [go-home den]]
tick
end
if i take out the line
if energy < 20000 [ask adults [go-home den]]
it runs perfectly, but i need that line or an equivalent. please help
Commands
;;-------------------------------------------------------------;;
;;------------------- ADULTS COMMANDS--------------------------;;
;;-------------------------------------------------------------;;
;; Need to add a private variable (wolves own) for wolves [state] and then need to code 4 states 1. Den 2. Search 3. Eat 4. Return
;; need to code all 4 states
;; Need to correctly allocate energy and the state of decline
To den ;when wolf is full
set energy energy - .04
end
to search ;when wolf is hungry
set energy energy - .07
fd v-wolf
if random 600 = 1 ;; frequency of turn
[ ifelse random 2 = 0 ;; 50:50 chance of left or right
[ rt 15 ] ;; could add some variation to this with random-normal 45 5
[ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5
;; check if it can see a prey/food item
;; here i think we probably pick one of several possible prey
;; that are detectable randomly using the one-of command.
;; We should probably select the nearest one instead ** The turtles are getting
;; caught between two prey species and dying because they cant choose which one **
if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
if energy < 0 [die]
end
To eat ;to kill prey and eat it
let kill one-of prey-here in-radius smell
;need to code in a variable for success too
if kill != nobody
[ask kill [ die ]
set energy energy + 10000]
end
to go-home ;to head home after they've eaten and den until they need to feed again
if energy > 30000 [set target-patch min-one-of (patches with [pcolor = white]) [distance myself]]
face target-patch
fd v-wolf
set energy energy - 1
end
if energy < 20000 [ask adults [go-home den]] will be a problem in go if energy is (as it appears) a turtle variable. This will make the context of the procedure a turtle context, not an observer context.
Edit:
For example, if energy is a turtle variable, perhaps you meant
ask adults [if (energy < 20000) [go-home den]]
First, you need to build your code much more gradually. You have multiple parts of your code that don't work and that you don't understand. Try adding the smallest amount you can and make sure that works before adding anything else. You have three different questions at the moment, with errors in different parts of your code.
On the context issue in Alan's answer, think about it this way: the variable 'energy' belongs to turtles. This means that if you have 10 turtles, you have 10 variables named 'energy', one for each turtle. Which one are you checking whether it is <20000?
What you probably want is to check it for EACH turtle individually and get the turtle to do the required action if it passes the test. So it must be inside ask turtles [], and that changes from the observer to the turtle context (what model entity is doing the thing).
to go
if ticks = day-length
[ set day day + 1
create-next-day
]
ask adults
[ search
eat
if energy < 20000
[ go-home
den
]
]
tick
end
I have also cleaned up your formatting. This is not a requirement, NetLogo is happy to deal with spaces wherever you put them. However, as your code gets longer and more complicated, it will be much easier for you to debug if you follow some basic practices (1) each call to a procedure, each command etc on a separate line (2) bracket [] and indent so that you can see the code block that the brackets enclose.
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).