Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于遗传算法的多人背包case-r实现_R_Mathematical Optimization_Genetic Algorithm_Knapsack Problem - Fatal编程技术网

基于遗传算法的多人背包case-r实现

基于遗传算法的多人背包case-r实现,r,mathematical-optimization,genetic-algorithm,knapsack-problem,R,Mathematical Optimization,Genetic Algorithm,Knapsack Problem,我试图在R中实现遗传算法。我发现R中有用于遗传算法实现的“GA”和“genalg”包。我在链接中遇到了示例。他们试图解决背包问题。这个问题可以简单地解释为: “你将在荒野中度过一个月。你随身携带一个背包,但背包的最大重量是20公斤。你有很多生存物品,每个物品都有自己的“生存点数”。你的目标是最大限度地增加生存点数。” 使用“genalg”软件包很容易解决这个问题,输出为二进制字符串。现在我有一个疑问,假设不是一个人,而是两个或更多人,即多人,我们需要分配生存点。重量约束适用于每个人。那么我们如何

我试图在R中实现遗传算法。我发现R中有用于遗传算法实现的“GA”和“genalg”包。我在链接中遇到了示例。他们试图解决背包问题。这个问题可以简单地解释为: “你将在荒野中度过一个月。你随身携带一个背包,但背包的最大重量是20公斤。你有很多生存物品,每个物品都有自己的“生存点数”。你的目标是最大限度地增加生存点数。”

使用“genalg”软件包很容易解决这个问题,输出为二进制字符串。现在我有一个疑问,假设不是一个人,而是两个或更多人,即多人,我们需要分配生存点。重量约束适用于每个人。那么我们如何解决这个问题呢?我们可以使用“genalg”或“GA”软件包吗?如果是,我们如何应用它们?有没有在R或其他软件中解决的例子


多亏了

一个简单的方法是让一条染色体包含组中的所有个体,并让评估功能将该染色体分成多个部分,每个个体一个,然后对这些部分进行评估。在下面的例子中(基于问题中的例子),我假设每个人都有相同的重量限制,多个人可以携带相同的物品

library(genalg)

#Set up the problem parameters
#how many people in the group
individual_count <-3
#The weight limit for one individual
weightlimit <- 20
#The items with their survivalpoints
dataset <- data.frame(item = c("pocketknife", "beans", "potatoes", "unions", 
                               "sleeping bag", "rope", "compass"), survivalpoints = c(10, 20, 15, 2, 30, 
                                                                                      10, 30), weight = c(1, 5, 10, 1, 7, 5, 1))
#Next, we choose the number of iterations, design and run the model.
iter <- 100
#Our chromosome has to be large enough to contain a bit for all individuals and for all items in the dataset
chromosomesize <- individual_count * nrow(dataset)


#Function definitions
#A function to split vector X in N equal parts
split_vector <- function(x,n) split(x, cut(seq_along(x), n, labels = FALSE)) 

#EValuate an individual (a part of the chromosome)
evalIndividual <- function(x) {
  current_solution_survivalpoints <- x %*% dataset$survivalpoints
  current_solution_weight <- x %*% dataset$weight
  if (current_solution_weight > weightlimit) 
    return(0) else return(-current_solution_survivalpoints) 
}

#Evaluate a chromosome
evalFunc <- function(x) {
  #First split the chromosome in a list of individuals, then we can evaluate all individuals 
  individuals<-split_vector(x,individual_count)

  #now we need to sapply the evalIndividual function to each element of individuals
  return(sum(sapply(individuals,evalIndividual)))
}

#Run the Genetic Algorithm
GAmodel <- rbga.bin(size = chromosomesize, popSize = 200, iters = iter, mutationChance = 0.01, 
                    elitism = T, evalFunc = evalFunc)
#First show a summary
summary(GAmodel,echo=TRUE)
#Then extract the best solution from the GAmodel, copy/paste from the source code of the summary function
filter = GAmodel$evaluations == min(GAmodel$evaluations)
bestSolution = GAmodel$population[filter, , drop= FALSE][1,]

#Now split the solution in the individuals.
split_vector(bestSolution,individual_count)
库(genalg)
#设置问题参数
#小组里有多少人

个人计数R软件包adagio()带有两个函数(背包和mknapsack),通过动态规划可以更有效地解决此类问题。

您还可以看看相对较新的R软件包ompr,它可以很容易地应用于您的问题。。