Netlogo: choosing ranked n-of turtles to add to a certain value to a variable - social-networking

I'm constructing a model to research opinion dynamics given certain network structures. In the model, there is a hypothetical 'dictator' who can hand out resources (or 'bribes') to certain nodes in the network. What I want is that the dictator can choose the top X% of nodes in the model who have the most positive opinions. (later I also want to the dictator to choose the nodes with the most network connections)
What's the best way to do this? I'm not sure how to use the n-of command for a 'ranked' n-of. Or is it better to use another term I'm not aware of?
ask n-of ??? turtles [set bribes (bribes + height-of-bribe)]
thanks!
edit:
currently, I have:
foreach sublist sort-on [(- total-motivation)] nodes 0 10 ask nodes [
set bribes (bribes + height-of-bribes)]
but I'm still getting errors. Any thoughts?
Edit 2:
Nevermind. It works. Thanks!

I think you probably want the max-n-of primitive. There is no need to sort and take the first (or last) of the list. You probably want something like
ask max-n-of 10 turtles [total-motivation] [set bribes (bribes + height-of-bribes)]
When you later want the ones with the most links, just put [count my-links] instead of [total-motivation]

Related

Preferential Attachement Netlogo

I'm trying to adapt the (simple) Preferential Attachment Network model (available in the Netlogo Models library) to include a slider variable that determines network structure. According to the theory of the Preferential Attachment model (or 'Opinion Leader' model) each individual in the network is assigned a number of ties, k, according to the distribution p(k) ∝ k^−γ, and connected randomly to this number of people. I thus want to have a slider for which i can adapt γ.
In the heart of the original code partners and links are chosen randomly, as such:
to go
if count turtles > num-nodes [ stop ]
;; choose a partner attached to a random link
;; this gives a node a chance to be a partner based on how many links it has
;; this is the heart of the preferential attachment mechanism
let partner one-of [both-ends] of one-of links
;; create new node, link to partner
create-turtles 1 [
set color red
;; move close to my partner, but not too close -- to enable nicer looking networks
move-to partner
fd 1
create-link-with partner
]
;; lay out the nodes with a spring layout
layout
tick
end
I'm a bit lost on how I should include this parameter.
Anyone who could help?
Thanks in advance.
EDIT: still can't get this to work. I got as far as making a 'normal' preferential attachment model in setup rather than go (again adapted from the models library). But still can't get my head around how I should adapt this code to include the gamma parameter. My code:
to create-new-nodes [n]
clear-all
ask patches [ set pcolor white ]
create-nodes n [
set color red
set shape "circle"
]
reset-ticks
end
to wire-pref-attach
create-new-nodes 2 ; create the first two nodes (0 and 1)
ask node 0 [ create-edge-with node 1] ; link them together
create-nodes num-nodes - 2 [
create-edge-with [one-of both-ends] of one-of edges ; pref select old node with more links
set color red
set shape "circle"
]
radial-layout
end
to radial-layout
layout-radial nodes edges (node 0)
end
Help is very much appreciated!
I think you have missed the point of my original comment, there is no place in the Barabasi-Albert (BA) algorithm to insert any such parameter. You need to build the network in an entirely different way. That is, you need to work out the method or process or algorithm for building the network, and then worry about writing the code to implement that method.
I think you need the algorithm described in Dorogovtsev et al (2000) Structure of Growing Networks with Preferential Linking (see https://journals.aps.org/prl/pdf/10.1103/PhysRevLett.85.4633 if you have access). In the BA algorithm, the nodes in the existing network for the new node to attach to are selected with probability proportional to in-degree (or k in your question). In the extended algorithm, each node has an inherent attractiveness A and the probability of selection is instead A+k.
Equation 12 in the paper describes the relationship between the exponent (your parameter gamma as: gamma = 2 + A/m where m is the out-degree (the number of edges being attached with each node).

NetLogo: comparing neighbors' values

during my model set up on innovation diffusion, another little programming issue occured to me in NetLogo. I would like to model that people more likely learn from people they are alike. Therfore the model considers an ability value that allocated to each agent:
[set ability random 20 ]
In the go procedure I then want them to compare their own ability value with the values from their linked neighbors.
So for example: ability of turtle1 = 5, ability of neighbor1 = 10, ability of neighbor2 = 4. Hence the (absoulute) differences are [ 5, 1]. Hence he will learn more from neighbor2 than from neighbor1.
But I don't know how to approach the problem of asking each single neighbor for the difference. As a first idea, I thought of doing it via a list-variable like [difference1, ..., difference(n)].
So far I only got an aggregated approach using average values, but this is not really consistent with recent social learning theory and might overlay situtations on which the agent has many different neighbors but one who is quite similar to him:
ask turtles
[
set ability random 20
set ability-of-neighbor (sum [ability] of link-neighbors / count link-neighbors)
set neighbor-coefficient (abs (ability - ability-of-neighbor))
;;the smaller the coefficient the more similar are the neighbors and the more the turtle learns from his neighbor(s)
]
Thank you again for your help and advice, and I really appreciate any comments.
Kind regards,
Moritz
I am having a bit of a time getting my head around what you want but here is a method of ranking link-neighbors.
let link-neighbor-rank sort-on [abs (ability - [ability] of myself)] link-neighbors
it produces a list of link neighbors in ascending order of difference of ability.
if you only want the closest neighbor use
let best min-one-of link-neighbors [abs (ability - [ability] of myself)]
I hope this helps.

NetLogo: ask link-neighbors for a tick-counter value

At the moment I am working on an agent-based model about successful innovation diffusion in social networks. So far I am a newbie in agent-based-modeling and programing.
The main idea is to model social learning among farmers, hence the agents decision to adopt an innovation mainly depends on his personal network, meaning that if he is well connected and his neighbours are using the innovation successfully, he will more likely adopt than if he is located remotely in the network.
Beside the network related arguments about social learning, I would like to implement a time dimension, for example the longer the neighbors of an agent use the innovation successfully, the more likely the agent will adopt the innovation as well. But this is exactly the point where I am stuck right at the moment. My goal is to implement the following argument. The Pseudo Code looks like the following so far.
1) a turtles-own tick counter
...
ask turtles
[
ifelse [adopted? = true]
[set ime-adopted time-adopted + 1] [set time-adopted 0]
]
...
2) In a second precedure each agent should check how long his neighbours use this innovation (in terms of "check time-adopted of neighbors").
ask turtles with [not adopted?]
[
[ask link-neigbhors with [adopted?]
[...*(Here I dont know how to ask for the time adopted value)*]
;the agent will then sum up all values he got from his neighbors from "time-adopted"
set time-neighbors-adopted [sum all "time-adopted" values of neighbors]
]
;The agent will then implement these values into his personal utility
;function which determines if he adopts the innovation or not
set utility utiltiy + 0.3 * time-neighbors-adopted
]
Many thanks for your help and advice.
Kind regards,
Moritz
To get the sum of time the neighbors have adopted the innovation you only need one line because Netlogo is amazing.
set time-neighbors-adopted sum [time-adopted] of link-neighbors with [adopted?]
like that

NetLogo, HubNet and allocating Who-numbers

So in my current HubNet application turtles are organized in various graph-structures. Whether or not two clients can see each other depends on whether the corresponding turtles are connected in the graph.
I currently build the graphs based on the turtles who-numbers and have thus built in the assumption that if there are n turtles at any given point these are numbered from 0 to n-1. I expect that this might cause problems if, for instance, a client connects, then drops and then re-connects since this (if I'm not mistaken) will give that client a new who-number (and the old number is not reused). So I'm wondering if there is a way to make sure that the turtles are numbered in the way I want?
Dropping everyone and then resetting who-numbers would be one (bad) solution. Can you help me either by suggesting a better solution or how to implement the bad solution?
If you want to use who numbers, you'll need to hide the turtles instead of killing them. If that makes things awkward because you find yourself needing to refer to e.g. turtles with [not hidden?], then consider making two breeds, call them actives and inactives or something like that, and then when hiding a turtle do hide-turtle set breed inactives. Then you can always refer to the set of active turtles just as actives. When someone joins the simulation, give them an inactive turtle if there is one, and have it do show-turtle set breed actives.
Or, if you decide not to use who numbers, you'll need a new turtle variable, say you call it id. When you make a new turtle, do set id count turtles - 1. When a turtle dies, you'll need to reassign new id numbers so there aren't gaps anymore. Does it matter exactly what scheme you use for that? Do you need there to be any particular relationship between a turtle's old number and its new number? I can think of several possible different approaches to this. Here's one that assigns the id numbers in ascending order by who number:
let whos sort [who] of turtles
ask turtles [ set id position who whos ]
P.S. But I have to wonder, is all this numbering really necessary? In a normal NetLogo model, it's almost never necessary to use who numbers for anything. There's almost always a simpler way. Why do you feel you need to use numbering in this model? Perhaps you do need it, but I'm at least a little skeptical.

Algorithm to get probability of reaching a goal

Okay, I'm gonna be as detailed as possible here.
Imagine the user goes through a set of 'options' he can choose. Every time he chooses, he get, say, 4 different options. There are many more options that can appear in those 4 'slots'. Each of those has a certain definite and known probability of appearing. Not all options are equally probable to appear, and some options require others to have already been selected previously - in a complex interdependence tree. (this I have already defined)
When the user chooses one of the 4, he is presented another choice of 4 options. The pool of options is defined again and can depend on what the user has chosen previously.
Among all possible 'options' that can ever appear, there are a certain select few which are special, call them KEY options.
When the program starts, the user is presented the first 4 options. For every one of those 4, the program needs to compute the total probability that the user will 'achieve' all the KEY options in a period of (variable) N choices.
e.g. if there are 4 options altogether the probability of achieving any one of them is exactly 1 since all of them appear right at the beginning.
If anyone can advise me as to what logic i should start with, I'd be very grateful.
I was thinking of counting all possible choice sequences, and counting the ones resulting in KEY options being chosen within N 'steps', but the problem is the probability is not uniform for all of them to appear, and also the pool of options changes as the user chooses and accumulates his options.
I'm having difficulty implementing the well defined probabilities and dependencies of the options into an algorithm that can give sensible total probability. So the user knows each time which of the 4 puts him in the best position to eventually acquire the KEY options.
Any ideas?
EDIT:
here's an example:
say there are 7 options in the pool. option1, ..., option7
option7 requires option6; option6 requires option4 and option5;
option1 thru 5 dont require anything and can appear immediately, with respective probabilities option1.p, ..., option5.p;
the KEY option is, say, option7;
user gets 4 randomly (but weighted) chosen options among 1-5, and the program needs to say something like:
"if you choose (first), you have ##% chance of getting option7 in at most N tries." analogous for the other 3 options.
naturally, for some low N it is impossible to get option7, and for some large N it is certain. N can be chosen but is fixed.
EDIT: So, the point here is NOT the user chooses randomly. Point is - the program suggests which option to choose, as to maximize the probability that eventually, after N steps, the user will be offered all key options.
For the above example; say we choose N = 4. so the program needs to tell us which of the first 4 options that appeared (any 4 among option1-5), which one, when chosen, yields the best chance of obtaining option7. since for option7 you need option6, and for that you need option4 and option5, it is clear that you MUST select either option4 or option5 on the first set of choices. one of them is certain to appear, of course.
Let's say we get this for the first choice {option3, option5, option2, option4}. The program then says:
if you chose option3, you'll never get option7 in 4 steps. p = 0;
if you chose option5, you might get option7, p=....;
... option2, p = 0;
... option4, p = ...;
Whatever we choose, for the next 4 options, the p's are re calculated. Clearly, if we chose option3 or option2, every further choice has exactly 0 probability of getting us to option7. But for option4 and option5, p > 0;
Is it clearer now? I don't know how to getting these probabilities p.
This sounds like a moderately fiddly Markov chain type problem. Create a node for every state; a state has no history, and is just dependent on the possible paths out of it (each weighted with some probability). You put a probability on each node, the chance that the user is in that state, so, for the first step, there will be a 1 his starting node, 0 everywhere else. Then, according to which nodes are adjacent and the chances of getting to them, you iterate to the next step by updating the probabilities on each vertex. So, you can calculate easily which states the user could land on in, say, 15 steps, and the associated probabilities. If you are interested in asymptotic behaviour (what would happen if he could play forever), you make a big pile of linear simultaneous equations and just solve them directly or using some tricks if your tree or graph has a neat form. You often end up with cyclical solutions, where the user could get stuck in a loop, and so on.
If you think the user selects the options at random, and he is always presented the same distribution of options at a node, you model this as a random walk on a graph. There was a recent nice post on calculating terminating probabilities of a particular random walks on the mathematica blog.

Resources