私はそのような問題を抱えています:私は最大重量を超えないアイテムの最良の組み合わせを見つける必要があります。この問題のために、私は遺伝的アルゴリズムを使用しました。
これが私のデータです
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)
}
}
それから私は2つのパッケージからgaを試しました:genalg
そしてGA
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 = ga_GA@solution
dim(ga_best_solution)
[1] 73 11
ソリューションは73行の行列です。また、ga_GA@bestSol
戻りますlist()
このパッケージの中で私の最善の解決策はどこにありますか?または、73行すべてをチェックして、最良のものを見つける必要があります(73個のゼロを試しました)?
PPS。2番目の質問の解決策:GA最大化関数とgenalg最小化関数= /。genalgパッケージから最良のソリューションを抽出する方法を知っている人はいますか?
ここにはたくさんの質問があります。私の意見では、GAは、最適なソリューションとフィットネススコアという、必要なものに対してより簡単な出力を提供します。
GAがフィットネススコアを最大化し、genalgが最小化するのは正しいです。私は、フィットネス値に-1を掛けた値を返さない2番目のフィットネス関数を作成しました。これにより、両方で同じソリューションが得られます。
また、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)
ga_GA@solution
# 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
ga_GA@fitnessValue
# [1] 9.993641
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加