How to plot a vertical line every X step in NetLogo? - netlogo

This might be really basic, but how can I plot a vertical line from Y= 0 to Y = plot-y-max every X time step ? I used if ticks mod 100 = 0 [plotxy ticks plot-y-max] but its not working.
Can anyone please help me with this simple problem ?

You could modify your approach to:
if ticks mod 100 = 0 [
plot-pen-up
plotxy ticks 0
plot-pen-down
plotxy ticks plot-y-max
]

Related

NetLogo - no more than 5 % of population has a certain value of variable

How can I give each tick a random amount of turtles a change in a binary variable (1 or 0), whereas no more than 5 % of the existing population at all times has a value of 0 in that variable?
In other words, I wish to have that the total amount of turtles having a variable value of 0 is between 0 % or 5 % of the total amount of turtles at every tick.
How can I achieve this?
My code is:
to setup
create-turtles 100
set var random 1 (only 5 % max shall have a 0 at start)
end
to start
change
end
to change
let %draw (random 1)
if (%draw < 0) … ; than I do not how to continue
end
The n-of primitive selects the specified number of agents. You want some number up to that, so you also need to randomly generate the number. Something like this:
to setup
create-turtles 100 [ set var 1 ] ; give them all value 1
ask n-of random 6 turtles [ set var 0 ] ; randomly selects 0 to 5 turtles, assigns value 0
end

netlogo - radial direction of the turtle

I have an ellipse shape. (created having vertical direction)
I want to place several such shapes on the circle contour
They should be placed having same distance one from another
;x,y - circle center coordinates
;r - radius
to draw-ellipses [x y r elNum ]
if elNum > 0 [
let theta 0
let delta 360 / elNum
loop[
if elNum = 0 [ stop ]
crt 1 [
setxy (x + r * cos theta) (y + r * sin theta)
set shape "ellipse"
set heading 90 + theta
set size 7
]
set theta (theta + delta)
set elNum (elNum - 1)
]
]
end
The ellipse shapes ate placed correctly but their direction is wrong. I want the ellipses to have radial direction. I.e to be the continues of the radius to the point, where shape is placed. but directions are not good.
Only for 0 and 180 degrees the directions are ok.
Some advices what am I missing?
Do I understand correctly that you want your turtles to face away from the radius? If that's true, try replacing your heading line with:
set heading atan xcor ycor
to do it in a way that is consistent with your angle math. Alternatively, you can use facexy, and replace the heading line instead with something like:
facexy x y
rt 180

Create 1000 turtles near each others

I have to create a lot of turtles forming a compact group of any shape, a simple 10x100 rectangle is enough. The important thing is that they must be near each others.
In c i would do something like this:
for(x = 1; x <= rows; x++)
{
for(y = 1; y <= columns; y++)
{
create_turtle(x,y);
}
}
And the equivalent in netlogo would be:
crt 1000
let n 0
let x 1
let y 1
while[y <= 10]
[
set x 1
while[x <= 100]
[
ask turtle n
[move-to patch x y]
set x x + 1
set n n + 1
]
set y y + 1
]
But it's not an elegant solution. Any suggestion?
Edit: More precisely I have to reproduce what has been done in this article: http://science.sciencemag.org/content/345/6198/795.full
Every turtle is a little robot.
And here you can see one way turtles could be positioned turtles schema
I'm using circle turtles like the robots of the article.
One of the trickiest things for programmers from other languages to do when learning NetLogo is getting rid of all the loops. Iterating through the agents or patches is embedded in the ask primitive, you don't need to code the iteration. ask also iterates in a random order so that repeated processes don't lead to any advantage to whichever agent happens to be first in the loop.
Also, when you create turtles, you can immediately give them instructions. You can also place them initially in an arbitrary position rather than move them there. Here is one solution that places them all in a rectangle that is 5 patches to the left/right of centre (0,0) and occupies half the height of the world.
create-turtles 1000 [ setxy random-float 10 - 5 random-ycor * 0.5 ]
From the edit, I think you are wanting them to be created at gridpoints rather than randomly within the space. If that is true, then select the patches you want and ask them to sprout a turtle.
let in-shape patches with [ pxcor >= -10 and pxcor <= 10 and pycor >= -10 and pycor <= 10 ]
ask in-shape [ sprout 1 ]
You will need to work out your own values and make sure they are within the world dimension.

How do you generate a histogram in NetLogo?

I have a breed called players. Each player-owns a player-strategy. There are 6 possible player-strategies. When I write
histogram [player-strategy] of players
nothing appears on my plot.
I have one plot with one pen -- and don't set either the plot or penname. plot-name returns the plot. pen-mode is bar. interval is 1.0
I don't know how to ask for the current penname. But since the plot has only one pen it shouldn't matter.
The histogram command is in both plot-update and pen-update. I've tried it in both together or one at a time. I also tried in code. Makes no difference.
What do I have to do to get a histogram to appear?
OK, here is what I think you are after.
If you create the following plot:
And write the following code:
extensions [table]
breed [players player]
players-own [player-strategy]
to setup
clear-all
create-players 100 [ set player-strategy one-of ["a" "b" "c" "d" "e"] ]
reset-ticks
end
to update-strategy-plot
set-current-plot "Player strategies"
clear-plot
let counts table:counts [ player-strategy ] of players
let strategies sort table:keys counts
let n length strategies
set-plot-x-range 0 n
let step 0.05 ; tweak this to leave no gaps
(foreach strategies range n [ [s i] ->
let y table:get counts s
let c hsb (i * 360 / n) 50 75
create-temporary-plot-pen s
set-plot-pen-mode 1 ; bar mode
set-plot-pen-color c
foreach (range 0 y step) [ _y -> plotxy i _y ]
set-plot-pen-color black
plotxy i y
set-plot-pen-color c ; to get the right color in the legend
])
end
You should get the following plot:
I believe this should answer your previous question as well.
(Note that I've put the plotting code in a code tab procedure, but you could just as well stick it in the "Plot update commands" field.)
I know it is a very old question, but in case anyone lands here on a search. The current version of NetLogo comes with example code for histogram usage: look in the file menu under 'Models Library'; see 'Code Examples'/'Histogram Example'.

Colormaps custom using cbfit matlab

Hi I am trying to costomize my colormap according to tick values of the of the colorbar. I defined my own colorbar with 5 colors eg 5 colors from blue to red
cmap_my=[0 0 1;0 1 1;0 1 0; 1 1 0; 1 0 0];
than i would like that the colour changes for every tick i put. By using cbfit the colors are changing according to the ticks if ticks are evenly distributed in the range of 0:40, but my ticks are
h = colorbar;
set(h,'YTick',[5,10,15,22,30,35,40]);
and my range is up to 45. In addition to that the colors i have defined dont show anymore when i use cbfit. Is there a possibility to give certain ranges for colors and eg from 5 to 10 dark blue 10 to 15 light blue 15 to 22 green 22 to 30 lightyellow 30 to 35 yellow 35 to 40 orange and over 40 red and than makes clear cuts at the ticks?
I hope I understood your problem. Here goes one example. You can work from here for you specific colors.
mri=load('mri');
mri=double(mri.D(:,:,1,13));
%making my image within the same range as yours
mri=(mri-min(mri(:)))./(max(mri(:))-min(mri(:)))*45;
figure,imagesc(mri),axis off,axis image,h_bar=colorbar;
%h_map(1) is related with the min(mri), and h_map(end) with max(mri)
h_map=colormap;
min_mri=0;
max_mri=45;
lim=eps*100;
my_map_int=[0-lim,5-lim,5+lim,10-lim,10+lim,40-lim,40+lim,45+lim];
my_map_color=[0,0,0;0,0,0;0,0,1;0,0,1;0,1,0;0,1,0;1,0,0;1,0,0];
new_map_color(:,1)=interp1(my_map_int,my_map_color(:,1),linspace(min_mri,max_mri,64),'nearest');
new_map_color(:,2)=interp1(my_map_int,my_map_color(:,2),linspace(min_mri,max_mri,64),'nearest');
new_map_color(:,3)=interp1(my_map_int,my_map_color(:,3),linspace(min_mri,max_mri,64),'nearest');
figure,imagesc(mri),axis off,axis image
colormap(new_map_color)
colorbar