how can i make a precise procedure every 30 ticks?I have written this procedure in "to go".
i tried to write like this:
'to go
if count population with [underPovertyLine] = count population or
count firms = 0 [stop]
ask population [
set min-indiv-cost-of-living runresult [max-indiv-cost-of-living]
]
if ticks mod 30 = 0 [
ask population with [employed = false and color = grey] [
set mymoneyOnBankOfGrey runresult [moneyOnBankOfGrey - min-indiv-
cost-of-living]
if mymoneyOnBankOfGrey < 0 [die]
]
ask population with [employed = false and color = blue ] [
set mymoneyOnBankOfBlue runresult[ moneyOnBankOfBlue - min-indiv-cost-
of-living]
if mymoneyOnBankOfBlue < 0 [die]
]
ask population with [employed = false and color = red] [
set mymoneyOnBankOfRed runresult [ moneyOnBankOfRed - min-indiv-cost-
of-living]
if mymoneyOnBankOfRed < 0 [die]
]
ask population with [employed = true and color = grey] [
set mymoneyOnBankOfGrey runresult[ moneyOnBankOfGrey + wage - min-
indiv-cost-of-living]
if mymoneyOnBankOfGrey < 0 [die]
]
ask population with [employed = true and color = blue] [
set mymoneyOnBankOfBlue runresult [ moneyOnBankOfBlue + wage - min-
indiv-cost-of-living]
if mymoneyOnBankOfBlue < 0 [die]
]
ask population with [employed = true and color = red ] [
set mymoneyOnBankOfRed runresult [ moneyOnBankOfRed + wage - min-indiv-
cost-of-living]
if mymoneyOnBankOfRed < 0 [die]
]
]
tick
end'
I would like the procedure to take place every 30 ticks but it happens only 1 time at the 30th tick and it stops and it does not update. how can i fix it?? thanks!!
Related
I'm trying to iterate over agents to change them randomly.
Basically there ar "n" number of coins (n is defined by a slider that creates those n number of agents)
The function to change them that I'm trying is the following
to tossing
if any? coins with [color = white] [
ask coins [
let coin-toss random-float 1.0
if coin-toss > 0.5 [
print "hi"
;set shape "coin tails"
set color lime
set flip 1
]
if coin-toss < 0.5 [
set color pink
set flip 0
]
]
]
if not any? coins with [color = white] [
ask coins [
set color white
set flip 3
]
]
end
This doesn't even change the colors, and I'm sure it won't change each coin differently.
I've been searching how to approach this but can't find a thing.
Edited solution and adding previous procedure:
to go
ifelse (any? turtles with [color = red]) [
print "red"
restart
;ask coins [ tossing ]
]
[
ask one-of cryptographers [ update ]
wait 0.6
ask coins [ tossing ]
;recolor
]
tick
end
to tossing
if any? coins with [shape = "circle"] [
ask coins [
;let a random-float 1
ifelse (flip = 3) and (random-float 1 > 0.5) [
;print "hi"
set shape "coin tails"
set color lime
set flip 1
]
[
set shape "coin heads"
set flip 0
set color pink
]
wait 0.2
]
]
end
I'm currently working through the telemarketer IBM in Grimm & Railsback's book. I'm sure it's something really obvious, but I can't figure out why I get the error:
this code can't be run by a patch
error while patch -38 75 running IF
called by procedure MAKE-CALLS
called by procedure GO
called by Button 'step'
This is the problematic code (specifically, "if pcolor = black").
to make-calls
ask turtles [
let territory ( 10 * sqrt size )
let max-calls floor ( 100 * size )
let potential-customers patches in-radius territory
set successful-sales 0
ifelse count potential-customers <= max-calls
[
ask potential-customers[ ;call all customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
[
ask n-of max-calls potential-customers[ ;call max-calls customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
set total-sales total-sales + successful-sales
]
end
I want to check whether the patches within the 'territory' of the turtles (the 'potential customers') are coloured black, but the turtles (telemarketers) can only make a certain number of calls. So if the number of patches in their territory exceeds max-calls, I check the colour of a number of patches within the territory equal to max-calls.
Any help would be appreciated :-)
FULL CODE:
globals[
sim-length
money-size-ratio
total-sales
]
patches-own[
;potential customers coloured black, unavailable customers coloured red
]
turtles-own[
;telemarketers
funds
successful-sales
]
to setup
ca
set sim-length 200
set money-size-ratio 0.001
set total-sales 0
crt initial-num-marketers [
set size 1.0
set funds 0.0
set successful-sales 0
setxy random-xcor random-ycor
set shape "circle"
]
ask patches [ set pcolor black ]
end
to go
reset-phones
make-calls
do-accounting
update-observer
tick
if ticks = sim-length [stop]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to reset-phones
ask patches [ set pcolor black ]
end
to make-calls
ask turtles [
let territory ( 10 * sqrt size )
let max-calls floor ( 100 * size )
let potential-customers patches in-radius territory
set successful-sales 0
ifelse count potential-customers <= max-calls
[
ask potential-customers[ ;call all customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
[
ask n-of max-calls potential-customers[ ;call max-calls customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
set total-sales total-sales + successful-sales
]
end
to do-accounting
ask turtles [
let costs ( size * 50 )
let income successful-sales * 2
set funds funds + income - costs
if funds > growth-param
[
let growth floor ( funds - growth-param )
set size size + ( size * growth * money-size-ratio )
]
if funds < 0 [ die ]
]
end
to update-observer
set-current-plot "number of businesses"
plot count turtles
set-current-plot "business size distribution"
histogram [size] of turtles
set-current-plot "total sales"
plot total-sales
end
The problem is successful-sales: it is a turtle attribute, but you are asking patches to set it. Change it everywhere to _sales, then change set _sales 0 to let _sales 0. This introduces a new local variable. Now your code should work. However, you are no longer using the successful-sales attribute of turtles. Get rid of it. If you cannot get rid of it for some reason, you can set it to _sales right before you update total-sales.
Hi I am writing simulation which uses ticks to represent time in simulation environment . Is it possible to display tick counter in monitor GUI? Also I have tried to input the code but it is not working.
My sample code:
if [agents count = 0]
show "count ticks"
This should show tick exactly value when agent unit value is zero. For example if agent = 0 at tick 200 it should display 200 on monitor even the whole simulation run on 500 ticks.
whole code is
patches-own[
nest?
nest-scent
food
food-source-number
chemical
]
breed [agents agent] ;agent is ant
breed [antiagents a-antiagent] ;antiagent spider
antiagents-own[energy]
agents-own[venom]
;;;;;;;;;;;;;;;;;;;;;;;;;; Setup ;;;;;;;;;;;;;;;;;;;
to setup
clear-all
set-default-shape agents "ant"
create-agents initial-ants ;; create the ants, then initialize their variables
[
set color orange
set size 2 ;; easier to see
set venom (ant-venom-strength)
]
set-default-shape antiagents "spider"
create-antiagents initial-spiders ;; create the spider, then initialize their variables
[
set color yellow
set size 3 ;; easier to see
set energy (spider-health)
setxy random-xcor random-ycor
]
setup-patch
display-labels
reset-ticks
end
;;;;; nest food patch;;;
to setup-patch
ask patches
[ setup-nest
setup-food
recolor-patch ]
end
;;;;; setup nest
to setup-nest ;; patch procedure
;; set nest? variable to true inside the nest, false elsewhere
set nest? (distancexy 0 0) < 5
;; spread a nest-scent over the whole world -- stronger near the nest
set nest-scent 200 - distancexy 0 0
end
to setup-food ;; patch procedure
;; setup food source one on the right
if (distancexy (0.6 * max-pxcor) 0) < 5
[ set food-source-number 1 ]
;; setup food source two on the lower-left
if (distancexy (-0.6 * max-pxcor) (-0.6 * max-pycor)) < 5
[ set food-source-number 2 ]
;; setup food source three on the upper-left
if (distancexy (-0.8 * max-pxcor) (0.8 * max-pycor)) < 5
[ set food-source-number 3 ]
;; set "food" at sources to either 1 or 2, randomly
if food-source-number > 0
[ set food one-of [1 2] ]
end
to recolor-patch ;; patch procedure
;; give color to nest and food sources
ifelse nest?
[ set pcolor violet ]
[ ifelse food > 0
[ if food-source-number = 1 [ set pcolor cyan ]
if food-source-number = 2 [ set pcolor sky ]
if food-source-number = 3 [ set pcolor blue ]
if food-source-number = 4 [ set pcolor brown ] ]
;; scale color to show chemical concentration
[ set pcolor scale-color green chemical 0.1 5 ] ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;; GO ;;;;;;;;;;;;;;;;;;;;;;
to go ;; forever button
;;;;;;;;;;;;Spider ;;;;;;;;
ask antiagents [
setup-spider-movemonet ; option for user to select spider movement
;move-spider
catch-ant
spider-death
]
;;;;;;;;;; Ant ;;;;;;;;;;;;
ask agents
[ if who >= ticks [ stop ] ;; delay initial departure
ifelse color = orange
[ look-for-food ] ;; not carrying food? look for it
[ return-to-nest ] ;; carrying food? take it back to nest
wiggle
fd 1 ]
diffuse chemical (diffusion-rate / 100)
ask patches
[ set chemical chemical * (100 - evaporation-rate) / 100 ;; slowly evaporate chemical
recolor-patch ]
tick
display-labels
;;;;;;;;;;; Stop function ;;;;;;;;;;
if count agents = 0 [stop]
if count patches with [pcolor = blue] = 0 [stop]
end
;;;;;;;;;;;;;;;;;;;;;;;;;; function in go ;;;;;;;;;;;;;;;
to look-for-food ;; turtle procedure
if food > 0
[ set color green + 1 ;; pick up food
set food food - 1 ;; and reduce the food source
rt 180 ;; and turn around
stop ]
;; go in the direction where the chemical smell is strongest
if (chemical >= 0.05) and (chemical < 2)
[ uphill-chemical ]
end
;;;;;;;;;;; ant function ;;;;;;;;;;
;; sniff left and right, and go where the strongest smell is
to uphill-chemical ;; turtle procedure
let scent-ahead chemical-scent-at-angle 0
let scent-right chemical-scent-at-angle 45
let scent-left chemical-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to return-to-nest
ifelse nest?
[ ;; drop food and head out again
set color orange
rt 180 ]
[ set chemical chemical + 60 ;; drop some chemical
uphill-nest-scent ] ;; head toward the greatest value of nest-scent
end
to wiggle ;; turtle procedure
rt random 40
lt random 40
if not can-move? 1 [ rt 180 ]
end
to uphill-nest-scent
let scent-ahead nest-scent-at-angle 0
let scent-right nest-scent-at-angle 45
let scent-left nest-scent-at-angle -45
if (scent-right > scent-ahead) or (scent-left > scent-ahead)
[ ifelse scent-right > scent-left
[ rt 45 ]
[ lt 45 ] ]
end
to-report chemical-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [chemical] of p
end
to-report nest-scent-at-angle [angle]
let p patch-right-and-ahead angle 1
if p = nobody [ report 0 ]
report [nest-scent] of p
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;; Spider-Function ;;;;;;;;;;;;;;;;;;;;;;
to move-spider
rt random 360
lt random 360
fd 3
end
to spider-death ;; turtle procedure
if energy <= 0 [ask antiagents-here [die]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to catch-ant
let prey one-of agents-here
if prey != nobody
[ ask prey [ die ]
set energy (energy - ant-venom-strength)]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; GUI Function ;;;;
to display-labels
ask antiagents [ set label "" ]
if show-spider-health [
ask antiagents [ set label round energy ]
]
end
;;;;;;; Spider movement GUI ;;;;;;
to setup-spider-movemonet
if setup-spider-movement
[
ask antiagents [ move-spider ]
]
end
To make a monitor that displays the tick count, the complete code to put in the monitor is:
ticks
That's all.
If you want the monitor to show the tick count when there are no agents, but be blank otherwise, you would put this code in:
ifelse-value any? agents
[ "" ]
[ ticks ]
Your syntax is incorrect. You could however include in an observer procedure (e.g., your go procedure) the following: if (count agents = 0) [show ticks]. This would display in the area above the command line.
However it sounds like you are looking for user-message, rather than show. E.g., if (count agents = 0) [user-message (word ticks)]. http://ccl.northwestern.edu/netlogo/docs/dict/user-message.html
If you want to update the monitor each tick but only when there are agents, you need to create a separate global variable (called counter below) and update that instead. The ticks internal variable will always increase so monitoring it will always increment. This is because the ticks variable is the simulation clock, it reports the passage of time.
globals [counter]
to setup
...
set counter 0 ; note, not required as initialise to 0 but helps readability
reset-ticks
end
to go
...
if any? turtles [ set counter counter + 1 ]
...
tick
end
Then create a monitor with just counter (and 0 decimal places etc)
i would like to simulate the disease spread using agent base model by using NetLogo software.
how to Update Birth and death rates in an SIR model.
As a starting point you could try the http://ccl.northwestern.edu/netlogo/models/SimpleBirthRates
that has a method reproduce which adjusts the fertility of the different populations
to reproduce
ask turtles
[
ifelse color = red
[
set fertility floor red-fertility
set fertility-remainder red-fertility - (floor red-fertility)
]
[
set fertility floor blue-fertility
set fertility-remainder blue-fertility - (floor blue-fertility)
]
ifelse (random-float 100) < (100 * fertility-remainder)
[ hatch fertility + 1 [ wander ]]
[ hatch fertility [ wander ]]
]
end
For the SIR model see https://en.wikipedia.org/wiki/SIR_model#The_SIR_model
You probably want three different populations blue(S susceptible), red(I infectious), and green(R recovered). You want to change the code so that you change beta I S blues into reds and change nu I reds into greens. beta and nu are model parameters. It might be easier to start by killing and hatching equal numbers of blues and reds.
The following code implements this. I have three different sets of turtles, initially many more blue than the other colours. The main part happens in the reproduce which turns blues to reds:
ifelse color = blue
[
if (random-float 100) < (beta * red-count * blue-count ) / 1000000
[set color red]
]
and reds to greens
ifelse color = red
[
if (random-float 100) < (nu * red-count ) / 10000
[set color green]
]
The full code is. You need to add sliders for beta and nu, add a line in the graph for green-count and a monitor for green-count. The numbers have been chosen by guess work which seem to show a good effect.
globals
[
red-count ; population of red turtles
blue-count ; population of blue turtles
green-count ; population of green turtles
]
turtles-own
[
]
to setup
clear-output
setup-experiment
end
to setup-experiment
cp ct
clear-all-plots
reset-ticks
crt carrying-capacity
[
setxy random-xcor random-ycor ; randomize turtle locations
ifelse who < (carrying-capacity / 10) ; start out with equal numbers of reds and blues
[ set color red ]
[
ifelse who < (2 * carrying-capacity / 10) ; start out with equal numbers of reds and blues
[ set color green ]
[ set color blue ]
]
set size 2 ; easier to see
]
reset-ticks
end
to go
reproduce
tick
end
to reproduce
ask turtles
[
ifelse color = blue
[
if (random-float 100) < (beta * red-count * blue-count ) / 1000000
[set color red]
]
[
ifelse color = red
[
if (random-float 100) < (nu * red-count ) / 10000
[set color green]
]
[
]
]
]
end
I am working on a project in netLogo in which i have a random network in which each and every link is assigned a bandwidth. The algorithm chooses a random Source and Destination by itself and after which it has to choose an optimal path between these two.
My question is how to choose the optimal path by exploring all the possible paths.
hereby attaching the sourcecode of my model:
breed[nodes node]
breed[ants ant ]
globals [nodename nodenumbersource nodenumberdestination relaynode dead-network num ]
nodes-own[visited ]
links-own[visit bandwidth]
ants-own
[
distance-gone
distance-to-go
target-node
current-node
]
to cr11
ask nodes with [label = "Source"]
[
set num count (link-neighbors)
]
create-ants num ;num-ants
[
let n one-of nodes with [label = "Source"]
setxy ([xcor] of n) ([ycor]of n)
set current-node n
set color white set size 0.95
set distance-gone 0
set distance-to-go 0
set target-node one-of nodes with [ label = "Relay Node" ] ;nobody
]
end
to face-targets
ask ants ;with [ target-node]; = node 4 ] ;nobody ]
[
let d 0
face (one-of nodes with [ label = "Relay Node" ]);target-node
ask current-node [
set d distance (one-of nodes with [ label = "Relay Node" ]);target-node)
]
set distance-to-go d
]
end
to move-forward
face-targets
ask ants [
while [ distance-gone < (distance-to-go )]
[
fd 1
set distance-gone (distance-gone + 1)
]
]
ask ants [
if distance-gone < distance-to-go
[
set current-node target-node
setxy ([xcor] of current-node) ([ycor] of current-node)
set distance-gone 0
set distance-to-go 0
]
]
end
;This is used to design the Network
to setup
setup1
setup-spatially-clustered-network
ask links [set color white
set visit false
set bandwidth (5 + random 10) ;min bw 5 max 15
]
end
to setup1
__clear-all-and-reset-ticks
set dead-network 0
create-nodes number-of-nodes
[
setxy (random-xcor * 0.95) (random-ycor * 0.95)
set shape "circle"
set color green
set visited false
set label who
]
end
;Links are created for the nodes
to setup-spatially-clustered-network
let num-links (6 * number-of-nodes) / 2
while [count links < num-links ]
[
ask one-of turtles
[
let choice (min-one-of (other turtles with [not link-neighbor? myself])
[distance myself])
if choice != nobody [ create-link-with choice
]
]
]
repeat 10
[
layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1
]
end
;This is to Generate Message for nodes
to test1
ask one-of nodes
[
set color red
set label "Source"
set nodenumbersource who
]
ask one-of nodes with [color = green]
[
set color red
set label "Destination"
set nodenumberdestination who
]
cr11
end
to test3
ask turtles with [label = "Source"]
[
set label "ants moving"
ask my-links
[
set color green
]
ask link-neighbors
[
set color blue
]
ask min-one-of turtles with [color = blue and my-links] [distance turtle nodenumberdestination ]
[
ask max-one-of my-links [bandwidth ]
[
set color red
]
set color white
set relaynode who
set label "Relay Node"
]
; face-targets
move-forward
end
to test4
ask turtle nodenumberdestination
[
while [color != white]
[
ask turtle relaynode
[
set label ""
ask my-links
[
set color green
]
ask link-neighbors
[
set color yellow
]
]
ask turtles with [color = yellow]
[
set color violet
]
ask turtles with [color = violet] with [visited = false]
[
set color magenta
]
ask min-one-of turtles with [color = magenta] [distance turtle nodenumberdestination]
[
set color white
set relaynode who
set label "Relay Node"
set visited true
]
move-forward
]
]
end
to test6
ask nodes ; turtles
[
set color green
set visited false
set label ""
]
ask links
[
set color white
set visit false
]
end
to test5
test1
test3
test4
end
You can use the NW-Extension for that! Just download it and stick it in NetLogo's extensions folder. Then, in your model, you can start using by adding
extensions [ nw ]
to the top of your code. Then you can use one of the weighted-path-to primitives to get the turtles or links along the shortest path between two nodes.
Note that the nw extension is under active development, though it is well tested. You also must be using the latest version of NetLogo.
If you want to implement it yourself, Dijkstra's algorithm is the classic solution if you have nonnegative weights in your graph.