# Generating Loop Patterns with a Genetic Algorithm and a Probabilistic Cellular Automata Rule

## Abstract

**:**

## 1. Introduction

#### 1.1. Main Aim

#### 1.2. The Problem

#### 1.2.1. Loop

- Plane Loop. Such a loop is a loop we commonly understand as a loop that can be easily recognized. The number of right turns minus the number of left turns sums up to $+4$ (360${}^{\circ}$ clockwise) or $-4$ (360${}^{\circ}$ counter-clockwise): $|\#R-\#L|=4$ .A plane loop can be visualized in a plane, but not necessarily in a field of size $n\times n$. Plane loops can use the inherent cycles available in a torus, but not necessarily. Then, they do not fit into a field of size $n\times n$ but they can be visualized by repeating the field periodically.
- Wrapped Loop. A wrapped loop is a loop that makes use of the inherent cycles in a torus. We can distinguish straight loops and wave loops.
- –
- A straight loop is just a horizontal or vertical line wrapped around using one or the other inherent cycle. The condition is $\#R=\#L=0$ .
- –
- A wave loop shows a wave-like structure vertically and/or horizontally wrapped around. The condition is $\#R=\#L=k,\phantom{\rule{3.33333pt}{0ex}}k\ge 2$. A wave loop can be very long, e.g., it can cycle many times around as long as there is enough space.

#### 1.2.2. Loop Pattern

#### 1.3. Related Work

#### 1.4. New Results

- A GA was designed that can use an arbitrary set of tiles (small matching patterns) in order to find optimal patterns depending on a global fitness function. The fitness function used here is the number of local tile matches being maximized. The working principle is demonstrated for an example: Generating patterns with points and squares.
- Then, the GA was used for generating loop patterns. A set of overlapping tiles was defined, which can aggregate to loops. Loop patterns appear by maximizing the number of tile hits. Most of the generated patterns are loop patterns, but not all of them.
- A CA Rule was defined that evolves stable loop patterns only. It uses (i) templates derived from the prior defined set of tiles, and (ii) an extra logical condition, the loop path condition. This extra condition can easily be modified in order to allow connections between loops.

#### 1.5. Paper Structure

## 2. Generating Loop Patterns with a Genetic Algorithm

#### 2.1. Tile

#### 2.2. Overlapping Tiles

#### 2.3. Genetic Algorithm

Algorithm 1: Generating a Binary Pattern with a Genetic Algorithm |

Types: Pattern = array [0 … n_{1} − 1, 0 … n_{2} − 1] of {0,1} |

Solution = (Pattern, Fitness) |

Data and Output: S is an array [1 … M] of Solution |

Temporary: Offspring is a Solution |

(1) S.Pattern ← RandomPatterns |

(2) while not TerminationCondition |

(3) foreach S_{i} in S |

(4) Select randomly a Mate S_{j} |

(5) Offspring.Pattern = Mutate (Crossover (S_{i}, S_{j})) |

(6) Offspring.Fitness ← fitness (Offspring.Pattern) |

(7) if Offspring.Pattern ∉ S.Pattern and Offspring.Fitness > S_{i}.Fitness |

then S_{i} ← Offspring |

(3) foreach end |

(2) while end |

(8) S ← SortByFitness (S) |

#### 2.4. Sample Pattern: Squares and Points

#### 2.5. Tiles for Generating Loop Patterns

#### 2.6. Loop Patterns Generated by GA

- (UFC) Uncovered cell. The given space is only partially filled with tiles. The isolated and marked blue cells in Figure 7a1,a2 are such cells. The GA generates for such uncovered cells either 0 or 1. Such cells can automatically be detected and then shown in a distinct color (e.g., white or green).
- (TCN) Touching corners. There exists a pair of one-pixels that touch each other on a diagonal.
- (3PC) 3-pin-connector. There exists a 3-pin connector in a loop path (Figure 7a5,b2,b5).
- (4PC) 4-pin-connector. Such a connector exists in a loop path (Figure 7b1).
- (ART) Artifact. There exist other black pixel structures that may be a part of a possible loop or an appendix of a loop. Typical such structures are: a line, a partial rectangle, or a branch (Figure 7b2–b5).

## 3. Generating Loop Patterns with a Probabilistic Cellular Automata Rule

#### 3.1. Choosing the Type of the Rule

- Synchronous updating.(Phase 1) For every cell $(x,y)$ its new state ${s}^{\prime}$ is computed by a local rule and buffered in the new state variable ${s}_{new}$.$\forall (x,y):{s}_{new}(x,y)\leftarrow {s}^{\prime}(x,y)=f(s(x,y),s\left(neighbo{r}_{1}(x,y)\right),s\left(neighbo{r}_{2}(x,y)\right),\dots )$We use z $\leftarrow v$ to denote that a variable z (with memory) is used to store the value v.(Phase 2) The state of each cell is updated (replaced) by its new state.$\forall (x,y):s(x,y)\leftarrow {s}^{\prime}(x,y)$It is important to notice that the order in which the cells are processed in phase 1 and in phase 2 does not matter, but the phases must be separated. This model can easily be implemented in software, or in clocked hardware using d-flipflops (internally supplying a master and a slave memory).
- Asynchronous updating.There are several schemes. In the pure case, there are no phases. A cell $(x,y)$ is selected at random, ${s}_{new}$ is computed, and then immediately updated: $s\leftarrow {s}_{new}$.We want to use the following scheme. A time step $t\to t+1$ is considered as a block of $N={n}_{1}\times {n}_{2}$ micro time steps $\tau \to \tau +1$. A selected cell is processed during one micro time step. For each time step, we select the cells sequentially in a different random order (elements are mutually exclusive). During one time step, each cell is updated once, but the order is random. We display CA configurations at time steps. This scheme is called random sequence or random new sweep.

#### 3.2. Templates Used for the CA Rule

- for each reference point i at $({x}_{ref\left(i\right)},{y}_{ref\left(i\right)}):$ ${A}_{i}=shift(-{x}_{ref\left(i\right)},-{y}_{ref\left(i\right)},L)$where the operator $\mathit{shift}(\Delta x,\Delta y,L)$ shifts a tile L by the given offsets, and isshifting-in null pixels and deleting shifted-out pixels.

#### 3.3. The CA Rule

**where**

- hit() is a test whether a template ${A}_{i}$ matches in the current CA neighborhood of the selected CA cell $(x,y)$:$$hit({A}_{i},(x,y)))=\{\begin{array}{ll}1\hfill & \mathbf{if}\phantom{\rule{3.33333pt}{0ex}}\mathit{template}{\mathit{A}}_{\mathit{i}}\mathit{matches}\mathit{in}\mathit{the}\mathit{CA}\mathit{neighborhood}\mathit{of}(\mathit{x},\mathit{y})\hfill \\ 0& \mathbf{else}\end{array}$$
- $\mathit{center}\left({A}_{{i}_{lasthit}}\right)$ is the value of the center pixel of a template match. Here, the value of the last hit is used, if there are several hits. It would be possible to use the value of the first hit or that of any hit randomly. To which extent the choice matters is a topic of further research.
- ${C}_{0}=Decision\left({\pi}_{0}\right)\wedge (hit=0)$is the main basic condition that drives the evolution through injecting noise if there is no hit, which means that such a cell is not covered by a tile pixel.
- –
- $Decision\left(\pi \right)$ is a function that is TRUE if a trial is successful under probability $\pi $, otherwise FALSE. In Python, the function can be defined as: “def decision(probability): return random() < probability”

- ${C}_{1}=Decision\left({\pi}_{1}\right)\wedge (sum(\mathit{NESW},hit=0)>0)$This additional condition avoids lonely uncovered cells that cannot be destroyed by ${C}_{1}$. This problem will further be discussed in Section 3.5.
- –
- $\mathit{NESW}=\left\{\right(0,-1),(1,0),(0,1),(-1,0\left)\right\}$is the North-East-South-West-Neighborhood (relative indexes)
- –
- $sum(\mathit{Neighborhood},ConditionForANeighbor)\in \{0..|Neighborhood\left|\right\}$is a function that tests for each neighbor in the Neighborhood (set of relative positions of the neighbors) a local condition ConditionForANeighbor and counts the fulfilled conditions.

- ${C}_{2}=Decision\left({\pi}_{2}\right)\wedge (sum(\mathit{NESW},s=1)\ne 2)$These additional conditions realize the loop path condition. Thereby faulty loops are excluded (see end of Section 3.6).
- ${C}_{2}=Decision\left({\pi}_{2}\right)\wedge (sum(\mathit{NESW},s=1)<2)$This alternative additional condition allows unconnected and connected loops.

#### 3.4. Simulation of the First Rule

- Seven patterns (a2, a3, a5, a7, a9, a19, a20) with unconnected (separated) loops, patterns that we mainly aim at.
- Four patterns (a4, a6, a8, a11) with loops that are connected, meaning that there are connections between them. It is a matter of definition whether such loops should be within the scope of objectives. In Section 1.2.2 we classed them as faulty patterns.
- Nine patterns (a1, a10, a12, a13, a14, a15, a16, a17, a18) are faulty loop patterns. Such patterns are not really what we want to achieve.
- Four patterns (a5, a7, a16, a17) contain an uncovered cell, marked by ‘0’. Such cells are toggling ($0\leftrightarrow 1$) because of noise injection through condition ${C}_{0}$. Such patterns can also be of interest but they are partially unstable (cycling). We may wish to exclude them, or we may fill the toggling cell constantly with zero to yield a lower-density loop pattern with less ones.

#### 3.5. Simulation of the Second Rule

#### 3.6. Simulation of the Third Rule

- (Case 1) ${C}_{2}=Decision\left({\pi}_{2}\right)\wedge (sum(\mathit{NESW},s=1)<2)$, or
- (Case 2) ${C}_{2}=Decision\left({\pi}_{2}\right)\wedge (sum(\mathit{NESW},s=1)\ne 2)$.

## 4. Discussion and Future Work

#### 4.1. Forming Loops by the Tiles

#### 4.2. The Genetic Algorithm Generating Loops

#### 4.3. The Cellular Automata Rule Generating Loops

#### 4.4. Future Work

- What are (all) possible loop patterns depending on the size of a field, and what is the distribution of different loop types with certain properties?A special question could be: How many loops (optionally restricted to a certain type) can maximal be placed in a certain field.
- Can it be proven that the presented CA rule always (for large field sizes) produces stable loop patterns? What are the probabilities that certain loop types are evolved? What happens for large fields?For this mainly experimental work, the minimal loop pattern size is $2\times 2$, and we see no limit for large sizes. For large sizes, we expect a mixture of different loops and loop sizes as in Figure 12. The CA rule always generated stable loop patterns so far, but the computation time increases exponentially. In the simulation, rectangular fields could be handled too. Nevertheless, the convergence and limits of this approach should be proved and further evaluated.
- How do tile matches lead to loops? The tiles were designed in a way that corners and lines can easily connect/overlap to a loop that is surrounded by zeroes. The tile matches are maximized, thereby each cell tries to obtain a match, and the idea used is that loops are paths with a maximal number of matches. Until now, the defined set of tiles did not always produce loop patterns, and therefore the CA rule needed an additional local condition (the path condition). This issue has to be further investigated, and it is the aim to find a proper set of tiles that securely stimulates the generation of loop patterns.
- How can optimal loops be generated on the basis of local conditions only when optimality depends on a global measure. Possible parameters for a global measure are:
- –
- The space between loops;
- –
- The number of loops;
- –
- The length of the loops;
- –
- The type of the loops (plane, straight, wave).

## Funding

## Data Availability Statement

## Acknowledgments

## Conflicts of Interest

## References

- Hoffmann, R. Forming Point Patterns by a Probabilistic Cellular Automata Rule. arXiv
**2022**, arXiv:2202.06656. [Google Scholar] - Hoffmann, R.; Désérable, D.; Seredyński, F. A cellular automata rule placing a maximal number of dominoes in the square and diamond. J. Supercomput.
**2021**, 77, 9069–9087. [Google Scholar] [CrossRef] - Hoffmann, R.; Désérable, D.; Seredyński, F. Cellular automata rules solving the wireless sensor network coverage problem. Nat. Comp.
**2022**, 21, 417–447. [Google Scholar] [CrossRef] - Hoffmann, R. How Agents Can Form a Specific Pattern. In International Conference on Cellular Automata; Springer: Cham, Switzerland, 2014; pp. 660–669. [Google Scholar]
- Hoffmann, R.; Désérable, D.; Seredyński, F. Minimal Covering of the Space by Domino Tiles. In Parallel Computing Technologies; Malyshkin, V., Ed.; PaCT 2021; LNCS 12942; Springer: Cham, Switzerland, 2021; pp. 453–465. [Google Scholar]
- Holland, J.H. Genetic algorithms. Sci. Am.
**1992**, 267, 66–73. [Google Scholar] [CrossRef] - Holland, J.H. Genetic algorithms. Scholarpedia
**2012**, 7, 1482. [Google Scholar] [CrossRef] - Beasley, D.; Bull, D.R.; Martin, R.R. An overview of genetic algorithms: Part 1, fundamentals. Univ. Comput.
**1993**, 15, 56–69. [Google Scholar] - Mitchell, M. Genetic algorithms: An overview. In Complex; Wiley: Hoboken, NJ, USA, 1995; Volume 1, pp. 31–39. [Google Scholar]
- Grünbaum, B.; Shephard, G.C. Tilings and Patterns; Courier Dover Publications: Mineola, NY, USA, 1987. [Google Scholar]
- Janin, D. On languages of one-dimensional overlapping tiles. In SOFSEM 2013: Theory and Practice of Computer Science: 39th International Conference on Current Trends in Theory and Practice of Computer Science, Špindlerův Mlýn, Czech Republic, 26–31 January 2013; Springer: Berlin/Heidelberg, Germany, 2013; pp. 244–256. [Google Scholar]
- Goumas, G.; Sotiropoulos, A.; Koziris, N. Minimizing completion time for loop tiling with computation and communication overlapping. In Proceedings of the 15th International Parallel and Distributed Processing Symposium, IPDPS 2001, San Francisco, CA, USA, 23–27 April 2001; IEEE: Piscataway, NJ, USA, 2001. [Google Scholar]
- Waychunas, G.A. Structure, aggregation and characterization of nanoparticles. Rev. Mineral. Geochem.
**2001**, 44, 105–166. [Google Scholar] [CrossRef] - Peano, G. Sur une courbe, qui remplit toute une aire plane. Math. Ann.
**1890**, 36, 157–160. (In French) [Google Scholar] [CrossRef] - Hilbert, D. Ueber die stetige Abbildung einer Linie auf ein Flächenstück. Math. Ann.
**1891**, 38, 459–460. (In Germany) [Google Scholar] [CrossRef] [Green Version] - Prusinkiewicz, P.; Lindenmayer, A.; Fracccia, D. Synthesis of space-filling curves on the square grid. In Fractals in the Fundamental and Applied Sciences; Peitgen, H.-O., Henrique, J.M., Pencdo, L.F., Eds.; Elsevier Science Publishers B.V.: Amsterdam, The Netherlands, 1991. [Google Scholar]
- Crossley, M. Hamiltonian Cycles in Two Dimensional Lattices. In Statistical Physics in Biology; Springer: Berlin/Heidelberg, Germany, 2013. [Google Scholar]
- Keshavarz-Kohjerdi, F.; Bagheri, A. Hamiltonian paths in some classes of grid graphs. J. Appl. Math.
**2012**, 2012, 475087. [Google Scholar] [CrossRef] - Chas, M.; Sullivan, D. String topology. arXiv
**1999**, arXiv:math/9911159. [Google Scholar] - Wolfram, S. A New Kind of Science; Wolfram Media: Champaign, IL, USA, 2002; Volume 5. [Google Scholar]

**Figure 1.**The three tiles used for the first pattern generation by GA: (

**a**) point; (

**b1**) a square (11/11) with upper-left zeroes; and (

**b2**) with the lower-right zeroes. The tile centers are marked by “.”. The tiles b1 and b2 can overlap forming a “full” square surrounded by 10 zeroes (at relevant positions), where “11” marks an overlap.

**Figure 2.**$9\times 9$ patterns generated by a Genetic Algorithm using three tiles, a point, and two overlapping partial squares that yield together a full square. The number of tile matches was optimized. The positions where tile centers match are marked. (

**a**) 18 matches. (

**b**) 18 matches. (

**c**) 17 matches. (

**d**) 16 matches.

**Figure 3.**(

**a**) The twelve tiles defining loop patterns. (

**b**) A small open square compound tile. (

**c**) A larger open square compound tile (“open” means here not fully surrounded by zeroes).

**Figure 4.**A $6\times 6$ loop pattern. (

**a**) The field boundary is marked in red. Pixels outside the boundary wrap around and thus make the field fully covered. (

**b**) The used tiles and their connections. Arrows mark pixels (and their associated tiles) that overlay. Dotted arrows show adjacent pixels/tiles that connect but do not overlay.

**Figure 5.**GA evolved loop patterns of size $4\times 4$. The quad patterns (

**b1**–

**b5**) repeat the patterns (

**a1**–

**a5**) horizontally twice and vertically twice in order to exhibit the inherent cyclic structures. The cyclic connections relevant for the loops in (

**a1**–

**a5**) are noted. (

**a1**) is a plane loop, (

**a2**) shows two straight loops, and (

**a3**–

**a5**) are wave loops.

**Figure 6.**GA evolved loop patterns of size $6\times 6$, depicted in quad representation. Patterns (

**a1**–

**a5**) consist of 18 one-pixels (blue) and 18 zero-pixels (green). Patterns (

**b1**–

**b5**,

**c1**–

**c4**) have 16 one-pixels. Pattern (

**c5**) has only 14 pixels.

**Figure 7.**Faulty loop patterns with different faults: (UNC) uncovered with wrong color (marked by ‘x’); (TCN) touching corners; (3PC) 3-pin-connector; (4PC) 4-pin-connector; (ART) artifact.

**Figure 8.**GA-evolved loop patterns of size $10\times 10$ in quad representation. Repeating $10\times 10$ structures are marked. (

**a1**–

**a6**) Patterns with one loop. (

**b1**–

**b5**) Patterns with two loops. (

**b6**) Three loops. (

**c1**–

**c6**) Connected loops.

**Figure 9.**(

**A**${\mathbf{0}}_{\mathbf{0}}$–

**A**${\mathbf{0}}_{\mathbf{6}}$) The templates derived from tile $A0=A{0}_{0}$. (

**B**${\mathbf{0}}_{\mathbf{0}}$–

**B**${\mathbf{0}}_{\mathbf{6}}$) The templates derived from tile $B0=B{0}_{0}$. (

**C**${\mathbf{0}}_{\mathbf{0}}$–

**C**${\mathbf{0}}_{\mathbf{6}}$) The templates derived from tile $C0=C{0}_{0}$. Templates are shifted tiles.

**Figure 10.**First CA Rule. the patterns evolved by 20 simulation runs. Seven patterns are loop patterns (

**a2**,

**a3**,

**a5**,

**a7**,

**a9**,

**a19**,

**a20**) we aim at. Four patterns show connected loops (

**a4**,

**a6**,

**a8**,

**a11**). The patterns (

**a5**,

**a7**,

**a16**,

**a17**) contain one uncovered cell (marked by ‘0’). Patterns (

**a1**,

**a10**,

**a12**–

**a18**) are faulty loops (not closed, diagonally touching 1-cells, branches). Simulation values: R (Run), t (time step), E (number of 1-cells), u (number of uncovered cells), M (number of tile matches).

**Figure 11.**Simulation results of the third CA Rule (Case 1) using the conditions ${C}_{1},{C}_{2},{C}_{3}$. The evolved patterns are stable and contain unconnected or connected loops.

**Figure 12.**Only loop patterns are evolved by the Third Rule (Case 2). Noise is injected if a path cell has not two path cells in NESW. Field size is $12\times 12$.

Pixel ${\mathit{p}}_{1}$ | Pixel ${\mathit{p}}_{2}$ | Result |
---|---|---|

0 | 0 | 0 |

1 | 1 | 1 |

- | 0/1/- | - |

0/1/- | - | - |

0 | 1 | forbidden |

1 | 0 | forbidden |

Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |

© 2023 by the author. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Hoffmann, R.
Generating Loop Patterns with a Genetic Algorithm and a Probabilistic Cellular Automata Rule. *Algorithms* **2023**, *16*, 352.
https://doi.org/10.3390/a16070352

**AMA Style**

Hoffmann R.
Generating Loop Patterns with a Genetic Algorithm and a Probabilistic Cellular Automata Rule. *Algorithms*. 2023; 16(7):352.
https://doi.org/10.3390/a16070352

**Chicago/Turabian Style**

Hoffmann, Rolf.
2023. "Generating Loop Patterns with a Genetic Algorithm and a Probabilistic Cellular Automata Rule" *Algorithms* 16, no. 7: 352.
https://doi.org/10.3390/a16070352