Skip to content

Commit

Permalink
Prototype for linear programming is done. We only need more examples …
Browse files Browse the repository at this point in the history
…of different CPLEX formats, specifically MOLP, big M, Q and ILP methods.
  • Loading branch information
psmgeelen committed Feb 10, 2018
1 parent b8af08e commit 071d1ee
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 127 deletions.
Binary file added figures/advanced_encoding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/cplexformat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/openfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/savefile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glpkAPI.pdf
Binary file not shown.
58 changes: 30 additions & 28 deletions linearprogramming.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,62 @@ output: html_notebook
---

This notebook with give you a start in Linear Programming in R. Arseny and I have been working many hours to develop a workflow by which you can truly write down the functions and constraints, as you will for the exam. It is our belief that this will prepare more adequately for the exam and we hope to improve the overall "feel" of the course.

<p></p><p></p>
<h3> CPLEX </h3>
In order to "just write down" the functions, we need to work with a particular format which is the "CPLEX". Its based on a more complicated engine that is used by big companies like IBM. R, of course, also provides a free alternative. The overal format is as follows.

<p>--Start-File--</p>
<p>Maximize</p>
<p>&nbsp; obj: x1 + 2 x2 + 3 x3 + x4</p>
<p>Subject To</p>
<p>&nbsp; c1: - x1 + x2 + x3 + 10 x4 <= 20</p>
<p>&nbsp; c2: x1 - 3 x2 + x3 <= 30</p>
<p>&nbsp; c3: x2 - 3.5 x4 = 0</p>
<p>Bounds</p>
<p>&nbsp; 0 <= x1 <= 40</p>
<p>&nbsp; 2 <= x4 <= 3</p>
<p>General</p>
<p>&nbsp; x4</p>
<p>End</p>
<p>--End-File--</p>
![](figures/cplexformat.png)

This link provides a concrete guide for the use of this format:
http://lpsolve.sourceforge.net/5.0/CPLEX-format.htm
Note that this package also contains a free book from Fernandez that provide excellent examples for this method. The first template is provided on page 34.
Note that this package also contains a free book from Fernandez that provide excellent examples for this method. The first template is provided on page 34, which is excerpted above.

<h4> Workflow CPLEX </h4>

The CPLEX format is based on ASCII coding. This coding is relates to how a file is stored. The workflow of creating a CPLEX file is a follows. You can create a new file in RStudio by clicking on the new file button. This will open a menu, select txt file.
![](figures/openfile.png)

When everything has been entered in the correct CPLEX format, you can save the file by going on the menubar and clicking on the file button.
![](figures/savefile.png)

From there you select the "Save file with encoding..." option. This will open another small screen with a few options of encoding, including ASCII. Select ASCII. After that you can select a path name for the file. Please note that if you save it as linear.programm.txt, you need to refer to it as such. In the examples above we used no extension (.txt, .doc, .docx, etc..) and therefore do not refer to one either in our commands.


<h3> Required Packages </h3>
There are 3 packages required to run these linear programms

<ul>
<li>lpSolve</li>
<li>lpSolveAPI</li>
<li>glpkAPI</li>
<li>Rglpk</il>
</ul>

So lets install those by executing the code below:
```{r}
install.packages("lpSolve")
install.packages("lpSolveAPI")
install.packages("glpkAPI")
install.packages("Rglpk")
```

Having installed the requried packages, we can run the generic code for linear programming which is:
```{r}
library(lpSolveAPI)
library(glpkAPI)
library(Rglpk)
x <- Rglpk_read_file("example_lp", type = "CPLEX_LP", ignore_first_row = FALSE, verbose = FALSE)
problem <- initProbGLPK(ptrtype = "glpk_prob")
solve <- Rglpk_solve_LP(x$objective, x$constraints[[1]], x$constraints[[2]], x$constraints[[3]], x$bounds, x$types, x$maximum)
```
readLPGLPK(problem, "example_lp")
solveSimplexGLPK(problem)
printRangesGLPK(problem, fname = "report.txt")
Here the CPLEX object is 1) imported into R, 2) An optimum is calculated and 3) the results are retrieved.
file.show("report.txt")
```

In order to undestand the code above, I want to elaborate on what an API is. An API is an "Application Programming Interface". More simply put, its a way of communicating to other software. In order to use GLPK (GNU Linear Programming Kit), we need to create an object within this package. To do this we use the initProbGLPK, which initiates an empty object within the GLPK package. Note that this object is connected to R through an object in R, called "problem". More simply put: the "problem" object in R is the connection to the "glpk_prob" object in GLPK. These objects are in effect mirrors and connections. The read, solve and print commands dont require an object within R, since they are executed in the GLPK package. Due to the fact that the real calculations are not run in R, the sensitivty report cannot be displayed in the terminal either. For this reason a file.show command has been used, to open the file when the calculations are done.

<h3> Sensitivity Report </h3>
<h3> Examples </h3>

Tornado graph would be kewl too
There are alot of concrete examples of how to write Linear Programs in CPLEX in the book that is also added to this zip file. The book is called "Linear Programming.pdf" There

To do: Sensitivity report, CPLEX formats for MOLP, ILP and big M methods, we need to test and formulate the ASCII issue
To do: Are there a couple of formats that we can steal from the book to cover the MOLP, ILP and LP principles? Maybe even Big M and stuff.
160 changes: 61 additions & 99 deletions linearprogramming.nb.html

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
GLPK 4.47 - SENSITIVITY ANALYSIS REPORT Page 1

Problem:
Objective: obj = 125.2083333 (MAXimum)

No. Row name St Activity Slack Lower bound Activity Obj coef Obj value at Limiting
Marginal Upper bound range range break point variable
------ ------------ -- ------------- ------------- ------------- ------------- ------------- ------------- ------------
1 c1 NU 20.00000 . -Inf -2.00000 -1.64583 89.00000 x4
1.64583 20.00000 22.00000 +Inf 128.50000 x4

2 c2 NU 30.00000 . -Inf 28.00000 -1.35417 122.50000 x4
1.35417 30.00000 52.00000 +Inf 155.00000 x4

3 c3 NS . . . -.50000 -Inf 123.00000 x4
4.41667 . 5.50000 +Inf 149.50000 x4

GLPK 4.47 - SENSITIVITY ANALYSIS REPORT Page 2

Problem:
Objective: obj = 125.2083333 (MAXimum)

No. Column name St Activity Obj coef Lower bound Activity Obj coef Obj value at Limiting
Marginal Upper bound range range break point variable
------ ------------ -- ------------- ------------- ------------- ------------- ------------- ------------- ------------
1 x1 NU 40.00000 1.00000 . 29.00000 -.29167 111.00000 x4
1.29167 40.00000 41.00000 +Inf 126.50000 x4

2 x2 BS 10.20833 2.00000 . 7.00000 -2.42857 80.00000 x1
. +Inf 10.50000 11.28571 220.00000 c2

3 x3 BS 20.62500 3.00000 . 19.50000 .59259 75.55556 c2
. +Inf 22.00000 13.33333 338.33333 x1

4 x4 BS 2.91667 1.00000 2.00000 . -14.50000 80.00000 x1
. 3.00000 4.44444 33.50000 220.00000 c2

End of report
38 changes: 38 additions & 0 deletions sar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
GLPK 4.47 - SENSITIVITY ANALYSIS REPORT Page 1

Problem:
Objective: obj = 125.2083333 (MAXimum)

No. Row name St Activity Slack Lower bound Activity Obj coef Obj value at Limiting
Marginal Upper bound range range break point variable
------ ------------ -- ------------- ------------- ------------- ------------- ------------- ------------- ------------
1 c1 NU 20.00000 . -Inf -2.00000 -1.64583 89.00000 x4
1.64583 20.00000 22.00000 +Inf 128.50000 x4

2 c2 NU 30.00000 . -Inf 28.00000 -1.35417 122.50000 x4
1.35417 30.00000 52.00000 +Inf 155.00000 x4

3 c3 NS . . . -.50000 -Inf 123.00000 x4
4.41667 . 5.50000 +Inf 149.50000 x4

GLPK 4.47 - SENSITIVITY ANALYSIS REPORT Page 2

Problem:
Objective: obj = 125.2083333 (MAXimum)

No. Column name St Activity Obj coef Lower bound Activity Obj coef Obj value at Limiting
Marginal Upper bound range range break point variable
------ ------------ -- ------------- ------------- ------------- ------------- ------------- ------------- ------------
1 x1 NU 40.00000 1.00000 . 29.00000 -.29167 111.00000 x4
1.29167 40.00000 41.00000 +Inf 126.50000 x4

2 x2 BS 10.20833 2.00000 . 7.00000 -2.42857 80.00000 x1
. +Inf 10.50000 11.28571 220.00000 c2

3 x3 BS 20.62500 3.00000 . 19.50000 .59259 75.55556 c2
. +Inf 22.00000 13.33333 338.33333 x1

4 x4 BS 2.91667 1.00000 2.00000 . -14.50000 80.00000 x1
. 3.00000 4.44444 33.50000 220.00000 c2

End of report

0 comments on commit 071d1ee

Please sign in to comment.