# code to generate Figure 1 and Table 2

# clean setting
source("fjofc.R")
require(mclust)

set.seed(12345)

# rt1= runtime for fJOFC
# rt2= runtime for INDSCAL

#rt1<-matrix(0,25,1)
#rt2<-matrix(0,25,1)

# stress1= final stress for fJOFC
# stress2= final stress for INDSCAL

stress1<-matrix(0,25,1)
stress2<-matrix(0,25,1)

# ari1= ARI for fJOFC
# ari2= ARI for INDSCAL

ari1<-matrix(0,25,1)
ari2<-matrix(0,25,1)

for(j in 1:25){
  
  X0 <- matrix(rnorm(400*2,5,1),400,2)
  X<-list()
  for(l in 1:3){ X[[l]]=jitter(X0,factor=1,amount=0)}
  DX <- list()
  for(l in 1:3){ DX[[l]]=as.matrix(dist(X[[l]]))}
  t<-proc.time();smds1 <- fJOFC(200,DX,2,verbose=FALSE);runtime1<-proc.time()-t
  t<-proc.time();smds2<-indscal(DX, ndim = 2,verbose=FALSE);runtime2<-proc.time()-t
  
#  rt1[j]<-runtime1
#  rt2[j]<-runtime2
  
  stress1[j]<-smds1$stress
  stress2[j]<-smds2$stress
  
  xx<-smds1$conf
  
  yy<-smds2$conf
  yy<-rbind(yy[[1]],yy[[2]],yy[[3]])
  
  
  r1<-kmeans(xx,400)$cluster
  r2<-kmeans(yy,400)$cluster
  rr<-c(1:400,1:400,1:400)
  
  ari1[j]<-adjustedRandIndex(r1,rr)
  ari2[j]<-adjustedRandIndex(r2,rr)
}
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
# fJOFC running time mean
#mean(rt1)
# INDSCAL running time mean
#mean(rt2)

# fJOFC ari mean
mean(ari1)
## [1] 0.6691634
# INDSCAL ari mean
mean(ari2)
## [1] 0.6984686
# fJOFC stress mean
mean(stress1)
## [1] 0.03540601
# INDSCAL stress mean
mean(stress2)
## [1] 0.04133163
#######################################################

# anomaly setting

set.seed(12345)

# rt1= runtime for fJOFC
# rt2= runtime for INDSCAL
#rt1<-matrix(0,25,1)
#rt2<-matrix(0,25,1)

# stress1= final stress for fJOFC
# stress2= final stress for INDSCAL
stress1<-matrix(0,25,1)
stress2<-matrix(0,25,1)

# ari1= ARI for fJOFC
# ari2= ARI for INDSCAL
ari1<-matrix(0,25,1)
ari2<-matrix(0,25,1)

# confratio1= the configuration ration for fJOFC
# confratio2= the configuration ration for INDSCAL
confratio1<-matrix(0,25,1)
confratio2<-matrix(0,25,1)

# anomalous indices
e<-c(391:400, 791:800, 1191:1200)

for(j in 1:25){
  
  X0 <- matrix(rnorm(400*2,5,1),400,2)
  Y0 <-matrix(rnorm(20,8,2),10,2)
  Y0 <-rbind(Y0,X0[11:400,])
  Y  <-list()
  for(l in 1:2){ Y[[l]]=jitter(X0,factor=1,amount=0)}
  Y[[ 3 ]]=jitter(Y0,factor=1,amount=0)
  DY <- list()
  for(l in 1:3){ DY[[l]]=as.matrix(dist(Y[[l]]))}
  t<-proc.time();smds1 <- fJOFC(200,DY,2,verbose=FALSE);runtime1<-proc.time()-t
  t<-proc.time();smds2<-indscal(DY, ndim = 2,verbose=FALSE);runtime2<-proc.time()-t

#  rt1[j]<-runtime1
#  rt2[j]<-runtime2
  
  xx2<-smds1$conf
  yy2<-smds2$conf
  yy2<-rbind(yy2[[1]],yy2[[2]],yy2[[3]])
  
  confratio1[j]<-((norm(xx2[1:11,]-xx2[401:411,],"f")+norm(xx2[1:11,]-xx2[801:811,],"f")+norm(xx2[1:11,]-xx2[801:811,],"f"))*390)/((norm(xx2[11:400,]-xx2[411:800,],"f")+norm(xx2[11:400,]-xx2[811:1200,],"f")+norm(xx2[411:800,]-xx2[811:1200,],"f"))*10)
  confratio2[j]<-((norm(yy2[1:11,]-yy2[401:411,],"f")+norm(yy2[1:11,]-yy2[801:811,],"f")+norm(yy2[1:11,]-yy2[801:811,],"f"))*390)/((norm(yy2[11:400,]-yy2[411:800,],"f")+norm(yy2[11:400,]-yy2[811:1200,],"f")+norm(yy2[411:800,]-yy2[811:1200,],"f"))*10)
  
  rr1<-kmeans(xx2[-e,],390)$cluster
  rr2<-kmeans(yy2[-e,],390)$cluster
  rrr<-c(1:390,1:390,1:390)
  ari1[j]<-adjustedRandIndex(rr1,rrr)
  ari2[j]<-adjustedRandIndex(rr2,rrr)
  
  stress1[j]<-smds1$stress
  stress2[j]<-smds2$stress

}
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
## Warning in smacofIndDiff(delta = delta, ndim = ndim, type = type,
## constraint = "indscal", : Iteration limit reached! Increase itmax argument!
# fJOFC running time mean
#mean(rt1)
# INDSCAL running time mean
#mean(rt2)

# fJOFC ari mean
mean(ari1)
## [1] 0.5668607
# INDSCAL ari mean
mean(ari2)
## [1] 0.394983
# fJOFC stress mean
mean(stress1)
## [1] 0.1563305
# INDSCAL stress mean
mean(stress2)
## [1] 0.1825665
# fJOFC confratio mean
mean(confratio1)
## [1] 76.34332
# INDSCAL confratio mean
mean(confratio2)
## [1] 10.39238
#######################################################################

# creating the plots for figure 1
# top left panel

plot(xx[1:400,])
points(xx[401:800,])
points(xx[801:1200,])
for(i in 1:400){segments(xx[i,1],xx[i,2],xx[400+i,1],xx[400+i,2],col='blue')}
for(i in 1:400){segments(xx[400+i,1],xx[400+i,2],xx[800+i,1],xx[800+i,2],col='blue')}
for(i in 1:400){segments(xx[i,1],xx[i,2],xx[800+i,1],xx[800+i,2],col='blue')}

#top right panel

plot(yy[1:400,])
points(yy[401:800,])
points(yy[801:1200,])
for(i in 1:400){segments(yy[i,1],yy[i,2],yy[400+i,1],yy[400+i,2],col='blue')}
for(i in 1:400){segments(yy[400+i,1],yy[400+i,2],yy[800+i,1],yy[800+i,2],col='blue')}
for(i in 1:400){segments(yy[i,1],yy[i,2],yy[800+i,1],yy[800+i,2],col='blue')}

#bottom left panel

plot(xx2[1:400,],xlim=c(-4,3), ylim=c(-4,3))
points(xx2[401:800,])
points(xx2[801:1200,])
for(i in 1:400){segments(xx2[i,1],xx2[i,2],xx2[400+i,1],xx2[400+i,2],col='blue')}
for(i in 1:10){segments(xx2[400+i,1],xx2[400+i,2],xx2[800+i,1],xx2[800+i,2],col='red')}
for(i in 1:10){segments(xx2[i,1],xx2[i,2],xx2[800+i,1],xx2[800+i,2],col='red')}
for(i in 11:400){segments(xx2[400+i,1],xx2[400+i,2],xx2[800+i,1],xx2[800+i,2],col='blue')}
for(i in 11:400){segments(xx2[i,1],xx2[i,2],xx2[800+i,1],xx2[800+i,2],col='blue')}

#bottom right panel

plot(yy2[1:400,],xlim=c(-2,2), ylim=c(-2,2))
points(yy2[401:800,])
points(yy2[801:1200,])
for(i in 1:400){segments(yy2[i,1],yy2[i,2],yy2[400+i,1],yy2[400+i,2],col='blue')}
for(i in 1:10){segments(yy2[400+i,1],yy2[400+i,2],yy2[800+i,1],yy2[800+i,2],col='red')}
for(i in 1:10){segments(yy2[i,1],yy2[i,2],yy2[800+i,1],yy2[800+i,2],col='red')}
for(i in 11:400){segments(yy2[400+i,1],yy2[400+i,2],yy2[800+i,1],yy2[800+i,2],col='blue')}
for(i in 11:400){segments(yy2[i,1],yy2[i,2],yy2[800+i,1],yy2[800+i,2],col='blue')}