# Problem: "A trading company is looking for a way to maximize profit per transportation of their goods. The company has a train available with 3 wagons. When stocking the wagons they can choose between 4 types of cargo, each with its own specifications. How much of each cargo type should be loaded on which wagon in order to maximize profit?"
# Constraints:
# - weight capacity per wagon
# - volume capacity per wagon
# - availability per cargo type
install.packages("lpSolveAPI")
library("lpSolveAPI")
The downloaded binary packages are in
    /var/folders/mr/m5qxlyt52516tbgsf2w8nmf80000gn/T//RtmpzTmaou/downloaded_packages
library("ggplot2")
library("reshape")   # why the error??
Error in library("reshape"): there is no package called ‘reshape’
Traceback:


1. library("reshape")

2. stop(txt, domain = NA)
library("gridExtra")   #why the error??
Error in library("gridExtra"): there is no package called ‘gridExtra’
Traceback:


1. library("gridExtra")

2. stop(txt, domain = NA)
train<-data.frame(wagon=c('w1','w2','w3'), weightcapacity=c(10,8,12), spacecapacity=c(5000,4000,8000))
cargo<-data.frame(type=c('c1','c2','c3','c4'), available=c(18,10,5,20), volume=c(400,300,200,500),profit=c(2000,2500,5000,3500))
lpmodel<-make.lp(2*NROW(train)+NROW(cargo),12)
column<-0
row<-0
for(wg in train$wagon){
    row<-row+1
    for(type in seq(1,NROW(cargo$type))){
    column<-column+1
    set.column(lpmodel,column,c(1, cargo[type,'volume'],1), indices=c(row,NROW(train)+row, NROW(train)*2+type))
    }}
set.constr.value(lpmodel, rhs=train$weightcapacity, constraints=seq(1,NROW(train)))
set.constr.value(lpmodel, rhs=train$spacecapacity, constraints=seq(NROW(train)+1,NROW(train)*2))
set.constr.value(lpmodel, rhs=cargo$available, constraints=seq(NROW(train)*2+1,NROW(train)*2+NROW(cargo)))
set.objfn(lpmodel, rep(cargo$profit,NROW(train)))
lp.control(lpmodel,sense='max')
<dt>$anti.degen</dt>
    <dd><ol class=list-inline>
<li>'fixedvars'</li>
<li>'stalling'</li>
<dt>$basis.crash</dt>
    <dd>'none'</dd>
<dt>$bb.depthlimit</dt>
    <dd>-50</dd>
<dt>$bb.floorfirst</dt>
    <dd>'automatic'</dd>
<dt>$bb.rule</dt>
    <dd><ol class=list-inline>
<li>'pseudononint'</li>
<li>'greedy'</li>
<li>'dynamic'</li>
<li>'rcostfixing'</li>
<dt>$break.at.first</dt>
    <dd>FALSE</dd>
<dt>$break.at.value</dt>
    <dd>1e+30</dd>
<dt>$epsilon</dt>
    <dd><dl class=dl-horizontal>
<dt>epsb</dt>
    <dd>1e-10</dd>
<dt>epsd</dt>
    <dd>1e-09</dd>
<dt>epsel</dt>
    <dd>1e-12</dd>
<dt>epsint</dt>
    <dd>1e-07</dd>
<dt>epsperturb</dt>
    <dd>1e-05</dd>
<dt>epspivot</dt>
    <dd>2e-07</dd>
<dt>$improve</dt>
    <dd><ol class=list-inline>
<li>'dualfeas'</li>
<li>'thetagap'</li>
<dt>$infinite</dt>
    <dd>1e+30</dd>
<dt>$maxpivot</dt>
    <dd>250</dd>
<dt>$mip.gap</dt>
    <dd><dl class=dl-horizontal>
<dt>absolute</dt>
    <dd>1e-11</dd>
<dt>relative</dt>
    <dd>1e-11</dd>
<dt>$negrange</dt>
    <dd>-1e+06</dd>
<dt>$obj.in.basis</dt>
    <dd>TRUE</dd>
<dt>$pivoting</dt>
    <dd><ol class=list-inline>
<li>'devex'</li>
<li>'adaptive'</li>
<dt>$presolve</dt>
    <dd>'none'</dd>
<dt>$scalelimit</dt>
    <dd>5</dd>
<dt>$scaling</dt>
    <dd><ol class=list-inline>
<li>'geometric'</li>
<li>'equilibrate'</li>
<li>'integers'</li>
<dt>$sense</dt>
    <dd>'maximize'</dd>
<dt>$simplextype</dt>
    <dd><ol class=list-inline>
<li>'dual'</li>
<li>'primal'</li>
<dt>$timeout</dt>
    <dd>0</dd>
<dt>$verbose</dt>
    <dd>'neutral'</dd>
write.lp(lpmodel,'model.lp',type='lp')
solve(lpmodel)  #output of 0 says that the optimal solution was found

0

get.objective(lpmodel)  #shows the maximum total profit given the current constraints in transportation resources and available cargo types

107500