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 122 123 124 125 126 127
| #{Algorithm 1}
#{Vertices and Attributes generation}
N <- 100
V <- sample(1:N, N, replace=FALSE) #V-> an arbitrary set of N Vertices
print(V)
#Choisir une valeur au hasard entre 1 et 100
for (i in 1:80)
{
v[i]=sample(1:100, 1, replace=FALSE)
}
Node<-v
# Vector of 100 values with a uniform distribution between 0 and 1
V_a=runif(100,0,1)
print(V_a)
# Creation of data frame Nodes-Mass
resultats <- data.frame(Nodes=V, Mass=V_a)
print(resultats)
#-----------------------------------------------------------------------
#{Community Inatialization}
#Algorithm 2
#{Build initial communities}
K=4
NbRep=8
V_init <- sample(V, K*NbRep, replace=FALSE)
print(V_init)
#data1 <- resultats[c(1:length(V_init)),] #Sous-dataframe de resultats
library(cluster)
P <-pam(V_init, K) #Custering des noeuds sans les attributs
#P <-pam(data1, K) #Clustering des noeuds avec les attributs
summary(P)
plot(silhouette(P), col = 2:5)
for (C in P)
MinRep <- min (length(C))
print(MinRep)
#{Resize the communities}
{
g <- (P$medoids) #Les centroides des clusters
print(g)
cc <-(P$clustering) #Le vecteur du clustering
print(cc)
#Fnction qui retourne les éléments du cluster
cluster<- function(cc,i)
{
a<- which(cc==i)
return (a)
}
for (i in 1:K)
{
print(cluster(cc,i))
}
library(fields)
#Fonction qui retourne la distance entre les éléments des clusters et les
#centroides
distance<- function(cc,i,g)
{
b<- rdist(which(cc==i),g)
return (b)
}
for (i in 1:K)
{
print(distance(cc,i,g))
}
#Fonction qui retourne la somme des distances dans chaque cluster
sum_distance<- function(cc,i,g)
{
d<- sum(rdist(which(cc==i),g))
return (d)
}
rslt<- matrix(NA,ncol=1,nrow=K)
for (i in 1:K)
{
rslt[i,]<- sum_distance(cc,i,g)
}
print(rslt)
dd<-which.min(rslt) #retourne l'indice du cluster ayant la somme des
#distances minimale
print(dd)
C <- which(cc==dd) #C contiendra les éléments du cluster ayant la somme
#des distances minimale
print(C)
RandUni <- function(E) {
S <- sample(1:E, 1, replace = FALSE)
return(S)
}
print(RandUni(80))
testRandUni <- function(C, E_maxWth = 80) {
singleIteration <- function(x) {
E_wth <- sapply(E_maxWth, RandUni)
print(E_wth)
lapply(seq_along(E_wth), function(y) {
v_prim <- RandUni(C[-which(C == x)][y])
data.frame(Nodes_V = Node, Nodes_V_prim = v_prim)
})
}
lapply(C, singleIteration)
}
#epsilon <- testRandUni(C) |
Partager