Andreas Wittmann
2009-Dec-04 08:27 UTC
[R] Solve linear program without objective function
Dear R-users,
i try to solve to following linear programm in R
0 * x_1 + 2/3 * x_2 + 1/3 * x_3 + 1/3 * x_4 = 0.3
x_1 + x_2 + x_3 + x_4 = 1
x_1, x_2, x_3, x_4 > 0,
x_1, x_2, x_3, x_4 < 1
as you can see i have no objective function here besides that i use the
following code.
library(lpSolve)
f.obj<-c(1,1,1,1)
f.con<-matrix(c(0,2/3,1/3,1/3,
1,1,1,1,
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1),nrow=6,byrow=TRUE)
f.dir <- c("=", "=", ">", ">",
">", ">")
f.rhs <- c(0.3, 1, 0, 0, 0, 0)
lp ("max", f.obj, f.con, f.dir, f.rhs)$solution
the problem is, the condition x_1, x_2, x_3, x_4 > 0 is not fulfilled.
Any advice would be very helpful.
best regards
Andreas
Andreas Wittmann <andreas_wittmann <at> gmx.de> writes:> > Dear R-users, > > i try to solve to following linear programm in R > > 0 * x_1 + 2/3 * x_2 + 1/3 * x_3 + 1/3 * x_4 = 0.3 > x_1 + x_2 + x_3 + x_4 = 1 > x_1, x_2, x_3, x_4 > 0, > x_1, x_2, x_3, x_4 < 1 > > as you can see i have no objective function here besides that i use the > following code. > > library(lpSolve) > > f.obj<-c(1,1,1,1) > f.con<-matrix(c(0,2/3,1/3,1/3, > 1,1,1,1, > 1,0,0,0, > 0,1,0,0, > 0,0,1,0, > 0,0,0,1),nrow=6,byrow=TRUE) > f.dir <- c("=", "=", ">", ">", ">", ">") > f.rhs <- c(0.3, 1, 0, 0, 0, 0) > > lp ("max", f.obj, f.con, f.dir, f.rhs)$solution > > the problem is, the condition x_1, x_2, x_3, x_4 > 0 is not fulfilled.With strict inequalities x_i > 0 your problem will not have a solution. That is why in linear programming strict inequalities are replaced with non-strict inequalities, or as the lp_solve manual says: "The inequalities can be <=, >= or Because all numbers are real values, <= is the same as < and >= is the same as > ." Try x_i >= eps > 0 with some eps appropriate for your problem. Hans Werner> Any advice would be very helpful. > > best regards > > Andreas > >
Soetaert, Karline
2009-Dec-04 12:22 UTC
[R] Solve linear program without objective function
Andreas,
I don't know what it is exactly that you want to do, but perhaps you can try
using limSolve,
which solves for the equations Ex>=F, Gx>=H. In the code below, lsei
calculates the least squares solution (min sum(x^2)), xranges estimates the
range of your variables, and xsample takes a random sample of solutions.
f.con<-matrix(c(0,2/3,1/3,1/3,
1,1,1,1,
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1),nrow=6,byrow=TRUE)
f.rhs <- c(0.3, 1, 0, 0, 0, 0)
E<- f.con[1:2,]
G<- f.con[-(1:2),]
F<- f.rhs[1:2]
H<-f.rhs[-(1:2)]
require(limSolve)
lsei(E=E,F=F,G=G,H=H)
xranges(E=E,F=F,G=G,H=H)
xs <- xsample(E=E,F=F,G=G,H=H)
pairs(xs$X)
Hope this helps,
Karline
[[alternative HTML version deleted]]