Home > other >  What is the most typographically safe way to write a linear program in R?
What is the most typographically safe way to write a linear program in R?

Time:01-24

In R, linear programming using the lpsolve library requires an extensive amount of manually typed values for the needed matrices and vectors for the objective function coefficients, left-hand side coefficients, constraints, etc. Messing up even one value or one comma will make the script error or worse, the program will find a solution but it will be the wrong one due to incorrect setup. For large, real-world problems like network flow, simply typing the program itself is time prohibitive. What am I missing about R's capabilities in this space? Or, is there an alternate tool better fit for the job? Open source preferred due to budget.

Here is an example of the type of code needed in R for a relatively simple optimization problem:

# fleet size optimization, with an added computation of total miles driven
library(lpSolve)

# Objective function coefficients
ObjCoeff<-c(1300, 690, 421.5, 531, 690, 427.50, 277.50, 421.5, 427.50, 303, 531, 277.50, 303,
            460, 281,   354, 460, 285,    185,    281,   285,    202, 354, 185,    202, 0)

# Constraint matrix            
Amatrix<-matrix(c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                  
                  0, 1, 1, 1, -1, 0, 0, -1, 0, 0, -1, 0, 0, 1, 1, 1, -1, 0, 0, -1,  0, 0, -1, 0, 0,0,
                  0, -1,0, 0,  1, 1, 1,  0, -1, 0, 0,-1, 0,-1, 0, 0,  1, 1, 1,  0, -1, 0, 0, -1, 0,0,
                  0, 0,-1, 0,  0,-1, 0,  1,  1, 1, 0, 0,-1, 0,-1, 0, 0, -1, 0,  1,  1, 1, 0,  0,-1,0,
                  0, 0, 0, -1, 0, 0,-1,  0,  0,-1, 1, 1, 1, 0, 0,-1, 0,  0,-1,  0,  0,-1, 1,  1, 1,0,
                  -1300, 460, 281,354,460,285,185,281,285,202,354,185,202,460,281,354, 460, 285,185, 281, 285,202, 354, 185, 202, 0,
                  0, 460, 281,354,460,285,185,281,285,202,354,185,202,460,281,354, 460, 285,185, 281, 285,202, 354, 185, 202,-1), nrow=18, byrow=TRUE)

# Right hand side constraint vector                  
Bvector<-c(10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0,0 )

# Constraint inequality direction vector
constrainttype<- c(">=", ">=",">=", ">=",">=", ">=",">=", ">=",">=", ">=",">=", ">=","=", "=" , "=" , "=" ,"<=", "=" )

# Solve the specified integer program by setting all.int=TRUE
optimum<-lp(direction="min", objective.in=ObjCoeff, const.mat = Amatrix, const.dir = constrainttype, const.rhs = Bvector, all.int =TRUE)

# Print constraint matrix to verify it was specified correctly
print(optimum$constraints)

# Check to see if the solver reached optimality (0 means yes)
print(optimum$status)

# Print values of each variable in the optimal solution
# note that they are all integer valued
print(optimum$solution)

# Print the optimal objective function value
print(optimum$objval)

CodePudding user response:

R has its strengths for sure, but formulation of non-trivial LP's is not one of them.

There are several other open source frameworks that are much more expressive. If you are a python user, you can pick from many including:

pyomo, gekko, cvxpy, pulp, or-tools and others I'm sure.

I'm a fan of pyomo for model building, but it requires installation of a separate solver, which isn't too difficult. There are several open-source free solvers that are excellent and in common use with different capabilities such as cbc, glpk, ipopt and others, and of course many excellent licensed solvers.

If you want to start with an "all in one" the pulp framework includes a solver with the build--I forget which one.

There are many examples on this site for all the pkgs above if you search by tag.

  • Related