0
votes

Here is my code and I am wondering why there are warning messages saying "number of items to replace is not a multiple of replacement length"?

    library(GA)
    library(readxl)
    > data1 <- 
    read_excel("C:/Users/nadiahalim/OneDrive/CS954/Scheduling/data1.xlsx")
    > View(data1)
    > data1

output for data1

# A tibble: 6 x 4
   Jobj    Pj    Dj    Wj
  <dbl> <dbl> <dbl> <dbl>
1     1    25    61     3
2     2     7   102     7
3     3    42    86     1
4     4    36    44     3
5     5    18   150     1
6     6    29   134     4

I want to reorder Jobj in such away that it will give the minimum completion time denoted by Cj. For-loops has been used in my code as follow:

Cj=0
i<- sample(data1$Jobj)
for(i in 1:i){
fitness <-function(j){
for(j in data1$Jobj){
Cj[j] <- data1$Pj[j]+sum(Cj[j-1])
}
print(Cj)
}
fitness()

Output for this code

numerical expression has 6 elements: only the first used[1]  25  32  74 110 128 157
[1]  25  32  74 110 128 157
[1]  25  32  74 110 128 157
[1]  25  32  74 110 128 157
[1]  25  32  74 110 128 157

Then, I run for GA code:

GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6, maxiter= 5, run = 20, optim = TRUE)

output for GA as follow and the warning stated that number of items to replace is not a multiple of replacement length

    number of items to replace is not a multiple of replacement length[1]  25  32  74 110 128 157
number of items to replace is not a multiple of replacement lengthGA | iter = 3 | Mean = 25 | Best = 25
[1]  25  32  74 110 128 157
number of items to replace is not a multiple of replacement length[1]  25  32  74 110 128 157
number of items to replace is not a multiple of replacement length[1]  25  32  74 110 128 157

then I run for

summary(GA)

output for summary GA

-- Genetic Algorithm ------------------- 

GA settings: 
Type                  =  permutation 
Population size       =  50 
Number of generations =  5 
Elitism               =  2 
Crossover probability =  0.8 
Mutation probability  =  0.1 

GA results: 
Iterations             = 5 
Fitness function value = 25 
Solutions = 
      x1 x2 x3 x4 x5 x6
[1,]   4  1  6  3  5  2
[2,]   6  3  1  5  4  2
[3,]   6  2  3  4  1  5
[4,]   3  6  4  1  5  2
[5,]   4  6  2  1  5  3
[6,]   5  3  4  1  2  6
[7,]   2  1  6  3  5  4
[8,]   4  1  6  3  2  5
[9,]   3  6  1  2  5  4
[10,]  3  6  2  1  5  4
 ...                   
[35,]  4  5  6  2  1  3
[36,]  3  4  5  6  2  1

When I run for summary(GA), it seems work but I am curious about the warning messages.I would be grateful if someone could help me concerning this. Thank you in advance.

1
@RonakShah I have edit my post and make it more clear. Thank you. - NDH

1 Answers

0
votes

What you are trying to do in fitness function can easily be achieved without for loop.

For example, the output that you get from for loop is

#[1] 25  32  74 110 128 157

and using cumsum we get the same thing.

cumsum(data1$Pj)
#[1]  25  32  74 110 128 157

The main issue however, is the fitness function should return only one value and not a vector of values like cumsum. So if you change the function to something like this

fitness <- function(j) sum(j)

You can do :

fitness(data1$Pj)
#[1] 157

library(GA)
GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6, 
         maxiter= 5, run = 20, optim = TRUE)

which returns no warning.