[英]lpSolve - find maximum value - formula with multiplications
我正在尋找一種使用 lpSolve 的方法,就像我在 Excel 中成功使用它的方式一樣。 我已經計算了各種產品的彈性。 根據產品是否具有彈性,我想就要詢問的價格給出建議。
我有以下價值觀:
current.price = 15
sales.lastmonth = 50
elasticity = -1.5
我想通過更改建議的價格來優化 sales.prediction
公式:sales.prediction = (sales.lastmonth - ((abs(elasticity) (suggested.price-current.price)) (sales.lastmonth/current.price)))*suggested.price
我嘗試了以下方法:
# install.packages("lpSolve")
library(lpSolve)
objective.fn <- c(sales.prediction) # determine what the objective is
# constrain sales.lastmonth and current.price
const.mat <- matrix(c(1,1),ncol=1,byrow=T)
const.dir <- c("=","=")
const.rhs <- c(sales.lastmonth, current.price)
lp("max",objective.fn,const.mat,const.dir,const.rhs,compute.sens=TRUE)
有什么建議么?
編輯 - V2:根據下面的評論,我想添加一個約束,即建議的價格應該比我在沒有優化的情況下的建議價格高 25% 或低 25%。 例如,假設我已經在考慮將價格降低到 12.5。 這也可能嗎?
如果我理解正確,您只是想優化(最大化)此功能,因為您的變量是恆定的,所以沒有任何限制。 如果這是真的,那么你可以做
optimize(
f=function(x){
sales.lastmonth-((abs(elasticity)*(x-current.price))*(sales.lastmonth/current.price))*x
},
interval=c(-100,100),
maximum=T
)
$maximum
[1] 7.5
$objective
[1] 331.25
最大值為 7.5。
編輯:限制優化的一個簡單技巧是使用 interval 參數進行編輯interval=c(current.price*0.75,current.price*1.25)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.