NetLogo (social network): How do you connect nodes based on probability? - netlogo

I am trying to model an "online forum" where the model starts with 2 connected agents that are of 2 breeds (A-agents and B-agents).
With each iteration, an agent of one of the 2 breeds is created. Which breed of node is created; A-agent vs. B-agent is determined by a probability (see code).
The newly created agent (1 x A-agents or B-agents) then connects to either one of A-agents or B-agents based on a probability.
How do I make the newly created agent attach to one of the 2 breeds of agents based on probability of selection?
If newly created node is A-agents = 58% chance of connecting to one of A-agents and 42% to one of B-agents?
If newly created node is B-agents = 56% probability of connecting to an A-agent and 44% probability of connecting to B-agents
Here is my code so far, up to point 1.
breed [A-agents A-agent]
breed [B-agents B-agent]
to setup
clear-all
reset-ticks
create-A-agents 1
[ set shape "triangle"
set size 1
set color blue
setxy random-xcor random-ycor
]
create-B-agents 1
[ set shape "circle"
set size 1
set color red
setxy random-xcor random-ycor
]
ask B-agents [create-links-with A-agents [set color green]]
tick
end
to go ;; create a new node based on the emprical user distribution of A-agents/B-agents
let p random-float 100 ;; create a random number between 1-100
if (p >= 97) [create-A-agents 1
[ set shape "triangle"
set size 1
set color blue
setxy random-xcor random-ycor]]
if (p < 97) [create-B-agents 1
[ set shape "circle"
set size 1
set color red
setxy random-xcor random-ycor
]]
tick
end

you need something like this (for A-agents, you can write a similar for B-agents) - not tested and probably has syntax errors.
let test-num random-float 1
ifelse test-num < 0.58
[ create-link-with one-of other A-agents ]
[ create-link-with one-of B-agents ]
I haven't included any testing for whether there are actually any agents to connect to, but this should get you going in the right direction.

Related

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

calculate steps of turtles among neighbors

In the world, the two types of agents, individuals and messages, are randomly positioned in the two-dimensional attitudinal space.
If an individual believes the messages, he or she creates links with the messages.
Then, an individual adjusts his or her attitudinal position based on the calculation of distances from oneself to the messages.
Here, I have a problem.
I would like to make an individual move twice more when exposed to a message with a high value than when exposed to one with a low value.
But what I can do right now is just averaging the distance among the messages.
Here's what I've done so far
breed [individuals individual]
breed [messages message]
messages-own [value]
undirected-link-breed [messagelinks messagelink]
to setup
ca
create-individuals 100 [initiate-individuals]
reset-ticks
end
to go
new-messages
end
to initiate-messages
ifelse random-float 1 < value-p [ set value "H" ] [set value "L"]
end
to initiate-individuals
setxy random-xcor random-ycor
end
to new-messages
create-messages 30 [
initiate-messages
ask individuals [
integrate-messages myself
]
]
end
to integrate-messages [newmessages]
if random-float 1 < 0.3
[create-messagelink-with newmessages
setxy mean [xcor] of messagelink-neighbors mean [ycor] of messagelink-neighbors]
end
I am making the assumption that integrate-messages is intended to handle a set of messages, even though when a receiver executes it in your code it is only the newly created message itself, not all newly created messages. There are likely several ways to handle the problem of giving "H" messages more influence than "L" messages, but perhaps the most straightforward to simply to use a weighted average of the messages' xcors and ycors. I've written a short model that does that in integrate-messages.
breed [messages message]
breed [receivers receiver]
undirected-link-breed [messagelinks messagelink]
messages-own [value]
to setup
clear-all
create-receivers 1 [
setxy random-xcor random-ycor
set color green
]
create-messages 5 [
setxy random-xcor random-ycor
set value ifelse-value (random-float 1 < 0.5) ["H"] ["L"]
set color blue
set label value
]
reset-ticks
end
to go
ask one-of receivers [
integrate-messages n-of 3 messages
]
end
to integrate-messages [newmessages]
;to indicate which messages are being received.
ask newmessages [set color red ]
create-messagelinks-with newmessages [set color red]
;now calculate the weighted position relative to ALL linked messages.
let mssgs [other-end] of my-messagelinks
let wghts map [x -> ifelse-value ([value] of x = "H") [2] [1]] mssgs
let xc sum (map [[m w] -> w * [xcor] of m ] mssgs wghts)
let yc sum (map [[m w] -> w * [ycor] of m ] mssgs wghts)
set xcor xc / sum wghts
set ycor yc / sum wghts
;to show where the receiver ends up.
set color red
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