Searching for Snake-in-the-Box Codes with Evolved Pruning Models

Daniel R. Tuohy Walter D. Potter and Darren A. Casella Stottler Henke Associates, Inc. Artficial Intelligence Center San Mateo, CA University of Georgia Athens, GA

0111 Abstract We present a method for searching for 0101 achordal open paths (snakes) in n-dimensional hyper- graphs (the box). Our technique first obtains a set 0011 0001 of exemplary snakes using an evolutionary algorithm. 1101 1111 These snakes are then analyzed to define a pruning 1001 1011 model that constrains the search space. A depth-first search of the constrained solution space has established 1100 1110 new lower bounds for the length of the longest snakes in 1000 1010 the 9 and 10 dimensional graphs. 0100 0110

Keywords: Search, Genetic Algorithms, Mathematics 0000 0010

Figure 1: A maximal length snake in Q4 1 Introduction

The snake-in-the-box problem is that of discovering 4. We perform a depth first search of all snakes, the longest in a such that pruning and backtracking whenever any at- the path is not adjacent to itself at any node. The tribute of the current snake escapes the model. longest such path for the dimension-4 hypercube is illustrated in Figure 1. The reader should note that we are not con- cerned with time efficiency. We are interested only For dimensions one through seven, longest max- in obtaining snakes which are longer than the cur- imal snakes have been found by exhaustive search rent theoretical lower bound, and therefore have techniques [7][15]. In higher dimensions, those not yet been discovered either through computa- greater than seven, the solution space is intractable. tional search or mathematical construction. We Several non-exhaustive computational search tech- have achieved this objective for the 9 and 10 di- niques have been employed to discover lengthy mensional hypercube graphs. snakes in these dimensions, including genetic algo- rithms [16], distributed computing [11], neural net- works [4], and other evolutionary algorithms [6]. 2 Applications We have developed a hybrid method described by this sequence of steps: The snake-in-the-box problem was first described in a paper by Kautz in the late 1950s, and was noted 1. An evolutionary algorithm is used to generate for its relevance to coding theory [12]. Snake-in- a set of very long snakes. the-box codes are useful because they are “spread 2” gray codes. This means that any two codewords 2. We compute several attributes of each snake are at least two entries apart in the list of codewords at each node in the snake. or that they differ in at least two positions. Conse- quently, errors at only one position (the most com- 3. The upper and lower bounds of these at- mon case) are easily detectable because they will tributes define a pruning model. reference code words at positions in the list which are more obviously inappropriate [10]. Snakes have Dimension n-snake n-coil been put to use in disjunctive normal form simplifi- cation, electronic combination locking mechanisms, Q1 1 2 disk sector encoding, and analog-to-digital conver- Q2 2 4 sion [14][13][5][15][8]. Q3 4 6 The specific use of snake-in-the-box codes in Q4 7 8 these domains is often error-detection and correc- Q5 13 14 tion, and the longest snakes are the most useful for Q6 26 26 this purpose. Consequently, methods for discov- Q7 50 48 ering the longest snakes in dimensions eight and Q8 97 96 above have been the subject of much research in Q9 186 180 both mathematics and computer science (See refer- Q10 358 344 ences). Q11 680 630 Q12 1260 1238

3 Background and Terminol- Table 1: Longest snakes and coils in Qn. For n≥8, ogy solutions are only the current best-known.

Following standard convention, we use Qn to de- “n-coil” to coils of maximal length in Qn. The note the n-dimensional hypercube, which is defined terms “snake” and “n-snake” are used in exactly inductively as the Cartesian product of Q1 and the same way to designate induced open paths. Qn−1. It is useful to think of Q1 as a line (two The lengths of n-snakes and n-coils up to n = 6 constituent nodes), Q2 as a square (four), Q3 as were computed via exhaustive search by Davies [7]. a cube (eight), and Q4 as the sixteen-node graph Those for n = 7 were determined by the Genetic > in Figure 1. For n 4, meaningful visualisation is Algorithm of Potter [16] and later the exhaustive tricky. n technique of Kochut [15], which we will be building There are 2 nodes in Qn that can be represented upon. Table 1 shows the lengths of the n-snakes as vectors of binary digits. The nodes are labeled and n-coils for n≤7, as well as current best-known in such a way that the binary vectors of adjacent lengths of longest snakes and coils in dimensions 8 nodes always differ by exactly one bit, as in Figure through 12 [2][6][7][9][15][16]. 1. An in Qn, as defined in [9], is a sequence of nodes P such that for any u,v∈P, if u 4 PBSHC: An Evolutionary and v are adjacent in Qn then they are also adjacent in P. Algorithm for Obtaining A path is expressed in node sequence representa- Model Snakes tion if it is a vector of base-10 integers correspond- ing to the binary labels on each node in the path. A Population-Based Stochastic Hill-Climber (PB- The node sequence of the snake in Figure 1 is {0 1 SHC) was used to generate our model snakes. This 376141213}. algorithm is described in more detail in [6]. The A path is expressed in transition sequence rep- PBSHC evolves a population of snakes from a zero resentation if it is a vector of integers in the range or small initial length to some maximum length. 0..n-1 [1]. These integers correspond to the index Each individual in the population consists of a of the bit in the bit string that was flipped to grow sequence of integers that represents the node se- the snake from one node to the next. The index of quence of a snake, or valid path through the hyper- the least significant bit is 0 and that of the most cube, in the dimension being searched. These in- significant bit is n-1. The transition sequence of the dividuals are initialized as either a snake of length snake in Figure 1 is {0120310}. zero, that is consisting of only the zero node, or The term “Snake in the Box” (derived from the seeded with a pre-existing snake of choice. Follow- visualization of a chain as a unit-radius tube) orig- ing initialization, the evolutionary begins its inally designated induced cycles, or closed paths first generation. Each generation begins with a fit- (also called closed chains), in a graph [12]. We ness evaluation. The fitness function used is based adopt the terminology introduced in [9], who as- on both the length and the ‘tightness’ of the snake. sign the term “coil” to simple cycles and the term The tightness of a snake, as defined for the PBSHC, is a measure of how many nodes are left available sion eight, unidirectional growth was chosen for this in the hypercube after subtracting all those nodes implementation to best allocate computational re- that are disqualified either by already being in the sources. This operator can be seen to perform a snake or by being adjacent to another node in the stochastic hill-climbing process on each snake in the snake. The choice of tightness as a component of population as the choice of which adjacent node the fitness function was inspired by the idea that to connect to is based on random selection from tighter snakes, since they make more efficient use the available nodes. A choice was made early to of nodes, might have more room to grow. grow all snakes in the population instead of only Initial attempts of integrating both length and the snake of best fitness (note: enhancing the best tightness into a fitness function met with limited individual in a population is a common approach success. Many combinations of linear and non- in hybrid genetic algorithms). This choice was also linear factors of length and tightness for the fitness based on results of a comparison of these two ap- function were tried. While they each performed proaches in an earlier GA implementation. Grow- well in some periods of evolution, they would in- ing all the individuals within the population also evitably perform worse in others. Once the average works well in conjunction with the fitness function fitness of the population slowed, the diversity of the which requires that all snakes in the population of population would fall and the fitness function would a given generation be of the same length in order to converge to a local optimum. Our first attempt at function properly. The fitness function consists of solving this problem was to develop an adaptive fit- the sum of the snake’s length and normalized tight- ness function that would balance between the im- ness. This results in the fitness function simplifying portance of tightness and length, using the diver- to a function of tightness alone as the length compo- sity of the population as the balancing factor. This nent of the fitness value will dominate for snakes of technique also met with limited success, reacting to different lengths, yet cancel for snakes of the same changes in diversity too quickly in some cases and length. This also results in the automatic elimi- not quickly enough in others. Our second attempt nation of snakes that can no longer grow, allowing was a more simplistic compromise between the two their place in the population to be reallocated to objectives. Length was set as the overriding objec- other snakes that are still capable of growth. tive within each generation, and tightness delegated The choice of parameter settings was found to to an ordered ranking factor of the rank-based se- be of key importance to the performance of the PB- lection within each generation. Since all snakes that SHC and these settings were tuned extensively in were able to grow in the previous generation have dimension eight before modified to run in dimen- the same length in the current generation, tight- sions nine and above. Our previous experience with ness becomes the only distinguishing factor of the genetic algorithm snake hunters supported the ap- fitness function. plication of lower-dimension parameter settings as After the individual fitness of each of the snakes a baseline for higher-dimension runs. The fitness in the population is determined, the population is function is set to the sum of length and normalized subjected to rank-based selection based upon each tightness. Normalized tightness was defined as the snake’s fitness. In order to keep the population number of nodes remaining available divided by the constant throughout the entire run, the remaining total number of nodes in the n-dimensional hyper- places available in the population of the next gener- cube. Rank-based selection was chosen in order to ation are filled starting again with the most fit indi- maximize diversity within the population. While viduals. For example, if the selection percentage is both unidirectional and bi-directional growth were 90%, the individuals are ranked by fitness and the used in the dimension-eight trials, the relatively first 90% are selected. This still leaves 10% of the memory-conservative, unidirectional implementa- next generation empty, so the first 10% from the tion was chosen for the higher-dimensional runs current generation are used to finish the selection in order to maximize potential population sizes operation. for dimensions nine and above. Because this par- After selection, the growth operator grows each ticular implementation’s chromosome size is con- snake in the population by one step each genera- stant, the use of unidirectional growth instead of tion, connecting each snake’s end node to one of its bi-directional growth allowed the required memory adjacent nodes that has not been disqualified by for each population size to be reduced by half. Pop- already being in the snake, or by being adjacent to ulation sizes from one hundred through ten thou- some node that is in the snake. While bi-directional sand were run in trials using mutation. growth was used during preliminary trials in dimen- The best results were achieved using popula- tions of ten thousand, a selection percentage of dimension 9, by only searching for snakes in canon- ninety percent, and by seeding each population ical form we eliminate 99.9997% (n!-1 /n!) of the with highly-fit snakes from a lower-dimension. For solution space from consideration. the purposes of the method described in this paper, we have used PBSHC to generate a set of very long 5.2 Constraints Based on Model snakes in dimensions 9 and 10. Snakes 5 Constraining the Solution The solution space afforded by Kochut’s approach is still far too massive to be searched exhaustively in Space for a Depth-First dimensions greater than 7. We therefore introduce Search further constraints that no longer guarantee the maximal length snake will be found. Consequently, To explore the solution space, we attempt to grow we can only find new lower bounds for maximal a snake until it reaches a dead end. The search snake lengths, rather than absolute bounds. The then backtracks until further progress can be made following three constraints ensure that every snake in a different direction. This process is repeated considered in the depth first search adheres to a until every snake has been explored. Unfortunately, set of parameter boundaries defined by the model there are far too many possible snakes to try every snakes found via PBSHC. In essence, we are mak- one, and we are forced to constrain the solution ing a much more strict definition of a “dead end” space artificially. We will describe five methods by in the search. Backtracking now occurs not only which we constrain the solution space so that it when there are no available adjacent nodes, but also may be fully explored by a depth first search. The when all available nodes cause the snake to escape first two, described in the following section, were the parameter boundaries. first implemented by Kochut for exploring Q7. Tightness. We have defined tightness as the de- gree to which different nodes in the snake render 5.1 Kochut’s Constraints identical nodes inaccessible and is proportional to the number of pairs of included nodes with a ham- The constraints implemented by Kochut are inde- ming distance of 2. For example, nodes 000000000 pendent of model snakes and were first used in an and 000000011 both render node 000000001 inac- exhaustive snake searching algorithm devised for cessible because they differ at exactly 2 bits. The the dimension 7 hypercube. Neither of these re- “tighter” a snake is wound, the more nodes re- strictions can possibly prevent the discovery of a main available at each step in the snake. We ex- maximal length snake, yet they drastically increase press tightness as “the number of unavailable nodes search efficiency. per node in the snake”. If there are 18 unavail- Always begin at node 0. To understand why this able nodes at node 4 in the snake, the tightness does not prevent us from finding possible maximal of the snake is 4.5. It tends to be desirable to length snakes, it is helpful to consider a snake in keep this number low. However, we have found that transition sequence (see Sect. 3). From a snake our model snakes do not always choose the tight- n starting at node 0 in Qn, one can obtain 2 iden- est possible path at every step. We therefore prune tical snakes in node sequence simply by applying branches both when snakes are too loose and when the corresponding transition sequence beginning at they are too tight. Figure 2 is an example of tight- every node in Qn. In dimension 9, by only search- ness boundaries that have been defined by model ing for snakes originating at node 0 we eliminate snakes in Dimension 9. 99.8% (2n-1/2n) percent of the solution space from Tightness Rate-of-change. Consider a snake that consideration. is on the tighter of the tightness boundary Only consider snakes in canonical form. Snakes defined by our model snakes at the kth step in the in canonical form are those which only use higher- snake. For the next several steps, the search has order dimensions after every lower-order dimension license to take inadvisable turns that needlessly eat has been used at least once. have n! up available nodes in the hypercube since it will still symmetries, and canonical form is but one of these. stay inside the tightness boundary. To eliminate Again, consider a snake in transition sequence. By this possibility, we only allow a snake’s tightness swapping, say, all of the 8’s and 1’s, we obtain an to increase or decrease at a certain rate. We do identical snake with a different node sequence. It this by extracting the “percent change in tightness simply uses the dimensions in a different order. In over the last 5 steps” at each step in our model Tightness Pruning Boundaries sion 4 is always the most used dimension. Each

10 dimension has a range of about 10 uses from which

9 it never deviates in the model snakes. Currently,

8 we only prune snakes which overuse any of the di-

7 mensions and do not prune for underuse.

6 snake 5 6 Results 4

3 Unavailable nodes/node in the We have two methods of control over how strictly 2 1 13 25 37 49 61 73 85 97 109 121 133 145 157 169 181 we constrain the solution space. The first is choos- Node in the snake ing a seed snake. Past research has found it useful to choose a portion of a best known snake from a Figure 2: The tightness boundaries defined by the lower dimension to seed a search algorithm [6]. A model snakes found via PBSHC. Snakes are pruned seed, in effect, sets in stone the initial section of as soon as they escape these boundaries. the path. A seed of 37 nodes, for example, decides the path through the highest 37 levels of the search tree. This reduces the solution space considerably. Tightness Rate-of-change Pruning Boundaries The second is the number of snakes used to de-

19 fine the pruning model. Fewer snakes intuitively re- sults in narrower pruning boundaries because there 14 is less variability among fewer snakes.

9 5 nodes

4 6.1 Dimension 9 %change in tightness over the last -1 1 12 23 34 45 56 67 78 89 100 111 122 133 144 155 166 177188 A base of 344 snakes was built using the Evolution- Node in snake ary Algorithm described in section 4 to define the pruning model. Our depth first search was given Figure 3: The tightness rate-of-change boundaries a seed snake of length 37 from a maximal length defined by the model snakes found via PBSHC. snake in dimension 8, which can be found in [17]. Snakes are pruned as soon as they escape these The seed was neccessary to provide a search space boundaries. that could be searched exhaustively. An exhaustive search with pruning from the 38th node yielded five new snakes of length 188. One of these snakes, in snakes. This corresponds to the rate of change in transition sequence notation: tightness at each step, and we do not allow snakes 01234532103253123452310326 to tighten or loosen faster than any of the model 054301350231035430137413015 snakes. Figure 3 shows the tightness rate-of-change 201305413062103453013205310 boundaries that have been define by model snakes. 345263026123678632052103453 Maximal Dimension Cumulative Usage. This 013205310345263025345031025 term refers to the number of times each dimen- 103124765231036203102562134 sion has been “used” in the snake, and was first 503102356250213025012613463 described by Bishop [3]. A dimension is “used” if This constitutes an improvement over the previ- it has been flipped from a 1 to a 0 or vice-versa in ous lower bound of 186. the course of creating the path. Usage is equiva- lent to the number of occurrences in the transition 6.2 Dimension 10 sequence. We have observed a great deal of ho- mogeneity in the Dimension Cumulative Usage of Because the solution space for Q10 is exponentially model snakes. This is in part due to the fact that all bigger than that of Q9, we were forced to con- model snakes are in canonical form, which ensures strain the solution space more harshly. The prun- that the dimensions are ordered the same way. ing model was defined using only 30 snakes found In the Q9 model snakes, the highest dimension by the Evolutionary Algorithm, and we used the is always used only once. Additionally, dimension 6 first 185 nodes of the best known Q9 snake (listed is always used more than dimension 5 and dimen- above) to seed the depth first search. It should be noted that our search of the constrained solu- [4] Bishop, J. 2006. Investigating the snake-in- tion space was not exhaustive. After two weeks of the-box problem with neuroevolution. Dept. of runtime, two snakes of length 363 were found (the Computer Science, The University of Texas at search proceeded for several more weeks without Austin. Forthcoming. termination or improvement). One of these snakes, in transition sequence notation: [5] Blaum, M., and Etzion, T. 2002. Use of 01234532103253123452310326 snake-in-the-box codes for reliable identification 054301350231035430137413015 of tracks in servo fields of a disk drive. U.S. 201305413062103453013205310 patent 6496312. 345263026123678632052103453 [6] Casella, D., and Potter, W. 2005. New lower 013205310345263025345031025 bounds for the snake-in-the-box problem: Using 103124765231036203102510364 evolutionary techniques to hunt for snakes. In 316210520312052653201305431 Proceedings of the Florida Artificial Intelligence 953402310250345263025345031 Research Society Conference. 025103143610318453201325316 532053152354315201560352301 [7] Davies, D. 1965. Longest ’separated’ paths and 325673621034530135613015201 loops in an n cube. IEEE Trans. Electron. Com- 305435682013250145031023562 put. 14:261. 502130250126134630152013028 0625430135423 [8] Etzion, T., and Paterson, K. 1996. Near op- This constitutes an improvement over the previ- timal single-track gray codes. IEEE Trans. In- ous lower bound of 358. For both dimensions 9 and form. theory. 42:779–789. 10, the new lower bounds represent greater than [9] Harary, F.; Hayes, J.; and Wu, H. 1988. A sur- 1% improvement over previous lower bounds. vey of the thoery of hypercube graphs. Compu- tational Mathematics Applications 40:277–289.

7 Conclusion [10] Hiltgen, A., and Paterson, K. 2000. Single- track circuit codes. IEEE Transactions on In- We intend to expand the search technique to the formation Theory 47(6):2587–2595. hypercubes of higher dimensions. In addition, we shall adapt the search to be applied to the problem [11] Juric, M.; Potter, W.; and Plaskin, M. 1994. of finding lengthy coils. We have no reason to be- Using pvm for hunting snake-in-the-box codes. lieve that the search will not work equally well on In Proceedings of the Transputer Research and these problems, and hope that further lower bounds Applications Conference, 97–102. will be established. It is likely that our method would benefit from [12] Kautz, W. 1958. Unit-distance error-checking further pruning heuristics. The three described in codes. IRE Trans. Elecronic Computers 7:179– this paper are simply those which seemed most rel- 180. evant, and more informative measures may well ex- [13] Klee, V. 1970a. The use of circuit codes in ist. analog-to-digital conversion. In and its Applications. B. Harris, ed. New York, References NY: Academic Press. [14] Klee, V. 1970b. What is the maximum length [1] Abbott, H., and Katchalski, M. 1988. On the of a d-dimensional snake? American Mathemat- snake in the box problem. Journal of Combina- ics Monthly 77:63–65. torial Theory 45:13–24. [15] Kochut, K. 1996. Snake-in-the-box codes for [2] Abbott, H., and Katchalski, M. 1991. On the dimension 7. Journal of Combinatorial Mathe- construction of snake-in-the-box codes. Utilitas matics and Combinatorial Computations 20:175– Mathematica 40:97–116. 185.

[3] Bishop, J. 2004. Snake hunting by genetic con- [16] Potter, W.; Robinson, R.; Miller, J.; and struction. Technical report, Artificial Intelligence Kochut, K. 1994. Using the genetic algorithm Center, The University of Georgia. to find snake-in-the-box codes. In Proceedings of the 7th International Conference On Industrial and Engineering Applications of Artificial Intel- ligence, 421–426. [17] Rajan, D., and Shende, A. 1999. Maximum and reversible snakes in hypercubes. In Proceed- ings of the Annual Australasian Conference on Combinatorial Mathematics and Combinatorial Computation.