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 .
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.
Comments