breed [nodes node] breed [links link] ;each undirected edge goes from a to b links-own [ weight-a weight-b ] nodes-own [ id num ; current number ;the-neighbors ; list of neighbor nodes -- Shouldn't need I can use __link-neighbors message-queue ; the message queue. New messages are added at the tail end. mode ; mode is either "wait-ok?" or "wait-improve" agent-view ; agent-view agent's beliefs about others' number. A list indexed by their 'who'. ok?-wait-list ; list of the 'who' of my neighbors from whom I have not received an ok? message improve-wait-list ; list of the 'who' of my neighbors from whom I have not receive an improve message improve-messages ; list of improve messages I have received. my-improve ; value containing my improvement from changing numbers new-num ; new number ;my-constraints ; TODO: Do I need? No I shouldn't I can use __my-links messages-handled ; Can be used to plot messages handled ] ;;; global vars globals [ number-of-nodes nodesize friction tot-edges init-power-edges num-list ] to setup ; Setup the model for a run, build a graph. ca clear-output setup-globals setup-patches setup-nodes setup-links setup-breakout end to setup-globals ; separate procedure for setting globals set number-of-nodes 9 * 9 set nodesize 2 set-default-shape nodes "square" set-default-shape links "line" set num-list [1 2 3 4 5 6 7 8 9] end to setup-patches let i 0 let col white ask patches [ ifelse ((i mod 3) > 0) [ set col yellow ] [ set col white ] set pcolor col set i i + 1 if (i >= 18) [ set i 0 ] ] end to setup-nodes let i 0 let x 1 let y 1 let max-x 18 cct-nodes number-of-nodes [ set num one-of num-list set color red set label-color black set size nodesize set label num set message-queue [] setxy x y set id i ;set label who ;set label x + "," + y ;print "node " + who + ", x: " + x + " y: " + y set x x + 2 if (x >= max-x) [ set x 1 set y y + 2 ] set i i + 1 ] end to setup-links ;;;TODO: create the edges between nodes ;;; each node should have neighbors in it's row, column and 3 x 3 square locals [i j k z cur-node temp sort-nodes] ;;; Netlogo doesn't create who id's that are incremental ;;; So I created an id property and I order my nodes by this id ;;; to make creating the links easier set sort-nodes sort-by [id-of ?1 < id-of ?2] nodes set i 0 while [i < count nodes] [ set cur-node item i sort-nodes ;;; calculate col index set j (i mod 9) ;;; calculate row index set z floor(i / 9) ;;; add all row neighbors set k 0 while [k < 9] [ ;set temp node (k + (9 * z)) set temp item (k + (9 * z)) sort-nodes ;ask temp [ set color blue ] ask cur-node [ ;; make sure this isn't cur-node, and that we aren't connected to it already if (temp != self) and (not __link-neighbor? temp) [ __create-link-with temp [ set weight-a 1 set weight-b 1 ] ] ] set k k + 1 ] ;;; add all column neighbors set k 0 while [k < 9] [ ;set temp node ((k * 9) + j) set temp item ((k * 9) + j) sort-nodes ;ask temp [ set color blue ] ask cur-node [ ;; make sure this isn't cur-node, and that we aren't connected to it already if (temp != self) and (not __link-neighbor? temp) [ __create-link-with temp [ set weight-a 1 set weight-b 1 ] ] ] set k k + 1 ] ;;; add all 3 x 3 neighbors ;;;TODO: Add links for 3 x 3 neighbors set k 0 while [k < 9] [ let boxcolid floor (j / 3) let boxrowid floor (z / 3) ;;; This is a bit tricky set temp item ((3 * boxcolid) + (k mod 3) + (9 * ((3 * boxrowid) + (floor (k / 3) ) ))) sort-nodes ;ask temp [ set color blue ] ask cur-node [ ;; make sure this isn't cur-node, and that we aren't connected to it already if (temp != self) and (not __link-neighbor? temp) [ __create-link-with temp [ set weight-a 1 set weight-b 1 ] ] ] set k k + 1 ] set i i + 1 ] ;;; Hide the links ask links [ set hidden? true ] end to-report total-cost ;;; TODO: Need to update cost function report sum values-from links [ ifelse-value (num-of __end1 = num-of __end2) [1][0] ] ;report sum values-from links [ ; ifelse-value (num-of a = num-of b) [1][0]] end ;;; Helper Routine: Create a list of size n and init's each item to element to-report make-list [n element] locals [i result] set i 0 set result [] while [i < n] [ set result lput element result set i i + 1 ] report result end ;;;;;;;;;; ;;;breakout algorithm ;Initialize the breakout algorithm to setup-breakout ask nodes [ ;;; TODO: Need to calculate these? ;set the-neighbors nodes with [member? self (links-of myself)] ;set my-constraints edges with [a = myself or b = myself] ;;; my neighbors are __link-neighbors ;;; my constraints are __my-out-links ;;; create the handle-ok? wait list set ok?-wait-list (sort __link-neighbors) ;;; create the handle-improve wait list set improve-wait-list (sort __link-neighbors) ;;; Set out initial mode as waiting for all the Handle-Ok? messages set mode "wait-ok?" ;;; Create the initial agent-view where we don't know anyones number set agent-view (make-list number-of-nodes -1) ;-1 means "I don't know". ;;; Update agent-view and add my number set agent-view replace-item who agent-view num ;;; Initialize the messages handled to 0 set messages-handled 0 ;;; Start the breakout by sending handle-ok? message my link neighbors ask __link-neighbors [ set message-queue lput (list "ok?" (who-of myself) (num-of myself)) message-queue ] ] end to go-breakout print "total-cost: " + total-cost if (total-cost = 0) [stop] ;yes, this is cheating, but the 2000 algorithm does not have a termination condition. ;;; Add a done message to keep track of the end of the message queue ask nodes [ set message-queue lput "done" message-queue ] ;;; Process all the message queues ask nodes [handle-messages-until-done] ;or get rid of "done" message and call handle-message to do one at a time. end ;In the 2000 paper they consider one cycle to be the handling of *all* messages in the ;queue. This seems unrealistic since agents could (and do) have varying numbers of messages. to handle-message ifelse (mode = "wait-ok?") [ handle-ok-message ][ handle-improve-message ] end to handle-messages-until-done while [first message-queue != "done"] [ handle-message ] ;;; remove the done message from queue set message-queue butfirst message-queue end ;;; Helper routine that returns the first message of type msg-type ;;; Remove the message from the message queue and returns the message to-report get-next-message [msg-type] locals [i msg] set i 0 set msg [] while [i < length message-queue] [ if (first item i message-queue = msg-type) [ set msg item i message-queue set message-queue remove-item i message-queue set i length message-queue ] set i i + 1 ] report msg end to handle-ok-message locals [msg] ;;; Get next handle-ok? message ;;; msg format: (who, number) set msg (get-next-message "ok?") ;;; If the message is empty then stop if (empty? msg) [stop] ;;; Increment the messages handled set messages-handled messages-handled + 1 ;;; Update the agent-view - set number of agent from msg set agent-view replace-item (item 1 msg) agent-view (item 2 msg) ;;; Remove neighbor from ok?-wait-list set ok?-wait-list remove (turtle (item 1 msg)) ok?-wait-list ;;; check if we have processed all the handle-ok? messages from our neighbors if (empty? ok?-wait-list) [ ;;; Re-init the ok?-wait-list set ok?-wait-list (sort __link-neighbors) send-improve set improve-messages [] ;;; change mode to wait-improve set mode "wait-improve" ] end ;;; reports the cost for the agent using its current agent-view to-report total-cost-local ;;;TODO: Calculate cost based on current agent-view ;;;TODO: Need to use __my-links instead of my-contraints report sum values-from __my-links [ ifelse-value ((item (who-of __end1) (agent-view-of myself)) = (item (who-of __end2) (agent-view-of myself))) [ ifelse-value (self = __end1) [weight-a][weight-b]][0]] ;report sum values-from my-constraints [ ; ifelse-value ((item (who-of a) (agent-view-of myself)) = (item (who-of b) (agent-view-of myself))) [ ; ifelse-value (self = a) [weight-a][weight-b]][0]] end to send-improve locals [current-eval current-num best-cost c] ;;; Calculate the current cost of this agent using current number set current-eval total-cost-local ;;; Set best-cost to some arbitrarily high number representing a high cost set best-cost 10000 set current-num num ;;; Calculate which number gives this agent the best cost foreach num-list [ ;;; Update agent view - replace my number with new number set agent-view replace-item who agent-view ? ;;; calculate total cost based on new number and everyone else's number set c total-cost-local ;;; if this new cost is better than previous best cost then mark this as a better number if (c < best-cost) [ set best-cost c set new-num ? ] ] ;;; Update agent view - restore agent view back to my current number set agent-view replace-item who agent-view current-num ;;; Calculate this agents improvement by switching to new number set my-improve current-eval - best-cost ;will be >= 0 ; show "improve " + who + " " + my-improve + " " + current-eval ;;; Send neighbors Handle-Improve message ask __link-neighbors [ set message-queue lput (list "improve" (who-of myself) (my-improve-of myself) current-eval) message-queue ] ; Algorithm never uses current-eval. Bug in the paper. end to handle-improve-message locals [msg] ;;; Get next Handle-Improve message set msg (get-next-message "improve") ;;; If the message is empty then stop if (empty? msg) [stop] ;;; Increment the messages handled set messages-handled messages-handled + 1 ;;; Remove neighbor from improve-wait-list set improve-wait-list remove (turtle (item 1 msg)) improve-wait-list ;;; TODO: What does this do? set improve-messages fput msg improve-messages if (empty? improve-wait-list) [ set improve-wait-list (sort __link-neighbors) send-ok ;;; Re-initialize agent view - we don't know what the agents have for numbers anymore set agent-view (make-list number-of-nodes -1) ;;; Update agent view and set my number set agent-view replace-item who agent-view num ;;; Change mode back to wait-ok set mode "wait-ok?" ] end to send-ok locals [max-improve] ;;; Calculate the max-improve from my neighbors set max-improve max map [item 2 ?] improve-messages ;"when its improvement is largest among neighbors" actually means: (from 1996) ;if improvement is 0 and either my improvement is strictly larger than all neighbors or ; there is a tie but my 'who' is smallest. if ((my-improve > 0) and ((my-improve = max-improve and who < min map [item 1 ?] (filter [item 2 ? = max-improve] improve-messages)) or ;;tie, I win if my who is smaller (my-improve > max-improve))) [ ;;my improvement is better show "setting num" set num new-num set label num ;;; Update Agent View set my number to new number set agent-view replace-item who agent-view num ] ;In the 1996 paper they define quasi-local minimum as: (it is not well-defined in 2000 paper): ; x is violating some constraint, and the possible improvements of x and all of its neighbors is 0. if (total-cost-local > 0 and my-improve = 0 and reduce [?1 and ?2] (map [0 = item 2 ?] improve-messages)) [ ask __my-links [ ;increase weights by 1. if ((item (who-of __end1) (agent-view-of myself)) = (item (who-of __end2) (agent-view-of myself))) [;if this edge is a constraint violation ifelse (__end1 = myself) [ set weight-a weight-a + 1 ;set label-of wa-turtle weight-a ][ set weight-b weight-b + 1 ;set label-of wb-turtle weight-b ] ] ] ] ask __link-neighbors [ set message-queue lput (list "ok?" (who-of myself) (num-of myself)) message-queue ] end @#$#@#$#@ GRAPHICS-WINDOW 139 10 529 421 -1 -1 20.0 1 16 1 1 1 0 1 1 1 0 18 0 18 CC-WINDOW 5 435 661 530 Command Center 0 BUTTON 51 88 117 121 NIL setup NIL 1 T OBSERVER T NIL BUTTON 542 65 651 98 NIL setup-breakout NIL 1 T OBSERVER T NIL BUTTON 542 106 652 139 NIL go-breakout NIL 1 T OBSERVER T NIL BUTTON 542 146 652 179 NIL go-breakout T 1 T OBSERVER T NIL @#$#@#$#@ Title: Sodoku with Distributed Breakout Author: Scott Langevin Description: This program utilizes the Distributed Breakout Algorithm (Yokoo and Hirayama) to solve a Sodoku puzzle. The program is based on the Graph Coloring NetLogo program published by Dr. Jose Vidal. -------- Limitations ----------- Occationally the algorithm will get stuck in a local optima. The program could be improved by allowing a user to input numbers for each square. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 link true 0 Line -7500403 true 150 0 150 300 link direction true 0 Line -7500403 true 150 150 30 225 Line -7500403 true 150 150 270 225 airplane true 0 Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15 arrow true 0 Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box false 0 Polygon -7500403 true true 150 285 285 225 285 75 150 135 Polygon -7500403 true true 150 135 15 75 150 15 285 75 Polygon -7500403 true true 15 75 15 225 150 285 150 135 Line -16777216 false 150 285 150 135 Line -16777216 false 150 135 15 75 Line -16777216 false 150 135 285 75 bug true 0 Circle -7500403 true true 96 182 108 Circle -7500403 true true 110 127 80 Circle -7500403 true true 110 75 80 Line -7500403 true 150 100 80 30 Line -7500403 true 150 100 220 30 butterfly true 0 Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240 Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240 Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163 Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165 Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225 Circle -16777216 true false 135 90 30 Line -16777216 false 150 105 195 60 Line -16777216 false 150 105 105 60 car false 0 Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180 Circle -16777216 true false 180 180 90 Circle -16777216 true false 30 180 90 Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89 Circle -7500403 true true 47 195 58 Circle -7500403 true true 195 195 58 circle false 0 Circle -7500403 true true 0 0 300 circle 2 false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 cow false 0 Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167 Polygon -7500403 true true 73 210 86 251 62 249 48 208 Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123 cylinder false 0 Circle -7500403 true true 0 0 300 dot false 0 Circle -7500403 true true 90 90 120 face happy false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240 face neutral false 0 Circle -7500403 true true 8 7 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Rectangle -16777216 true false 60 195 240 225 face sad false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183 fish false 0 Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166 Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165 Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60 Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166 Circle -16777216 true false 215 106 30 flag false 0 Rectangle -7500403 true true 60 15 75 300 Polygon -7500403 true true 90 150 270 90 90 30 Line -7500403 true 75 135 90 135 Line -7500403 true 75 45 90 45 flower false 0 Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135 Circle -7500403 true true 85 132 38 Circle -7500403 true true 130 147 38 Circle -7500403 true true 192 85 38 Circle -7500403 true true 85 40 38 Circle -7500403 true true 177 40 38 Circle -7500403 true true 177 132 38 Circle -7500403 true true 70 85 38 Circle -7500403 true true 130 25 38 Circle -7500403 true true 96 51 108 Circle -16777216 true false 113 68 74 Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218 Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240 house false 0 Rectangle -7500403 true true 45 120 255 285 Rectangle -16777216 true false 120 210 180 285 Polygon -7500403 true true 15 120 150 15 285 120 Line -16777216 false 30 120 270 120 leaf false 0 Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195 Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195 line true 0 Line -7500403 true 150 0 150 300 line half true 0 Line -7500403 true 150 0 150 150 pentagon false 0 Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120 person false 0 Circle -7500403 true true 110 5 80 Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90 Rectangle -7500403 true true 127 79 172 94 Polygon -7500403 true true 195 90 240 150 225 180 165 105 Polygon -7500403 true true 105 90 60 150 75 180 135 105 plant false 0 Rectangle -7500403 true true 135 90 165 300 Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285 Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285 Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210 Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135 Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135 Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60 Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90 square false 0 Rectangle -7500403 true true 30 30 270 270 square 2 false 0 Rectangle -7500403 true true 30 30 270 270 Rectangle -16777216 true false 60 60 240 240 star false 0 Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108 target false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 Circle -7500403 true true 60 60 180 Circle -16777216 true false 90 90 120 Circle -7500403 true true 120 120 60 tree false 0 Circle -7500403 true true 118 3 94 Rectangle -6459832 true false 120 195 180 300 Circle -7500403 true true 65 21 108 Circle -7500403 true true 116 41 127 Circle -7500403 true true 45 90 120 Circle -7500403 true true 104 74 152 triangle false 0 Polygon -7500403 true true 150 30 15 255 285 255 triangle 2 false 0 Polygon -7500403 true true 150 30 15 255 285 255 Polygon -16777216 true false 151 99 225 223 75 224 truck false 0 Rectangle -7500403 true true 4 45 195 187 Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194 Rectangle -1 true false 195 60 195 105 Polygon -16777216 true false 238 112 252 141 219 141 218 112 Circle -16777216 true false 234 174 42 Rectangle -7500403 true true 181 185 214 194 Circle -16777216 true false 144 174 42 Circle -16777216 true false 24 174 42 Circle -7500403 false true 24 174 42 Circle -7500403 false true 144 174 42 Circle -7500403 false true 234 174 42 turtle true 0 Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210 Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105 Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105 Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87 Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210 Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99 wheel false 0 Circle -7500403 true true 3 3 294 Circle -16777216 true false 30 30 240 Line -7500403 true 150 285 150 15 Line -7500403 true 15 150 285 150 Circle -7500403 true true 120 120 60 Line -7500403 true 216 40 79 269 Line -7500403 true 40 84 269 221 Line -7500403 true 40 216 269 79 Line -7500403 true 84 40 221 269 x false 0 Polygon -7500403 true true 270 75 225 30 30 225 75 270 Polygon -7500403 true true 30 75 75 30 270 225 225 270 @#$#@#$#@ NetLogo 3.1.2 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@