Systèmes Complexes : Une introduction par la pratique

Le site web du livre

3.2 Réseaux homogènes d'amitiés : réseau aléatoire

to setup
  clear-all
  set-default-shape turtles "circle"
  create-turtles 1 [
    setxy random-xcor random-ycor 
    set color blue
  ]
  reset-ticks
end

to go
  ajouter-etudiant
  tick
end

to ajouter-etudiant
  create-turtles 1 [
    setxy random-xcor random-ycor
    set color blue               
    create-links-with other turtles with [ random-float 1.0 < p ] 
  ]
end

to spatialisation
  let len (max-pxcor - min-pxcor) / (2 * sqrt count turtles) 
  repeat 3 [
    layout-spring (turtles with [any? link-neighbors]) 
                  links 0.5 len 2
    display   
  ]
end	     

haut

3.3 Comment propager l'information à l'ensemble de la communauté ?

turtles-own [
  etat        
  ancien-etat 
]

globals [
  nb-informe
  temps-propagation
]

to setup
  clear-all
  set-default-shape turtles "circle"
  create-turtles 1 [
    setxy random-xcor random-ycor 
    set color blue
  ]
  reset-ticks
end

to setup-information 
  ask turtles [ nonInforme self ]
  
  set nb-informe 0
  set temps-propagation 0
end

to informe [ a ]
  ask a [
    set etat "I"
    set color red
  ]
end

to nonInforme [ a ]
  ask a [
    set etat "NI"
    set color blue
  ]
end

to go
  ajouter-etudiant
  tick
end

to ajouter-etudiant
  create-turtles 1 [
    setxy random-xcor random-ycor 
    nonInforme self               
    create-links-with other turtles with [ random-float 1.0 < p ] 
  ]
end

to select-un-informe
  if nb-informe < count turtles [
    let i nobody
    while [ i = nobody ] [ 
      while [ not mouse-down? ] [ ]
      let a turtles-on (patch mouse-xcor mouse-ycor)
      set i one-of a with [ etat = "NI" ] 
    ]
    
    informe i
    set nb-informe nb-informe + 1
  ]
end

to select-un-informe-alea
  if nb-informe < count turtles [
    let i one-of turtles with [ etat = "NI" ] 
    informe i
    set nb-informe nb-informe + 1
  ]
end

to propage-information
  ask turtles [ set ancien-etat etat ]
  let ancien-nb-informe nb-informe
  
  ask turtles with [ ancien-etat = "I" ] [
    ask link-neighbors with [ etat = "NI" ] [
      informe self
      set nb-informe nb-informe + 1
    ]
  ]
  
  if ancien-nb-informe < nb-informe 
  [ set temps-propagation temps-propagation + 1 ]
end

to spatialisation
  let len (max-pxcor - min-pxcor) / (2 * sqrt count turtles) 
  repeat 3 [
    layout-spring (turtles with [any? link-neighbors]) 
                  links 0.5 len 2
    display   
  ]
end
	     

haut

3.4 Réseaux petit monde

turtles-own [
  cc     
  dist   
]

globals [
  infini                   
  distance-moyenne         
  coefficient-agglomeration
]

to setup
  clear-turtles
  set-default-shape turtles "circle"
  set infini 999999  
  reset-ticks
end

to go
  clear-turtles
  ajouter-etudiants
  calcul-stats
end

to ajouter-etudiants
  create-turtles n [
    setxy random-xcor random-ycor
  ]

  ask turtles [ set color blue ]
  
  liens-courts
  liens-longs 
end

to liens-courts
  ifelse count turtles > k-plus-proche [
    ask turtles [
      let l min-n-of k-plus-proche (other turtles) 
           [ distance myself ]
      create-links-with l [ set color blue ]
    ]
  ] 
  [
    ask turtles [ 
      create-links-with other turtles [ set color blue ]
    ]
  ]  
end

to liens-longs 
  let l-prob []
  ask turtles  [
    set l-prob []
    let i 0
    while [ i < count turtles ] [
      let d distance turtle i
      ifelse d = 0 [
        set l-prob lput 0 l-prob
      ]
      [
        set l-prob lput (1 / exp (r * ln d)) l-prob
      ]
      set i i + 1
    ]
    
    set i 0
    while [ i < q ] [
      let t turtle roue l-prob
      if self != t [
        create-link-with t [ set color green ]
      ]
      set i i + 1
    ]
  ]
end

to-report roue [ l ]
  let s sum l
  let rnd random-float s
  let i 0
  let cumul item i l
  while [ cumul < rnd ] [
    set i i + 1
    set cumul cumul + item i l
  ]
  report i
end

to calcul-stats
  calcul-coefficient-agglomeration

  calcul-distances
end

to-report voisin? [ voisinage ]
  report ( member? end1 voisinage and member? end2 voisinage )
end


to calcul-coefficient-agglomeration
  let total 0
  ask turtles [
    ifelse count link-neighbors <= 1 [ 
      set cc "non-defini" 
    ]
    [
      let voisinage link-neighbors
      set cc (2 * count links with [ voisin? voisinage ] /
        ((count voisinage) * (count voisinage - 1)) )
     
      set total total + cc
    ]
  ]      
   
  let nb-sup1 count turtles with [count link-neighbors > 1]
  ifelse nb-sup1 = 0 [
    set coefficient-agglomeration 0 
  ] 
  [    
    set coefficient-agglomeration total / nb-sup1
  ]
end

to setup-dijkstra [ source ]
  ask source [
    ask other turtles [
      set dist infini
    ]
    set dist 0
  ]
end

to distance-dijkstra [ source ]
  setup-dijkstra source
  let A turtles
  while [ any? A ] [
    let s min-one-of A [ dist ]
    ask s [ 
      set A other A  ; reduction de l'ensemble A = A - { s }
      let d dist
      ask link-neighbors [
        if dist > d + 1 [ set dist d + 1 ]
      ] 
    ]
  ] 
end

to calcul-distances
  let d 0
  let connecte? true
  ask turtles [
    distance-dijkstra self
    ask other turtles [ 
      ifelse dist != infini 
      [ set d d + dist ]
      [ set connecte? false ]
    ]
  ]
  ifelse connecte?
  [ set distance-moyenne d / (count turtles * (count turtles - 1)) ]
  [ set distance-moyenne infini ] 
end
	     

haut

3.5 Réseaux sans échelle caractéristique

to setup
  clear-all
  set-default-shape turtles "circle"

  create-turtles 1 [ 
    set color blue 
    setxy (max-pxcor + min-pxcor) / 2 (max-pycor + min-pycor) / 2
  ]
  
  reset-ticks
end

to go
  ajouter-etudiant
  tick
end

to-report liens-avec-preference 
  ifelse count other turtles <= m [
    report other turtles
  ]
  [
    let l-prob []
    let i 0
    while [ i < count turtles - 1 ] [
      let preference count [ link-neighbors ] of turtle i
      set l-prob lput preference l-prob
      set i i + 1
    ]
   
    let L []
    let j 0
    while [ j < m ] [
      let k roue l-prob
      set L fput k L
      set l-prob replace-item k l-prob 0
      set j j + 1
    ]
    
    report turtles with [ member? who L ]
  ] 
end

to-report liens-sans-preference
  ifelse count other turtles <= m
  [ report other turtles ]
  [ report n-of m other turtles ]
end

to ajouter-etudiant
  create-turtles 1 [
    let L no-turtles
    ifelse preference? 
      [ set L liens-avec-preference ]
      [ set L liens-sans-preference ]
    
    ask L [ create-link-with myself ]

    move-to one-of link-neighbors
    forward 4                    
    set color blue
  ]  
end

to ajouter-n-etudiants
  while [ count turtles < n ] [
    ajouter-etudiant
    tick
  ]
end

to-report roue [ l ]
  let s sum l
  let rnd random-float s
  let i 0
  let cumul item i l
  while [ cumul < rnd ] [
    set i i + 1
    set cumul cumul + item i l
  ]
  report i
end

to tracer-lin
  let max-degre max [count link-neighbors] of turtles 

  set-current-plot "Distribution"
  plot-pen-reset                              
  set-plot-x-range 0 (max-degre + 1)         
  histogram [count link-neighbors] of turtles 
end

to tracer-log
  let max-degre max [count link-neighbors] of turtles

  set-current-plot "Distribution (log-log)"
  plot-pen-reset                              

  let deg 1
  while [ deg <= max-degre ] [
    let noeud-deg turtles with [ count link-neighbors = deg ]
    if any? noeud-deg [
      plotxy (log deg 10) (log count noeud-deg 10) 
    ]
    set deg deg + 1
  ]
end

to dimensionner-noeud
  ask turtles [
    set size log (1 + count link-neighbors) 4
  ]
end

to spatialisation
  let l (max-pxcor - min-pxcor) / (2 * log (1 + count turtles) 2) 
  repeat 3 [
    layout-spring (turtles with [any? link-neighbors]) 
                  links (l-vide / l) l (repulsion / l)
    display  
  ]
end

to reduire  
  let x-centre (max-pxcor + min-pxcor) / 2
  let y-centre (max-pycor + min-pycor) / 2
  
  ask turtles [
    setxy (x-centre + 0.9 * (xcor - x-centre)) 
          (y-centre + 0.9 * (ycor - y-centre)) 
  ]
end
	     

haut