solution of linear programming by R software

osimer pothe

I am trying to solve linear programming problem by R software . I have three files named A.txt, B.txt,F.txt . I have read this by the following code:

library( linprog )
Amat<-read.table("D:/Simplex/SimplexInitialTheoryWithRsoftware/src/A.txt",header=FALSE,sep=" ")
bvec<-read.table("D:/Simplex/SimplexInitialTheoryWithRsoftware/src/B.txt",header=FALSE,sep=" ")
cvec<-read.table("D:/Simplex/SimplexInitialTheoryWithRsoftware/src/F.txt",header=FALSE,sep=" ")

The link of the file is A.txt , B.txt , F.txt . I am trying to solve this problem by R software by the following code .

res <- solveLP( cvec, bvec, Amat, TRUE )
## print the results
print( res )

But I am getting the following error .

Error in solveLP(cvec, bvec, Amat, TRUE) : 
  Matrix A must have as many rows as constraints (=elements of vector b) and as many columns as variables (=elements of vector c).

But I have checked my file again and again . The dimension of A is 10*10 , dimension of B is 10*1 and dimension of F is 1*10 .

So why I am getting the error ?

Update : I have got an error after the following code .

cvec<-read.table("D:/Simplex/SimplexInitialTheoryWithRsoftware/src/F.txt",header=FALSE,sep=" ")

And the error is

   Warning message:
In read.table("D:/Simplex/SimplexInitialTheoryWithRsoftware/src/F.txt",  :
  incomplete final line found by readTableHeader on 'D:/Simplex/SimplexInitialTheoryWithRsoftware/src/F.txt'

My assumption is that , the error is caused due to this line of code .

Ioannis

Based on Roland's comment, the error comes indeed from incorrect specification of the input objects. The code below is tried and tested:

library( linprog )
Amat<-as.matrix(read.table("C:\\Users\\ifragkos\\Documents\\Fun\\R\\A.txt",header=FALSE,sep=" "))
bvec<-c(read.table("C:\\Users\\ifragkos\\Documents\\Fun\\R\\B.txt",header=FALSE,sep=" "), recursive=T)
cvec<-c(read.table("C:\\Users\\ifragkos\\Documents\\Fun\\R\\F.txt",header=FALSE,sep=" "), recursive=T)
res<-solveLP(cvec, bvec, Amat, TRUE)
## print the results
print( res )

Result:

All Variables (including slack variables)
              opt   cvec      min.c      max.c       marg  marg.reg
V1      0.2374153  29.02  21.255258  73.654167         NA        NA
V2      0.0000000  47.98       -Inf  79.114957  -31.13496 0.0672461
V3      0.0000000 -37.64       -Inf  53.342215  -90.98222 0.0751398
V4      0.0000000 -22.01       -Inf  95.534330 -117.54433 0.0529444
V5      0.0000000  -1.36       -Inf  45.246038  -46.60604 0.1159432
V6      0.0000000   4.72       -Inf  55.904385  -51.18439 0.0139935
V7      0.0000000  19.67       -Inf 122.901935 -103.23194 0.0450489
V8      0.0000000  24.32       -Inf  93.315656  -68.99566 0.0420672
V9      0.0000000  46.15       -Inf  57.745346  -11.59535 0.0444139
V10     0.0132743  48.21  18.994909  87.790333         NA        NA
S V11  20.1358135   0.00  -0.497478   0.546882    0.00000        NA
S V12  24.2133223   0.00 -10.046381   0.432213    0.00000        NA
S V13  44.8115881   0.00         NA   0.155625    0.00000        NA
S V14  75.7667354   0.00  -1.509427   0.454518    0.00000        NA
S V15   1.3688361   0.00  -0.826885   0.312089    0.00000        NA
S V16  66.5207843   0.00         NA   0.123910    0.00000        NA
S V17  47.0617343   0.00         NA   0.181379    0.00000        NA
S V18   0.0000000   0.00       -Inf   0.456100   -0.45610 0.8502727
S V19   0.0000000   0.00       -Inf   1.084168   -1.08417 2.8651162
S V110 72.7149517   0.00  -0.919381   0.124553    0.00000        NA

Disclaimer:This is my first post in R, I am by no means an R expert. Based on the linprog API and @Roland's comment, I did a bit of tweaking of as.vector, looked up the examples of the linprog reference, and this SO post and came up with the above suggestion.

The reference of the c() function explains that recursive = TRUE "recursively descends through lists (and pairlists) combining all their elements into a vector." Omitting the recursion leads to the default recursive = FALSE, which results in the whole dataframe being returned as one element only:

# This one gives a proper vector
>bvec<-c(read.table("C:\\Users\\ifragkos\\Documents\\Fun\\R\\B.txt",
  header=FALSE,sep=" "), recursive=T)
>bvec
V11   V12   V13   V14   V15   V16   V17   V18   V19  V110 
28.60 34.69 63.04 84.67 12.78 85.24 61.21  7.50  3.79 93.81

# This gives one element only
>bvec<-c(read.table("C:\\Users\\ifragkos\\Documents\\Fun\\R\\B.txt",
  header=FALSE,sep=" "))
>bvec
[1] 28.60 34.69 63.04 84.67 12.78 85.24 61.21  7.50  3.79 93.81

I hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

linear programming with dual simplex in R

From Dev

Flaw in linear programming solution for multi-commodity flow?

From Dev

Fantasy football linear programming in R with RGLPK

From Dev

Constraints in R Multiple Integer Linear Programming

From Dev

R: How to solve the following linear programming problem

From Dev

Linear Programming in R using lpSolve dismisses constrains

From Dev

R: LPsolve (linear programming) with "missing values"

From Dev

How to get only one feasible solution using JOptimizer for a linear programming p‌r‌o‌b‌l‌e‌m that having multi or unlimited number of solutions?

From Dev

How to set up linear programming optimization in R using LpSolve?

From Dev

Error in match.names(clabs, nmi) - Linear programming model - R

From Dev

Efficient and fast way of solving linear programming problems in R

From Dev

Error in match.names(clabs, nmi) - Linear programming model - R

From Dev

Linear Programming -- Minimizing t

From Dev

Linear programming: Logic condition

From Dev

linear programming with python

From Dev

Linear programming - combining models

From Dev

Linear Programming solvable by MATLAB

From Dev

Linear Programming -- Minimizing t

From Dev

Matlab: Binary Linear Programming

From Dev

Linear programming: Logic condition

From Dev

Linear programming with power pivot

From Dev

Shortest path linear programming

From Dev

Shift planning with Linear Programming

From Dev

Expressing an OR constraint in linear programming

From Dev

K&R Exercise 1-9 solution C programming

From Dev

Converting conditional constraints to linear constraints in Linear Programming

From Dev

R linear programming set up in linprog package ignores constraint (less than or equal to) using solveLP

From Dev

R linear programming set up in linprog package ignores constraint (less than or equal to) using solveLP

From Java

Python Mixed Integer Linear Programming

Related Related

  1. 1

    linear programming with dual simplex in R

  2. 2

    Flaw in linear programming solution for multi-commodity flow?

  3. 3

    Fantasy football linear programming in R with RGLPK

  4. 4

    Constraints in R Multiple Integer Linear Programming

  5. 5

    R: How to solve the following linear programming problem

  6. 6

    Linear Programming in R using lpSolve dismisses constrains

  7. 7

    R: LPsolve (linear programming) with "missing values"

  8. 8

    How to get only one feasible solution using JOptimizer for a linear programming p‌r‌o‌b‌l‌e‌m that having multi or unlimited number of solutions?

  9. 9

    How to set up linear programming optimization in R using LpSolve?

  10. 10

    Error in match.names(clabs, nmi) - Linear programming model - R

  11. 11

    Efficient and fast way of solving linear programming problems in R

  12. 12

    Error in match.names(clabs, nmi) - Linear programming model - R

  13. 13

    Linear Programming -- Minimizing t

  14. 14

    Linear programming: Logic condition

  15. 15

    linear programming with python

  16. 16

    Linear programming - combining models

  17. 17

    Linear Programming solvable by MATLAB

  18. 18

    Linear Programming -- Minimizing t

  19. 19

    Matlab: Binary Linear Programming

  20. 20

    Linear programming: Logic condition

  21. 21

    Linear programming with power pivot

  22. 22

    Shortest path linear programming

  23. 23

    Shift planning with Linear Programming

  24. 24

    Expressing an OR constraint in linear programming

  25. 25

    K&R Exercise 1-9 solution C programming

  26. 26

    Converting conditional constraints to linear constraints in Linear Programming

  27. 27

    R linear programming set up in linprog package ignores constraint (less than or equal to) using solveLP

  28. 28

    R linear programming set up in linprog package ignores constraint (less than or equal to) using solveLP

  29. 29

    Python Mixed Integer Linear Programming

HotTag

Archive