---
title: "R Notebook"
output: html_notebook
---

Setting up a linear program and solving


# Load the lpSolve library

Install Lp Solve package -- Once you've done this once on your machine, you won't need to do it again

```{r}
install.packages("lpSolve")  
```
Load the LpSolve library - you would need to do this each time you open the worksheet

```{r}
library(lpSolve)
```


## Setup the LP

We need: 

array of coefficients, 

set of directional inequalities (should be as long as the height of your coefficients array), 

set of right hand side coefficients

objective equation (should be as long as the width of your coefficients array)


Example - EQ 24 on page 5944

** Coefficients -- note that these are the coefficients of the variables in the inequality equations, so the first two rows below will be t_1 leq to 0.4z and then t_1 geq to 0.1z -- we set the righthand side and the direction inequality later.  Z is set as the 7th column.

The second last line is the equation that all 6 variables add to 1
The very last line is that z is greater than 0 (although somtimes this will be automatic for lp solvers)

```{r}
coefficients.array <- rbind(
  c(1,0,0,0,0,0,-0.4),
  c(-1,0,0,0,0,0,0.1),
  c(0,1,0,0,0,0,-0.5),
  c(0,-1,0,0,0,0,0.2),
  c(0,0,1,0,0,0,-0.6),
  c(0,0,-1,0,0,0,0.25),
  c(0,0,0,1,0,0,-0.55),
  c(0,0,0,-1,0,0,0.2),
  c(0,0,0,0,1,0,-0.45),
  c(0,0,0,0,-1,0,0.15),
  c(0,0,0,0,0,1,-0.38),
  c(0,0,0,0,0,-1,0.15),
  c(1,1,1,1,1,1,0),
  c(0,0,0,0,0,0,1)
)
```

** set of directional inequalities -- we use an array command as a shortcut, then we attach "==" for the equality of the t and y variables, and just one >= for the z >= 0 at the end.

```{r}
dir.ineq <- c(array(c("<="),12),   "==", ">=")
```

** RHS coefficients.  We've set it up so that most of these will be zero, because all of the values are multiples of z, so now included as part of the LHS.

```{r}


RHS <- c(array(0,12),1,0)

```

** Objective equation

```{r}
objective.eqn <- c(0.8, 0.7, 0.4, 0.9, 0.9, 0.9,0)
```


Then the solver will do the rest.  Usually I will save it as an object so you can get at the outputs

```{r}
lp.output <- lp("max",objective.eqn,coefficients.array,dir.ineq,RHS)
```


Then the things that you usually want to look at are:

```{r}
lp.output$solution
```

So this is the corresponding value of t1, t2, t3, y1, y2, y3 and z that was obtained.



and perhaps the objective value that was obtained

```{r}
lp.output$objval
```



And so obviously you can put it all together in one place.

```{r}
coefficients.array <- rbind(
  c(1,0,0,0,0,0,-0.4),     #t1
  c(-1,0,0,0,0,0,0.1),
  c(0,1,0,0,0,0,-0.5),     #t2
  c(0,-1,0,0,0,0,0.2),
  c(0,0,1,0,0,0,-0.6),     #t3
  c(0,0,-1,0,0,0,0.25),
  c(0,0,0,1,0,0,-0.55),    #y1
  c(0,0,0,-1,0,0,0.2),
  c(0,0,0,0,1,0,-0.45),    #y2
  c(0,0,0,0,-1,0,0.15),
  c(0,0,0,0,0,1,-0.38),    #y3
  c(0,0,0,0,0,-1,0.15),
  c(1,1,1,1,1,1,0),        #sum t1,t2,t3,y1,y2,y3
  c(0,0,0,0,0,0,1)         #z 
)

dir.ineq <- c(array(c("<="),12),   "==", ">=")
RHS <- c(array(0,12),1,0)
objective.eqn <- c(0.7, 0.6, 0.3, 0.8, 0.7, 0.8,0)


lp.output <- lp("min",objective.eqn,coefficients.array,dir.ineq,RHS)
lp.output$solution
lp.output$objval
```

