#                  A linear programming problem                     %
#===================================================================%
#            Modeled & coded by Ebert Brea (c)2021                  %
#===================================================================%
# Set up problem: 
#  maximize(x1 + x2) 
#
#  subject to:
#       x1 + 2 x2 <= 4
#     5 x1 + 2 x2 <= 10
#       x1 >= 0
#       x2 >= 0
#
#  Solution: 
#           x1 = 1.50
#           x2 = 1.25
#           f  = 2.75
require(lpSolve)
## Loading required package: lpSolve
#=================================================%
#         Parameters of the model                 %
#=================================================%
#all.int=TRUE

#Set decision variables
objective.in <- c(1,1)

# Constraint matrix
const.mat<- 
  matrix(c(1, 2,
           5, 2,
           1, 0,
           0, 1), 
         nrow = 4, byrow = TRUE)

const.rhs <-c(4,10,0,0)

#Direction for constraints
const.dir<- c("<=", "<=", ">=",">=")

solution <-c(0,0,0)

##############################################################
#        Solve the Linear Programming Model
##############################################################

opt=lp(direction = "max", objective.in , const.mat, const.dir, const.rhs, int.vec=FALSE)$solution

for(j in 1:2){solution[j]=opt[j]}
for(j in 1:2){solution[3]=solution[3]+opt[j]*objective.in[j]}
ObjFunt=solution[3]

print(solution)
## [1] 1.50 1.25 2.75