NetLogo - Breed and Characteristics - netlogo

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

Related

How do you go about making a convert probability chance for an agent in netlogo?

I'm currently working on a zombie apocalypse simulation in NetLogo 2D. In my current simulation, when the zombies collide with humans the humans have a 100% chance to turn into a zombie. I would like to change this so that I can use a slider to set the conversion rate - for example if I have a rate of 50% on my slider, then when the zombies collide with humans there's a 50% chance they would turn into a zombie, otherwise kill the attacking zombie.
This is how I have currently setup my project, the way I've done this so far is to detect when they collide, and set the humans health to -1, and then made an if statement that says if the health is below 1, to make them a zombie.
I would appreciate any help in the right direction as I've spent time pondering about this and haven't come up with any solution.
breed[humans person]
breed[zombies zombie]
globals[rad]
humans-own[zombies_seen zombies_hit humans_speed per_vis_rad per_vis_ang health]
zombies-own[zombies_speed humans_around_me closest_humans]
patches-own[solid]
to setup_world
clear-all
reset-ticks
set rad 1
ask patches[
set pcolor green
]
create-humans number_of_humans [
setxy random-xcor random-ycor
set color blue
set size 5
set shape "person"
set humans_speed 1 + random-float 0.1
set health 100
adjust_vision_cone
]
ask humans[
ask patches in-radius 5[
set pcolor yellow
]
]
create-zombies number_of_zombies [
setxy random-xcor random-ycor
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
ask humans[
ask patches in-radius 5[
set pcolor green
]
]
draw_solid_patches
end
to draw_solid_patches
ask patches [
set solid false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set solid true
]
end
to detect_wall
if[solid] of patch-ahead 1 = true[
right 90
left 90
]
end
to run_model
Every 0.01 [
make_humans_move
make_zombies_move
tick
reset_patch_colour
if not any? humans [stop]
if ticks = 500000 [stop]
]
end
to make_humans_move
ask humans[
if health > 1 [
show_visualisations
right 45
left 45
let have_seen_zombies human_function
detect_wall
forward humans_speed
]
if health < 1 [
; ; ; this is where it looks at the health of the humans,
; ; ; and checks to see if they have been hit by a zombie,
; ; ; because their health would be below 1.
set breed zombies
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
]
end
to make_zombies_move
ask zombies[
detect_wall
right 45
left 45
smell_humans
detect_wall
forward zombies_speed
]
end
to smell_humans
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
detect_wall
end
to show_visualisations
if show_vis_cone = true [
ask patches in-cone per_vis_rad per_vis_ang with [pcolor = green] [
set pcolor orange
]
]
end
to reset_patch_colour
ask patches with [pcolor = orange] [
set pcolor green
]
end
to adjust_vision_cone
set per_vis_rad 10 + random 10
set per_vis_ang 90
end
to-report human_function
let seen [false]
let hit [false]
ask zombies in-cone per_vis_rad per_vis_ang [
set seen true
]
ask zombies in-radius rad [
set hit true
]
ifelse seen = true [
set zombies_seen zombies_seen + 1
right 180
][
right (random zwr - (zwr / 2))
]
if hit = true [
set zombies_hit zombies_hit + 1
set color black
set health -1
; ; ; this is where I make the human into a zombie,
; ; ; I need to have a method here that will refer to
; ; ; the convert_probability variable.
]
report seen
end
Making minimal adjustments to your code you could substitute the set health -1 to
if random 100 < conversion_probability [ set health 0 ]
where conversion_probability is a slider going from 0 to 100, as you said in the question.
I also want to point out that you could detect walls like this
to detect_wall
if [solid] of patch-ahead 2
[
face one-of patches in-radius 2 with [not solid]
]
end
doing left x and then right x only makes the turtle turn to the left and then face back where it was originally facing, unless it sees a zombie it won't change the behavior of the humans all that much.

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.

set a demand and supply curve for the model tragedy of the commons in the case of an overfished pond

I am new at net logo and I want to write a model based on tragedy of the commons in the case of an overfished pond. The purpose is to find an equilibrium between fishers and fishes based on an economic model with demand and supply. If there are less fishers, more fishes will be in the pond, then after a certain time (ticks) the number of fishers increases and less fishes will be in the pond. Maybe set like a number of fishes per day that can be fished. Thus, the solution is to find a convenient number of fishers as the fishes can still reproduce. I want to have a box in the interface where I can type in a number and see what happens with the number of fishes.
I have no idea how to set this up. I hope to hear from you :)
I started with this code:
breed [fishers fisher]
breed [fishes fish]
to setup
clear-all
reset-ticks
ask patches [set pcolor blue ] ;; lake/pond in form of a rectangle in color
ask patches [ if pxcor > 8 [ set pcolor green ]]
ask patches [ if pycor > 8 [ set pcolor green ]]
ask patches [ if pycor < -8 [ set pcolor green ]]
ask patches [ if pxcor < -8 [ set pcolor green ]]
ask one-of patches with [ pcolor = blue ] [ sprout 20 [set shape "fish" set color pink set size 1.5 ]] ;; creates fishes
ask one-of patches with [ pcolor = green ] [ sprout 2 [set shape "person" set color black set size 3 ] ] ;; creates fishers
end
to go
tick
;;fishes
ask turtles with [ shape = "fish" and color = pink ]
[ right random 360 forward 1
if [pcolor] of patch-ahead 1 = green [ right 180 fd 1 ]]
;; fishers
ask turtles with [ shape = "person" and color = black]
[;right random 360 forward 1
if any? patches with [pcolor = blue]
[set heading towards one-of patches with [pcolor = blue] forward 1]
if [pcolor] of patch-ahead 1 = blue [ right 180 fd 2 ]]
ask turtles with [shape = "person" and color = black]
[if any? turtles with [shape = "fish" and color = pink] in-radius 2
[ask turtles with [shape = "fish" and color = pink] in-radius 2 [die]]]
end
Firstly, I suggest you look through existing models in the Netlogo library (Wolf-sheep-predation model may help). You roughly have the right idea in your current code, but you should look at other models to improve. You've already set your different breeds of turtles, but you should also set up their respective shapes under 'setup'. This would help you a great deal later - instead of calling for
ask turtles with [ shape = "fish"...]
you can simply
ask fishes [do sth...]
For that 'box at the interface', you can have a slider at the interface determining the number of fishers you want your run to start with. With another slider, you can set the fishing pressure in your simulated run (i.e. how many fish each fisher will catch) and I suppose you can also consider how this changes when population of fish decreases.
Finally, for a model like yours, you can observe the supply and demand trend by plotting the curves of no. of fishers over time and no. of fishes over time. Again, look at the wolf-sheep-predation model to have an idea of how to do this.
I can't give you more than this I'm afraid since I'm no pro myself but hope this helps a little. Hope someone else would be able to give you a clearer idea.

Variables in different breeds

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.