**ProbaPerception**, and kindly contributed to R-bloggers)

As you will certainly see later on this blog, I am extremely interested in neural networks. My reference book is “Networks, crowds and markets: reasoning about a highly connected world” by David Easley and Jon Kleinberg who are professors at Cornell University.

In this simple representation of a neural network, each node is represented by a person, each edge by a dashed line. |

__The code (R):__#package installation

install.packages(“hydroTSM”)

library(hydroTSM)

#initialization

size = 200

q = matrix(0, nrow = size, ncol = size)

countMinus1 = matrix(0, nrow = size, ncol = size)

countPlus1 = matrix(0, nrow = size, ncol = size)

prob = runif(size*size)

for (i in 1:size){

for (j in 1:size){

if(prob[(i-1)*size + j]<0.4){ # row 1: column 1 to 200; then row 2: column 1 to 200; etc…

q[i,j] = -1 # 40% of type -1, randomly distributed.

}

if(prob[(i-1)*size + j]>0.5){

q[i,j] = 1 # 50% of type 1, randomly distributed.

} # 10 other % of type 0, randomly distributed.

}

}

#the recursive process

for (k in 1:50){ # 50 steps of population aggregation = 50 plots.

#the function file.path is really useful to save automatically many plots.

mypath <- file.path(“C:”,”Users”,”PCordier”,”Documents”,”Blog”,”Post4″,”Plots”, paste(“myplot_”, k, “.png”, sep = “”))

png(file=mypath)

mytitle = paste(“my title is”, k)

print(matrixplot(q)) # At each step, saving of the precedent plot.

dev.off() # k=0: initial random q matrix, then matrix result of the followings.

for(i in 1:size){

for(j in 1:size){

value = q[i,j]

if(i==1){ # row 1

if(j==1){ # top left cell of the row 1 : one house with 3 neighbours

a = table(c(q[i+1,j], q[i,j+1], q[i+1, j+1])) # Number of 0, -1, 1 among the 3 surrounding houses (max = 3!).

if(“-1″ %in% names(a)){ # If there are some -1, there is a column with name -1.

countMinus1[i,j] = a[names(a)==-1]} # Number of -1 around the house of coordinates 1,1.

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){ # Same for number of 1 in the neighbourhood.

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else if(j==size){ # top right cell of the row 1 : one house with 3 neighbours

a = table(c(q[i+1,j], q[i,j-1], q[i+1, j-1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else{ # all top middle cells : 198 houses with 5 neighbours

a = table(c(q[i+1,j], q[i+1,j+1],q[i,j-1],q[i,j+1], q[i+1, j-1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

}

else if(i==size){ # last row

if(j==1){ # bottom left : one house with 3 neighbours

a = table(c(q[i-1,j], q[i,j+1], q[i-1, j+1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else if(j==size) # bottom right : one house with 3 neighbours

a = table(c(q[i-1,j], q[i,j-1], q[i-1, j-1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else{ # bottom center : 198 houses with 5 neighbours

a = table(c(q[i-1,j], q[i-1,j+1],q[i,j-1],q[i,j+1], q[i-1, j-1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

}

else{

if(j==1){ # center left : 198 houses with 5 neighbours

a = table(c(q[i-1,j], q[i+1,j], q[i-1,j+1],q[i,j+1],q[i+1,j+1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else if(j==size){ # center right : 198 houses with 5 neighbours

a = table(c(q[i-1,j], q[i+1,j], q[i-1,j-1],q[i,j-1],q[i+1,j-1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

else{ # center center : 39204 houses with 8 neighbours

a = table(c(q[i-1,j], q[i+1,j], q[i-1,j-1],q[i,j-1],q[i+1,j-1], q[i-1,j+1],q[i,j+1],q[i+1,j+1]))

if(“-1″ %in% names(a)){

countMinus1[i,j] = a[names(a)==-1]}

else{countMinus1[i,j] = 0}

if(“1″ %in% names(a)){

countPlus1[i,j] = a[names(a)==1]}

else{countPlus1[i,j]= 0}

}

}

}

}

listEmpty = which(q == 0)

listMissingMinus1 = which( countMinus1 < 4)

listMissingPlus1 = which( countPlus1 < 4)

listToMovePlus = which((countPlus1 < 4 & q == 1))

listToMoveMinus = which((countMinus1 < 4 & q == -1))

listOfPlacePlus = which((countPlus1 >=4 & q == 0))

listOfPlaceMinus = which((countMinus1 >= 4 & q == 0))

while (length(listToMovePlus)!=0 && length(listOfPlacePlus) != 0){

number = sample(1:length(listOfPlacePlus), 1)

number2 = sample(1:length(listToMovePlus), 1)

q[listToMovePlus[number2]] = 0

q[listOfPlacePlus[number]] = 1

listToMovePlus = listToMovePlus[-number2]

listOfPlacePlus = listOfPlacePlus[-number]

}

while (length(listToMoveMinus)!=0 && length(listOfPlaceMinus) != 0){

number = sample(1:length(listOfPlaceMinus), 1)

number2 = sample(1:length(listToMoveMinus), 1)

q[listToMoveMinus[number2]] = 0

q[listOfPlaceMinus[number]] = -1

listToMoveMinus = listToMoveMinus[-number2]

listOfPlaceMinus = listOfPlaceMinus[-number]

}

}

**leave a comment**for the author, please follow the link and comment on their blog:

**ProbaPerception**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...