I am trying to extend the preferential attachment model that is in the model library of Netlogo to Bianconi-Barabasi model (https://en.wikipedia.org/wiki/Bianconi%E2%80%93Barab%C3%A1si_model), and I am stuck with how to do it. With the "newest" model in the model library, we have
to-report find-partner
report [one-of both-ends] of one-of links
end
and I understand how it causes the preferential attachment. But I do not know how to incorporate the "fitness" into this simple procedure.
Also, in the previous version of the preferential attachment model that was in the model library, we have
to-report find-partner
let total random-float sum [count link-neighbors] of turtles
let partner nobody
ask turtles
[
let nc count link-neighbors
;; if there's no winner yet...
if partner = nobody
[
ifelse nc > total
[ set partner self ]
[ set total total - nc ]
]
]
report partner
end
and I am again wondering how to incorporate the fitness into this procedure. I want to incorporate the fitness that comes from the exponential distribution with mean 1, so, let's say, do I multiply something like "let nc (count link-neighbors) * random-exponential 1?" Please let me know.
JenB, thank you. I rewrote my code as below, and it seems this one produces what Bianconi and Barabasi described in their paper. Once again, thank you.
;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;
turtles-own [fitness]
to setup
clear-all
set-default-shape turtles "circle"
;; make the initial network of two turtles and an edge
make-node nobody ;; first node, unattached
make-node turtle 0 ;; second node, attached to first node
reset-ticks
end
;;;;;;;;;;;;;;;;;;;;;;;
;;; Main Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;
to go
;; new edge is green, old edges are gray
ask links [ set color gray ]
make-node find-partner ;; find partner & use it as attachment
;; point for new node
tick
if layout? [ layout ]
end
;; used for creating a new node
to make-node [old-node]
crt 1
[
set color red
set fitness random-exponential 1
if old-node != nobody
[ create-link-with old-node [ set color green ]
;; position the new node near its partner
move-to old-node
fd 8
]
]
end
;; This code is the heart of the "preferential attachment" mechanism, and acts like
;; a lottery where each node gets a ticket for every connection it already has.
;; While the basic idea is the same as in the Lottery Example (in the Code Examples
;; section of the Models Library), things are made simpler here by the fact that we
;; can just use the links as if they were the "tickets": we first pick a random link,
;; and than we pick one of the two ends of that link.
to-report find-partner
let total random-float sum [(count link-neighbors) * fitness] of turtles
let partner nobody
ask turtles
[
let nc (count link-neighbors) * fitness
;; if there's no winner yet...
if partner = nobody
[
ifelse nc > total
[ set partner self ]
[ set total total - nc ]
]
]
report partner
end
;;;;;;;;;;;;;;
;;; Layout ;;;
;;;;;;;;;;;;;;
;; resize-nodes, change back and forth from size based on degree to a size of 1
to resize-nodes
ifelse all? turtles [size <= 1]
[
;; a node is a circle with diameter determined by
;; the SIZE variable; using SQRT makes the circle's
;; area proportional to its degree
ask turtles [ set size (sqrt count link-neighbors) ]
]
[
ask turtles [ set size 1 ]
]
end
to layout
;; the number 3 here is arbitrary; more repetitions slows down the
;; model, but too few gives poor layouts
repeat 3 [
;; the more turtles we have to fit into the same amount of space,
;; the smaller the inputs to layout-spring we'll need to use
let factor (sqrt count turtles)
;; numbers here are arbitrarily chosen for pleasing appearance
layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
display ;; for smooth animation
]
;; don't bump the edges of the world
let x-offset max [xcor] of turtles + min [xcor] of turtles
let y-offset max [ycor] of turtles + min [ycor] of turtles
;; big jumps look funny, so only adjust a little each time
set x-offset limit-magnitude x-offset 0.1
set y-offset limit-magnitude y-offset 0.1
ask turtles [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]
end
to-report limit-magnitude [number limit]
if number > limit [ report limit ]
if number < (- limit) [ report (- limit) ]
report number
end
; Copyright 2005 Uri Wilensky.
; See Info tab for full copyright and license.
If you do as you suggest, then the random number will be regenerated each time the procedure is called. It looks to me like the fitness for each node is assigned randomly but, once assigned, it is a fixed value for that node. If I have interpreted the model correctly, you will need to add a variable to the turtles-own list for fitness and simply assign it when the turtle is created. Then you are going to need a weighted probability selection, which you will have to build from scratch (I think), there's no obvious way to modify the procedure you have provided. Have a look at this question for weighted selection ideas.
Related
I'm rather new to NetLogo but I want to simulate the collective charging behaviour of drivers. For now, I want to implement a schedule (e.g. that the drivers are on the road between 8am and 5pm and therefore are eligible to charge before and after these hours). In order to do so, I anchored the time to the ticks. But now the implementation of the "to setup-schedule" does not work. I already looked at the collinsheppard/time example: "Discrete Event Scheduling" but that does not run either on my computer (an internal error is announced). I would appreciate your help a lot!
extensions time]
globals [
cars-charging ;; number of cars charging
blackout-patch ;; patch showing the blackout label
home-patches ;; agentset of green patches representing BEVs being at home
charge-patches ;; agentset of blue patches representing the charging stations
work-patches ;; agentset of grey patches representing BEVs not being at home
energy-demanded ;; kW requested by BEVs in charging events
q ;; probability of driving somewhere (i.e. work)
tick-datetime ;; time in the model
; blackout-threshold
]
turtles-own [
battery-level
battery-level-minimum
charge?
]
to setup
__clear-all-and-reset-ticks
;; set the start date to January 1, 2020 and link the ticks to one hour
time:anchor-schedule time:create "2020-01-01 00:00" 1 "hour"
set tick-datetime time:anchor-to-ticks (time:create "2020-01-01 00:00") 1 "hour"
setup-world
setup-turtles
setup-schedule
end
to setup-world
;; Creation of Work
set work-patches patches with [pxcor > 1 and pycor < 1]
ask work-patches [set pcolor 37
]
;; Creation of Home
set home-patches patches with [pxcor < 1]
ask home-patches [set pcolor 57 ]
;; Creation of Charging Stations
set charge-patches patches with [pxcor > 1 and pycor > 1]
ask charge-patches [ set pcolor 98 ]
;; use one of the patch labels to visually indicate whether or not the electricity grid is overloaded
ask patch (1 * max-pxcor) (0.33 * max-pycor) [
set blackout-patch self
set plabel-color red
]
end
to setup-turtles
crt 10
ask turtles [
set size 1
set shape "car"
set color 105
move-to-empty-one-of home-patches ;; start at without charging and at home
]
end
to setup-schedule
;; schedule all of the turtles to perform the "move-to-empty-one-of" work-patches procedure at 12am
time:schedule-event turtles [ [] -> move-to-empty-one-of work-patches ] time:create "2020-01-01 12:00"
;; See what's on the schedule
;print time:show-schedule
end
to go
;; stop when there is a blackout
if energy-demanded > blackout-threshold [stop]
;; update the global variables
ask blackout-patch [ set plabel "" ]
;; advance the clock
tick
end
to move-to-empty-one-of [locations] ;; turtle procedure of not using the same patch = same charging station
move-to one-of locations
while [any? other turtles-here]
[move-to one-of locations]
end
I am afraid that the discrete event scheduling part of the time extension is not working with the current version of NetLogo. (The rest of the extension still does, even though you may get a warning when starting a model with NetLogo 6.1.)
The NetLogo development team started to fix the time extension for packaging with NetLogo but the work seems to have slowed down. (The original programmer of the extension is no longer available to support it.) I will ask them to give it more priority but anyone else wanting to do discrete event simulation (in addition to the extension's other features, such as linking ticks to specific amounts of time) might also want to let the developers know.
If you have a tick representing one hour and you want all your turtles to go to do something at a particular time each day, then just use the mod function. Something like:
if ticks mod 24 = 9 [ask turtles [go-to-work] ]
Hello: I'm using Netlogo and want agents to calculate the distance between themselves and one other agent that they chose earlier. I keep getting the error of "expected a literal value." Code below. Help says that this reporter of distance "distance agent: Reports the distance from this agent to the given turtle or patch." Since I earlier defined "myneighbor," I can't figure out what is wrong. Any help most sincerely appreciated.
turtles-own [
myneighbor ;; closest other male frog to myself
mycall ;; the amplitude (loudness) of my own call
myminthresh ;; when my neighbor's call is below this threshold, I move toward him
mymaxthresh ;; when my neighbor's call is above this threshold, I move away from him
myNND ;; the distance to my nearest neighbor
settle? ;; true if male decides to create a territory and stop moving
]
to setup
clear-all
create-turtles population [ ;; use the population slider to choose number of males
set size 1.5 ;; easy to see but is it actual agent size or just agent image size?
setxy random-xcor random-ycor ;; distribute frogs randomly in the landscape
set mycall random 100 ;; choose the amplitude of my own call from a random distribution 0 to 100
set color scale-color red mycall 0 100 ;; allows easy visualization of variability in call amplitude
;; lighter color is a higher amplitude
set myminthresh inputminthresh ;; use the input on interface to set the min-threshold
set mymaxthresh inputmaxthresh ;; use the input on the interface to set the max-threshold
set myNND 0 ;; initialize nearest neighbor distance for all
]
reset-ticks
end
to go
choose-neighbors
move-turtles
tick
end
to choose-neighbors
ask turtles [
set myneighbor min-one-of other turtles [distance myself] ;; choose my nearest neighbor based on distance
set myNND [ distance myneighbor ]
]
end
to move-turtles
ask turtles [
face myneighbor
;;set heading (random 360)
fd 5
pendown
]
end
The problem line is:
set myNND [ distance myneighbor ]
Change it to:
set myNND distance myneighbor
The [] conventions of NetLogo can be somewhat obscure.
If i add if to my go command i get the error message
you cant use tick in a turtle context because tick is observer only
here is my go commands. search eat go-home and den are all defined in my commands.
energy is also defined as a global variable that turtles own
to go
if ticks = day-length [set day day + 1 create-next-day]
ask adults [search eat]
if energy < 20000 [ask adults [go-home den]]
tick
end
if i take out the line
if energy < 20000 [ask adults [go-home den]]
it runs perfectly, but i need that line or an equivalent. please help
Commands
;;-------------------------------------------------------------;;
;;------------------- ADULTS COMMANDS--------------------------;;
;;-------------------------------------------------------------;;
;; Need to add a private variable (wolves own) for wolves [state] and then need to code 4 states 1. Den 2. Search 3. Eat 4. Return
;; need to code all 4 states
;; Need to correctly allocate energy and the state of decline
To den ;when wolf is full
set energy energy - .04
end
to search ;when wolf is hungry
set energy energy - .07
fd v-wolf
if random 600 = 1 ;; frequency of turn
[ ifelse random 2 = 0 ;; 50:50 chance of left or right
[ rt 15 ] ;; could add some variation to this with random-normal 45 5
[ lt 15 ]] ;; so that it samples from a dist with mean 45 SD 5
;; check if it can see a prey/food item
;; here i think we probably pick one of several possible prey
;; that are detectable randomly using the one-of command.
;; We should probably select the nearest one instead ** The turtles are getting
;; caught between two prey species and dying because they cant choose which one **
if any? prey in-radius smell [set heading towards one-of prey in-radius smell]
if energy < 0 [die]
end
To eat ;to kill prey and eat it
let kill one-of prey-here in-radius smell
;need to code in a variable for success too
if kill != nobody
[ask kill [ die ]
set energy energy + 10000]
end
to go-home ;to head home after they've eaten and den until they need to feed again
if energy > 30000 [set target-patch min-one-of (patches with [pcolor = white]) [distance myself]]
face target-patch
fd v-wolf
set energy energy - 1
end
if energy < 20000 [ask adults [go-home den]] will be a problem in go if energy is (as it appears) a turtle variable. This will make the context of the procedure a turtle context, not an observer context.
Edit:
For example, if energy is a turtle variable, perhaps you meant
ask adults [if (energy < 20000) [go-home den]]
First, you need to build your code much more gradually. You have multiple parts of your code that don't work and that you don't understand. Try adding the smallest amount you can and make sure that works before adding anything else. You have three different questions at the moment, with errors in different parts of your code.
On the context issue in Alan's answer, think about it this way: the variable 'energy' belongs to turtles. This means that if you have 10 turtles, you have 10 variables named 'energy', one for each turtle. Which one are you checking whether it is <20000?
What you probably want is to check it for EACH turtle individually and get the turtle to do the required action if it passes the test. So it must be inside ask turtles [], and that changes from the observer to the turtle context (what model entity is doing the thing).
to go
if ticks = day-length
[ set day day + 1
create-next-day
]
ask adults
[ search
eat
if energy < 20000
[ go-home
den
]
]
tick
end
I have also cleaned up your formatting. This is not a requirement, NetLogo is happy to deal with spaces wherever you put them. However, as your code gets longer and more complicated, it will be much easier for you to debug if you follow some basic practices (1) each call to a procedure, each command etc on a separate line (2) bracket [] and indent so that you can see the code block that the brackets enclose.
I'm modeling territory colonization in Netlogo. Turtle 0 selects a territory center, then builds a territory by adding patches in order of value. Value is based on benefit / cost, where benefit is amount of food in the patch and cost is the distance from the territory center. Turtle 0 finishes building its territory once the summed patch values meets a threshold. Next, Turtle 1 sprouts and repeats the process to select its own territory, then Turtle 2, and so on. Importantly, new turtles should avoid traveling across other territories and cannot picked already-owned patches.
The Problem: Turtles need to travel around patches that are already owned. A patch's value (benefit/cost) should account for costs accurately based on the real travel distances required, not euclidean distance.
IMAGE: As a visual, here is one result from running this model. The turtle in lime green has a huge territory that required traveling over other territories and did not account for actual travel costs around what should be considered obstacles.
How might I code this to account for actual travel distance required (i.e., around obstacles of the existing home ranges) to have turtles pick patches in order of real value? Some code is below. Any ideas? Thanks in advance!
patches-own [
benefit ;; ranges 0.1-1 and represents food available in the patch
owner ] ;; patches are owned once selected for a territory
turtles-own
[ sum-value ] ;; sum of values of patches in my territory
globals [threshold = 25]
to setup
ask patches [ set owner nobody ]
end
to go
ask turtles [build-territory]
tick
end
to build-territory
if sum-value > threshold [stop] ;; keeps picking patches for territory until I've met my threshold
pick-patch
reproduce ;; sprouts a new hatchling when done building territory
end
to pick-patch
let _destination highest-value ;; this is calculated in reporters, below
ifelse _destination != nobody [
face _destination forward 1 ;; turtle will need to avoid obstacles here, haven't figured that out yet.
if patch-here = _destination
[ claim-patch _destination ]
]
[stop] ;; if there is no _destination
end
to claim-patch [_patch]
ask _patch [set owner myself]
set sum-value sum-value + (benefit / (distance start-patch))
set pcolor [color] of self
;; etc....
end
to reproduce
if sum-value > threshold [ ask patch 75 75 [sprout 1 ] ]
end
;;;; --reporters for calculations:--
to-report highest-value ;; calculates value, benefit / cost.
let available-destinations edge-patches
report max-one-of available-destinations [benefit-to-me / cost-to-me]
end
to-report edge-patches ;; constrains to searching along edge of territory so it remains contiguous.
report (patch-set [neighbors4] of territory) with [owner = nobody]
end
to-report benefit-to-me
report mean [benefit] of patches in-radius 2 ;; this is basically a moving windows analysis to find a cluster of high-benefit patches.
end
to-report cost-to-me
report distance myself ;; this is where turtle needs to account for actual travel costs (non-euclidean distance), including around other territories. how to accomplish?
end
I am currently learning NetLogo and I need help. In my model I have same sized 10 turtles which moves randomly. When 2 or more turtles are on the same patch they will combine and form a new turtle with the double size. In this manner, the main rule is max. 5 turtles can combine to each other. And this formation will continue until the there will be 2 turtles (with each contain 5 turtles) remain.
I had created turtles and made them move randomly, but I could not managed to combine them. Can you show me a way to do this? Any help appreciated. Regards.
EDIT: I tried the "in-radius" command unsuccessfully. 5-5 distribution of the turtles (as you can can see from the code, they represent H2O molecules) is vital for the system definition and any other distributions are not allowed in the model.
In detail, when randomly moving 2 H2O molecules meet on the same patch, they will combine to form a new molecule (2H2O). The main rule is as previously mentioned, max. 5 molecules can combine which ends with forming 5H2O. Since, initially there are 10H2O molecules in the system, there will be 2 5H2O molecules at the end.
The code I tried to implement is as follows,
breed [h2o-molecules h2o]
to setup
clear-all
reset-ticks
create-h2o-molecules h2o-num [
set color 105
set sIze .5
set shape "circle"
setxy random-xcor random-ycor
set pen-mode "up"
]
end
to setup-patches
ask patches [set pcolor 0]
show count turtles
end
to set-label
ask patches [
ifelse count turtles-here > 0
[set plabel count turtles-here]
[set plabel ""]
]
end
to move-h2o-molecules
ask h2o-molecules [
let dice random 1000
let change (dice - 1)
forward 2
set HEADING (HEADING + change * 2)
]
end
to go
setup-patches
move-h2o-molecules
ask turtles [rt random 1
fd 0.3]
set-label
tick
end
Thanks for your time and patience. Regards,
Using turtles-here
You don't need to ask patches for turtles-here (as you did to set patches labels). The function runs as well if called by a turtle (and is more efficient when there are more patches than turtles). But take care to use other turtles-here if you don't want to include the calling turtle.
Combine procedure
If you declare
a turtle variable after your breed declaration:
h2o-molecules-own [
turtles-inside
]
(set the variable value inside your create-h2o-molecules)
and your combination limit max-inside as a global variable (use slider widget with 5 as default value)
then the combine procedure can look like:
to combine ;; turtle procedure
; take one turtle from the same patch as a target
; which has turtles-inside low enough to combine with
let target one-of other h2o-molecules-here with
[turtles-inside <= max-inside - [turtles-inside] of myself]
if target != nobody
[
set turtles-inside turtles-inside +
[turtles-inside] of target ;; increase turtles-inside
ask target [ die ] ;; kill the target
set size sqrt turtles-inside ;; increase size
]
end
Stop
You can stop the simulation by
if not any? h2o-molecules with [turtles-inside < max-inside] [ stop ]
Comment
The condition used to select the target turtle is using turtles-here, other and the maximum constraint which is compared to the sum of turtles inside the target and turtles inside the calling turtle (using myself function).