How to visualize two (or more) subnetworks in Netlogo using circle layout - netlogo

I would like to know if it could be possible to visualize two distinct subnetworks (e.g., made up by turtles white and another one made up by turtles orange) with some links between them.
Visually, they should look like two networks having layout circle (layout-circle), for instance.
Currently I can visualize only one network made up by turtles orange and white (to give you an idea: https://www.dreamstime.com/royalty-free-stock-photos-people-join-merge-social-two-circles-as-network-business-groups-image30895498).
Any help would be great.
Thanks.
An example of what I am trying to do is shown below.
to WS [N k p]
create-types1 N [
set color white
setxy random-xcor random-ycor
]
create-types2 N [
set color orange
setxy random-xcor random-ycor
]
layout-circle sort types1 max-pycor * 0.9
let lis (n-values (k / 2) [ [i] -> i + 1 ])
ask types1 [
let w who
foreach lis [ [i] -> create-link-with (type1 ((w + i) mod N)) ]
]
rewire p
end
to rewire [p]
ask links [
let rewired? false
if (random-float 1) < p
[
let node1 end1
if [ count link-neighbors ] of node1 < (count types1 - 1)
[
let node2 one-of types1 with [ (self != node1) and (not link-neighbor? node1) ]
ask node1 [ create-link-with node2 [ set rewired? true ] ]
]
]
if (rewired?)
[
die
]
]
end
The code above should create a small world network where there are two types of breeds: type1 and type2, distinguished by the color.
Instead of visualizing the types within the same network, I would like to create one small world network for type1 and another one for type2. These two networks would be linked by a few links that there might exist between the two types.

Are you looking for something along these lines?
I create both circles independently and then move them to their desired position, as well as creating the links separately for both types.
breed [types1 type1]
breed [types2 type2]
to WS [N k p]
create-types1 N [
set color white
]
create-types2 N [
set color orange
]
layout-circle sort types1 max-pycor * 0.5
ask types1 [setxy xcor - 5.5 ycor]
layout-circle sort types2 max-pycor * 0.5
ask types2 [setxy xcor + 5.5 ycor]
let lis (n-values (k / 2) [ [i] -> i + 1 ])
ask types1 [
let w who
foreach lis [ [i] -> create-link-with (type1 ((w + i) mod N)) ]
]
;let links1 link-set [my-links] of types1
ask types2 [
let w who
foreach lis [ [i] -> create-link-with (type2 ((w + i) mod N + N)) ]
]
;let links2 link-set [my-links] of types2
end

Related

Patch network to "connect the dots" and assess overall area belonging to an agent's "home range"

I have agents moving through a landscape with varying resources. Say my agent begins in the center, and then moves around the landscape in a curved line. I would like to somehow make a patchset or other grouping of ALL the patches that encompass the area within the patches through which the agent moved, simulating a home range calculation. I haven't been able to think conceptually of how to do this so I have no code to show, but any help is much appreciated.
The most efficient way is probably just to keep track of the min/max of the coordinates of the patches visited by turtles. Here is a simplistic example:
turtles-own [ min-x max-x min-y max-y ]
to setup
clear-all
create-turtles 1
reset-ticks
end
to go
ask patches [ set pcolor black ]
ask turtles [
rt random 30 lt random 30 fd 1 ; move randomly
update-min-max
ask home-range [ set pcolor [ color ] of myself - 2 ]
]
tick
end
to update-min-max ; turtle command
set min-x min (list min-x pxcor)
set min-y min (list min-y pycor)
set max-x max (list max-x pxcor)
set max-y max (list max-y pycor)
end
to-report home-range ; turtle reporter
report patches with [
pxcor >= [ min-x ] of myself and
pxcor <= [ max-x ] of myself and
pycor >= [ min-y ] of myself and
pycor <= [ max-y ] of myself
]
end
This assumes that the world doesn't wrap.

NetLogo nw extension: how to use nw:extension for multiple destination

Hi guys is it possible for netlogo nw:extension to calculate path for multiple destination.
I wanted my source 0 to pass by all the red nodes destination.
I've attempt by first putting the node-links of to all destination is a list. Then from there i take the minimum number of node-links as my first path and then put the nodes(turtle) and node-link to visited so it doesn't check the node and it's link again. Eg (node-link 0 4) (node-link 0 8), then add the links and the destination node 8 to visited. I do not know how to check that the node 8 is selected.
Any idea??
to setup
ca
crt Nodes
set-default-shape turtles "circle"
let positions [
[-7 7] [-1 7] [5 7] [11 7] [-7 1] [-1 1] [5 1] [11 1] [-7 -5] [-1 -5] [5 -5] [11 -5]
[-7 -11] [-1 -11] [5 -11] [11 -11]
]
foreach sort turtles [
nodePos -> ask nodePos [
setxy (first first positions) (last first positions)
set positions but-first positions
]
]
ask turtles [;setxy random-xcor random-ycor
if Show_Names? = True [show-names]]
;ask patches [set pcolor white]
end
to create-random-graph
ask links [die]
ask turtles [
set color blue
let neighbor-nodes other turtles in-radius 6
create-node-links-with neighbor-nodes [
set weight 1
set label weight
set color grey
set thickness 0.1
]
]
to TEST
let FDestin[ 9 6 8]
let Origin 0
let a 0
let b []
let i 0
while [a < length(FDestin) ][
let Destin item a FDestin
ask turtle Origin [
set path nw:weighted-path-to turtle Destin weight
set b lput(path ) b
]
set a a + 1
]
let findMinPath sort-by [ [list1 list2] -> length(list1) < length (list2) ]b
let findMin []
set findMin lput item 0 findMinPath findMin
;foreach findMin [ x -> ask one-of node-links x [die]]
end
This is sort of rough but may get you started. With these extensions and setup:
extensions [ nw ]
undirected-link-breed [ node-links node-link ]
breed [ nodes node ]
breed [ walkers walker ]
turtles-own [ path target-nodes ]
links-own [ weight ]
to setup
ca
set-default-shape nodes "circle"
set-default-shape walkers "arrow"
let vals ( range 11 -11 -5 )
foreach vals [ y ->
foreach reverse vals [ x ->
ask patch x y [
sprout-nodes 1 [
set color blue
set label who
set size 2
]
]
]
]
create-network
ask one-of nodes [
hatch-walkers 1 [
set color green
set pen-size 5
pd
set target-nodes nobody
set path []
]
ask n-of 3 other nodes [ set color red ]
]
reset-ticks
end
That creates a grid of nodes, as well as a single walker randomly placed on one of the nodes. Three of the nodes without a walker are red to act as 'target' nodes in the path. Then, your network procedure as in your question:
to create-network
ask links [die]
ask nodes [
set color blue
let neighbor-nodes other turtles in-radius 5
create-node-links-with neighbor-nodes [
set weight one-of [ 1 2 3 ]
set label weight
set color grey
set thickness 0.1
]
]
end
That gives you a randomly weighted network of links for the walker to follow.
Now, to build paths, get the walkers to recognize the red nodes as possible targets. Then, generate all possible path permutations, always starting at the node that the walker is on.
Permutations are generated using code modified from this answer
to-report path-permutations [ node-list ] ;Return all permutations of `lst`
let n length node-list
if (n = 0) [report node-list]
if (n = 1) [report (list node-list)]
if (n = 2) [report (list node-list reverse node-list)]
let result []
let idxs range n
foreach idxs [? ->
let xi item ? node-list
foreach (path-permutations remove-item ? node-list) [?? ->
set result lput (fput xi ??) result
]
]
report result
end
Edit: instead of fewest turtles en route, turtles now select the route with the smallest weighted distance.
Count the number of turtles of each possible path, and select the path with the smallest weighted distance over the entire route.
to set-path
if target-nodes = nobody [
; Designate any red nodes as targets
set target-nodes nodes with [ color = red ]
let start-node one-of nodes-here
; Get a list of nodes
let target-node-list sort target-nodes
; Build all possible paths
let possible-paths map [ i -> sentence start-node i ] path-permutations target-node-list
; Get the weighted distance turtles for each possible path
let path-turtles map [ i -> turtles-on-path i ] possible-paths
; Keep the path with the smallest overall weighted distance
let shortest-path reduce [
[ shortest next ] ->
ifelse-value ( weighted-dist-of-path shortest < weighted-dist-of-path next ) [ shortest ] [ next ] ] path-turtles
set path shortest-path
]
end
set-path uses these two reporters:
to-report turtles-on-path [ in-path ]
; A reporter that returns the path from the start node of a given path
; to the final node of that path.
let temp-path []
( foreach ( but-last in-path ) ( but-first in-path ) [
[ from to_ ] ->
ask from [
ifelse length temp-path = 0 [
set temp-path nw:turtles-on-weighted-path-to to_ weight
] [
set temp-path sentence temp-path but-first nw:turtles-on-weighted-path-to to_ weight
]
]
] )
report temp-path
end
to-report weighted-dist-of-path [ in-path ]
let weighted-dist 0
( foreach ( but-last in-path ) ( but-first in-path ) [
[ f t ] ->
ask f [
set weighted-dist weighted-dist + nw:weighted-distance-to t weight
]
] )
report weighted-dist
end
Once the turtle knows what path it should take, it can follow that path somehow- here is a simple example.
to follow-path
if length path > 0 [
let target first path
face target
ifelse distance target > 0.5 [
fd 0.5
] [
move-to target
ask target [
set color yellow
]
set path but-first path
]
]
end
All that is wrapped up in go like so:
to go
if not any? nodes with [ color = red ] [
stop
]
ask walkers [
set-path
follow-path
]
tick
end
To give behavior something like:
Edit:
The much-simpler option is to just have the walker check the nearest (by weight) target node, build the path, follow that path, then select the next nearest target once it reaches the end of that path (and so on). However, that may not give the overall shortest path- for example, look at the image below:
The green trace is the path taken by the path-permutations walker. The blue square indicates the starting node, the orange squares designate the target nodes. The orange trace is the one taken by the simpler walker (as described above). You can see that overall, the path taken by the simpler walker has a higher overall weight cost because it is only assessing the weighted path to the next target rather than the overall weighted cost of the entire path.

Netlogo , How to display number of ticks

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)

Line of sight NetLogo

How do I implement a line of sight in NetLogo whereby
A turtle calculates/counts all turtles between it and the a given patch on the line of sight.
Thanks.
Let's say that each turtle has a turtles-own called target, which is the given patch, you can ask each turtle to face the target patch and count all the turtles between it and the given patch:
ask turtles [
face target
let n 1
let c 0
while [patch-ahead (n - 1) != target][
ask patch-ahead n [
if any? turtles-here [set c (c + 1)]
]
set n (n + 1)
]
print (word who " has " c " turtles on the line of sight")
]
A while loop doesn't look very clean in NetLogo but it works.
See Line of Sight Example, in the Code Examples section of NetLogo's Models Library.
Very similar to Dr_stein
To-report count-turtles-on-line-to[target]
Let c 0
Let d distance target
Face target
While d > 0
[
Let c c + count turtles-on patch-ahead d
Let d d - 1
]
Report c
End
I would suggest using the patches-ahead reporter from this answer:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
You can use it like this:
to setup
clear-all
create-turtles 100 [ setxy random-xcor random-ycor ]
ask turtles [
let target one-of patches
face target
show (word
count other turtles-on patches-ahead distance target 0.1
" turtles between me and " target)
]
end

Update Birth and death rates in an SIR model

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