Expanding TikZ automata from a single line to a grid

You can start with following code, you jsut need a matrix and some foreach loops to draw this kind of schemes.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}

        \begin{tikzpicture}
        \tikzset{
            state/.style = {draw, minimum size=1cm, anchor=center},
            array/.style = {matrix of nodes, nodes=state, row sep=1cm, column sep=1cm},
        }
        \matrix[array] (A) {
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
        };
        \foreach \j in {1,...,5}{
                \node[right= 2mm of A-\j-6] {\dots};
                \foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
                    \draw (A-\j-\lasti) edge[bend left] node[above] {$\lambda$} (A-\j-\i);}
            } 

        \foreach \i in {1,...,6}
                \node[below= 2mm of A-5-\i] {\vdots};

        \end{tikzpicture}
\end{document}

enter image description here

Update:

I've managed to find a relation to define edge labels with foreach loops for lambda, sigma and theta. But mu labels have been applied in manually defined loops. Now your homework will be to understand them.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}

        \begin{tikzpicture}
        \tikzset{
            state/.style = {draw, minimum size=1cm, anchor=center},
            array/.style = {matrix of nodes, nodes=state, row sep=1cm, column sep=1cm},
        }
        \matrix[array] (A) {
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
            0 & 1 & 2 & 3 & 4 & 5 \\
        };
          % end row dots and \lambdas
        \foreach \j in {1,...,5}{
                \node[right= 2mm of A-\j-6] {\dots};
                \foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
                    \draw[->] (A-\j-\lasti) edge[bend left] node[above] {$\lambda$} (A-\j-\i);}
            } 

        %Sigma
        \foreach \i [count= \ni] in {2,...,5}{
            \foreach \j [remember=\j as \lastj (initially \ni), count=\nj, evaluate=\nj as \text using {ifthenelse(\nj==1,,\nj)} ] in {\i,...,5}
                \draw[<-] (A-\lastj-\ni) edge[bend right] node[left] {$\text\sigma$} (A-\j-\ni);            
        }

        %Theta
        \foreach \i [count= \ni, remember=\i as \lasti (initially 1)] in {2,...,5}{
            \foreach \j [count=\nj, evaluate=\nj as \text using {ifthenelse(\nj==1,,\nj)} ] in {\i,...,6}
                \draw[->] (A-\lasti-\j) edge[bend left] node[right] {$\text\theta$} (A-\i-\j);          
        }

       %mus second row
        \foreach \i [remember=\i as \lasti (initially 1)] in {2,...,6}{
            \draw[<-] (A-2-\lasti) edge[bend right] node[below] {$\mu$} (A-2-\i);}

       %third row mus
        \foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$2\mu$,$2\mu$,$2\mu$}{
            \draw[->] (A-3-\nexti) edge[bend left] node[below] {\i} (A-3-\ni);}

       %fourth row mus
        \foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$3\mu$,$3\mu$,$3\mu$}{
            \draw[->] (A-4-\nexti) edge[bend left] node[below] {\i} (A-4-\ni);}

       %fifth row mus
        \foreach \i [count=\ni, evaluate=\ni as \nexti using {int(\ni+1)}] in {$\mu$,$2\mu$,$3\mu$,$4\mu$,$4\mu$}{
            \draw[->] (A-5-\nexti) edge[bend left] node[below] {\i} (A-5-\ni);}

        \foreach \i in {1,...,6}
                \node[below= 2mm of A-5-\i] {\vdots};

        \end{tikzpicture}
\end{document}

enter image description here


It takes a while to compile. but here I TABstack various tikz components.

\documentclass{article}
\usepackage{tikz,tabstackengine,amsmath}
\usetikzlibrary{automata,positioning}
\stackMath
\setstackgap{S}{-4pt}
\newcommand\rlarrow[2]{\smash{\vcenter{\hbox{\kern-5pt%
\begin{tikzpicture}
        \node             (0) {};
        \node[right=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {$#1$} (1)
            (1) edge[bend left, auto=left] node {$#2$} (0);
\end{tikzpicture}%
\kern-5pt}}}}
\newcommand\rarrow[1]{\kern-5pt\smash{%
\begin{tikzpicture}
        \node             (0) {};
        \node[right=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {$#1$} (1);
\end{tikzpicture}%
}\kern-5pt}
\newcommand\uarrow[1]{\makebox[0pt]{%
\begin{tikzpicture}
        \node             (0) {};
        \node[below=of 0] (1) {};
        \draw[every loop]
            (1) edge[bend left, auto=left] node {\makebox[0pt][r]{$#1$}} (0);
\end{tikzpicture}%
\kern5pt}}
\newcommand\darrow[1]{\makebox[0pt]{\kern5pt%
\begin{tikzpicture}
        \node             (0) {};
        \node[below=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {\makebox[0pt][l]{$#1$}} (1);
\end{tikzpicture}%
}}
\begin{document}
\tabbedShortstack{
\fbox{0}&\rarrow{\lambda}&\fbox{1}&\rarrow{\lambda}&\fbox{2}&\rarrow{\lambda}&
  \fbox{3}&\rarrow{\lambda}&\fbox{4}&\rarrow{\lambda}&\fbox{5}&\,\cdots\\
\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&\darrow{3\theta}&&\darrow{4\theta}&&
  \darrow{5\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{\mu}&\fbox{2}&
  \rlarrow{\lambda}{\mu}&\fbox{3}&\rlarrow{\lambda}{\mu}&\fbox{4}&
  \rlarrow{\lambda}{\mu}&\fbox{5}&\,\cdots\\
\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&
  \darrow{3\theta}&&\darrow{4\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{2\mu}&\fbox{3}&\rlarrow{\lambda}{2\mu}&\fbox{4}&
  \rlarrow{\lambda}{2\mu}&\fbox{5}&\,\cdots\\
\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
  \darrow{2\theta}&&\darrow{3\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{3\mu}&\fbox{4}&
  \rlarrow{\lambda}{3\mu}&\fbox{5}&\,\cdots\\
\uarrow{4\sigma}&&\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
  \darrow{2\theta}&\\
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{4\mu}&\fbox{4}&
  \rlarrow{\lambda}{4\mu}&\fbox{5}&\,\cdots\\
  \vdots&&\vdots&&\vdots&&\vdots&&\vdots&&\vdots&
}
\end{document}

enter image description here

I can get it to compile in about half the time, if I change the TABstack to a very customized array.

\documentclass{article}
\usepackage{tikz,amsmath,array}
\usetikzlibrary{automata,positioning}
\newcommand\rlarrow[2]{\smash{\vcenter{\hbox{\kern-5pt%
\begin{tikzpicture}
        \node             (0) {};
        \node[right=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {$#1$} (1)
            (1) edge[bend left, auto=left] node {$#2$} (0);
\end{tikzpicture}%
\kern-5pt}}}}
\newcommand\rarrow[1]{\kern-5pt\smash{%
\begin{tikzpicture}
        \node             (0) {};
        \node[right=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {$#1$} (1);
\end{tikzpicture}%
}\kern-5pt}
\newcommand\uarrow[1]{\makebox[0pt]{%
\begin{tikzpicture}
        \node             (0) {};
        \node[below=of 0] (1) {};
        \draw[every loop]
            (1) edge[bend left, auto=left] node {\makebox[0pt][r]{$#1$}} (0);
\end{tikzpicture}%
\kern5pt}}
\newcommand\darrow[1]{\makebox[0pt]{\kern5pt%
\begin{tikzpicture}
        \node             (0) {};
        \node[below=of 0] (1) {};
        \draw[every loop]
            (0) edge[bend left, auto=left] node {\makebox[0pt][l]{$#1$}} (1);
\end{tikzpicture}%
}}
\begin{document}
\arraycolsep=0pt
\renewcommand\arraystretch{0}
$\begin{array}{cccccccccccc}
\fbox{0}&\rarrow{\lambda}&\fbox{1}&\rarrow{\lambda}&\fbox{2}&\rarrow{\lambda}&
  \fbox{3}&\rarrow{\lambda}&\fbox{4}&\rarrow{\lambda}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&\darrow{3\theta}&&\darrow{4\theta}&&
  \darrow{5\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{\mu}&\fbox{2}&
  \rlarrow{\lambda}{\mu}&\fbox{3}&\rlarrow{\lambda}{\mu}&\fbox{4}&
  \rlarrow{\lambda}{\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&\darrow{2\theta}&&
  \darrow{3\theta}&&\darrow{4\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{2\mu}&\fbox{3}&\rlarrow{\lambda}{2\mu}&\fbox{4}&
  \rlarrow{\lambda}{2\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
  \darrow{2\theta}&&\darrow{3\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{3\mu}&\fbox{4}&
  \rlarrow{\lambda}{3\mu}&\fbox{5}&\,\cdots\\[-5pt]
\uarrow{4\sigma}&&\uarrow{3\sigma}&&\uarrow{2\sigma}&&\uarrow{\sigma}&&\darrow{\theta}&&
  \darrow{2\theta}&\\[-5pt]
\fbox{0}&\rlarrow{\lambda}{\mu}&\fbox{1}&\rlarrow{\lambda}{2\mu}&\fbox{2}&
  \rlarrow{\lambda}{3\mu}&\fbox{3}&\rlarrow{\lambda}{4\mu}&\fbox{4}&
  \rlarrow{\lambda}{4\mu}&\fbox{5}&\,\cdots\\[-2pt]
  \vdots&&\vdots&&\vdots&&\vdots&&\vdots&&\vdots&
\end{array}$
\end{document}

enter image description here


A \foreach-only solution.

I think to have found a rule also for the \mus.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{%
    state/.style={draw, rectangle, minimum height=2em, minimum width=2em},
    >=stealth
}

\begin{document}
\begin{figure}
\resizebox{\textwidth}{!}{%
    \begin{tikzpicture}
        % nodes, dots and lamba arrows
        \foreach \linenum in {1,2,...,5}{%
            \foreach \nodenum in {0,1,...,5}{%
                \node[state] at (2.5*\nodenum,2-2.5*\linenum) (N-\linenum-\nodenum) {\nodenum};
            }
            \foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1)] in {0,1,...,4}{% 
                \draw[->] (N-\linenum-\nodenum) edge[bend left, auto=left] node {$\lambda$} (N-\linenum-\nextnode);
            }
            \node[right=1em of N-\linenum-5] {\Huge \dots}; 
        }
        \foreach \nodenum in {0,1,...,5}{%
            \node[below=-1ex of N-5-\nodenum, scale=2] {$\vdots$}; 
        }
        % mu arrows
        \foreach \linenum [evaluate=\linenum as \k using {ifthenelse(\linenum==2,,int(\linenum-1))},
                           evaluate=\linenum as \startnode using int(\linenum-2)] in {2,3,4,5} {%  
            \foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1)] in {\startnode,...,4} {%
                \draw[<-] (N-\linenum-\nodenum) edge[bend right, auto=right] node {$\mathstrut\k\mu$} (N-\linenum-\nextnode);
            }
        }
        \foreach \linenum [evaluate=\linenum as \endnode using int(\linenum-3)] in {4,5} {%  
            \foreach \nodenum [evaluate=\nodenum as \nextnode using int(\nodenum+1),
                               evaluate=\nodenum as \k using {ifthenelse(\nodenum==0,,int(\nodenum+1))}] in {0,...,\endnode} {%
                \draw[<-] (N-\linenum-\nodenum) edge[bend right, auto=right] node {$\mathstrut\k\mu$} (N-\linenum-\nextnode);
            }
        }
        % sigma arrows
        \foreach \linenum [evaluate=\linenum as \i using int(\linenum+1),
                           evaluate=\linenum as \endnode using int(\linenum-1)] in {1,2,3,4} {%
            \foreach \nodenum [evaluate=\nodenum as \k using {ifthenelse(int(\linenum-\nodenum)==1,,int(\linenum-\nodenum))}] in {0,...,\endnode}{%
                \draw[->] (N-\i-\nodenum) edge[bend left, auto=left] node {$\mathstrut\k\sigma$} (N-\linenum-\nodenum);
            }
        }
        % theta arrows
        \foreach \linenum [evaluate=\linenum as \nextline using int(\linenum+1)] in {1,2,...,4} {%
            \foreach \nodenum [evaluate=\nodenum as \k using {ifthenelse(int(\linenum)==int(\nodenum),,int(\nodenum-\linenum+1))}] in {\linenum,...,5} {%
                \draw[->] (N-\linenum-\nodenum) edge[bend left, auto=left] node {$\mathstrut\k\theta$} (N-\nextline-\nodenum);
            }               
        }   
    \end{tikzpicture}
}
\caption{De M/M/1-wachtrij.}
\end{figure}
\end{document}

enter image description here

Tags:

Tikz Pgf