Variables in different breeds - netlogo

i'm trying to make an archaeological model, in which hunters are making paintings in shelters, depending on their quality.
breed [shelters shelter]
breed [hunters hunter]
shelters-own [quality paintings]
The value of each shelter quality is set in the setup (with a slider for the actual number of shelters).
create-shelters number-shelters [set quality random 100]
The action of painting-or-not is then defined by random against the quality of each shelter:
to make-painting
ask shelters [
if any? hunters-on patch-here [
if random 100 < quality [set paintings paintings + 1]
]
]
end
Now, I would like to complexify it a bit more: the quality wouldn't be defined by the shelter itself (and thus be the same for every hunter), but by the hunters: each of them would attribute a different quality for each shelter. The action of painting-or-not would still be a test against random, but with this new variable defined by each individual hunter...
But I can't find a way to code it down properly.
Does anyone have a suggestion?

I think I got it right. Basically, I had every hunter to create a matrix filled with random numbers. The size is 33x33, so it goes all over the world (had to set it to start in a corner, or it gets negative coordinates).
create-hunters number-hunters [
set color white
set size 1
setxy random-xcor random-ycor
set hunter-matrix matrix:make-constant 33 33 random 10
]
ask n-of number-shelters patches [
sprout-shelters 1 [
set color one-of base-colors
set size 1
set xpatch xcor set ypatch ycor
]
Then, when they would reach a shelter, the value corresponding to that patch location would be extracted and used to paint-or-not.
ask patches [
ask hunters-here [set quality matrix:get hunter-matrix xpatch ypatch]
let paintings-here sum [paintings] of shelters-here
if any? hunters-here and any? shelters-here [
if random 10 < quality [
ask shelters-here [
set paintings paintings + 1]
]
]
]
I'm still not completely sure it actually does what I think it's doing, tho.

Related

How can I add real world location to the agent based model in Netlogo?

I have a set of positive COVID-19 cases by district in a state. I want to use agent based modelling to make the infected cases move around the state and count the number of infected, number of healthy and number of immune. I have a set of code which the number of population is random and not based on the location. Tried to watch youtube for the tutorial but could not find one which really helps. the code is as below. I want to insert the map of the state, number of population for each district and number of infected in Netlogo. The codes in Netlogo are as below.
turtles-own [illness]
to setup
clear-all
reset-ticks
create-turtles 200 [
setxy random-xcor random-ycor
set shape "person"
set color pink
set size 1.0
set illness 0
]
ask n-of starting-number-infected turtles [
set color yellow
set shape "X"
set size 1.0
]
end
to go
tick
move
infect
recover
lose-immunity
end
to move
ask turtles [
rt random 360
fd movement
]
end
to infect
ask turtles [
if color = yellow [
ask turtles in-radius infect-distance [
if random-float 200 < infect-chance [
if color = pink [
set color yellow
set shape "X"
]
]
]
]
]
end
to recover
ask turtles [
if color = yellow [
set illness illness + 1
if illness > infectious-period [
set color white
set shape "circle"
set size 1.0
set illness 0
]
]
]
end
to lose-immunity
ask turtles [
if color = white and waning-immunity < random-float 100 [
set color pink
set shape "person"
]
]
end
Our trout model provides an example of using the GIS extension to import a set of polygons that represent the places where agents live. In our case, the polygons are habitat "cells", and the agents are fish; but you could use the same approach to import districts and keep track of which people are in which district.
Our model does not represent where a fish is within a polygon, but only which polygon they are in. We use a separate breed of agent called "cells" that contain the variables of the polygon. Each NetLogo patch has a variable "patches-cell" that says which cell it belongs to.
The model is here: https://ecomodel.humboldt.edu/instream-7-and-insalmo-7
(look for inSTREAM7.3). Look at the procedure build-cells.
Steve R.

NetLogo - Breed and Characteristics

I've a little simulation where zombies and humans fight each other. The code below is the code for creating my zombies.
create-zombies 5 [
setxy random-xcor random-ycor
set color black
set size 2
set shape "person"
set zombies_speed 0.5
]
This code is the code used to turn humans into zombies. The convert-h variable is a global variable that i set it to = to convert-h-2 which is a slider that can be used to to determine the probability of humans turning into zombies.
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
set heading random 360]
die]]]
end
However when a human turns into a zombie, it doesn't take in all the characteristics of a zombie ( size 2 shape "person"). It only takes the speed, breed and color. Is there a way to add these 2 characteristics of a zombie without having to manually write it in the 2nd code snippet? I hope everything I described makes sense
You can unify your "zombie setup" logic into a procedure, then call that when they are created and when they infect/hatch:
to setup-zombie
set color black
set size 2
set shape "person"
set zombies_speed 0.5
end
...
create-zombies 5 [
setup-zombie
setxy random-xcor random-ycor
]
...
to infect
set convert-h convert-h-2
if any? humans in-radius 1 [
ask humans in-radius 1 [
if random 10 < convert-h [
hatch-zombies 1 [
setup-zombie
set heading random 360]
die]]]
end

Network modelling using Netlogo

I am new in using NetLogo, so I hope you can help me with this code.
I would like to build a network with two subnetwork, A and B, where A has N nodes and B beta*N nodes, where beta=0.5. Each network should be initialised with five fully connected nodes. I need to add a new node at a time, assuming that each has fixed out-degree k. Once a new node i comes into the network, it links to a randomly selected target node j. The other remaining k-1 nodes should be selected as following: with probability p, i should be linked to a random node of j's; with probability 1-p, i should be connected to another randomly selected node in A.
On the other hand, the nodes in B should be linked (directed link) to each node in A with probability P. P can vary from 0 to 1.
What I already tried is built the two networks with N and alpha*N nodes respectively. But, as I said, I am new in using NetLogo and I am finding many difficulties to build this network that should be really easy in a different programming language, I would be more familiar with.
; Global variables
breed [agents agent]
breed [bagents bagent]
to setup
clear-all
setup-agent
setup-bagent
end
; Defining agents
to setup-agent
set-default-shape turtles "person" ; agent shape
create-agents n-of-agents ; # of agents
[set size 2 ; agent size
set color white
setxy (random-xcor) (random-ycor)
]
; Random Network
ask agents [create-link-with one-of other agents with [not link-neighbor? myself]
ask links [set color white]
]
end
; Defining bagents
to setup-bagent
set-default-shape turtles "circle" ; bagents shape
set beta=0.5
let n-of-bagents beta*n-of-agents
create-bagents beta*n-of-agents ; # of bagents
[set size 2 ; bagent size
set color red
setxy (random-xcor) (random-ycor)
; Network
ask bagents [create-link-with one-of other bagents with [not link-neighbor? myself]
ask links [set color yellow]
]
end
to go
end
I hope you can help me to understand how to build such a network in NetLogo.
Many thanks
This does what you said. I don't think it's actually what you want as your algorithm is much better but still somewhat confused. But hopefully this gets you on the correct path.
UPDATED to make one node add each tick
globals
[ beta
prob
k
]
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
set beta 0.5
set prob 0.2
set k 3
setup-A-seed
setup-B-seed
reset-ticks
end
to go
add-A-node
if random-float 1 < beta [add-B-node]
tick
end
; Defining A seed network
to setup-A-seed
create-A-agents 5
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
]
ask A-agents
[ create-links-to other A-agents
[ set color white ]
]
end
; Defining B seed network
to setup-B-seed
create-B-agents 5
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
]
ask B-agents
[ create-links-to other B-agents
[ set color yellow ]
]
end
to add-A-node
create-A-agents 1
[ set shape "person"
set size 2
set color white
setxy random-xcor random-ycor
let target one-of other A-agents ; target is j in description
create-link-to target
repeat k - 1
[ let candidates (other [link-neighbors] of target) with [not link-neighbor? myself]
ifelse random-float 1 < prob or not any? candidates
[ create-link-to one-of other A-agents with [not link-neighbor? myself]
[ set color white ]
]
[ create-link-to one-of candidates
[ set color white ]
]
]
]
end
to add-B-node
create-B-agents 1
[ set shape "circle"
set size 2
set color red
setxy random-xcor random-ycor
let thisB self
ask A-agents
[ if random-float 1 < prob
[ create-link-from thisB
[ set color yellow
]
]
]
]
end
Some of the NetLogo issues I noticed in your code:
NetLogo does not use = for set
you must have space around mathematical operators (or NetLogo will think it's part of the name)
Some of the things you need to think about in your algorithm:
why do you have an initial B network if all Bs connect to each A with fixed probability?
what do you do if the selected A doesn't have any edges to follow?
As general advice, don't try writing something this complicated in one piece. Create your seed networks with 5 fully connected nodes. Make that work. Then do network A and make that work. Then bring in B. This iterative building is important for all programming languages. It is particularly important when using a new language so that you only have to debug one or two errors at a time and you know where the bugs are.

create turtles and move to a position in a radius of another breed agent

I would like to create some turtles in a radius of another breed agent.
I have this example code:
breed [ readers reader ]
undirected-link-breed [ rris rri ]
breed [ tags tag ]
to setup
clear-all
set xy-file "locations.txt"
setup-readers
setup-tags
end
to setup-readers
create-readers num-readers [
set shape "circle"
set color white ; means idle state 'red' is active
setxy random-xcor random-ycor
]
end
to setup-tags
create-tags tag-population [
setxy random-xcor random-ycor in-radius 6 of one-of readers
]
end
The line setxy... to place the tags in a circle of distance 6 from one of the readers it does not work, but I do not know how to fix it. I have also tested move-to primitive without good result.
What I would like is to have a population of tags in a radius of each reader and if possible select the number of tags for each reader to be different.
The sprout primitive might get you what you need- instead of creating your tags and then moving them, just have the appropriate patches spawn them directly. For example, with this setup:
breed [ readers reader ]
breed [ tags tag ]
to setup
clear-all
setup-readers
setup-tags
reset-ticks
end
to setup-readers
create-readers 5 [
set shape "circle"
set color white
setxy random-xcor random-ycor
]
end
You can make a population of patches that are within the radius of readers and have however many you like sprout a tag:
to setup-tags
let radii-patches patch-set []
ask readers [
set radii-patches ( patch-set radii-patches patches in-radius 6 )
]
; Code above defines the patch-set of patches within 6 of readers
; Then, just ask 10 of those radii patches to sprout a tag
ask n-of 10 radii-patches [
sprout-tags 1
]
end
If you are doing this on a per-reader basis and you want each to have a different number of tags, you could try something like:
to setup-tags-reader
let n-tag random 5
ask readers [
ask n-of n-tag patches in-radius 6 [
sprout-tags 1
]
]
end
but then set the n-tag value some other way (for example, from another .csv file as you seem to be setting reader location).
Side note- when you post on here try to strip out any code than cannot be directly copied and pasted into Netlogo by other users (eg the 'xy-file' line, the tag-population and num-readers variables)- it just makes things super simple!
Edit
As per your comment- try this option (using the same setup as above)
to setup-tags
create-tags 10 [
move-to one-of readers
rt random-float 360
fd random-float 6
]
end
This just creates tags, moves them to a reader, has them randomly select a direction, then has them step forward a random amount from 0 to 6.
As to how to assign patches to each reader- just make a readers-own variable then have them assign patches to that variable in their setup (eg, set my-patches patches in-radius 6). You could do a similar thing with tags to define a reader-specific set of tags (eg `set my-tags tags in-radius 6). Note that in both cases you can get overlap where two readers share patches/tags- you will have to account for that.

To build patch clusters at large spatial scales

I used the code from How to create cluster patches that do not overlap between them to build patches as shown in the first figure below.
Here is the code :
to make-cluster
loop [
let cluster [patches in-radius (2 + random-float 2)] of one-of patches
if all? (patch-set [neighbors] of cluster) [pcolor = black] [
ask cluster [ set pcolor green ]
stop ] ]
clear-all repeat 20 [ make-cluster ]
When I use this code in a large spatial extent (i.e. 1000 x 1000 patches with patch size = 1 pixel), green patches are like circles (see the second figure below).
How can I have patches as shown in the first figure ?
Thank you very much for your help.
If your goal is to simply have heterogeneous regions (rather than specifically blocky, symmetric things), you might play around with some of the answers here: Creating a random shape (blob) of a given area in NetLogo
Frank's solution and my first solution will probably run pretty slow on that large of a world. I just added a solution that should scale to a world of your size. I've put it here too for convenience:
to make-blob [ area x y ]
let blob-maker nobody
crt 1 [ set blob-maker self setxy x y ]
let border patch-set [ patch-here ] of blob-maker
repeat area [
ask blob-maker [
ask min-one-of border [ distance myself ] [
set pcolor green
set border (patch-set border neighbors4) with [ pcolor = black ]
]
rt random 360
fd .8
]
]
ask blob-maker [ die ]
end
That said, if you like the blockiness, it's often the case that models with a large number of patches in a blocky formation can be reworked into models with a smaller number of patches that behave quite similarly. For example, one strategy is to scale down the size and movements of the turtles so that the world is still relatively large to them.