;SUDOKU BREAKOUT!!!! ;Solves Sudoku puzzles using a modified distributed breakout algorithm ; essentially calculates an inverse utility for each 'number' and shuffles numbers minimizing ; global inverse utility. breed [ numbers number ] breed [ maj_nodes maj_node ] ;the bars that outline the grid are actually link turtles these are the endpoints undirected-link-breed [ maj_bars maj_bar ] ;neighborhood: the constraint neighbors for each node, determined statically for 9x9 sudoku ;value: the 1-9 'number' value numbers-own[ neighborhood value invu ok?-wait-list new-value improve-wait-list my-improve mode messages-handled message-queue improve-messages wt ] globals [ num_maj_nodes num_maj_bars stoptick ] to setup ca set-default-shape numbers "circle" set num_maj_nodes 12 set num_maj_bars 8 set stoptick -2 ask patches [set pcolor white] setup-grid setup-grid-turtles initialize-numbers setup-breakout end to setup-grid ;Grid: 15 X 15 space create-maj_nodes num_maj_nodes [set hidden? true] setup-maj create-turtles num_maj_bars [set hidden? true] ;workaround since links are no longer turtles (4.0) we need to use up 8 whos. end to setup-maj ;brute force isn't the prettiest, but its just a game board... ;making a modulating dynamic board is more complex than first contimplated ask maj_node 0 [ setxy -15 15 ] ask maj_node 1 [ setxy -5 15 ] ask maj_node 2 [ setxy 5 15 ] ask maj_node 3 [ setxy 15 15 ] ask maj_node 4 [ setxy -15 5 ] ask maj_node 5 [ setxy 15 5 ] ask maj_node 6 [ setxy -15 -5 ] ask maj_node 7 [ setxy 15 -5 ] ask maj_node 8 [ setxy -15 -15 ] ask maj_node 9 [ setxy -5 -15 ] ask maj_node 10 [ setxy 5 -15 ] ask maj_node 11 [ setxy 15 -15 ] ask maj_node 0 [ create-maj_bar-with maj_node 3 ] ask maj_node 4 [ create-maj_bar-with maj_node 5 ] ask maj_node 6 [ create-maj_bar-with maj_node 7 ] ask maj_node 8 [ create-maj_bar-with maj_node 11 ] ask maj_node 0 [ create-maj_bar-with maj_node 8 ] ask maj_node 1 [ create-maj_bar-with maj_node 9 ] ask maj_node 2 [ create-maj_bar-with maj_node 10 ] ask maj_node 3 [ create-maj_bar-with maj_node 11 ] end to setup-grid-turtles create-numbers 81 ;9x9 grid ask numbers [ set color 0 set label who set shape "none" set label-color black set message-queue [] ] ;Sector 1 ask number ( num_maj_nodes + num_maj_bars + 0) [ set neighborhood [ 21 22 23 24 25 26 27 28 29 30 31 38 39 40 47 50 53 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 1) [ set neighborhood [ 20 22 23 24 25 26 27 28 29 30 31 38 39 40 48 51 54 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 2) [ set neighborhood [ 20 21 23 24 25 26 27 28 29 30 31 38 39 40 49 52 55 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 3) [ set neighborhood [ 20 21 22 24 25 26 27 28 32 33 34 41 42 43 47 50 53 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 4) [ set neighborhood [ 20 21 22 23 25 26 27 28 32 33 34 41 42 43 48 51 54 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 5) [ set neighborhood [ 20 21 22 23 24 26 27 28 32 33 34 41 42 43 49 52 55 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 6) [ set neighborhood [ 20 21 22 23 24 25 27 28 35 36 37 44 45 46 47 50 53 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 7) [ set neighborhood [ 20 21 22 23 24 25 26 28 35 36 37 44 45 46 48 51 54 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 8) [ set neighborhood [ 20 21 22 23 24 25 26 27 35 36 37 44 45 46 49 52 55 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 2 ask number ( num_maj_nodes + num_maj_bars + 9) [ set neighborhood [ 30 31 32 33 34 35 36 37 20 21 22 38 39 40 56 59 62 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 10) [ set neighborhood [ 29 31 32 33 34 35 36 37 20 21 22 38 39 40 57 60 63 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 11) [ set neighborhood [ 29 30 32 33 34 35 36 37 20 21 22 38 39 40 58 61 64 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 12) [ set neighborhood [ 29 30 31 33 34 35 36 37 23 24 25 41 42 43 56 59 62 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 13) [ set neighborhood [ 29 30 31 32 34 35 36 37 23 24 25 41 42 43 57 60 63 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 14) [ set neighborhood [ 29 30 31 32 33 35 36 37 23 24 25 41 42 43 58 61 64 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 15) [ set neighborhood [ 29 30 31 32 33 34 36 37 26 27 28 44 45 46 56 59 62 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 16) [ set neighborhood [ 29 30 31 32 33 34 35 37 26 27 28 44 45 46 57 60 63 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 17) [ set neighborhood [ 29 30 31 32 33 34 35 36 26 27 28 44 45 46 58 61 64 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 3 ask number ( num_maj_nodes + num_maj_bars + 18) [ set neighborhood [ 39 40 41 42 43 44 45 46 20 21 22 29 30 31 65 68 71 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 19) [ set neighborhood [ 38 40 41 42 43 44 45 46 20 21 22 29 30 31 66 69 72 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 20) [ set neighborhood [ 38 39 41 42 43 44 45 46 20 21 22 29 30 31 67 70 73 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 21) [ set neighborhood [ 38 39 40 42 43 44 45 46 23 24 25 32 33 34 65 68 71 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 22) [ set neighborhood [ 38 39 40 41 43 44 45 46 23 24 25 32 33 34 66 69 72 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 23) [ set neighborhood [ 38 39 40 41 42 44 45 46 23 24 25 32 33 34 67 70 73 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 24) [ set neighborhood [ 38 39 40 41 42 43 45 46 26 27 28 35 36 37 65 68 71 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 25) [ set neighborhood [ 38 39 40 41 42 43 44 46 26 27 28 35 36 37 66 69 72 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 26) [ set neighborhood [ 38 39 40 41 42 43 44 45 26 27 28 35 36 37 67 70 73 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (15 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 4 ask number ( num_maj_nodes + num_maj_bars + 27) [ set neighborhood [ 48 49 50 51 52 53 54 55 56 57 58 65 66 67 20 23 26 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 28) [ set neighborhood [ 47 49 50 51 52 53 54 55 56 57 58 65 66 67 21 24 27 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 29) [ set neighborhood [ 47 48 50 51 52 53 54 55 56 57 58 65 66 67 22 25 28 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 30) [ set neighborhood [ 47 48 49 51 52 53 54 55 59 60 61 68 69 70 20 23 26 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 31) [ set neighborhood [ 47 48 49 50 52 53 54 55 59 60 61 68 69 70 21 24 27 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 32) [ set neighborhood [ 47 48 49 50 51 53 54 55 59 60 61 68 69 70 22 25 28 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 33) [ set neighborhood [ 47 48 49 50 51 52 54 55 62 63 64 71 72 73 20 23 26 74 77 80 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 34) [ set neighborhood [ 47 48 49 50 51 52 53 55 62 63 64 71 72 73 21 24 27 75 78 81 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 35) [ set neighborhood [ 47 48 49 50 51 52 53 54 62 63 64 71 72 73 22 25 28 76 79 82 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 5 ask number ( num_maj_nodes + num_maj_bars + 36) [ set neighborhood [ 57 58 59 60 61 62 63 64 47 48 49 65 66 67 29 32 35 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 37) [ set neighborhood [ 56 58 59 60 61 62 63 64 47 48 49 65 66 67 30 33 36 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 38) [ set neighborhood [ 56 57 59 60 61 62 63 64 47 48 49 65 66 67 31 34 37 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 39) [ set neighborhood [ 56 57 58 60 61 62 63 64 50 51 52 68 69 70 29 32 35 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 40) [ set neighborhood [ 56 57 58 59 61 62 63 64 50 51 52 68 69 70 30 33 36 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 41) [ set neighborhood [ 56 57 58 59 60 62 63 64 50 51 52 68 69 70 31 34 37 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 42) [ set neighborhood [ 56 57 58 59 60 61 63 64 53 54 55 71 72 73 29 32 35 83 86 89 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 43) [ set neighborhood [ 56 57 58 59 60 61 62 64 53 54 55 71 72 73 30 33 36 84 87 90 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 44) [ set neighborhood [ 56 57 58 59 60 61 62 63 53 54 55 71 72 73 31 34 37 85 88 91 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 6 ask number ( num_maj_nodes + num_maj_bars + 45) [ set neighborhood [ 66 67 68 69 70 71 72 73 47 48 49 56 57 58 38 41 44 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 46) [ set neighborhood [ 65 67 68 69 70 71 72 73 47 48 49 56 57 58 39 42 45 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 47) [ set neighborhood [ 65 66 68 69 70 71 72 73 47 48 49 56 57 58 40 43 46 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 48) [ set neighborhood [ 65 66 67 69 70 71 72 73 50 51 52 59 60 61 38 41 44 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 49) [ set neighborhood [ 65 66 67 68 70 71 72 73 50 51 52 59 60 61 39 42 45 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 50) [ set neighborhood [ 65 66 67 68 69 71 72 73 50 51 52 59 60 61 40 43 46 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 51) [ set neighborhood [ 65 66 67 68 69 70 72 73 53 54 55 62 63 64 38 41 44 92 95 98 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 52) [ set neighborhood [ 65 66 67 68 69 70 71 73 53 54 55 62 63 64 39 42 45 93 96 99 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 53) [ set neighborhood [ 65 66 67 68 69 70 71 72 53 54 55 62 63 64 40 43 46 94 97 100] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 7 ask number ( num_maj_nodes + num_maj_bars + 54) [ set neighborhood [ 75 76 77 78 79 80 81 82 83 84 85 92 93 94 20 23 26 47 50 53 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 55) [ set neighborhood [ 74 76 77 78 79 80 81 82 83 84 85 92 93 94 21 24 27 48 51 54 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 56) [ set neighborhood [ 74 75 77 78 79 80 81 82 83 84 85 92 93 94 22 25 28 49 52 55 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 57) [ set neighborhood [ 74 75 76 78 79 80 81 82 86 87 88 95 96 97 20 23 26 47 50 53 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 58) [ set neighborhood [ 74 75 76 77 79 80 81 82 86 87 88 95 96 97 21 24 27 48 51 54 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 59) [ set neighborhood [ 74 75 76 77 78 80 81 82 86 87 88 95 96 97 22 25 28 49 52 55 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 60) [ set neighborhood [ 74 75 76 77 78 79 81 82 89 90 91 98 99 100 20 23 26 47 50 53 ] setxy (-15 + ((9.75 / 3) - (9.75 / 6))) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 61) [ set neighborhood [ 74 75 76 77 78 79 80 82 89 90 91 98 99 100 21 24 27 48 51 54 ] setxy (-15 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 62) [ set neighborhood [ 74 75 76 77 78 79 80 81 89 90 91 98 99 100 22 25 28 49 52 55 ] setxy (-15 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 8 ask number ( num_maj_nodes + num_maj_bars + 63) [ set neighborhood [ 84 85 86 87 88 89 90 91 74 75 76 92 93 94 29 32 35 56 59 62 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 64) [ set neighborhood [ 83 85 86 87 88 89 90 91 74 75 76 92 93 94 30 33 36 57 60 63 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 65) [ set neighborhood [ 83 84 86 87 88 89 90 91 74 75 76 92 93 94 31 34 37 58 61 64 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 66) [ set neighborhood [ 83 84 85 87 88 89 90 91 77 78 79 95 96 97 29 32 35 56 59 62 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 67) [ set neighborhood [ 83 84 85 86 88 89 90 91 77 78 79 95 96 97 30 33 36 57 60 63 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 68) [ set neighborhood [ 83 84 85 86 87 89 90 91 77 78 79 95 96 97 31 34 37 58 61 64 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 69) [ set neighborhood [ 83 84 85 86 87 88 90 91 80 81 82 98 99 100 29 32 35 56 59 62 ] setxy (-5 + ((9.75 / 3) - (9.75 / 6))) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 70) [ set neighborhood [ 83 84 85 86 87 88 89 91 80 81 82 98 99 100 30 33 36 57 60 63 ] setxy (-5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 71) [ set neighborhood [ 83 84 85 86 87 88 89 90 80 81 82 98 99 100 31 34 37 58 61 64 ] setxy (-5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ;Sector 9 ask number ( num_maj_nodes + num_maj_bars + 72) [ set neighborhood [ 93 94 95 96 97 98 99 100 74 75 76 83 84 85 38 41 44 65 68 71 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 73) [ set neighborhood [ 92 94 95 96 97 98 99 100 74 75 76 83 84 85 39 42 45 66 69 72 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 74) [ set neighborhood [ 92 93 95 96 97 98 99 100 74 75 76 83 84 85 40 43 46 67 70 73 ] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - ((9.75 / 3) - (9.75 / 6))) ] ask number ( num_maj_nodes + num_maj_bars + 75) [ set neighborhood [ 92 93 94 96 97 98 99 100 77 78 79 86 87 88 38 41 44 65 68 71 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 76) [ set neighborhood [ 92 93 94 95 97 98 99 100 77 78 79 86 87 88 39 42 45 66 69 72 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 77) [ set neighborhood [ 92 93 94 95 96 98 99 100 77 78 79 86 87 88 40 43 46 67 70 73 ] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (2 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 78) [ set neighborhood [ 92 93 94 95 96 97 99 100 80 81 82 89 90 91 38 41 44 65 68 71 ] setxy (5 + ((9.75 / 3) - (9.75 / 6))) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 79) [ set neighborhood [ 92 93 94 95 96 97 98 100 80 81 82 89 90 91 39 42 45 66 69 72 ] setxy (5 + (2 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] ask number ( num_maj_nodes + num_maj_bars + 80) [ set neighborhood [ 92 93 94 95 96 97 98 99 80 81 82 89 90 91 40 43 46 67 70 73 ] setxy (5 + (3 * (9.75 / 3)) - (9.75 / 6)) (-5 - (3 * (9.75 / 3)) + (9.75 / 6)) ] end to initialize-numbers ask numbers [ set value ((random 9) + 1) set invu inv-utility set label value ] end to-report inv-utility let counter 0 set counter 0 foreach neighborhood [ if ( value = [value] of (number ?) ) [ set counter (counter + 1) ] ] report counter 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 edit if mouse-down? [ let closest-number min-one-of numbers [distancexy-nowrap mouse-xcor mouse-ycor] set [value] of closest-number (([value] of closest-number) + 1) mod 10 set [label] of closest-number ([value] of closest-number) wait .2 ] end ;;;;;;;;;; ;;;breakout algorithm ;Initialize the breakout algorithm to setup-breakout ask numbers [ set wt 1 set ok?-wait-list neighborhood ;;; set improve-wait-list neighborhood ;;; set mode "wait-ok?" set messages-handled 0 ; show "ok? " + who + " " + value without-interruption [ foreach neighborhood [ ask number ? [ set message-queue lput (list "ok?" ([who] of myself) (value)) message-queue ] ] ] ] end to go-breakout plot (sum [total-cost-local value] of numbers) ask numbers [ ifelse ((total-cost-local value) = 0) [ set label-color green ] [ set label-color red ] ] if (sum [ total-cost-local value ] of numbers = 0) [stop] ;yes, this is cheating, but the 2000 algorithm does not have a termination condition. tick ask numbers [ set message-queue lput "done" message-queue ] ask numbers [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] set message-queue but-first message-queue end to-report get-next-message [msg-type] let i 0 let msg 0 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 let msg 0 set msg (get-next-message "ok?") if (empty? msg) [stop] set messages-handled messages-handled + 1 set ok?-wait-list remove (item 1 msg) ok?-wait-list if (empty? ok?-wait-list)[ set ok?-wait-list neighborhood ;;; send-improve set improve-messages [] set mode "wait-improve"] end ;reports the cost for the agent using its current agent-view to-report total-cost-local [ val ] let counter 0 set counter 0 foreach neighborhood [ if ( val = [ value ] of (number ?) ) [ set counter (counter + (counter + ( [ wt ] of (number ?)))) ] ] report counter end to send-improve let current-eval 0 let current-number 0 let best-cost 0 let c 0 set current-eval (total-cost-local value) set best-cost 10000 set current-number value foreach [1 2 3 4 5 6 7 8 9] [ set c total-cost-local ? if (c < best-cost) [ set best-cost c set new-value ?] ] ;show "Current-eval " + current-eval + " Best cost " + best-cost set my-improve current-eval - best-cost ;will be >= 0 ;show "improve " + who + " " + my-improve + " " + current-eval foreach neighborhood [ ask number ? [ 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 let msg 0 set msg (get-next-message "improve") if (empty? msg) [stop] set messages-handled messages-handled + 1 set improve-wait-list remove (item 1 msg) improve-wait-list set improve-messages fput msg improve-messages if (empty? improve-wait-list)[ set improve-wait-list neighborhood ;;; send-ok set mode "wait-ok?"] end to send-ok let max-improve 0 set max-improve max map [item 2 ?] improve-messages ;show "maxi=" + max-improve + " myi=" + my-improve ;show "im=" + improve-messages ;show "otherw=" + min map [item 1 ?] (filter [item 2 ? = max-improve] 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)) ;;tie, I win if my who is smaller or (my-improve > max-improve)))[ ;;my improvement is better ; show "setting value" set value new-value set invu inv-utility] ;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 value) > 0 and my-improve = 0 and reduce [?1 and ?2] (map [0 = item 2 ?] improve-messages))[ foreach neighborhood [ if ( value = [ value ] of (number ?) ) [ set wt (wt + 1) ] ] ] set label value ; show "ok? " + who + " " + color foreach neighborhood [ ask number ? [ set message-queue lput (list "ok?" ([who] of myself) (value)) message-queue] ] end @#$#@#$#@ GRAPHICS-WINDOW 303 10 733 461 17 17 12.0 1 10 1 1 1 0 0 0 1 -17 17 -17 17 0 0 1 ticks CC-WINDOW 5 475 742 570 Command Center 0 BUTTON 69 79 132 112 NIL setup NIL 1 T OBSERVER NIL NIL NIL NIL BUTTON 32 127 132 160 NIL go-breakout NIL 1 T OBSERVER NIL NIL NIL NIL BUTTON 145 127 245 160 NIL go-breakout T 1 T OBSERVER NIL NIL NIL NIL MONITOR 69 194 217 247 Total Cost - Current Tick sum [ total-cost-local value ] of numbers 0 1 13 PLOT 40 242 240 392 Total Cost Time (ticks) Cost 0.0 10.0 0.0 10.0 true false PENS "default" 1.0 0 -16777216 true BUTTON 146 79 209 112 NIL edit T 1 T OBSERVER NIL NIL NIL NIL @#$#@#$#@ Title: Sodoku with Distributed Breakout Author: William Wiles and Jose M. Vidal Description: Generates and solves a Sodoku problem using distributed breakout