1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#ifndef NEIGHBOUR_HDR
#define NEIGHBOUR_HDR
#include <iostream>
int n {100000};
int f = sqrt(n);
#endif
#include <iostream>
#include <valarray>
#include <cmath>
#include <random>
#include <chrono>
#include "neighbour.h"
//defining grid
class Grid{
private:
int nr_rows_;
int nr_cols_;
std::valarray<int> matrix_;
public:
Grid(const int nr_rows, const int nr_cols):
nr_rows_{nr_rows},
nr_cols_{nr_cols},
matrix_(1, nr_rows*nr_cols) {}
int& operator()(const int row, const int col){ return matrix_[nr_cols_*row+col];}
int nr_rows() const { return nr_rows_;}
int nr_cols() const { return nr_cols_;}
};
void predictMatrix(Grid& grid,
int range0a,
int range0b,
Grid& grid2)
{
int c = 0;
int t = 1;
while (t<n){
for (int i = 1 ; i <(2*f+1) -1 ; i++) {
for (int j = 1 ; j <(2*f+1) -1 ; j++) {
c = 0;
// Het tellen van de buren met een 0
if (i > 0 && grid(i + 1, j) == 0)
c++;
if (j > 0 && grid(i, j - 1) == 0)
c++;
if (i < (2*f+1) - 1 && grid(i - 1, j) == 0)
c++;
if (j < (2*f+1) - 1 && grid(i , j + 1) == 0)
c++;
//random number generator
std::random_device rd;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::ranlux48 generator (seed);
double g = generator();
double m = generator.max();
double p = g/m;
// Het opstellen van de kans op een huis ivm het aantal buren
if (t<n && grid(i,j) == 1 && c >= range0a && c <= range0b) {
if (c==1 && p<=0.25) {grid2(i,j)=0 && t++;}
if (c==2 && p<=0.5) {grid2(i,j)=0 && t++;}
if (c==3 && p<=0.75) {grid2(i,j)=0 && t++;}
if (c==4) {grid2(i,j) = 0 && t++;}
}
t=0;
for (int i = 1 ; i <(2*f+1) -1 ; i++) {
for (int j = 1 ; j <(2*f+1) -1 ; j++) {
if (grid2(i,j)==0)
t++;
}
}
}
}
// De veranderingen overbrengen naar de matrix
for (int k = 0; k < 2*f+1; k++)
for (int m = 0; m < 2*f+1; m++)
grid(k, m) = grid2(k,m);
}
}
int main()
{
Grid grid(2*f+1,2*f+1);
// Grootte van het grid werd bepaald zodat deze in de meeste situaties groot genoeg zou zijn zonder te veel opslag in beslag te nemen
grid (f,f)=0;
int range0a = 1;
int range0b = 4;
Grid grid2(2*f+1,2*f+1);
grid2 (f,f)=0;
// Function call to calculate the resultant matrix after 'n' iterations.
predictMatrix(grid, range0a, range0b, grid2);
//Printing Result
for (int i=0; i< (2*f+1); i++) {
std::cout << std::endl;
for (int j=0; j<(2*f+1); j++)
std::cout << grid2(i,j) << " ";
}
return 0;
}
|