使用 nsga2 包 mco 优化具有多个输入变量的公式
Optimizing a formula with multiple input variables using nsga2 package mco
我有一个公式,我想用 8 个输入来最大化 parameters/variables/dimensions/criteria。对于下面的示例,我将其简化为一个由两部分组成的公式。根据资料here我一直在使用调用nsga2的mco包。
设置如下:
#calculate an s curve for advertising1
Index = (0:250)
advertising1.sAlpha = .953
advertising1.sBeta = 0.0000000003
advertising1.Max = 53460404
advertising1.Media = Index*advertising1.Max/100
advertising1.scurve = advertising1.sBeta^(advertising1.sAlpha^Index)
advertising1.Beta = 2989.589
advertising1.Cost = .095
#function to convert spend to media, get the response, find the s curve, and return number of sales for advertising1
advertising1.Spend = function(advertising1Spend) {
monthly.spend = advertising1Spend/12
Media = monthly.spend/advertising1.Cost
response.index = findInterval(Media, advertising1.Media)
scurve = advertising1.scurve[response.index]
sales = scurve*advertising1.Beta
return(sales)
}
#calculate an s curve for advertising2
advertising2.sAlpha = .6
advertising2.sBeta = 0.000000001
advertising2.Max = 90
advertising2.Media = Index*advertising2.Max/100
advertising2.scurve = advertising2.sBeta^(advertising2.sAlpha^Index)
advertising2.Beta = 4597.285
advertising2.Cost = 38540.12
#function to convert spend to media, get the response, find the s curve, and return number of sales for advertising2
advertising2.Spend = function(advertising2Spend) {
monthly.spend = Spend/12
Media = monthly.spend/advertising2.Cost
response.index = findInterval(Media, advertising2.Media)
scurve = advertising2.scurve[response.index]
sales = scurve*advertising2.Beta
return(sales)
}
这些功能按预期工作。我可以向他们传递年度支出金额,他们将 return 每月预计销售数字。
下一步:定义要优化的函数。
Optimize.Spend = function(advertising1Spend, advertising2Spend) {
advertising1.Spend(advertising1Spend) +
advertising2.Spend(advertising2Spend)
}
此功能也按预期工作。我还想将预算设置为约束条件,例如 advertising1Spend + advertising2Spend <= 50000000。
Budget = function(advertising1Spend, advertising2Spend) {
advertising1Spend + advertising2Spend <= 50000000
}
最后,我对功能的优化尝试如下。我已经为各个变量设置了界限并输入了我的约束条件。
nsga2(Optimize.Spend, 2, 2, lower.bounds = c(0, 0), upper.bounds =
c(60944860.56, 41623333.92), generations = 100, constraints = Budget, cdim = 1)
换句话说,我想找到每个输入变量的最佳支出金额,以便在给定预算的情况下产生最多的销售额。首先,这种优化在 R 中是否可行?我使用的是正确的 package/call 吗?
目前代码失败 "Error in advertising2(advertising2Spend): argument 'advertising2Spend' is missing, with no default." 我觉得这个问题可能与优化函数的编写方式有关,但我不知道如何改进它。我见过的例子中没有一个在优化函数中采用多个输入参数。
我还需要以某种方式 "reverse" 公式,因为 nsga2 是一个最小化函数,我想最大化。但第一件事是第一...我怎样才能让这个包为我工作?
感谢您的帮助。
下面是我最终使用包 DEoptimR 所做的事情。
parameters = c(FALSE, FALSE)
Optimize.Spend = function(parameters) {
-Advertising1.Spend(parameters[1]) - Advertising2.Spend(parameters[2])
}
Budget = function(parameters) {-parameters[1] - parameters[2] + 100000000 <= 0
}
JDEoptim(lower = c(0,0), upper = c(60944860.56, 41623333.92), fn = Optimize.Spend, constr = Budget, maxiter = 2000)
RccpDE 包也有效。我的主要问题是我需要一个可以进化优化的包。
我有一个公式,我想用 8 个输入来最大化 parameters/variables/dimensions/criteria。对于下面的示例,我将其简化为一个由两部分组成的公式。根据资料here我一直在使用调用nsga2的mco包。
设置如下:
#calculate an s curve for advertising1
Index = (0:250)
advertising1.sAlpha = .953
advertising1.sBeta = 0.0000000003
advertising1.Max = 53460404
advertising1.Media = Index*advertising1.Max/100
advertising1.scurve = advertising1.sBeta^(advertising1.sAlpha^Index)
advertising1.Beta = 2989.589
advertising1.Cost = .095
#function to convert spend to media, get the response, find the s curve, and return number of sales for advertising1
advertising1.Spend = function(advertising1Spend) {
monthly.spend = advertising1Spend/12
Media = monthly.spend/advertising1.Cost
response.index = findInterval(Media, advertising1.Media)
scurve = advertising1.scurve[response.index]
sales = scurve*advertising1.Beta
return(sales)
}
#calculate an s curve for advertising2
advertising2.sAlpha = .6
advertising2.sBeta = 0.000000001
advertising2.Max = 90
advertising2.Media = Index*advertising2.Max/100
advertising2.scurve = advertising2.sBeta^(advertising2.sAlpha^Index)
advertising2.Beta = 4597.285
advertising2.Cost = 38540.12
#function to convert spend to media, get the response, find the s curve, and return number of sales for advertising2
advertising2.Spend = function(advertising2Spend) {
monthly.spend = Spend/12
Media = monthly.spend/advertising2.Cost
response.index = findInterval(Media, advertising2.Media)
scurve = advertising2.scurve[response.index]
sales = scurve*advertising2.Beta
return(sales)
}
这些功能按预期工作。我可以向他们传递年度支出金额,他们将 return 每月预计销售数字。
下一步:定义要优化的函数。
Optimize.Spend = function(advertising1Spend, advertising2Spend) {
advertising1.Spend(advertising1Spend) +
advertising2.Spend(advertising2Spend)
}
此功能也按预期工作。我还想将预算设置为约束条件,例如 advertising1Spend + advertising2Spend <= 50000000。
Budget = function(advertising1Spend, advertising2Spend) {
advertising1Spend + advertising2Spend <= 50000000
}
最后,我对功能的优化尝试如下。我已经为各个变量设置了界限并输入了我的约束条件。
nsga2(Optimize.Spend, 2, 2, lower.bounds = c(0, 0), upper.bounds =
c(60944860.56, 41623333.92), generations = 100, constraints = Budget, cdim = 1)
换句话说,我想找到每个输入变量的最佳支出金额,以便在给定预算的情况下产生最多的销售额。首先,这种优化在 R 中是否可行?我使用的是正确的 package/call 吗?
目前代码失败 "Error in advertising2(advertising2Spend): argument 'advertising2Spend' is missing, with no default." 我觉得这个问题可能与优化函数的编写方式有关,但我不知道如何改进它。我见过的例子中没有一个在优化函数中采用多个输入参数。
我还需要以某种方式 "reverse" 公式,因为 nsga2 是一个最小化函数,我想最大化。但第一件事是第一...我怎样才能让这个包为我工作?
感谢您的帮助。
下面是我最终使用包 DEoptimR 所做的事情。
parameters = c(FALSE, FALSE)
Optimize.Spend = function(parameters) {
-Advertising1.Spend(parameters[1]) - Advertising2.Spend(parameters[2])
}
Budget = function(parameters) {-parameters[1] - parameters[2] + 100000000 <= 0
}
JDEoptim(lower = c(0,0), upper = c(60944860.56, 41623333.92), fn = Optimize.Spend, constr = Budget, maxiter = 2000)
RccpDE 包也有效。我的主要问题是我需要一个可以进化优化的包。