-1

A car company produces 3 models, model A / B / C. Long-term projections indicate an expected demand of at least 100 model A cars, 80 model B cars and 120 model C cars each day. Because of limitations on production capacity, no more than 200 model A cars and 170 model B cars and 150 model C cars can be made daily. To satisfy a shipping contract, a total of at least 300 cars much be shipped each day. If each model A car sold results in a $1500 loss, but each model B car produces a $3800 profit, each model C car produces a $2500 profit, how many of each type should be made daily to maximize net profits?

I am using lpSolve in R so far. See the code below.

library(lpSolve) #Loading lpSolve library
obj.fun=c(-1500,3800,2500) #Loading the objective function in obj.fun

constr <- matrix(c(1,0,1,0,0,1,0,1,1,1), ncol=3, byrow=TRUE) #Loading the constraints constr.dir=c(">=","<=",">=","<=",">=", "<=",">=") constr.rhs=c(100,200,80,170,120,150,300) mod=lp("max",obj.fun,constr,constr.dir,constr.rhs,compute.sens = TRUE)

Using lp() to solve our problem

mod$solution #Displaying the values of x, y.c y=170 C=200 z = (3800*y)-(2500*x) #Putting the values of x and y in the objective function options("scipen"=200, "digits"=4) cat("Net profit =", z) #Displaying the maximum profit

After running first code line get this message:

data length [10] is not a sub-multiple or multiple of the number of rows [4]number of columns of result is not a multiple of vector length (arg 2)[1] 0 170 200

Then run second code and get net profit of 446000.

Not sure if these are right. I think I understand how to do this problem with two car models but I do not understand how to do it with three car models A/B/C.

Cettt
  • 11,460
  • 7
  • 35
  • 58
daisy
  • 11
  • I'd tried to format your code, but you seem to be missing a lot of line breaks that would put code on individual lines properly – camille Jul 18 '19 at 02:25

1 Answers1

0

I can't recommend that you try to solve this problem using R. Its packages for optimization are primitive in comparison to what's available through Python and Julia and this is the sort of problem where using the right tools can make your life much easier.

Julia's JuMP is the tool I'd most recommend using, but Python is still, I think, the stronger language to work in due to a more complete and functional set of libraries for handling other parts of one's programs. cvxpy in particular has many advanced features which are simply not available in any other tool.

I've rewritten your problem using cvxpy below. Note how much easier it is to understand than the R version.

#!/usr/bin/env python3
import cvxpy as cp

A = cp.Variable(pos=True)
B = cp.Variable(pos=True)
C = cp.Variable(pos=True)

objective = cp.Maximize(-1500*A+3800*B+2500*C)

constraints = [
  A<=200,
  B<=170,
  C<=150,
  A+B+C==300
]

prob = cp.Problem(objective, constraints)

optimal_value = prob.solve()
print("Optimal value = {0}".format(optimal_value))
print("A.value = {0}".format(A.value))
print("B.value = {0}".format(B.value))
print("C.value = {0}".format(C.value))
Richard
  • 56,349
  • 34
  • 180
  • 251