breed [ nodes ] breed [ edges ] breed [ payments ] ;each undirected edge goes from a to b edges-own [a b payment-a payment-b pa-turtle pb-turtle weight other-edges-from-a other-edges-from-b] ;links list of neighbor nodes ;the-neighbors the same but as an agentset ;done boolean that says if node is done or not nodes-own [the-links the-neighbors done] ;color-listlist of colors to be used globals [x-max y-max diam friction tot-edges filename stoptick init-power-edges color-list] to setup-globals ; separate procedure for setting globals set diam 4 set x-max max-pxcor - (diam / 2) + 1; 0.5 set y-max max-pycor - (diam / 2) + 1; 0.5 set filename "" ; change to collect images (or just use command center after setup) set stoptick -2 ; set to some number to stop, generally for image collections set-default-shape nodes "circle" set-default-shape edges "line" set friction .25 set init-power-edges 2 set tot-edges min list round (number-of-nodes * edge-ratio) ((number-of-nodes ^ 2 - number-of-nodes) / 2) set color-list [yellow green blue] end to setup ; Setup the model for a run, build a graph. ca clear-output set-default-shape payments "none" setup-globals setup-patches setup-turtles setup-random-graph graph-edges end to setup-patches ask patches [set pcolor white] end to setup-turtles create-nodes number-of-nodes [ set color one-of color-list set label-color black set size diam set the-links [] set label who set done false setxy random-float (x-max) * (2 * (random 2) - 1) random-float (y-max) * (2 * (random 2) - 1) ] end to setup-power-graph ; Build a powerlaw graph let n 0 let prob 0 let p 0 let elist 0 let t1 0 let t2 0 set prob (list turtle 0) repeat min list init-power-edges (floor number-of-nodes / 3) [ ask last prob [connect-edge turtle (length prob)] set prob lput turtle (length prob) prob ] set elist n-values (number-of-nodes - length prob) [1] while [reduce [?1 + ?2] elist < (tot-edges - count edges)] [ set n random length elist set elist replace-item n elist (1 + item n elist) ] while [length elist > 0] [ set t1 turtle (number-of-nodes - length elist) set p prob repeat min list [who] of t1 first elist [ set t2 one-of p ask t1 [connect-edge t2] set p remove t2 p set prob lput t1 prob set prob lput t2 prob ] set elist but-first elist ] end to setup-random-graph ; Build a random graph let t 0 let t1 0 let g 0 set g (list turtle 1) while [length g < number-of-nodes] [ set t1 one-of nodes with [not member? self g] set t item random length g g ask t1 [connect-edge t] set g subgraph turtle 1 ] while [count edges < tot-edges] [ set t one-of nodes set t1 one-of nodes with [self != t and not member? t the-links] if t1 != nobody [ask t1 [connect-edge t]] ] end to-report subgraph [n] ; report the complete connected subgraph containing n1 let stack 0 let graph 0 set graph (list n) set stack (list n) while [length stack > 0] [ foreach [the-links] of first stack [ if not member? ? graph [ set graph lput ? graph set stack lput ? stack ] ] set stack but-first stack ] report graph end ; The run procedure which makes the model take one step. ; It moves the nodes so that we get a better layout. You can also click on a node and move it by hand. to go let t 0 if filename = 0 [setup] ; an attempt to work even tho user forgets setup if stoptick = -1 [stop] no-display step display if mouse-down? [ set t closest-xy mouse-xcor mouse-ycor nodes ; while [mouse-down?] [ ask t [setxy mouse-xcor mouse-ycor] no-display ask edges with [a = t or b = t][adjust-edge] step display ] ] check-movie if stoptick = ticks [stop] end to step ; Adjust the edges and nodes for one step of the model let delta 0 without-interruption [ ask edges [ set delta (spring-force * (size - spring-length)) / 2.0 ask a [set heading towards-nowrap [b] of myself jump-nowrap delta] ask b [set heading towards-nowrap [a] of myself jump-nowrap delta] ] ask nodes [ ask nodes with [self != myself] [ set delta distance-nowrap myself set delta mutual-repulsion / (delta * delta) set heading towards-nowrap myself jump-nowrap (- delta) ] ] ask edges [adjust-edge] ] end to graph-edges ; Make a simple edge histogram ; set-current-plot "edge-distribution" ; set-plot-x-range 1 1 + max [length the-links] of nodes ; histogram-from nodes [length links] end to check-movie ; if filename is non-empty, make another snapshot if length filename > 0 [ export-view (word filename substring "0000" (int log ticks 10) 3 ticks ".png") ] end to-report total-cost report sum [ifelse-value ([color] of a = [color] of b) [1][0]] of edges end ;;;; Edge & Node utilities to connect-edge [other-node] ; node proc: attach an edge between self and other hatch 1 [ set breed edges set a myself set b other-node set weight 1 + random 100 set payment-a weight / 2 set payment-b weight / 2 hatch 1 [ set breed payments set ([pa-turtle] of myself) self set label ([payment-a] of myself)] hatch 1 [ set breed payments set ([pb-turtle] of myself) self set label ([payment-b] of myself)] ask a [set the-links lput [b] of myself the-links] ask b [set the-links lput [a] of myself the-links] set color black set label weight adjust-edge ] end to-report sign [num] ifelse num < 0 [report -1][report 1] end to-report closest-xy [x y agent-set] ; Return closest agent to x, y report min-one-of agent-set [distancexy-nowrap x y] end to jump-nowrap [dist] ; turtle proc: jump but don't wrap, bounce w/ friction instead let x 0 let y 0 set x xcor + dist * dx set y ycor + dist * dy if (abs x) > x-max [set x sign x * (x-max - (1 - friction) * ((abs x) - x-max))] if (abs y) > y-max [set y sign y * (y-max - (1 - friction) * ((abs y) - y-max))] setxy x y end to adjust-edge ; edge proc: reattach to a & b nodes setxy [xcor] of b [ycor] of b set heading towards-nowrap a fd diam / 2 + 1 set ([xcor] of pb-turtle) xcor set ([ycor] of pb-turtle) ycor setxy [xcor] of a [ycor] of a set size distance-nowrap b - diam set heading towards-nowrap b fd diam / 2 + 1 set ([xcor] of pa-turtle) xcor set ([ycor] of pa-turtle) ycor setxy [xcor] of a [ycor] of a jump (size / 2) + (diam / 2) end to-report make-list [num element] let i 0 let result 0 set i 0 set result [] while [i < num] [ set result lput element result set i i + 1 ] report result end to-report copy-list [l] let r 0 set r [] foreach l [ set r lput ? r] report r end ;;;;;;;;;; ;;iterated-equiresistance algorithm ;Initialize the breakout algorithm to setup-iterated-equiresistance set-current-plot "Social Welfare" ask edges [ set other-edges-from-a edges with [who != [who] of myself and (a = [a] of myself or b = [a] of myself)] set other-edges-from-b edges with [who != [who] of myself and (a = [b] of myself or b = [b] of myself)]] end to go-iterated-equiresistance ask edges [ calculate-payments ] ask edges [ choose-winners ] plot sum [weight] of (edges with [color = red]) end ;;edges functions ;sets the payments for the nodes (a and b) that this edge connects. ;Calculates new payments using the equiresistance function: to calculate-payments let payment-a-con ifelse-value (any? other-edges-from-a) [max [ifelse-value (a = [a] of myself) [payment-a][payment-b]] of other-edges-from-a] [0] let payment-b-con ifelse-value (any? other-edges-from-b) [max [ifelse-value (a = [b] of myself) [payment-a][payment-b]] of other-edges-from-b] [0] let old-payment-a payment-a let old-payment-b payment-b ifelse (payment-a-con > weight - 1 or payment-b-con > weight)[ ;if a or b can get more elswhere then no deal. set payment-a 0 set payment-b 0 ][ ;equiprobability function: solving for payment-a (pi) set payment-a ((payment-a-con - payment-b-con - weight * weight + weight * (payment-b-con + 1))/ (2 - 2 * weight + payment-b-con + payment-a-con)) set payment-b weight - payment-a] ; plot abs (old-payment-a - payment-a) ; plot abs (old-payment-b - payment-b) set ([label] of pa-turtle) round payment-a set ([label] of pb-turtle) round payment-b end ;winners are set to red to choose-winners let payment-a-con ifelse-value (any? other-edges-from-a) [max [ifelse-value (a = [a] of myself) [payment-a][payment-b]] of other-edges-from-a] [0] let payment-b-con ifelse-value (any? other-edges-from-b) [max [ifelse-value (a = [b] of myself) [payment-a][payment-b]] of other-edges-from-b] [0] ifelse (payment-a > payment-a-con and payment-b > payment-b-con)[ set color red ][ set color black ] end @#$#@#$#@ GRAPHICS-WINDOW 194 10 555 392 19 19 9.0 1 10 1 1 1 0 1 1 1 -19 19 -19 19 0 0 1 ticks CC-WINDOW 5 419 827 514 Command Center 0 BUTTON 7 64 62 97 NIL setup NIL 1 T OBSERVER NIL NIL NIL NIL BUTTON 68 65 142 98 Layout go T 1 T OBSERVER NIL NIL NIL NIL SLIDER 6 29 193 62 number-of-nodes number-of-nodes 2 50 7 1 1 NIL HORIZONTAL SLIDER 6 177 182 210 spring-force spring-force 0 2 0.1 0.1 1 NIL HORIZONTAL SLIDER 6 218 182 251 spring-length spring-length 0 10 10 0.25 1 NIL HORIZONTAL SLIDER 6 256 182 289 mutual-repulsion mutual-repulsion 0 10 5.25 0.25 1 NIL HORIZONTAL SLIDER 7 103 182 136 edge-ratio edge-ratio 0.8 5 1.2 0.1 1 NIL HORIZONTAL MONITOR 558 10 615 63 edges count edges 3 1 13 BUTTON 557 60 789 93 NIL setup-iterated-equiresistance NIL 1 T OBSERVER NIL NIL NIL NIL BUTTON 557 94 768 127 NIL go-iterated-equiresistance NIL 1 T OBSERVER NIL NIL NIL NIL BUTTON 557 128 768 161 NIL go-iterated-equiresistance T 1 T OBSERVER NIL NIL NIL NIL TEXTBOX 569 175 783 295 1- setup\n2- Layout until it's pretty\n3- setup-iterated-equiresistance\n4- go-iterated-equiresistance 13 0.0 0 PLOT 561 255 818 405 Social Welfare NIL NIL 0.0 1.0 0.0 1.0 true false PENS "default" 1.0 0 -16777216 true @#$#@#$#@ Title: Iterated Equiresitance Author: Jose M Vidal Description: A quick and dirty implemenation of the iterated equiresistance algorithm for exchange networks use in Sociology. The algorithm is described in: