I want to plot a histogram that shows the distribution of the cset-size variable among the turtles. But when I specify the histogram [cset-size] of turtles for the histogram plot it does not show anything. Below I put the screenshot of my histogram setting and the entire code.
Is there something I forgot to specify or anything wrong with my code?
globals [ cset-size ]
to setup
ca
crt 100
[
set shape "person"
set color yellow
setxy random-xcor random-ycor
]
end
to go
ask turtles
[
let a 4
let b 1 / 6
set cset-size round random-gamma a b
set label cset-size
]
end
There are two problems - the values to plot and the lack of time. The histogram construction itself is fine.
The first problem is that cset-size is in the globals list. That is, it is a global variable with only one value and each turtle is changing that value rather than storing their own. From your code, it is clear that you want each of your turtles to have its own value of cset-size. To have each turtle with its own value:
Delete globals [ cset-size ]
Try this instead: turtles-own [ cset-size ]
The second problem is that you don't have any concept of time in your model. By default, NetLogo updates plots each tick, so that it is not constantly updating the display and slowing down the model. This requires two changes:
add the command reset-ticks as the last line in your setup procedure
add the command tick as the last line in your go procedure
These will initialise the timer as part of the model setup, including telling NetLogo to display. And it will advance time each pass through the go procedure, which triggers the plots to update.
Related
I want to create a model which stimulates cell replication in human tissues. To do this I will only be working with patches and not turtles.
A key concept to cell replication is fitness. Fitness in simplified terms is how 'strong' a cell is to replace the cell next to it.
Initially I created a tissue like stimulation where each color is a cell type with a fixed fitness 100. Then I introduced a mutated cell whose fitness ranges from 90 to 110. What I want to do now is introduce probabilities for cell replication based on different fitness values.
So if we have 2 cells next to each other, one with fitness 95 and the other with fitness 100, I want to have a code that says the cell with fitness 100 has a 75% to replace the cell with fitness 95. Of course this should go across the ranges from 90-110 and this probability will depend on what the fitness values of cells next to each other have.
patches-own [ fitness ]
to setup
clear-all
setup-patches
reset-ticks
end
to setup-patches
ask patches ;; randomly set the patches' colors
[ set fitness 100
set pcolor (random colors) * 10 + 5
if pcolor = 75 ;; 75 is too close to another color so change it to 125
[ set pcolor 125 ] ]
end
to go
if (variance [pcolor] of patches) = 0
[ stop ]
ask patches [
;; each patch randomly picks a neighboring patch
;; to copy a color from
set pcolor [pcolor] of one-of neighbors
set fitness [fitness] of one-of neighbors
if fitness > 100
[set pcolor 65]
]
tick
end
to mutate
;let mutateSet [patches with [ pcolor = 125]]
ask patches
[
if ( (random-float 1) < 0.05 ) [
set pcolor 65
set fitness ((random 20) + 90)
]
]
end
This is what I have so far, and I cannot figure out how to introduce this probability parameter accordingly inside the go section. I saw somewhere the rnd function helps with probabilities, but it was using turtles and not patches.
One very important tip I want to give you is to think about the stochasticity and scheduling in your model. Currently your agents take their action one at a time, with the order within each tick being randomised. This means that the order in which the patches change their pcolor has an influence on the outcome.
A way to circumvent this is to ask turtles twice. The first one lets each patch choose whether or not they want to change, the second ask actually does the changing. That way they all choose before any of them change.
The segregation model is a good example of that (it uses turtles but that doesn't make any important difference).
This choosing part (probably a separate procedure that you write) is where the magic happens. You can have each patch check their own fitness and the fitness of all nearby patches ([fitness] of neighbors). When you have these fitness values, you can use them to calculate the probabilities that you want (which depends completely on what you are trying to model).
When you have all your probabilities, you can use one of various methods to determine which one is randomly picked. I'm not going to write this out as there are already numerous examples of this exact thing on stackoverflow:
Multiple mutually exclusive events and probabilities in netlogo
In NetLogo how do use random-float with known percentage chances?
Netlogo - selecting a value from a weighted list based on a randomly drawn number
I have a student who created a global variable called random-patch and a patch variable called elevation. They then used elevation to randomly choose a patch to set random-patch to. Problem is the code is running and choosing a patch without setting the patch variable elevation. I understand it is automatically set to zero but shouldn't this code usually come up with nobody? Any help understanding the issue here would be appreciated.
Code:
globals [ random-patch ]
patches-own [ elevation ]
to setup
ca
set random-patch one-of patches with [ elevation = (random 50) ]
;Also still reports a patch if only the pre to-setup code is entered and the
;following is entered on the interface observer console:
show one-of patches with [elevation = (random 50)]
This is actually a really subtle error. The reason it is not returning nobody is because it constructs the agentset before selecting from it. What it is doing is testing each patch independently and drawing a random integer for each. The following code separates these two steps.
globals [ random-patch ]
patches-own [ elevation ]
to setup
clear-all
let zeros patches with [ elevation = (random 50) ]
type "Patches with zero: " print count zeros
set random-patch one-of zeros print random-patch
end
If you are using the standard settings, there are 1089 patches. 1/50 of the random draws will return 0 so 1/50 or around 22 will patches will satisfy the condition and be in the implicitly constructed patchset.
Change the random 50 to random 100000 in your original code and you will likely get nobody. It will also operate as you expect if you separate out the draw (so let roll random 50 then set random-patch one-of patches with [ elevation = roll ]
I am building a model that tries to simulate a network-based market. In this model the turtles/nodes get a reward called points, which is a turtles-own variable.
I am now trying to plot a graph of the degree of the nodes against the average number of points that nodes with a given degree have. I have attempted to do this by creating a plot from the interface tab but I cannot manage to make this work.
Here are images of the windows of the plot settings.
Anybody know how can I make this work?
Also, I keep getting these "Runtime error: Can't find the maximum of an empty list" in all the plots/histograms I create. It is not a big deal at the moment as they seem to work fine, however if you know why these appear please let me know!
Thanks beforehand,
Carlos
For simplicity and to avoid overloading your plot setup, I like to use to-report procedures for things like this. As a quick example try this setup:
turtles-own [ points degree ]
to setup
ca
crt 50 [
set degree 5 + random 5
set points random 10
setxy random-xcor random-ycor
]
reset-ticks
end
Make a to-report each for a list of existing degrees, the average points of turtles that have each degree, and the maximum of those average point values:
to-report degrees-list
report sort remove-duplicates [degree] of turtles
end
to-report avg-points-list
let avg-list map [ i ->
mean [points] of turtles with [ degree = i ]
] degrees-list
report avg-list
end
to-report max-avg
report precision ( max avg-points-list + 1 ) 2
end
In this example, degrees-list reports [ 1 2 3 4 5 ], avg-points-list reports something like [6.5 3.9285714285714284 6 3.75 4.2], and max-avg reports something like 7.5- note that of course the exact values will vary since the setup is random.
Now, you can set up your plot window:
The actual plotting is handled by the foreach primitive in the plot pen, which uses plotxy to plot the point value in avg-points-list against the corresponding value in degrees-list. Should give a plot that looks something like:
Hope that's sort of what you're after!
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 doing a simple radio propagation model in netlogo where i have to generate a plot of power loss Vs. Distance.Is there a way to plot one quantity vs other instead of one vs ticks. Any advice on procedures is greatly appreciated.
Thank you!
The plotxy primitive lets you do that.
I don't know how your power loss and distance data is stored, but let's build a quick example using turtles xcor and ycor as data. You should be able to adapt it easily.
Here is the very basic model:
to setup
ca
ask n-of 100 patches [ sprout 1 ]
reset-ticks
end
to go
ask turtles [ fd 1 ]
tick
end
Now you can create a plot. Put clear-plot in the plot update commands and ask turtles [ plotxy xcor ycor ] in the pen updates commands:
Also make sure that your pen is set to "point mode" in the advanced pen options, or you'll get a jumble of lines:
(Access the pen options by clicking on the pen icon next to the pen definition in the plot dialog.)