How to construct & solve a linear programming problem using lpsolve in r?

Viewed 38

I'm new to linear programming and trying to solve in R using lpsolve package.

I have taken this from a Youtube tutorial done in excel and below is the problem Constrains:

Problem Constrains

I have tried below code to construct this problem but I am getting error on running lpsolve:

library(lpSolve)
library(tidyverse)

options(scipen = 999)
  1. ROI which need to be maximized
Channel <- c("tv","seo","adwords","facebook")
ROI <- c(.09, .14, .10, .05)

cbind(Channel,ROI)

# --------------- output ---------------
     Channel    ROI   
[1,] "tv"       "0.09"
[2,] "seo"      "0.14"
[3,] "adwords"  "0.1" 
[4,] "facebook" "0.05"
  1. Creating Problem Constrains
budget_constr <- 1000000
seo_budg_60_per <- .60 * 1000000
fb_budg_20_per <- .20 * 1000000
tv_min_200grand <- 200000
fb_min_cont_80grand <- 80000
seo_min_cont_60grand <- 60000
seo_max_cont_220grand <- 220000
adwords_max_cont_3xseo <- 0
Market_size <- 1300000


const.rhs <- c(budget_constr,seo_budg_60_per,fb_budg_20_per,tv_min_200grand,fb_min_cont_80grand,
  seo_min_cont_60grand,seo_max_cont_220grand,adwords_max_cont_3xseo,Market_size)
constraint_descr <- c("Budget 1 Million","Seo adwords atleast 60% of the budget","Facebook max 20% of budget",
                   "Tv is min 200000","Facebook min contract 80000","Seo min contract 60000","Seo max contract 220000",
                   "Adwords max contract is 3 times SEO","Allocate Money <= Market Size (which is 1300000)")

cbind(constraint_descr, const.rhs)

# --------------- output ---------------
      constraint_descr                                   const.rhs
 [1,] "Budget 1 Million"                                 "1000000"
 [2,] "Seo adwords atleast 60% of the budget"            "600000" 
 [3,] "Facebook max 20% of budget"                       "200000" 
 [4,] "Tv is min 200000"                                 "200000" 
 [5,] "Facebook min contract 80000"                      "80000"  
 [6,] "Seo min contract 60000"                           "60000"  
 [7,] "Seo max contract 220000"                          "220000" 
 [8,] "Adwords max contract is 3 times SEO"              "0"      
 [9,] "Allocate Money <= Market Size (which is 1300000)" "1300000"
  1. Creating Constrain direction
const.dir <- c("<=",">=","<=",">=",">=",">=","<=",">=","<=")
  1. Creating Matrix
const.mat <- rbind(c(1,1,1,1),
                c(0,1,0,0),
                c(0,0,0,1),
                c(1,0,0,0),
                c(0,0,0,1),
                c(0,1,0,0),
                c(0,1,0,0),
                c(0,1,3,0),
                c(1,1,1,1))
  1. Using lpsolve
lpSolve::lp(direction = "max", objective.in = ROI,
            const.mat, const.dir, const.rhs)

Error: no feasible solution found

1 Answers

I prefer to use the linprog package.

Your example:

library(linprog)

Channel <- c("tv","seo","adwords","facebook")
ROI <- c(.09, .14, .10, .05)
cbind(Channel,ROI)

budget_constr <- 1000000
seo_budg_60_per <- .60 * 1000000
fb_budg_20_per <- .20 * 1000000
tv_min_200grand <- 200000
fb_min_cont_80grand <- 80000
seo_min_cont_60grand <- 60000
seo_max_cont_220grand <- 220000
adwords_max_cont_3xseo <- 0
Market_size <- 1300000

const.rhs <- c(budget_constr,
               seo_budg_60_per,
               fb_budg_20_per,
               tv_min_200grand,
               fb_min_cont_80grand,
               seo_min_cont_60grand,
               seo_max_cont_220grand,
               adwords_max_cont_3xseo,
               Market_size)

constraint_descr <- c("Budget 1 Million",
                      "Seo adwords atleast 60% of the budget",
                      "Facebook max 20% of budget",
                      "Tv is min 200000",
                      "Facebook min contract 80000",
                      "Seo min contract 60000",
                      "Seo max contract 220000",
                      "Adwords max contract is 3 times SEO",
                      "Allocate Money - Market Size (which is 1300000)")

cbind(constraint_descr, const.rhs)

const.mat <- rbind(c(1,1,1,1),
                   c(0,1,0,0),
                   c(0,0,0,1),
                   c(1,0,0,0),
                   c(0,0,0,1),
                   c(0,1,0,0),
                   c(0,1,0,0),
                   c(0,1,3,0),
                   c(1,1,1,1))

solveLP(ROI, const.rhs, const.mat, const.dir = c("<=",">=","<=",">=",">=",">=","<=",">=","<="), maximum = TRUE)

Result:

Results of Linear Programming / Linear Optimization

Objective function (Maximum): 102800 
Related