Not to use breed but deal with two types of turtles - netlogo

I have two sets of agents: retailerA and retailerB. Previously, I used breed to create them, but as I wanted to create a random network in which it was asking all the turtles and randomly choosing some to be connected, I decided it is better to keep turtles in the random network code (in the library) and do this change:
create-turtles 130 ; 100 of which are retailerA and 30 are retialerB.
Now I want each set of agents to have a different display. RetailerA will be scattered randomly while retailerB should be shown in a circle. However, the following code does not do what I want.
to A
create-turtles 100
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
end
to B
create-turtles 30 [
set retailerB? true
set shape "person"
set color gray
]
ask turtles with [retailerB?] [
layout-circle turtles with [retailerB?] (max-pxcor - 5)
]
end
This treats all the turtles to have a circle display.
Thanks,

When you create a turtles-own variable, the default value is 0. So, if you do
create-turtles 100 [
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
]
You have correctly set retailerA? to true, but since you did not set retailerB? for this agentset, they will all have a value of 0 for retailerB?. So, if you try to evaluate a true/false expression using with (eg turtles with [retailerB?]...) but some of the turtles return a value of 0 instead of either true or false, NetLogo doesn't know what to do. To fix this, you could either explicitly set those variables in the setup procedure, like this:
to spawn-a
create-turtles 100 [
set retailerA? true
set retailerB? false
set shape "person"
setxy random-xcor random-ycor
]
end
or you could explicitly say with [retailerB? = true].
Additionally, check out the syntax for layout-circle- you don't need to ask turtles with that primitive. To fix your issues, then, you'd need something like:
turtles-own [ retailerA? retailerB? ]
to setup
ca
spawn-a
spawn-b
reset-ticks
end
to spawn-a
create-turtles 100 [
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
]
end
to spawn-b
create-turtles 30 [
set retailerB? true
set shape "person"
set color gray
]
layout-circle turtles with [retailerB? = true] (max-pxcor - 5)
end

Related

I am trying to model infection between mosquito and human, when setting different states for human it works but for mosquito it does not work

I am trying to model infection between mosquito and human, when setting different states for human it works but for mosquito it does not work. this is the code I used to set mosquito agent to infected
ask one-of turtles [set infected? true]
if infected? [ set color blue]
The complete code is
breed [human humans]
breed [mosquito mosquitoes]
turtles-own
[
infected? ;; If true, the person is infected
recovered? ;; If true, the person has lived through an infection.
susceptible? ;; Tracks whether the person was initially susceptible
]
;;; SETUP PROCEDURES
to setup
clear-all
setup-human
setup-mosquito
reset-ticks
end
to setup-human
create-turtles 100
[
setxy random-xcor random-ycor
set recovered? false
set infected? false
set susceptible? true
set shape "person"
set color white
set size 1
ask one-of turtles[set infected? true]
if infected? [set color red]
]
end
to setup-mosquito
create-turtles 10
[
setxy random-xcor random-ycor
set susceptible? true
set infected? false
set shape "bug"
set color brown
set size 1
ask one-of turtles [set infected? true]
if infected? [ set color blue]
]
end
to go
ask turtles
[
set heading random 360
forward random 2
]
tick
end
The bracketing is unclear in your code, I can't see what the if statement is within. But if the goal is to do several actions when you infect the mosquito, then put them all together like:
ask one-of turtles
[ set infected? true
set color blue
]

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

How to stop NetLogo simulation

I am trying to create a simulation of Covid19 spread for months: May and June
The code I am using to create turtles and start simulation is showed below:
to gh-month
if month="May"//month is a chooser in interface tabe
[
clear-all
create-turtles 3000
[
setxy random-xcor random-ycor
set shape forma
set color blue
]
set no_infected 106
ask n-of 1 turtles with [color = blue]
[
set infectedTurtle? True
set color red
]
set percentage(no_infected / 3000)* 100
]
if month="June"
[
clear-all
create-turtles 3000
[
setxy random-xcor random-ycor
set shape forma
set color blue
]
set no_infected 2030
ask n-of 1 turtles with [color = blue]
[
set infectedTurtle? True
set color red
]
set percentage(no_infected / 3000)* 100
]
end
And setup and go commands are below
to setup
gh-month
end
to go
reset-ticks ;; netlogo time
ask turtles [
forward 0.005 ;; moving turtles command and speed
]
ask turtles with [color = red] [
ask other turtles-here [
if random 100 < percentage[set color red]
]
]
set %infected (count turtles with [color = red] / count turtles) * 100
if %infected= percentage[stop]//percentage is declared above also is a monitor in interface tab
end
My question after simulation start, why for the month' May' is stopped ate based on given condition(if %infected= percentage[stop]), and for June no, it goes up to 100% and don't stop at given condtion.
Thanks to everyone
You have a basic misunderstanding of how time operates in NetLogo so there are several problems. You have also written way too much code at once. For example, just create the turtles and get that working before having them move, and get that working before trying to have different months.
NetLogo works with a counter or clock. Each tick increments the counter. The reset-ticks is part of the initialisation because it sets the counter to 0 and makes it available. The command tick advances the clock, and is usually the last command in the procedure that contains all the things that happen in a day (or hour or whatever a tick represents). The go procedure therefore needs the command tick, not the command reset-ticks.

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.

Set Radius to find a partner turtle

I'm very new at Netlogo and programming in general.
I want to create a netlogo model, with female and male turtles. Both populations move through the world via random walk. The female population should find a partner and has the property 'radius'. Her own radius should expand if she has not found a partner until the moment she finds one. How can I program a radius around the female turtles, that expand after each time step if she has not found a partner?
Thanks for your help!
First, you need a turtle attribute that stores the value for each turtle. The way to do that is with a turtles-own statement. Then you simply change the value as required. The primitive in-radius looks at everything within the specified distance, and then you can set a condition by whether there are any? suitable mates. Your code would look something like (this is a complete model):
turtles-own
[ search-radius
mate
]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set color blue
set search-radius 1
]
reset-ticks
end
to go
check-for-mate
tick
end
to check-for-mate
ask turtles with [color = blue]
[ let candidates other turtles in-radius search-radius
ifelse any? candidates
[ set mate one-of candidates
set color red
]
[ set search-radius search-radius + 0.5 + random-float 1
]
]
end