hhk12
hhk12

Reputation: 1

How to select turtles with maximum or minimum centrality on network and assign them certain status

I am a beginner with Netlogo, experimenting with 'Information diffusion process model' by Emilio Sulis and Marcella Tambuscio. This Netlogo model shows how misinformation spreads on networks with different values for the spreading rate of the news, the hoax credibility, the probability of fact-checking and the forgetting rate of the agents.

I have changed a few parts of the code from the original model and tried to, if you choose 'hub' mode, set only the turtle who has maximum closeness centrality 'Believer' and other turtles 'Susceptible' in the beginning. If you choose 'periphery' mode, you set only the turtle who hase the minimum closeness centrality 'Believer' and other turtles 'Susceptible'.

However, when I run the Netlogo code, the hub mode begins with 'Believer' turtles whose centrality is less than the maximum value, and the periphery mode begins with zero number of "Believer' when the minimum centrality value more than 0.2 exists.

Here is the related part of the script.

 if mode = "hub"
    [set centrality nw:closeness-centrality
      ifelse centrality = max [centrality] of turtles
        [ set state "B" ][ set state "S" ]]


  if mode = "periphery"[
      set centrality nw:closeness-centrality
    ifelse centrality = min [centrality] of turtles
        [ set state "B" ][ set state "S" ]]

I am wondering if this is due to some bugs in Netlogo language.

I am also curious as to if there is any way possible that I can sort the 5 turtles whose centrality are the biggest in the ranking of centrality and set them 'Believer, and sort the 5 turtles whose centrality are the smalliest on the ranking set them 'Believer, and again set them 'Believer'.

Here is the full code :

extensions [ nw ]


turtles-own [ state
              alpha-hoaxC

              pForget
              centrality

] ;; Three states of agents: "B" (believer) ;  "F" (factChecker) ; "S" (susceptible)

links-own [ weight ]  ;; the weight of the links between agents

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   SETUP   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  ca

  setup-turtles
  update-plot

  reset-ticks

end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   GO   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  tick
  if ticks > 1000 [stop]   ;; stop condition (300 units of time)

  spreading               ;
  forgetting              ;-- Three main procedures for agent's behavior
  veryfing                ;

  update-colors           ;; just to improve the visualisation
  update-plot             ;; update plots of the Interface


end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  SETUP PROCEDURES  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup-var
  set-default-shape turtles "person"
  set-default-shape links "curved link"
  update-output
end

to update-output
  clear-output
  output-print "* Diffusion model in social networks *"
  if Type-of-network = "BA" [output-print "Barabási–Albert network"]
  if Type-of-network = "ER" [output-print "Erdős–Rényi network"]
end

to setup-turtles



  if Type-of-network = "Barabási–Albert algorithm" [ nw:generate-preferential-attachment turtles links number-of-agents 3]
  if Type-of-network = "Erdős–Rényi model" [
    if number-of-agents > 100 [
      if PC-low-performance? and ask-proceed? [
        clear-output output-print (word "Erdős–Rényi model with " number-of-agents " nodes.")
        nw:generate-random turtles links number-of-agents 0.00585
      ]
    ]
  ]

  if Type-of-network = "Star Network" [nw:generate-star turtles links (number-of-agents) ]


 init-edges
end



to init-edges
  ask links [set color 3]

  ask turtles
  [ setxy random-xcor random-ycor
    set shape "person"


    if mode = "random" [
    set centrality nw:closeness-centrality
    ifelse random 100 <= initial-prop [ set state "S" ][ set state "B" ]

  ]
    
    

  if mode = "hub"
    [set centrality nw:closeness-centrality
      ifelse centrality = max [centrality] of turtles
        [ set state "B" ][ set state "S" ]]


  if mode = "periphery"[
      set centrality nw:closeness-centrality
    ifelse centrality = min [centrality] of turtles
        [ set state "B" ][ set state "S" ]]



  if distribution = "fixed"
    [set alpha-hoaxC avg-alpha-hoaxC
      set pForget avg-pForget]


  if distribution = "uniform"
    [set alpha-hoaxC random-float avg-alpha-hoaxC * 2
     set pForget random-float avg-pForget * 2 ]
]




  update-colors
end


to update-colors
  ask turtles [
    if state = "B" [ set color blue ]
    if state = "F" [ set color red ]
    if state = "S" [ set color gray ]
  ]
end

to update-plot
  set-current-plot "State-of-people"
  set-current-plot-pen "BELIEVERS"
  plot count turtles with [state = "B"]
  set-current-plot-pen "FACT-CHECKERS"
  plot count turtles with [state = "F"]
  set-current-plot-pen "SUSCEPTIBLES"
  plot count turtles with [state = "S"]
end


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  GO PROCEDURES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to spreading  ;; each agent modifies with some probability its state considering the points of view (states) of its neighbors;
  ; S -> B and S -> F according to:
  ; S -> B : "spreading" function for the hoax (fi)
  ; S -> F : disseminate among the immediate neighborhood of a vertex (gi)



  ask turtles with [state = "S"][
    let nB count link-neighbors with [state = "B"] ; n-of neighbors Believers
    let nF count link-neighbors with [state = "F"] ; n-of neighbors Fact-checkers


    let _1PlusA ( 1 + alpha-hoaxC)
    let _1MinusA ( 1 - alpha-hoaxC)
    let den (nB * _1PlusA + nF * _1MinusA)
    let f 0
    let g 0
    if den != 0 [
      set f beta-spreadingRate * ( nB * _1PlusA / den )
      set g beta-spreadingRate * ( nF * _1MinusA / den )
    ]

    let random-val-f random-float 1
    ifelse random-val-f < f
    [ set state "B" ]
    [ if (random-val-f < (f + g) and ticks > delay-time ) [ set state "F" ]
    ]
  ]
end



to forgetting  ;; B -> S; F -> S  -- Each agent, regardless of belief state, forgets the news with a fixed probability pforget
  ask turtles with [state = "B" or state = "F"][
    if random-float 1 < pForget [
      set state "S"
    ]
  ]
end

to veryfing  ;; B-> F ; each agent can fact-check the hoax with a fixed probability pverify;
  ask turtles with [state = "B"][
    if random-float 1 < pVerify [
      set state "F"
    ]
  ]
end

;;;;;;;;;;;;;;;;;;;;;;; UTILS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report ask-proceed?
  report user-yes-or-no? "The network can be too wide to display in old PC: may suggest you to disable 'view updates' before press 'GO' button? Press Y to continue"
end

Upvotes: 0

Views: 52

Answers (1)

JenB
JenB

Reputation: 17678

When you say 'some bugs in the NetLogo language' do you mean bugs in the actual NetLogo language itself, or bugs in your code that is written in NetLogo?

Anyway, here is all the relevant code from your model:

  ask turtles
  [ setxy random-xcor random-ycor
    set shape "person"
    if mode = "random"
    [ set centrality nw:closeness-centrality
      ifelse random 100 <= initial-prop
      [ set state "S" ]
      [ set state "B" ]
    ]
    if mode = "hub"
    [ set centrality nw:closeness-centrality
      ifelse centrality = max [centrality] of turtles
      [ set state "B" ]
      [ set state "S" ]
    ]
    if mode = "periphery"
    [ set centrality nw:closeness-centrality
      ifelse centrality = min [centrality] of turtles
      [ set state "B" ]
      [ set state "S" ]
    ]
   ...
  ]

The important thing here is that the code you provided is inside an ask turtles block. The ask will loop through all the turtles one by one in a random order and run the code for each. Let's say it starts with 'turtle A'.

  1. The value for centrality for turtle A is calculated
  2. That value is compared to all the values of the centrality variable for the other turtles - but they have not been calculated so the stored value is 0
  3. As the calculated centrality value is not 0, it is larger than the stored values for all the other turtles, so it is the maximum
  4. Turtle A gets the state for the maximum

The same thing will happen for multiple turtles. The first turtle and then any turtle with a larger centrality value than all the previous turtles will all get assigned to the state you want for the maximum.

The way to resolve this is to calculated centrality for all turtles first. Then you can assign states based on it.

For your other question, look up max-n-of. If you need more help on that question, ask a separate question.

Upvotes: 0

Related Questions