Breed won't appear on interface - Netlogo - netlogo

working on a project for varsity here and we're working in Netlogo. Basically i have a function that spawns medkits every 15 ticks, yet they don't appear on the screen. The code is basically as follows:
breed[kits kit]
kits-own [qtEner]
to Go
spawn-kits
end
to spawn-kits
if (remainder ticks intervalo) = 0
[
create-kits 1
[
set nrg qtEner
set shape "medkit"
set size 1.5
set heading 0
setxy random-xcor random-ycor
output-type "Created"
]
]
end
Would appreciate some feedback. Thanks a million.

What is your setup procedure? Do you have a tick somewhere to increment your counter?
Also, I think you may want set qtEner nrg if you want the kits to have their kits-own variable independently set.
This works for me:
breed[kits kit]
kits-own [qtEner]
to setup
ca
reset-ticks
end
to Go
spawn-kits
tick
end
to spawn-kits
if (remainder ticks 10) = 0
[
create-kits 1
[
set qtEner 5
set shape "circle"
set size 1.5
set heading 0
setxy random-xcor random-ycor
print "Created"
]
]
end

Related

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.

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

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.

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.

How to check if a patch remains a state for a certain ticks\time?

Based on the “sheep eat grass” model. The patches are changing color with the agents move. I want to check if a patch has remain a centain color for a while (such as 5 ticks). If a patch has stayed in a centain color for a centain times without any change it will turn into black.
I try to use countup but it didn’t work. I need a accumulated state. Thanks a lot
If pcolor=green[
Ifelse countup>=5[
Set pcolor black
Set countup 0]
[set countup countup+1]]
Can you give a little more detail as to what is going wrong with the code you've shown? For example, with this setup:
patches-own [ time-spent-green ]
to setup
ca
crt 3
reset-ticks
end
Something very similar to your example works fine for me:
to go
ask turtles [
rt random 61 - 30
fd 1
ask patch-here [
set pcolor green
]
]
ask patches with [ pcolor = green ] [
ifelse time-spent-green >= 5 [
set pcolor black
set time-spent-green 0
] [
set time-spent-green time-spent-green + 1
]
]
tick
end
Where the patches stay green for 5 ticks then turn back to black.

How to set a default color(turtle's color when its enter in the model) of turtle after it changes its color?

I write a line of code which is this
ask turtles [if count other turtles in-radius 1 > 5 [set color white]]
now when turtle has changed color to white, but after the certain amount of time this property is not true for that same turtle, it should change its color to default color?
how can I fix it?
I think you're after a turtles-own counter that decreases whenever the condition is not satisfied. With this setup:
turtles-own [ default-color countdown ]
to setup
ca
crt 150 [
setxy random-xcor random-ycor
set default-color blue
set color default-color
]
reset-ticks
end
Now you can get your turtles to wander around and change their countdown variable whenever they change color. When that condition is not met, they can decrease the counter until it hits zero, at which point they can revert to their default color. More detail in comments:
to go
ask turtles [
rt random 60 - 30
fd 0.25
; if there are more than 5 turtles in radius 3,
; turn white and set countdown to 5
ifelse count other turtles in-radius 3 > 5 [
set color white
set countdown 5
] [
; If not, and counter is greater than 0,
; decrease the counter.
if countdown > 0 [
set countdown countdown - 1
; If counter gets down to 0,
; set color back to the default.
if countdown = 0 [
set color default-color
]
]
]
]
tick
end