# Residential Buildings Complex Boundaries Generation Based on Spatial Grid System
## 4 Algorithm
### Generation of 5 by 5 Array  
The first stage of the method generates a random array using a random number generator. The only input into the algorithm is the desired area of the building. In the first stage, that number is divided into a 5 by 5 array. In order to get valid results, new rules are applied. Every cell of the 5 by 5 array will be transformed into 16 cells in the final 20 by 20 solution. The first rule applied states that any cell cannot have a value greater than 16 or less than 0. If one cell of this array is \(c_{(x,y)}\) then we can use the expression:  
\[0\ \leq\ \forall c_{(x,y)}\ \leq\ 16 \tag{2}\]  
The next constraint is that the middle cell of the array \(Lf_{(2,2)}\) is always 16. This means that in the final array the middle of the layout is always labeled as part of the building (expression (3)). \[\left[ \begin{array}{ccccc}c_{(0,0)}&c_{(1,0)}&c_{(2,0)}&c_{(3,0)}&c_{(4,0)} \\ c_{(0,1)}&c_{(1,1)}&c_{(2,1)}&c_{(3,1)}&c_{(4,1)}\\ c_{(0,2)}&c_{(1,2)}&\mathbf{16}&c_{(3,2)}&c_{(4,2)}\\ c_{(0,3)}&c_{(1,3)}&c_{(2,3)}&c_{(3,3)}&c_{(4,3)}\\ c_{(0,4)}&c_{(1,4)}&c_{(2,4)}&c_{(3,4)}&c_{(4,4)}\\ \end{array} \right] \tag{3}\]  
Figure 4: The general algorithm. As the consequence, in the final 20 by 20 array, not all corners will be labeled as part of the building. In the 5 by 5 array the corner cells are as \(C=\{C_{1},C_{2},C_{3},C_{4}\}\) and 8 middle cells as \(M=\{M_{1},M_{2},M_{3},M_{4}\}\) as in showed in expression (2). \[\left[ \begin{array}{c|c|c|c|c|c|c}C_{1}&\cdots&\cdots&M_{1}&\cdots&C_{2}& \cdots&\cdots\\ M_{2}&\cdots&\cdots&\mathbf{16}&M_{3}&\cdots&\cdots\\ C_{3}&\cdots&\cdots&M_{4}&\cdots&\cdots&C_{4}&\cdots&\cdots\\ \end{array} \right] \tag{4}\]  
Their corresponding values are \(C_{v}\) for the sum of \(C_{1},C_{2},C_{3}\), and \(C_{4}\) and \(M_{v}\) for the sum of \(M_{1},M_{2},M_{3}\), and \(M_{4}=\) Relationship of values of all corner cells \(C_{v}\) and middle cells \(M_{v}\) is defined by the formulas:  
\[C_{v}=\lfloor N*0.6-15\rfloor \tag{5}\]  
\[M_{v}=N-16-C_{v} \tag{6}\]  
where \(N\) represents the area of the building. The next step was to get unique random solutions for the layout. This is solved by the generation of four random real numbers \(r_{1}\), \(r_{2}\), \(r_{3}\), and \(r_{4}\) where \(\{r_{n}\in\mathbb{R}\mid 0\leq\ r_{n}\leq\ 1\}\) which is applied in order to get values of cells in four corners of the array. \(C_{v1},C_{v2},C_{v3}\), and \(C_{v4}\) are values of cells \(C_{1}\), \(C_{2}\), \(C_{3}\), and \(C_{4}\) respectively. Their value is calculated by the formula:  
\[C_{vn}=\left\lfloor C_{v}*\frac{r_{n}}{\sum_{i=1}^{4}r_{i}}\right\rfloor\mid n =\{1,\ 2,\ 3,\ 4\} \tag{7}\]  
This will determine where the most cells labeled as built are in the final solution. If \(C_{vn}\) is bigger than 49, then the value is 49 and the difference is added to the other cells. The next step is to fill \(C_{vn}\) values into corner cells of a 5 by 5 array. This is accomplished by forming a set of 4 numbers \(\{c_{vn1},\ c_{vn2},\ c_{vn3},c_{vn4}\}\) which sum is the value of the cell \(C_{n}\). They are calculated by the rules in the equation:  
\[[c_{vn1},\ c_{vn2},\ c_{vn3},\ c_{vn4}]=\left\{ \begin{array}{c}[C_{vn},0,\ 0,\ 0]\ if\ C_{vn}\leq 16\\ [16,\ C_{vn1}-16,\ 0,\ 0]\ if\ C_{vn}\leq 32\\ [16,\ 16,\ C_{vn}-16,\ 0]\ if\ C_{vn}\leq 48\\ [16,\ 16,\ 16,\ C_{vn}-16]otherwise\end{array} \right. \tag{8}\]  
This list is then transformed so the biggest values are closest to the middle of the array, and the lowest is at the corners. The other two cells are randomly placed in the other two places for \(C_{n}\) values. This is illustrated by the next formula:  
\[\left[ \begin{array}{ccccc}\cdots&\cdots&\cdots&\cdots&\cdots\\ \cdots&\cdots&\cdots&\cdots&\cdots\\ \cdots&\cdots&\cdots&\cdots&\cdots\\ \cdots&\mathbf{16}&\cdots&\cdots\\ \cdots&\cdots&\cdots&\cdots\\ \cdots&\cdots&\cdots&\cdots\\ \end{array} \right]\rightarrow\left[