Resize grid in the world based on number of turtles (input) and link specific breeds together - netlogo

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.

Related

More patches per turtle according to its size Netlogo

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.

NetLogo: use value of 'stock' in SDM as input for ABM

I made two simple models; one System Dynamics Model and one Agent Based Model in NetLogo. The SDM has a stock 'tourists' and its value depends on the in- and outflow. The value is re-calculated each tick. The tourists are sprouted in the ABM each tick. Now, I would like to use the value of the touristsStock as an input for the turtles that are sprouted each tick in the Agent Based Model. What is the best way to do this? I have already checked the example codes in the Model Library (Like the Tabonuco Yagrumo model) but it doesn't make any sense to me. What is the best way to integrate these models with each other? Thanks in advance!
The relevant piece of code of the ABM is as given below:
to go
clear-turtles
;sprouts turtles only on patches with value beach AND patches that are close to the lagoon (calculated in ArcMap)
;the initial number of tourists is multiplied by the percentage of tourists that were satisfied in the previous tick.
;percentage of tourists that were not satisfied is subtracted from 100 and divided by 100 to make it a factor between 0-1.
ask n-of (initial-number-tourists * ((100 - percent-dissatisfied) / 100)) (patches with [ beach_close_to_lagoon = 1])
[
sprout 1 [
set color black
set size 10
]
]
ask turtles [
set neighbor min-one-of other turtles [distance myself] ;; choose my nearest tourist based on distance
set distance-tourist distance neighbor ; measure/calculate distance of closest neighboring tourist
]
ask turtles
[ ifelse distance-tourist > 5
[ set satisfied? True]
[ set satisfied? False ] ]
update-turtles ;before the end of each tick, the satisfaction of each turtle is updated
update-globals ;before the end of each tick, the percentage of satisfied tourists is updated in globals
;clear-turtles ;because each tick represents one day, the tourists will leave the beach after one tick so the turtles will die
tick
end

Determining the radius of turtle clusters and number of turtles in them - postprocessing

If I have a situation in which about a 1000 black turtles disperse at random angles and steps throughout the netlogo world for a given duration of ticks. Each turtle is assigned a random probability at each timestep during dispersal, and if this number exceeds a given threshold for any given turtle it changes it's color to red and stops moving. Additionally, black turtles (still moving) that happen to move within a patch of red turtles (stopped/settled), change their color to grey and settle (stop moving) as well. Finally, other black turtles (still moving) that happen to be move within a patch of either grey or red turtles (stopped/settled), also change their color to grey and settle (stop moving)
My question is a post-processing question for when the simulation duration is reached. How do I determine the number of clusters of red-grey turtles in the sea of black turtles? Also, how do I determine the size (radial extent) of each cluster? And finally, how do I determine the number of turtles in each cluster?
Jen is right: you need a clear idea of what constitutes a cluster before being able to truly answer that question.
That being said, one possible option is to use a clustering algorithm. I'd suggest taking a look at Christopher Frantz's dbscan extension.
Here is a quickly thrown together example:
extensions [ dbscan ]
to setup
clear-all
ask patches [ set pcolor white ]
create-turtles 1000 [
set color black
set label-color blue
setxy random-xcor random-ycor
]
ask n-of 5 turtles [
ask turtles in-radius 3 [
set color one-of [red grey]
]
]
end
to find-clusters
let red-grey-turtles turtles with [ member? color [red grey] ]
let clusters dbscan:cluster-by-location red-grey-turtles 3 3
(foreach clusters range length clusters [ [c i] ->
foreach c [ t ->
ask t [ set label i ]
]
])
end
Sorry for the lack of further explanations: I have a plane to catch...

Create more than 1 turtles on patchset

How to create more than 1 turtles on patchset such that no two turtles have the same center?
Number of turtles to create defined as density.
Therefore, I require more agents per patch.
Eluciadtion: There a a set of patches in shape of box in which I wish to fill agents. Equivalent to distributing agents in a room.
This answer Distribute turtles on patches would create turtles outside the box as well.
Thanks.
Assuming patchset means all the patches.
Crt number
[Setxy random-xcor random-ycor]
Gives you a number of turtles uniformly distributed on the patches with a very small chance of having the same center. The birthday problem with floating points.
Or this if you want n turtles uniformly distributed on any set of patches P.
Repeat n [ask one-of p
[
Sprout 1
setxy (pxcor + random-float 1 - .5) (pycor + random-float 1 -.5)
]
]
Pick a random member of your set and put a turtle on a random part of that patch
If density is literally the number of turtles to create and my-patches is your patchset:
ask n-of density my-patches [ sprout 1 ]
If density is the fraction of patches that should have turtles on them (e.g. density = 0.5 would mean half the patches should have turtles):
ask n-of (density * count my-patches) my-patches [ sprout 1 ]
If density should be treated probabilistically (e.g. density = 0.5 would mean each patch has a 50% chance of having a turtle):
ask patches with [ random-float 1 < density ] [ sprout 1 ]

Distribute turtles on patches

let pop area * density
distribute-turtles
to distribute-turtles [ pop box ]
if (pop > count box) [ error "Box can't hold all turtles!" ]
ask n-of pop box [sprout-inboxturtles 1[
set color blue
set exit true ;ignore
set alpha alpha-exit ;ignore
set beta beta-exit ;ignore
set size 0.9
]
]
end
The above code distributes turtles on patches.
box-patches
pop-number of turtles
I will create turtles such in accordance to the density within the box such that:
a.) no 2 turtles have the same centre, therefore there could more than 2 turtles per patch but shouldn't have the same centre.
The turtles already have random headings so if you have them go forward a random float less than .5 they will have unique coordinates.
fd random-float .5
Aesthetically the turtle size should be small enough to see each.
Remember turtles-here will report all turtles on the patch.