K1 <- read.csv("knapPI_15_50_1000.csv", header=FALSE, skip=5)
colnames(K1) <- c("Index","p","w","optx")
n <- 50
c <- 997

Define fitness function and total weight:

f <- function(x)
{ t(K1$p) %*% x }

tw <- function(x)  #total weight
{ t(K1$w) %*% x  }

Define distance function:

distance <- function(x)
{
    opt <- K1$optx
    norm(opt-x, type = "2")
}

Sampling

totalFDC <- numeric()
totalfv <- numeric()

## Sampling
for(rep in 1:30)
{
    maxstep <- 5000
    fv <- numeric(maxstep)
    dv <- numeric(maxstep)
    recordx <- matrix(nrow = 50, ncol = maxstep)
    x <- numeric(50)
    
    i <- 1
    while(i <= maxstep)
    {
        index <- sample(1:50,1)
        if(x[index]==1)
        {
            x[index] <- 0
            recordx[,i] <- x
            fv[i] <- f(x)
            dv[i] <- distance(x)
            i <- i+1
        }
        else if(tw(x)+K1[index,"w"]<=c)
        {
            x[index] <- 1
            recordx[,i] <- x
            fv[i] <- f(x)
            dv[i] <- distance(x)
            i <- i+1
        }
    }
    s <- seq(1025, 5000, by=25)
    selectx <- recordx[,s]
    sfv <- fv[s]
    sdv <- dv[s]
    nsample <- 160
    
    ## FDC
    fvbar <- mean(sfv)
    dvbar<- mean(sdv)
    sigmaF<- sd(sfv)
    sigmaD <- sd(sdv)
    C_FD <- sum((sfv-fvbar)*(sdv-dvbar))/nsample
    FDC<- C_FD/(sigmaF*sigmaD)
    totalFDC <- c(totalFDC, FDC)
    totalfv <- c(totalfv, sfv)
}

Plot fitness

t.test(totalFDC)
## 
##  One Sample t-test
## 
## data:  totalFDC
## t = 17.526, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.1890147 0.2389572
## sample estimates:
## mean of x 
##  0.213986
library(ggplot2)
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
step <- 1:160
plotf <- cbind(step,totalfv)
plotf <- as.data.frame(plotf)

png("A4CI3.png")
g <- ggplot(plotf, aes(x=step, y=totalfv))+
    stat_summary(geom="ribbon", fun.data=mean_cl_normal, 
                 fun.args=list(conf.int=0.95), fill="lightblue")+
    stat_summary(geom="line", fun.y=mean, linetype="dashed")+
    ylab("Average Fitness Value") + xlab("Walk Length")
print(g)
dev.off()
## png 
##   2
autolen<-numeric()
for(rep in 1:30)
{
    rangei <- ((rep-1)*160+1):(rep*160)
    t <- plotf[rangei,"totalfv"]
    fbar <- mean(t)
    e<-0
    for(i in 1:159)
        e <- e + (t(i)-fbar)*(t(i+1)-fbar)
    e <- e/159
    ro1 <- e/var(t)
    autolen <- c(autolen, 1/log(ro1, base = exp(1)))
}
print(autolen)
##  [1] 0.4053184 0.4252736 0.4976966 0.3772432 0.4675330 0.3933212 0.4320900
##  [8] 0.4134674 0.4079911 0.4461971 0.4287728 0.4590764 0.4155634 0.3982518
## [15] 0.4289570 0.4200513 0.4260797 0.3866668 0.3919791 0.4174925 0.4790190
## [22] 0.4680795 0.4329612 0.4044391 0.3772145 0.4598303 0.3953502 0.3771849
## [29] 0.4622186 0.4277237
t.test(autolen)
## 
##  One Sample t-test
## 
## data:  autolen
## t = 72.689, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.4121687 0.4360342
## sample estimates:
## mean of x 
## 0.4241014
vH <- numeric()
s <- numeric(159)
eps <- 10

getH <- function(s,H,p,q)
{
    count<-0
    for(i in 1:158)
        if(s[i]==p && s[i+1]==q)
            count <- count+1
    if(count>0)
        H <- H - count/159*log(count/159,base=6)
    return(H)
}

for(rep in 1:30)
{
    rangei <- ((rep-1)*160+1):(rep*160)
    t <- plotf[rangei,"totalfv"]
    fbar <- mean(t)
    for(i in 1:159)
        if(t[i+1]>t[i]+eps)
            s[i] <- 1
        else if (t[i+1] < t[i]-eps)
            s[i] <- -1
        else s[i] <-0
    H <- 0
    H <- getH(s,H,-1,0)
    H <- getH(s,H,-1,1)
    H <- getH(s,H,0,-1)
    H <- getH(s,H,0,1)
    H <- getH(s,H,1,-1)
    H <- getH(s,H,1,0)
    vH <- c(vH, H)
}
print(vH)
##  [1] 0.5195586 0.5800791 0.6404194 0.5499643 0.5375586 0.6474600 0.5095435
##  [8] 0.6111295 0.6267694 0.5727292 0.5579891 0.5271194 0.6129875 0.5294719
## [15] 0.5296985 0.5301172 0.4810578 0.4701365 0.6382783 0.5696979 0.5861700
## [22] 0.6266559 0.6306721 0.5417109 0.5282683 0.5831172 0.6139420 0.5844337
## [29] 0.4991543 0.5279010
t.test(vH)
## 
##  One Sample t-test
## 
## data:  vH
## t = 61.71, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.5467190 0.5842004
## sample estimates:
## mean of x 
## 0.5654597
vM <- numeric()
s <- numeric(159)
eps <- 10
for(rep in 1:30)
{
    rangei <- ((rep-1)*160+1):(rep*160)
    t <- plotf[rangei,"totalfv"]
    for(i in 1:159)
        if(t[i+1]>t[i]+eps)
            s[i] <- 1
        else if (t[i+1] < t[i]-eps)
            s[i] <- -1
        else s[i] <-0
    M <- 0
    j <- 0
    k <- 0
    i <- 1 
    
    while(1)
    {
        if(i>159) 
        {
            M<-k/159
            break
        }
        else if(j==0 && (s[i]!=0))
        {
            j <- i
            k <- k+1
            i<-i+1
        }
        else if(j>0 && s[i]!=0 && s[i]!=s[j])
        {
            j <- i
            k <- k+1
            i<-i+1
        }
        else 
            i<-i+1
    }   
    vM <- c(vM, M)
}
print(vM)
##  [1] 0.6855346 0.6352201 0.6226415 0.6792453 0.6226415 0.6603774 0.6981132
##  [8] 0.6729560 0.6603774 0.6540881 0.6477987 0.6540881 0.6981132 0.6540881
## [15] 0.6037736 0.6855346 0.6981132 0.6855346 0.6855346 0.6918239 0.6540881
## [22] 0.6163522 0.6918239 0.6226415 0.6792453 0.6729560 0.7044025 0.6163522
## [29] 0.6100629 0.6981132
t.test(vM)
## 
##  One Sample t-test
## 
## data:  vM
## t = 116.87, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.6504682 0.6736409
## sample estimates:
## mean of x 
## 0.6620545
vh <- numeric()
s <- numeric(159)
eps <- 10

geth <- function(s,h,p)
{
    count<-0
    for(i in 1:158)
        if(s[i]==p && s[i+1]==p)
            count <- count+1
    if(count>0)
        h <- h - count/159*log(count/159,base=3)
    return(h)
}

for(rep in 1:30)
{
    rangei <- ((rep-1)*160+1):(rep*160)
    t <- plotf[rangei,"totalfv"]
    for(i in 1:159)
        if(t[i+1]>t[i]+eps)
            s[i] <- 1
        else if (t[i+1] < t[i]-eps)
            s[i] <- -1
        else s[i] <-0
    h <- 0
    h <- geth(s,h,1)
    h <- geth(s,h,0)
    h <- geth(s,h,-1)
    vh <- c(vh, h)
}
print(vh)
##  [1] 0.5031644 0.5286329 0.5081466 0.4852288 0.5435892 0.4621592 0.4892639
##  [8] 0.5307101 0.5182822 0.5138768 0.5318893 0.5274839 0.4609518 0.5198673
## [15] 0.5596736 0.5132999 0.4979435 0.5193467 0.4666820 0.4744514 0.5404483
## [22] 0.5112690 0.4493163 0.5392474 0.5091435 0.5223589 0.4618578 0.5370606
## [29] 0.6060460 0.4940171
t.test(vh)
## 
##  One Sample t-test
## 
## data:  vh
## t = 83.404, df = 29, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.4983199 0.5233739
## sample estimates:
## mean of x 
## 0.5108469