Distributed breakout алгоритм

Інша ідея - розподілений breakout. (див відео на ютубі або відповідний розділ книжки Jose Vidal)

breed [nodes node]

globals[colors]

links-own[weight]

to setup
  clear-all
  reset-ticks
  ask patches [set pcolor white]
  set colors [red blue green]

  create-nodes num-nodes [
    set shape "circle"
    setxy random-pxcor random-pycor
    ;set color black ;
    set color one-of colors
  ]

  repeat num-edges [
    ask one-of nodes [
      create-link-with one-of other nodes
    ]
  ]
  ask links [
  set weight 1
  set label-color black
  set label weight
  ]
end

to go
  tick
  ask nodes [change-color]
end

to layout

  layout-spring nodes links 0.2 5 2

end

to-report get-value [alist key]
  foreach alist [
    [i] -> if(first i = key) [report item 1 i
    ]
  ]
  report "ERROR. No such key"
end

to-report get-all-values [alist]
  report map [[i] -> item 1 i] alist
end

to-report get-all-keys [alist]
  report map [[i] -> first i] alist

end

to-report get-all-keys-for-value [alist value]

  report get-all-keys (filter [[i] -> (item 1 i) = value] alist )
end

to-report conflict-links [the-color]
  report my-links with [(end1 != myself and [color] of end1 = the-color) or
  (end2 != myself and [color] of end2 = the-color)]


end

to-report cost-of-color [the-color]
  report sum ([weight] of conflict-links the-color)
end


;; node methods
to change-color
  let gain-color best-gain-color
  let my-gain (first gain-color)
  let max-neighbors-gain max get-all-keys ([best-gain-color] of link-neighbors)
  if (my-gain >= max-neighbors-gain) [
    ask (conflict-links color)[
    set weight weight + 1
    set label weight
    ]
  set color ( item 1 gain-color)
  ]
end

to-report best-gain-color
  let color-costs map [[i] -> (list i cost-of-color i)] colors
  let min-cost min get-all-values color-costs
  let best-colors get-all-keys-for-value color-costs min-cost
  let current-cost (get-value color-costs color)
  let gain (current-cost - min-cost)
  report (list gain (one-of best-colors))
end