Pietro della Briotta Parolo Daniela Perrotta Stefania Pezzoli

Simulation models for economics

Project work on

"Modified Deffuant Network Model."

 

An introduction to A Modified Deffuant Model - Multiopinion and Network Structure.


The modified Deffuant basic model.


powered by NetLogo

view/download model file: modified_deffuant_network_model.nlogo

WHAT IS IT?

This is a model of opinion dynamics developed on network structures: random graph, small-world and network scale-free.


HOW IT WORKS

An agent is randomly chosen; then, another agents, belonging to the neighbourhood of the first one, is chosen: if their difference in opinion is below a given threshold the interaction takes place following Deffuant's equations.


HOW TO USE IT

With the chooser button, it’s possible to choose the type of network to use. For the random graph, it’s also necessary to define the average degree and the number of nodes. For the small-world, you have to decide which value to give to the rewiring probability. For the preferential graph, you can choose to select “layout” to esthetically re-arrange the graph.


THINGS TO NOTICE

The user can use the graphics to better understand how the distribution of opinion changes in time. With the counter it's possible to compare the number of interactions with the number of the time steps.


THINGS TO TRY

The user can hold the same parameter and compare what happens on the three networks or use different values for mu and d on the same graph.


EXTENDING THE MODEL

It's possible to create new networks as adaptive networks or to introduce some variations: different value of d for each agents, extremists, group of media, and so on.


RELATED MODELS

The user can see the same model on a full mixing topology.


PROCEDURES

globals [temp1 partner protagonist neighbour interactions]
links-own
[
  rewired? ; in the Small World Network it memorizes wether a link has been rewired and kills the old one      
]

turtles-own [opinion1]


to setup
  
  set interactions 0
  clear-all
  set-default-shape turtles "person"
  
  
  if NetworkType = "RandomGraph"[
  setup-spatially-clustered-network]  
  if NetworkType = "SmallWorld" [
  setup-SmallWorld]
  if NetworkType ="Preferential"
  [setup-Preferential]
  
end




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Network Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


to setup-Preferential
  
  
  make-node nobody   ;an unlinked node is created     
  make-node turtle 0      

  ask links [ set color gray ]
 
  repeat number-of-nodes - 2 [ ;we start with 2 nodes and we need to create the remaining n-2 nodes
  make-node find-partner         
    if layout? [ layout ]
  ]
end






;; used for creating a new node
to make-node [old-node]
  crt 1
  [
    set size 2
    set opinion1 random 100   
    set color opinion1 
    
    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

; the preferential partner is chosen in the following way. A random number (total) is generated between 0 and the total number of links. Then turtles are asked 
; to check wether their number of links is greater than total. If yes, that turtle is the partner. In this way a node with more links is more likely to be chosen

to-report find-partner
  let total random-float sum [count link-neighbors] of turtles
  set partner nobody
  
  ask turtles
  [
    let nc count link-neighbors ; it counts the number of nodes linked to the "asked" turtle
  
    if partner = nobody ;once a winner is found nothing is asked to the turtles anymore
    [
      ifelse nc > total
        [ set partner self ]
        [ set total total - nc ]
    ]
  ]
  report partner ;immediately exits from the current to-report procedure and reports value as the result of that procedure
end



to layout
  
  repeat 3 [ ;not to make calculations heavy
   
    let factor sqrt count turtles
    
    layout-spring turtles links (1 / factor) (7 / factor) (1 / factor) ; link repel each other to find an equilibrium
    display  ; view updated immediately
  ]
  ;; 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) ] ; the world is rescaled 
end

to-report limit-magnitude [number limit] ;limit is the maximum value reported
  if number > limit [ report limit ]
  if number < (- limit) [ report (- limit) ]
  report number

end

to setup-spatially-clustered-network
  
   
   
   crt number-of-nodes [ ;slider
   set size 2
   setxy (random-xcor * 0.95) (random-ycor * 0.95) ; we avoid the turtles to be on the edges
   
   set opinion1 random 100 ; we initialize the opinion values
   set color opinion1
 
  ]
  
 ;setting the number of nodes and the average node degree is equivalent to setting the total number of links
  
  let num-links (average-node-degree * number-of-nodes) / 2 ;links are undirected, so we need to count them twice
  while [count links < num-links ]; the cylce goes on until there is the desidered number of links
  [
    ask one-of turtles
    [
      ;choose the closest turtle I am not linked to
      let choice (min-one-of
                    (other turtles with [not link-neighbor? myself]) ; set of turtles among which to look for the minimizing turtle                 
                   [distance myself]) ;value that has to be minimized from "min-one-of"
      if choice != nobody [ create-link-with choice ]
    ]
  ] 
  
  
  repeat 10 ;esthetical rearrangement
  [
    layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1
  ]
end


to setup-SmallWorld
  
  
   
   crt number-of-nodes
   [
   set size 2
   set opinion1 random 100   
   set color opinion1 
  ]  
   
   layout-circle turtles max-pxcor - 1 ; we keep things separate, layout has to work on an already existing agentset
   
  let n 0 ;while cycle that wires all the first and second neighbours
  while [n < count turtles]
  [
  
    make-edge turtle n
              turtle ((n + 1) mod count turtles)
    make-edge turtle n
              turtle ((n + 2) mod count turtles)
    set n n + 1
  ]
  
  ask links [
      if (random-float 1) < rewiring-probability
      [
        let node1 end1 ; node1 is the turtle (node) at the end of the link
        if [ count link-neighbors ] of end1 < (count turtles - 1) 
        [
          let node2 one-of turtles with [ (self != node1) and (not link-neighbor? node1) ] 

          ask node1 [ create-link-with node2 [ set color cyan ] ]
                        set rewired? true
                ]
      ]
      if (rewired?)
      [
        die ;if the link has been rewired, the old one dies
      ]
    ]     
end

to make-edge [node1 node2]
  ask node1 [ create-link-with node2  [
    set rewired? false ; we initialize as false the rewiring variable
  ] ]
end


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

to go
  
  
  set protagonist one-of turtles
  set temp1 [opinion1] of protagonist ; we save the opinion of the interacting turtle in order to use it in a later interaction
  
  interact protagonist find-neighbour
  
  do-plots 
  tick
  
end

to interact [ turtle1 turtle2]
  
  ifelse turtle2 != nobody
 
  [
         ask turtle1 [
      
              set opinion1 (opinion1 + mu * ( ([opinion1] of turtle2) - opinion1))
              set color opinion1
              ]
  
          ask turtle2 [
                    
             set opinion1 (opinion1 + mu * (temp1 - opinion1))   ; we use temp1 because the opinion of the first turtle has already been changed            
             set color opinion1
             ]
          
          set interactions (interactions + 1)
               
  ]
  
  []  ;the ifelse structure is needed to avoid a error caused if the the program asks nobody. if the second turtles does not exist, the program simply moves on

end

to-report find-neighbour 
  
   
    set neighbour nobody
    
    set neighbour one-of other turtles with [ link-neighbor? protagonist] ;network structure
   
    if neighbour != nobody and abs (temp1 - [opinion1] of neighbour) < d ; Deffuant condition
    
       [report neighbour] 
    
        report nobody
      
  end






;;;;;;;;;;;;;;;;
;;; Plotting ;;;
;;;;;;;;;;;;;;;;


to do-plots
  
  
  
  
  set-current-plot "Turtles for each opinion"
  
    histogram [opinion1] of turtles
    set-histogram-num-bars 10
  
  
end