I want some of my turtles to leave at trace of the steps they make. I want them to change the color of the patches they pass by on their move. Similar to what the "pen-down" command does - just with the effect, that the patches change color. It is esay for me to change the color of the patch, the turtle reach - but I want all the patches colored - like if you were walking on a lawn, and the grass under you momentaneouesly turns red :-) Nut just the steps with every tick, but the continous route.
Is there a way? I would be glad to se a small coded example. Thanks very much - this homepage does a great job.
Magellancruiser
Here is one basic solution. The key is realizing that instead of having the turtle do something like forward (random 10) + 1 you can instead have it go forward 1 a number of times equal to (random 10) + 1. Because the distances are based on path sizes (1 = 1 patch across), if you color the patches as you go forward 1 at a time, you should "draw" the color on the patch.
turtles-own [ my-color ]
to setup
clear-all
create-turtles 10 [
set my-color color ; the turtles will have random colors, store them to use later
set color white ; but they're easier to see if they're white
]
reset-ticks
end
to go
ask one-of turtles [
left (random 50) - 25 ; wiggle a bit to not just go in a straight line
let d (random 10) + 1 ; the turtle will move 1 to 10 steps
repeat d [
forward 1
set pcolor my-color ; the turtle can directly set the patch's pcolor variable to its own
]
]
tick
end
You can either use setup and go from the command center or add buttons for them.
Related
I 'm new in Netlogo programming. I would like to make turtles with cloud shape and big size so if another turtle i.e. a person be at the same patch with the cloud to lose energy. The problem is that I can't have a turtle to be in more than one patches, netlogo "can see" that it's in only one patch.
Regardless of the size of an icon depicting a turtle, the turtle is located only at a single point (defined by the variables xcor and ycor). However, you can instead use distance to find if other turtles are close
As JenB said, the turtle only exists as a point, you'll have to come up with logic to make the clouds seem bigger than they are if you want them to be turtles.
Here is some code that demonstrates how to use size and in-radius to make the clouds breed affect the leaves breed color as they move past. It works best with shape = "circle" since then the radius of the cloud will match where the leaves are affected. You can add this code to a basic new NetLogo model to see it work:
breed [ clouds cloud ]
breed [ leaves leaf ]
to setup
clear-all
ask patches [
set pcolor blue + 2
]
create-clouds 10 [
set xcor random-xcor
set ycor random-ycor
set size 1 + random 4
set color white - 2
set shape "circle"
]
create-leaves 35 [
set xcor random-xcor
set ycor max-pycor
set shape "leaf"
set color green
set heading 180
]
end
to go
ask clouds [
ask leaves in-radius (size / 2) [
set color (color - 1)
]
]
ask leaves [
fd (1 + random 10) / 10
]
end
You can also reverse the logic a bit so it's the leaves that check if they are inside a cloud using distance. I find this option more confusing, but it might work better in your case:
to go-leaves
ask leaves [
if any? clouds with [distance myself < (size / 2)] [
set color (color - 1)
]
fd (1 + random 10) / 10
]
end
And finally, instead of using turtles to represent your large areas that turtles move through, you could use patches instead. It would simplify some things, but wouldn't work in every case.
I've made a animal behavior model involving "turtles" and "roads" and I want the model to report back to me when the turtle "crosses" a road. All I want is that it tells me when the turtle moves from a patch that is the grey color to the red color. I've included the code asking it to report this and the program has no issue with the code. To give me a visual representation of what I want it to report, I put a monitor on interface. But it always gives me a "0" for road crossings, even as I can see that my turtle has crossed roads. I would count it by hand, but it's impossible to tell for certain how many road crossings there are and this is for scientific publication. My code is as follows...
turtles-own [
road-crossings
]
to setup
clear-all
;; create turtles on random patches.
ask patch 6 -15 [
sprout 1 [
set color one-of [green]
set size 1
set road-crossings 0
]
]
ask turtles [
if [pcolor] of patch-here = 14.9 [
set road-crossings road-crossings + 1
]
]
reset-ticks
end
to go
ask turtles [
repeat 100 [
repeat 39 [
pen-down
rt random-float 360
lt random-float 360
fd random-float 1.375
]
setxy 6 -15
]
]
tick
end
Any help is appreciated! Thank you!
There are several potential problems with this that I can see.
First, road-crossings is a turtle variable, which is the correct thing to do if you want each turtle to remember how many times it crosses a road. If so, however, the monitor must report sum [road-crossings] of turtles to get the road crossings of all turtles.
Second, which I think is actually your problem: you have the turtle checking whether it crosses the road in the setup procedure rather than the go procedure. The setup procedure is only run at the beginning.
Third, you don't actually have any roads in your example code, but I suspect that's just a failure to create a proper example. I assume that there are patches with pcolor of 14.9 in your real code. If not, though, that would also cause your error. You can make sure by going into the command center and asking count patches with [pcolor = 14.9]
In my model, the number of turtle is dynamic based on the value defined by the user using a slider. The slider can take values between 2 and 10. Each turtle has its own set of co-ordinates and characteristics, hence i used the following code to create them.
create-parties 1
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ]
create-parties 1
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ]
if Num-of-parties >= 3
[ create-parties 1
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ]
I have repeated the above till Num-of-parties =10.
In one of the modules i have created a condition where if a certain value of the turtle reaches 0 it will die.
In the later part of the model i have used set-current-plot to create a chart using the below code:
set-current-plot "Voter Support"
set-current-plot-pen "Party1"
plot 100 * [my-size] of turtle 0 / sum[votes-with-benefit] of patches
set-current-plot-pen "Party2"
plot 100 * [my-size] of turtle 1 / sum[votes-with-benefit] of patches
if Num-of-parties >= 3 [ set-current-plot-pen "Party3"
plot 100 * [my-size] of turtle 2 / sum[votes-with-benefit] of patches ]
so on and so forth for all ten possible turtles.
The problem is if the user has defined 5 turtles and turtle 3 dies at tick 10, then chart portion of the code is throwing an error since there is no turtle 3 but num-of-turtles slider defined by the user has a value of 5.
Please advise on how to solve this. Thanks, appreciate the help.
Regards
When writing model code, you should try to apply the DRY principle: Don't Repeat Yourself. Creating each turtle separately and then trying to do something with each of them by addressing them separately as turtle 0, turtle 1, etc. will lead to all sorts of problems. What you're experiencing with plotting is only the tip of the iceberg.
Fortunately, NetLogo gives you all the facilities needed to deal with a "dynamic" number of turtles. ask is the primitive you will use most often for this, but there are plenty of other primitives that deal with whole agentsets. You can read more about agentsets in the programming guide.
In the case of plotting, you can ask each of your parties to create a "temporary plot pen". We'll use the who number to give a unique name to each of these pens. (That's one of the very few legitimate uses of the who number in NetLogo.)
Put this code in the "Plot setup commands" field of your plot:
ask parties [
create-temporary-plot-pen (word "Party" (who + 1))
set-plot-pen-color color ; set the pen to the color of the party
]
(Note that you won't need the plot pens that you previously defined anymore: you can just delete them. New plot pens will be dynamically created every time you set up your plot.)
To do the actual plotting, we can use very similar code. Put this code in the "Plot update commands" field of your plot:
ask parties [
set-current-plot-pen (word "Party" (who + 1))
plot 100 * my-size / sum [ votes-with-benefit ] of patches
]
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 have turtles moving across the view, and I'd like to be able to follow where they go by making them leave a trail behind them, as though they were emitting smoke as they went. Of course, I could use the turtle pen (pen-down), but since there are many turtles, the view rapidly gets filled with old trails. The solution could be trails that last only for a few ticks before they dissipate. But I don't know how to achieve that.
To be more specific:
1) Is there a technique for making the line drawn following a pen-down command gradually fade away over the period of some ticks?
2) If not, is there a way of removing the line drawn using the pen a few ticks after it was drawn?
3) If not, is there some other technique that would have a similar visual effect?
There is no way to fade the trails in the drawing layer over time. If you want trails that fade, you'll need to represent the trails using turtles instead.
Here's sample code for having "head" turtles that trail ten-turtle "tails" behind them:
breed [heads head]
breed [tails tail]
tails-own [age]
to setup
clear-all
set-default-shape tails "line"
create-heads 5
reset-ticks
end
to go
ask tails [
set age age + 1
if age = 10 [ die ]
]
ask heads [
hatch-tails 1
fd 1
rt random 10
lt random 10
]
tick
end
I'm just killing off the old trails outright, but you could also add code that fades their color over time. (An example of a model that does that is the Fire model, in the Earth Science section of the NetLogo Models Library.)
Here's a version based on the same principle as the one by #SethTisue, but the tails fade away:
globals [ tail-fade-rate ]
breed [heads head] ; turtles that move at random
breed [tails tail] ; segments of tail that follow the path of the head
to setup
clear-all ;; assume that the patches are black
set-default-shape tails "line"
set tail-fade-rate 0.3 ;; this would be better set by a slider on the interface
create-heads 5
reset-ticks
end
to go
ask tails [
set color color - tail-fade-rate ;; make tail color darker
if color mod 10 < 1 [ die ] ;; die if we are almost at black
]
ask heads [
hatch-tails 1
fd 1
rt random 10
lt random 10
]
tick
end
Here's another approach but without using additional turtles. I include it for variety sake - I would recommend going with Seth's approach first.
In this approach, each turtle keeps a fixed length list of previous locations and headings and stamps out the last position. There's some unwanted artifacts with this approach and is not as flexible as using additional turtles, but I think it uses less memory which may help on larger models.
turtles-own [tail]
to setup
ca
crt 5 [set tail n-values 10 [(list xcor ycor heading)] ]
end
to go
ask turtles [
rt random 90 - 45 fd 1
stamp
; put current position and heading on head of tail
set tail fput (list xcor ycor heading) but-last tail
; move to end of tail and stamp the pcolor there
let temp-color color
setxy (item 0 last tail) (item 1 last tail)
set heading (item 2 last tail)
set color pcolor set size 1.5 stamp
; move back to head of tail and restore color, size and heading
setxy (item 0 first tail) (item 1 first tail)
set heading item 2 first tail
set size 1 set color temp-color
]
end