kkz
kkz

Reputation: 306

How to maximize sales using lpSolve with constraints

Let's say I'm trying to maximize sales and have two variables, the amount spent on two types of advertisements, ads and ads2 in dollars, that affect the sales.

After calculating a linear model on the effect of sales I extract the coefficients to get the formula for the predicted sales. Then I try to maximize the predicted sales by optimizing for the amount of ads.

The problem is that there should be also a budget for the types of advertising. Without this constraint all of the advertising budget would be spent on ads rather than ads2.

This causes the sales to be about 140 dollars, but a little bit of sales could be sacrificed to meet this constraint.

I don't know how to implement this. I tried adjusting the constraints in the const matrix but the optimization tried to force all advertising to only one of the types of ads.

library(tibble)
library(dplyr)
library(lpSolve)

data <- tribble(~"ads", ~"ads2", ~"sales",
                 100,    120,      100,
                 50,     90,       40,
                 20,     10,       10,
                 150,    110,      130,
                 190,    90,       160,
                 180,    300,     250,
                 110,    200,      100,
                 80,     70,       20,
                 50,     20,       10,
                 30,     100,      200,
                 100,    190,      40,
                 100,    200,     100,
                 40,     90,       80,
                 60,     80,       60,
                 200,    20,       100,
                 20,     200,      80,
                 30,     10,       30,
                 40,     90,       100,
                 80,     20,       80,
                 200,    150,      100,
                 80,     80,       40,
                 100,    10,       300,
                 150,    100,      60,
                 10,     100,      10
)

forecast <- lm(sales ~ ads + ads2, data = data)

coef <- forecast$coefficients

obj <- coef[2:3] # coefficients
const <- matrix(c(1, 1,  # ads
                  1, 1), # ads2
                nrow = 2)

min_items <- 250 # minimum total budget
max_items <- 300 # maximum total budget

rhs <- c(min_items, max_items)

direction  <- c(">", "<")

optimum <-  lp(direction = "max", obj, const, direction,  rhs)

optimum$solution # budget spent on ads and ads2
optimum$objval + coef[1] # forecasted sales by using this budget

optimum$solution should not be 0 for either of the ad types, it should return for example 50 100 instead of 300 0 or 0 300.

Upvotes: 0

Views: 85

Answers (1)

kkz
kkz

Reputation: 306

The problem was solved by adding a new constraint, max_ads, and setting it to the maximum value of ads that you want. Also the constraint matrix had to be changed by setting zero to the corresponding constraint for ads2. Also the sign for the direction had to be changed.

const <- matrix(c(1, 1,  # ads
                  0, 1), # ads2
                nrow = 2)

max_ads <- 100 # maximum ads budget
max_items <- 300 # maximum total budget

rhs <- c(max_ads, max_items)

direction  <- c("<", "<")

Upvotes: 0

Related Questions