My simulation space is a total of 1024 patches. I need to randomly place 113 turtles within the middle area of my simulation space which is only 78 patches. Does anyone have any input on how to do this and make sure that the turtles are only in that specified area? Thanks!
Positions are simply numbers so you can set xcor and ycor in appropriate ranges by using the random-float generator and rescaling as required. For example, set xcor 3 + random-float 5 will locate the turtle with an x coordinate in the interval from 3 to 8
Related
I would need to visualize the below turtles (from three different breeds) in a squared grid where links are only between turtles from the same breed, except for breeds types2 and types3, that can be also linked to each other.
So what I would like to have is a 2D grid where the number of turtles per each type is
40% of type1
40% of type2
20% of type3
(in total 100 turtles).
set-default-shape types1 "circle"
set-default-shape types2 "circle"
set-default-shape types3 "triangle"
ask n-of 100 patches [ sprout-types1 1 ]
ask n-of (100 * 0.4) types1 [set breed types2]
ask n-of (100 * 0.2) types1 [set breed types3]
the values are ok but the turtles are 'free' in the world, not displayed on a grid.
How can I display them into a grid and link them based on the above conditions?
This answer Different types of turtles in a lattice grid has provided some help on this, but the resize of the grid and the number of turtles are not the expected ones.
You should resize the world before creating agents.
That is, from the perspective of the code's workflow: if you want to have n agents, then n is first and foremost the number of patches. Then, once this is the case, all patches will sprout.
You need the resize-world command.
You mentioned that you want to have 100 turtles, that is 100 patches, that is a 10x10 world.
This means that you could do:
to setup
clear-all
resize-world 0 9 0 9 ; This creates a 10x10 world.
set-patch-size 30
ask patches [
sprout 1 [
set shape "circle"
set size 0.5
]
]
end
The code above works as long as you are happy for your world to be the size of exactly 100 patches, given that the size = 100 is hard-coded.
You might want to think about some way to accomodate a change in the number of agents.
For example, the approach below works as long as the number of agents is the perfect square of an integer:
globals [
n-agents
]
to setup
clear-all
set n-agents 100
let side-length n-agents ^ (1 / 2) - 1
resize-world 0 side-length 0 side-length
set-patch-size 30
ask patches [
sprout 1 [
set shape "circle"
set size 0.5
]
]
end
After all the point is that the shape of the world in NetLogo can only be a square or a rectangle; i.e. you cannot have a NetLogo world that is made of a prime number of patches (only exception being a world whose world-height and/or world-width equal 1).
So, in order to have your code be the most accomodating to changes in n, you could come up with more elaborated steps that resize the world based on n so that it gives you n patches even when n is not a perfect square; but for example, unless you are happy to have a monodimensional world, you can never have 53 patches. However, since you are talking of grids, I think this shouldn't be a problem for you.
I am working in NetLogo on an animal movement model. I have created six different agentsets that contain the patches in-cone 16 60 (asking my turtles to turn rt 60 before calculating the next agentset). I have then asked each agentset to calculate the sum of weighted-index (a patch variable between 0-1) and save the value into a global variable. So now I gave six areas surrounding each turtle, and a value that represents the resource value of that entire agentset to the turtle. I am trying to get my turtles to recognize what agentset has the highest sum of the weighted index and move to a random patch within it. I am running into issues because the global variable that represents the summed weighted index is not attached to the location of the agentset so I don't know how to move on from this step. My only idea is to do a complicated ifelse chain where I ask turtles to compare the summed values and face the agentset corresponding to that value, but this seems pretty lengthy. Any ideas or suggestions on how to solve this or make my question clearer are much appreciated!
I have tried to make a list and call for with-max but again, this reports the max sum as a number, not the agentset to which it belongs to.
; This code gets called in my go procedure as ask bears []
;I am first creating the agentsets
set heading 0
set n-patches patches in-cone 16 60
rt 60
set ne-patches patches in-cone 16 60
rt 60
set se-patches patches in-cone 16 60
rt 60
set s-patches patches in-cone 16 60
rt 60
set sw-patches patches in-cone 16 60
rt 60
set nw-patches patches in-cone 16 60
; Now I'm adding the index value for all patches within each agentset
set n-sum sum [weighted-index] of n-patches
set ne-sum sum [weighted-index] of ne-patches
set se-sum sum [weighted-index] of se-patches
set s-sum sum [weighted-index] of s-patches
set sw-sum sum [weighted-index] of sw-patches
set nw-sum sum [weighted-index] of nw-patches
; Lost after this
First thing, there's no need to use global variables, just use let to create a temporary local variable.
This is a surprisingly tricky problem because you can't use any of the built in primitives that find the maximum within an agentset and list primitives don't have that capability.
I have written a standalone model that demonstrates what you want. It uses pcolor as the value instead of the weighted index, and I chose colours that get darker as the number increases.
to testme
clear-all
ask patches [ set pcolor one-of [ 28 54 110 ] ]
create-turtles 3
[ setxy random-xcor random-ycor
set color white
]
ask turtles
[ set heading 0
let n-patches patches in-cone 16 60
rt 60
let ne-patches patches in-cone 16 60
rt 60
let se-patches patches in-cone 16 60
rt 60
let s-patches patches in-cone 16 60
rt 60
let sw-patches patches in-cone 16 60
rt 60
let nw-patches patches in-cone 16 60
let directions-list shuffle (list n-patches ne-patches se-patches s-patches sw-patches nw-patches)
print directions-list
let sums-list map [ thisDir -> sum [pcolor] of thisDir ] directions-list
print sums-list
let max-sums max sums-list
print max-sums
let chosen position max-sums sums-list
print chosen
face one-of item chosen directions-list
]
end
You could use a to-report procedures to simplify the calculation of the six agentsets, but I haven't done that as I wanted to use your code to help keep it readable
This prints things out so you can see what it's doing. Note that the shuffle is to ensure random selection if 2 or more happen to have the same totals. Remember that NetLogo lists are 0-indexed, so position will return 0 to 5, not 1 to 6.
What it does is puts the agentsets into a list, calculates the value of the sums (using map) for each item on the list and puts those sums into a new list in the same order. It then searches for the maximum of that new list, finds the position of that maximum, uses that position to extract the correct agentset from the first list, and then faces a random patch within that agentset.
I have the set of points implemented in netlogo and agents are moving from one point to another. Each point has a weight (number approximately between 0 and 9, its not a probability). What I want to made is a simple rule.
I want to give all points probability of visit by the value of weight.
So the next point which will be visited by agent should be calculated by the probability based on point weight and the closeness point (more close point - bigger probability), but that closeness isn't so much big factor as the point weight. For example, I would like to set in formula that closeness is twice lower factor then point weight.
I investigated rnd extension, but I am not sure how to append probabilities to points which I am having a lot (approximately around 250 points).
You're on the right track with the rnd extension. From that extension you need the weighted-one-of primitive and you just put the formula into the reporter block.
I think this is something like what you want. It's a complete model so you can run it and see what it does. The reporter block uses the weight and the distance in the probability. Since you want the probability to be larger for closer, then I have used the inverse of the distance, but you could simply subtract the distance from something like the maximum distance in the model. You will also need an appropriate scaling factor (replacing the 10 in my example) so that the weight is worth twice an average value of closeness.
extensions [rnd]
turtles-own [weight]
to testme
clear-all
create-turtles 10
[ setxy random-xcor random-ycor
set weight 1 + random 3
set size weight
set color blue
]
ask one-of turtles
[ set color red
let target rnd:weighted-one-of other turtles [ 2 * weight + 10 / distance myself ]
ask target [ set color yellow ]
]
end
I have a NetLogo model. each turtle has two attributes, "closeness" and "deviation_from_oracle". Now let's say there are 1000 agents in the model. The question is, how can I plot the "closeness" against "deviation_from_oracle" ?
It would also be helpful if I can get a csv file from NetLogo that has the value of closeness and deviaiton_from_oracle of all turtles after for example 1500 steps.
I definitely agree with Hugh_Kelley regarding using Behaviorspace to output your values (or custom export functions that might make for easier data cleanup if you're looking to report values for a large dynamic number of turtles- depends on your comfort with your statistical software of choice).
If you do need to plot something on the interface to show your users or something, you may find the plotxy function does what you need. For example, you'll need a plot on the interface called "plot 1" and a single blank pen in that plot called "pen-0".
You can control that plot either by manually setting up its x and y extent or by using the set-plot-... commands as in this setup:
to setup
ca
crt 10
set-current-plot "plot 1"
set-current-plot-pen "pen-0"
set-plot-pen-mode 2
set-plot-x-range 0 17
set-plot-y-range 0 25
reset-ticks
end
If you need to have a value plotted for each of your turtles, you can get the turtles to call plotxy for whatever values you're looking to plot- here I just use their absolute x coordinate and distance to the center as an example:
to go
ask turtles [
rt random 61 - 30
fd 1
set-plot-pen-color color
plotxy ( abs xcor ) distance patch 0 0
]
tick
end
This gives output like:
Where each point was plotted by an individual turtle.
If you want instead some reported mean value, have the observer call plotxy instead- another example that plots the average distance to other turtles and the average distance to center:
to go
plotxy mean-closeness-to-others mean-distance-center
ask turtles [
rt random 61 - 30
fd 1
]
tick
end
to-report mean-closeness-to-others
report mean [ mean map distance sort other turtles ] of turtles
end
to-report mean-distance-center
report mean [ distancexy 0 0 ] of turtles
end
For an output like:
In the image below, the black box represents a patch. I wish to create agents of size-
(Patch-size/8)
and distribute them at the top of the patch as such.
Also, possible to to create a code such that it takes :
Patch-Size as input and distributes them accordingly.
Previous Code Used:
My previous approach sprout at the centre and moves them to align with the desired positions but it is considerably length and not effective if I wish to variate number of agents.
`
Ok, so, forget about patch-size for a moment, as we are going to be working with unit patches, without regard to the number of pixels.
I'm assuming the number of turtles is variable, and that we want the turtles to fit exactly inside the width of the patch, and with the tops of the turtles aligned with the top edge of the patch.
If the number of turtles is constant, or the maximum number is constant, and/or the size is constant, then the code can be a little more simple, as we can avoid recalculating some things.
Anyway, for C turtles:
The gap between the turtles is 1 / C
The "half-gap" is also useful, that's gap / 2
This is also the size of the turtles
Hence, the turtles will definitely just fit across the patch
The ycor of the turtles will be pxyor + .5 - half-gap
Since gap is the size, this puts the turtles along the top
The xcor will be pxcor - .5 + gap * N - half-gap
N is the number of the current turtle. So, the xcor varies from a half-gap from the left edge to a half-gap from the right edge.
Here, pxcor - .5 shifts the turtle center all the way to the left edge
Then, + gap * N shifts the turtle over N turtle widths,
Then, - half-gap shifts the turtle back one half turtle size.
This makes sure the first and last turtles are just touching the edge.
So, let's do it:
to align-inside-at-top ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap 1 / counter ;; size of turtles, gap between turtles
let half-gap gap / 2 ;; half-size of turtles
let ytop pycor + .5 - half-gap
let xleft pxcor - .5 - half-gap
ask turtles-here
[ set size gap
set ycor ytop
set xcor xleft + gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
Hope this helps!
keep in mind no matter the patch-size patches are always 1 step across.
( pxcor - .5, pxcor + .5) X (pycor - .5, pycor +.5)
Patch-size is a sort of zoom it doesn't normally effect a simulation.
With that said and assuming you are doing this as some sort of demonstration or visualization. Anything you do for aesthetics tends to slow things down but here you go.
To line-up
Let c count turtles-here
Ask turtles-here
[
Set ycor pycor - .45
Set xcor pxcor + .45 - c / patch-size
Let c c - 1
]
End
If turtles on a patch > patch size it will mess up.