genetic_algorithm


\documentclass[margin=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
\usetikzlibrary{calc, shapes.geometric,shapes.symbols,fit,positioning,shadows, arrows,automata}


\begin{document}


\tikzstyle{scale all}=[every node/.style={scale=#1}, scale=#1]

\tikzset{
	solution/.pic = {
		\begin{scope}
			\draw (0, 0) rectangle ++(1, 1);
			\draw (0, 1) rectangle ++(1, 1);
			\draw (0, 2) rectangle ++(1, 1);
			\draw (0, 3) rectangle ++(1, 1);
			\node[anchor=center] at (0.5, 0.5) {$w_{n}$};
			\node[anchor=center] at (0.5, 1.5) {$\ldots$};
			\node[anchor=center] at (0.5, 2.5) {$\ldots$};
			\node[anchor=center] at (0.5, 3.5) {$w_1$};
		\end{scope};
	},
	population with number/.pic = {
		\begin{scope}
			\draw node at (0.5,4.75) {$P_1$};
			\draw node at (2,4.75) {$P_2$};
			\draw node at (5,4.75) {$P_k$};
			\draw node at (-1,-0.75) {Error:};
			
			\draw (0, 0) pic[blue] {solution} node at (0.5,-0.75) {$0.12$};
			\draw (1.5, 0) pic[Magenta] {solution} node at (2,-0.75) {$0.65$};
			\draw (4.5, 0) pic[YellowOrange] {solution} node at (5,-0.75) {$0.8$};
			\draw (3, 2) circle (0.1);
			\draw (3.5, 2) circle (0.1);
			\draw (4, 2) circle (0.1);
			\draw (0.3, 4.3) -- (-0.3, 4.3) -- (-0.3, -0.3) -- (0.3, -0.3);
			\draw (5.2, 4.3) -- (5.8, 4.3) -- (5.8, -0.3) -- (5.2, -0.3);
		\end{scope}
	},
	population/.pic = {
		\begin{scope}
			\draw (0, 0) pic {solution};
			\draw (1.5, 0) pic {solution};
			\draw (4.5, 0) pic {solution};
			\draw (3, 2) circle (0.1);
			\draw (3.5, 2) circle (0.1);
			\draw (4, 2) circle (0.1);
			\draw (0.3, 4.3) -- (-0.3, 4.3) -- (-0.3, -0.3) -- (0.3, -0.3); % left square bracket
			\draw (5.2, 4.3) -- (5.8, 4.3) -- (5.8, -0.3) -- (5.2, -0.3); % right square bracket
		\end{scope}
	},
	population selection/.pic = {
		\begin{scope}
			\draw (0, 0) pic {solution};
			\draw (1.5, 0) pic {solution};
			\draw[#1] (4.5, 0) pic {solution};
			\draw (7.5, 0) pic {solution};
			
			\draw (3, 2) circle (0.1);
			\draw (3.5, 2) circle (0.1);
			\draw (4, 2) circle (0.1);
			
			\draw (6, 2) circle (0.1);
			\draw (6.5, 2) circle (0.1);
			\draw (7, 2) circle (0.1);
			
			\draw (0.3, 4.3) -- (-0.3, 4.3) -- (-0.3, -0.3) -- (0.3, -0.3);
			\draw (8.2, 4.3) -- (8.8, 4.3) -- (8.8, -0.3) -- (8.2, -0.3);
		\end{scope}
	},
	population sorted/.pic = {
		\begin{scope}
			\draw[blue] (0, 0) pic {solution} node at (0.5,-0.75) {0.1};
			\draw[PineGreen] (1.5, 0) pic {solution} node at (2,-0.75) {0.12};
			\draw[Maroon] (4.5, 0) pic {solution} node at (5,-0.75) {$T-\varepsilon$};
			\draw[red] (6, 0) pic {solution} node at (6.5,-0.75) {$T+\varepsilon$};
			\draw[red] (9, 0) pic {solution} node at (9.5,-0.75) {1};
			
			\draw (3, 2) circle (0.1);
			\draw (3.5, 2) circle (0.1);
			\draw (4, 2) circle (0.1);
			
			\draw[red] (7.5, 2) circle (0.1);
			\draw[red] (8, 2) circle (0.1);
			\draw[red] (8.5, 2) circle (0.1);
			
			\draw (0.3, 4.3) -- (-0.3, 4.3) -- (-0.3, -0.3) -- (0.3, -0.3);
			\draw (10.2, 4.3) -- (10.8, 4.3) -- (10.8, -0.3) -- (10.2, -0.3);
		\end{scope}
	},
	initial population/.pic = {
		% Initial population drawing
		\draw (0, -0.3) node {$\mathbf{P} = $};
		\draw (0.75, -1) pic [scale all=0.35] {population with number};
	},
	pics/gene mutation/.style args ={#1,#2}{
		code={
			% Mutate a gene with a mutation Mx
			\begin{scope}
				\draw (0, 0) rectangle ++(1, 1);
				\draw (0, 1) rectangle ++(1, 1);
				\draw[fill={#1}] (0, 2) rectangle ++(1, 1);
				\draw (0, 3) rectangle ++(1, 1);
				\draw (0, 4) rectangle ++(1, 1);
				\node[anchor=center] at (0.5, 0.5) {$m_{123}$};
				\node[anchor=center] at (0.5, 1.5) {$\ldots$};
				\node[anchor=center] at (0.5, 2.5) {{#2}};
				\node[anchor=center] at (0.5, 3.5) {$\ldots$};
				\node[anchor=center] at (0.5, 4.5) {$m_1$};
			\end{scope};
		}
	},
	mutate a gene/.pic = {
		\draw[->] (-1, 3.725) -- ++(0, -1.25) -- ++(0.75, 0) node [pos=0.25, scale=0.75, anchor=north, yshift=-2] {cloning};
		\draw (0, 1.575) pic [scale all=0.35] {gene mutation={yellow,$m_k$}};
		\draw[->] (0.75, 2.5) -- ++(1, 0) node [yshift=-2, scale=0.75, pos=0.5, anchor=north] {$\textbf{Mx}(m_k)$};
		\draw[fill=green] (2, 2.325) rectangle ++(0.35, 0.35);
		\node[anchor=center, scale=0.35] at (2.175, 2.495) {$m_{k}'$};
		\draw[->] (2.75, 2.5) -- ++(1, 0) node[pos=0.5, yshift=-2, scale=0.75, anchor=north] {mutation};
		\draw (4, 1.575) pic [scale all=0.35] {gene mutation={green, $m_k'$}};
		\draw[->] (4.175, 3.5) -- ++(0, 1.2) -- ++(-3.5, 0) node[pos=0.5, scale=0.75, anchor=north] {insertion};
	},
	pics/cluster population/.style args = {#1}{
		code = {
			\draw (0, 0) pic [scale all=0.35] {population};
			\draw (0.85, 2) node {{#1}};
		}
	},
	clusters populations/.pic = {
		\draw (0, 0) pic {cluster population={$\mathbf{P}_{G(s) = 0}$}};
		\draw[dashed] (2.225, -0.5) -- ++(0, 2.5) node [pos=0, anchor=north] {$ < 123$};
		\draw (2.5, 0) pic {cluster population={$\mathbf{P}_{G(s) = 1}$}};
		\draw[dashed] (4.725, -0.5) -- ++(0, 2.5) node [pos=0, anchor=north] {$ < 123$};
		\draw (5, 0) pic {cluster population={$\mathbf{P}_{G(s) = 2}$}};
		\draw (7.225, -0.5) -- ++(0, 2.5) node [pos=0, anchor=north] {$\geq 123$};
		\draw (7.5, 0) pic[red] {cluster population={$\mathbf{P}_{G(s) = 3}$}};
		\draw[red] (9.725, -0.5) -- ++(0, 2.5) node [pos=0, anchor=north] {$> 123$};
		
		\draw (-0.25, -1) -- (-0.25, -1.3) -- (7.225, -1.3) -- (7.225, -1);
	},
	new population/.pic = {
		\draw node at (0.5,4.75) {$C_1$};
		\draw node at (2,4.75) {$C_2$};
		\draw node at (5,4.75) {$C_{k-1}$};
		\draw node at (6.5,4.75) {$C_k$};
		
		\draw (0, 0) pic {double crossing=PineGreen/blue};
		\draw (1.5, 0) pic {double crossing=blue/PineGreen};
		\draw (4.5, 0) pic {double crossing=green/Maroon};;
		\draw (6, 0) pic {double crossing=Maroon/green};
		\draw (3, 2) circle (0.1);
		\draw (3.5, 2) circle (0.1);
		\draw (4, 2) circle (0.1);
		\draw (0.3, 4.5) -- (-0.3, 4.5) -- (-0.3, -0.3) -- (0.3, -0.3);
		\draw (6.7, 4.5) -- (7.3, 4.5) -- (7.3, -0.3) -- (6.7, -0.3);
		
	},
	pics/double crossing/.style args={#1/#2}{
		code={
			\draw[#2] (0, 0) rectangle ++(1, 1);
			\draw[#2] (0, 1) rectangle ++(1, 1);
			\draw[dash pattern={on 1pt off 1pt on 2pt off 1pt on 2pt off 1pt on 2pt off 1pt on 1pt}] (-0.1,2.1) -- ++(1.2,0);
			\draw[#1] (0, 2.2) rectangle ++(1, 1);
			\draw[#1] (0, 3.2) rectangle ++(1, 1);
			\node[anchor=center] at (0.5, 0.5) {\textcolor{#2}{$w_{n}$}};
			\node[anchor=center] at (0.5, 1.5) {\textcolor{#2}{$\ldots$}};
			\node[anchor=center] at (0.5, 2.7) {\textcolor{#1}{$\ldots$}};
			\node[anchor=center] at (0.5, 3.7) {\textcolor{#1}{$w_1$}};
		}
	},
	sorting cost/.pic = {
		\draw[<-] (2, 2) -- ++(0, 0.5);
		\draw (0, 0) pic[scale all=0.35] {population sorted};
		\draw[dashed, Red] (2, -0.5) -- ++(0, 2.25) node [pos=0, anchor=north, Red] {$ < T $};
		\draw (0, -0.5) node [anchor=center, scale=0.75] {$\min\,E(x)$};
		\draw (3.8, -0.5) node [anchor=center, scale=0.75] {$\max\,E(x)$};
		\draw[<-] (2, -1.6) -- ++(0, 0.5);
	}
}



\begin{tikzpicture}
\draw (0, 0) pic {initial population};
\draw (0, -4) pic {sorting cost};    
\draw (0.75,-7.5) pic[scale all=0.35] {new population};
\draw (3, -11.5) pic {mutate a gene};
\end{tikzpicture}
\end{document}