2016-12-26 99 views
1

我有這樣的問題: 我需要找到不超過最大重量的項目的最佳組合。 對於這個問題我使用遺傳算法。遺傳算法在R

這裏是我的數據

dataset <- data.frame(name = paste0("x",1:11), 
        Weight = c(2.14083022,7.32592911,0.50945094,4.94405846,12.02631340,14.59102403,0.07583312,0.36318323,10.64413370,3.54882187,1.79507759), 
        stringsAsFactors = F) 

這裏是我的成本函數:

max_weight = 10 
fitness_function <- function(x){ 
    current_weight <- x %*% dataset$Weight 
      if (current_weight > max_weight){ 
     return(0) 
    } else { 
     return(-1* current_weight) 
    } 
} 

然後我試圖從兩個包GA:genalgGA

genalg

ga_genalg <- rbga.bin(size = 11, 
          popSize = 100, 
          mutationChance = .1, 
          evalFunc = fitness_function) 

好,這裏是結果:

cat(summary(ga_genalg)) 
GA Settings 
    Type     = binary chromosome 
    Population size  = 100 
    Number of Generations = 100 
    Elitism    = 20 
    Mutation Chance  = 0.1 

Search Domain 
    Var 1 = [,] 
    Var 0 = [,] 

GA Results 
    Best Solution : 0 1 1 0 0 0 0 1 0 0 1 

我查了最佳的解決方案,看起來不錯:

genalg_best_solution = c(0,1,1,0,0,0,0,1,0,0,1) 
dataset$Weight %*% genalg_best_solution 
     [,1] 
[1,] 9.993641 

PS。任何人都知道如何得到這個最佳的解決方案載體,而無需輸入正則表達式?

GA

ga_GA <- ga(type = "binary", fitness = fitness_function, popSize = 100, pmutation = .1, nBits = 11) 
ga_best_solution = [email protected] 
dim(ga_best_solution) 
[1] 73 11 

解是矩陣73點的行。也[email protected]返回list()

我在這個包最好的解決方案在哪裏?或者我需要檢查所有73行,找到最好的(我試過,得到73個零)?

PPS。第二個問題解決方案:GA最大化函數和genalg最小化函數= /。 任何人都知道如何從genalg包中提取最佳解決方案?

回答

2

這裏有很多問題。我的意見是,GA爲您提供了更容易的輸出:最佳解決方案和健身評分。

你說得對,遺傳算法最大化健身得分,而genalg最小化 - 我創建了第二個適應度函數,它不返回適應值乘以-1。這導致兩者都有相同的解決方案。

此外,我沒有得到您爲ga()輸出呈現的維度。在我的情況下,這只是一個單行與11個二進制值:

library(GA) 
library(genalg) 

dataset <- data.frame(name = paste0("x",1:11), 
    Weight = c(
    2.14083022,7.32592911,0.50945094,4.94405846, 
    12.02631340,14.59102403,0.07583312,0.36318323, 
    10.64413370,3.54882187,1.79507759 
), 
    stringsAsFactors = F 
) 

max_weight = 10 


# genalg ------------------------------------------------------------------ 

# fitness function for genalg 
fitness_function <- function(x){ 
    current_weight <- x %*% dataset$Weight 
    if (current_weight > max_weight){ 
     return(0) 
    } else { 
     return(-current_weight) 
    } 
} 


ga_genalg <- rbga.bin(size = 11, 
    popSize = 100, 
    mutationChance = .1, 
    evalFunc = fitness_function 
) 
tail(ga_genalg$best, 1) # best fitness 
summary(ga_genalg, echo=TRUE) 

plot(ga_genalg) # plot 

# helper function from ?rbga.bin 
monitor <- function(obj) { 
    minEval = min(obj$evaluations); 
    filter = obj$evaluations == minEval; 
    bestObjectCount = sum(rep(1, obj$popSize)[filter]); 
    # ok, deal with the situation that more than one object is best 
    if (bestObjectCount > 1) { 
     bestSolution = obj$population[filter,][1,]; 
    } else { 
     bestSolution = obj$population[filter,]; 
    } 
    outputBest = paste(obj$iter, " #selected=", sum(bestSolution), 
         " Best (Error=", minEval, "): ", sep=""); 
    for (var in 1:length(bestSolution)) { 
     outputBest = paste(outputBest, 
      bestSolution[var], " ", 
      sep=""); 
    } 
    outputBest = paste(outputBest, "\n", sep=""); 

    cat(outputBest); 
} 

monitor(ga_genalg) 
# 100 #selected=4 Best (Error=-9.99364087): 0 1 1 0 0 0 0 1 0 0 1 



# GA ---------------------------------------------------------------------- 

# fitness function for GA (maximizes fitness) 
fitness_function2 <- function(x){ 
    current_weight <- x %*% dataset$Weight 
    if (current_weight > max_weight){ 
     return(0) 
    } else { 
     return(current_weight) 
    } 
} 

ga_GA <- ga(type = "binary", fitness = fitness_function2, popSize = 100, pmutation = .1, nBits = 11) 
[email protected] 
#  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 
# [1,] 0 1 1 0 0 0 0 1 0 0 1 
dim(ga_best_solution) 
# [1] 1 11 

[email protected] 
# [1] 9.993641 
+0

嗨馬克,偉大的答案在那裏!你能解釋'fitness_function2(x)'採用的參數'x'嗎? x是染色體嗎?在'ga()'函數中,爲什麼不需要傳遞'x'作爲參數,即'fitness = fitness_function2'? –

+0

@jacky_learns_to_code - 是的,這是由nBits參數定義的基因向量(在本例中爲二進制)。 –

+0

有沒有辦法控制基因的行爲,這樣我的nBits' = 5000,但是'1'的數量總是= 10? –