0

我有一個公式,我想用8個輸入參數/變量/維度/標準最大化。對於下面的例子,我將它簡化爲只包含兩部分的公式。根據信息here我一直在使用調用nsga2的mco軟件包。使用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) 
} 

這些功能工作。我可以通過他們的年度消費金額,他們將返回每月預計的銷售數字。

Next:定義要優化的函數。

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?我是否使用正確的包裝/電話?

目前代碼失敗,「廣告2(廣告2安裝)中的錯誤:參數'advert2Spend'丟失,沒有默認值。」我覺得這個問題可能與如何編寫優化函數有關,但我無法弄清楚如何改進它。沒有我見過的例子在優化函數中使用多個輸入參數。

我也需要以某種方式「反轉」公式,因爲nsga2是一個最小化函數,我想最大化。但是第一件事是第一件...我怎麼才能讓包裝爲我工作?

感謝您的任何幫助。

回答

0

這是我最終用包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也工作。我的主要問題是我需要一個可以優化進化的軟件包。