Problem: Find optimal discount for each product such that spend budget is fully utilized. In simpler terms, I need to maximize sales by changing discount with the following constraints:
- min discount <= discount <= max discount
- spend_value <= 100 #spend_budget
Formula used(relation between diff variables): (details shared, at the end in the section, where I have used excel to solve the problem.)
sales_value = discount discount_coef + constant [Excel formula = F2G2 + H2]
spend_value = (sales_value/(mrp-discount))*discount [Excel formula = (E2/(B2-G2))*G2]
Work done: with naive knowledge on optimization, and extreme googling/ checking various SOs post, I managed to find some relevant post related to my problem here, which suggested use of NlcOptim::solnl. and code as follows:
Input data
structure(list(product = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N"), mrp = c(159, 180, 180, 230,
230, 500, 500, 310, 288, 310, 500, 425, 425, 465), discount_coef = c(0.301594884229324,
0.614829352312733, 0.149146787052132, 0.248723558155458, 0.138769169527518,
0.330703149210594, 0.335917219291645, 0.296582160231912, 0.357483743973616,
0.24978922074796, 0.334178652809571, 0.292011550773066, 0.157611497322651,
0.357562105368776), min_discount = c(14.31, 25.2, 25.2, 29.9,
29.9, 100, 100, 71.3, 66.24, 71.3, 100, 51, 51, 51.15), max_discount = c(39.75,
30.6, 30.6, 39.1, 39.1, 200, 200, 179.8, 155.52, 179.8, 200,
174.25, 174.25, 190.65)), row.names = c(NA, 14L), class = "data.frame") -> optim_data
code
library("NlcOptim")
(coeff <- optim_data$discount_coef)
#> [1] 0.3015949 0.6148294 0.1491468 0.2487236 0.1387692 0.3307031 0.3359172
#> [8] 0.2965822 0.3574837 0.2497892 0.3341787 0.2920116 0.1576115 0.3575621
(min_discount <- optim_data$min_discount)
#> [1] 14.31 25.20 25.20 29.90 29.90 100.00 100.00 71.30 66.24 71.30
#> [11] 100.00 51.00 51.00 51.15
(max_discount <- optim_data$max_discount)
#> [1] 39.75 30.60 30.60 39.10 39.10 200.00 200.00 179.80 155.52 179.80
#> [11] 200.00 174.25 174.25 190.65
(mrp <- optim_data$mrp)
#> [1] 159 180 180 230 230 500 500 310 288 310 500 425 425 465
(discount <- numeric(length = 14L))
#> [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## objective function
obj <- function(discount) {
sales_value <- (discount/mrp) * coeff
return(sum(sales_value))
}
## constraint
con <- function(discount) {
sales <- (discount/mrp)*coeff
spend <- (sales/(mrp-discount))*discount
f = NULL
f = rbind(f, sum(spend)-100) # 100 is spend budget
return(list(ceq = f, c = NULL))
}
## optimize
result <- solnl(X = discount, objfun = obj, confun = con,
lb = min_discount, ub = max_discount)
#> Error in solnl(X = discount, objfun = obj, confun = con, lb = min_discount, : object 'lambda' not found
Created on 2020-07-03 by the reprex package (v0.3.0)
Issue:
- It constantly throwing error message "object 'lambda' not found" and I am clueless on how to solve the issue.
- How to solve non-linearity optimization problem shared in R?? Is there other way to solve the problem??
Details: Excel solution
- pre-setup(input data) in excel
- post setup (after running excel solver)