Re: [R] linear programming in R | limits to what it can do, or my mistake?

2024-01-30 Thread Jinsong Zhao



On 2024/1/30 20:00, Martin Becker wrote:
Apart from the fact that the statement "such that t1+t2+t3+t4=2970 (as 
it must)" is not correct, the LP can be implemented as follows:


I was confused by "such that t1+t2+t3+t4=2970 (as it must)", otherwise, 
I also get the same solution.

library(lpSolve)
LHS <- rbind(
c(0,0,0,0, 1, 0, 0,0),
c(1,0,0,0,-1, 1, 0,0),
c(0,1,0,0, 0,-1, 1,0),
c(0,0,1,0, 0, 0,-1,1),
cbind(-diag(4),diag(4)),
c(0,0,0,0,0,1,0,0),
c(0,0,0,0,0,0,1,0),
c(0,0,0,0,0,0,0,1)
)

RHS <- c(640,825,580,925,0,0,0,0,1000,1000,1000)

DIR <- c(rep("==",4),rep(">=",3),"=",rep("<=",3))

OBJ <- c(35,55,50,65,0,0,0,0)

lp("min",OBJ,LHS,DIR,RHS)

Best,
Martin


Am 29.01.24 um 22:28 schrieb Evan Cooch:

Question for 'experts' in LP using R (using the lpSolve package, say) --
which does not apply to me for the sort of problem I describe below.
I've run any number of LP's using lpSolve in R, but all of them to date
have objective and constraint functions that both contain the same
variables. This lets you set up a LHS and RHS matrix/vector that are
symmetrical.

But, for a problem a student posed in class, I'm stuck with how to do it
in R, if its even possible (its trivial in Maxima, Maple...even using
Solver in Excel, but I haven't been remotely successful in getting
anything to work in R).

Suppose you have a production system that at 4 sequential time steps
generate 640, 825, 580, and 925 units. At each time step, you need to
decide how many of those units need to be 'quality control' (QC) checked
in some fashion, subject to some constraints.

   --> at no point in time can the number of units in the system be 
>1000

   --> at the end of the production cycle, there can be no units left
   --> 'QC checking' costs money, varying as a function of the time step
-- 35, 55, 50 and 65 for each unit, for each time step in turn.

Objective is to minimize total cost. The total cost objective function
is trivial. Let p1 = number sent out time step 1, p2 number sent out at
time step 3, and so on. So, total cost function we want to minimize is
simply

    cost=(35*p1)+(55*p2)+(50*p3)+(65*p4)

where p1+p2+p3+p4=(640+825+580+925)=2970 (i.e., all the products get
checked). The question is, what number do you send out at each time step
to minimize cost?

Where I get hung up in R is the fact that if I let t(i) be the number of
products at each time step, then

      t1=640,
      t2=t1-p1+825
      t3=t2-p2+580
      t4=t3-p3+925

such that t1+t2+t3+t4=2970 (as it must), with additional constraints 
being


    p1<=t1, p2<=t2, p3<=t3, p4<=t4, {t1..t4}<=1000, and t4-p4=0.

There may be algebraic ways to reduce the number of functions needed to
describe the constraints, but I can't for the life of me see how I can
create a coefficient matrix (typically, the LHS) since each line of said
matrix, which corresponds to the constraints, needs to be a function of
the unknowns in the objective function -- being, p1, p2, p3 and p4.

In Maple (for example), this is trivial:

   cost:=35*p10+55*p12+50*p14+65*p16;
cnsts:={t10=640,t12=t10-p10+825,t14=t12-p12+580,t16=t14-p14+925,t16-p16=0,p10<=t10,p12<=t12,p14<=t14,p16<=t16,t10<=1000,t12<=1000,t14<=1000,t16<=1000}; 


       Minimize(cost,cnsts,assume={nonnegative});

which yields (correctly):

p1=640, p2=405, p3=1000, p4=925

for minimized cost of 154800.

Took only a minute to also set this up in Maxima, and using Solver in
Excel. But danged if I can suss out any way to do this in R.

Pointers to the obvious welcome.
[[alternative HTML version deleted]]






__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] linear programming in R | limits to what it can do, or my mistake?

2024-01-30 Thread Martin Becker
Apart from the fact that the statement "such that t1+t2+t3+t4=2970 (as 
it must)" is not correct, the LP can be implemented as follows:


library(lpSolve)
LHS <- rbind(
c(0,0,0,0, 1, 0, 0,0),
c(1,0,0,0,-1, 1, 0,0),
c(0,1,0,0, 0,-1, 1,0),
c(0,0,1,0, 0, 0,-1,1),
cbind(-diag(4),diag(4)),
c(0,0,0,0,0,1,0,0),
c(0,0,0,0,0,0,1,0),
c(0,0,0,0,0,0,0,1)
)

RHS <- c(640,825,580,925,0,0,0,0,1000,1000,1000)

DIR <- c(rep("==",4),rep(">=",3),"=",rep("<=",3))

OBJ <- c(35,55,50,65,0,0,0,0)

lp("min",OBJ,LHS,DIR,RHS)

Best,
Martin


Am 29.01.24 um 22:28 schrieb Evan Cooch:

Question for 'experts' in LP using R (using the lpSolve package, say) --
which does not apply to me for the sort of problem I describe below.
I've run any number of LP's using lpSolve in R, but all of them to date
have objective and constraint functions that both contain the same
variables. This lets you set up a LHS and RHS matrix/vector that are
symmetrical.

But, for a problem a student posed in class, I'm stuck with how to do it
in R, if its even possible (its trivial in Maxima, Maple...even using
Solver in Excel, but I haven't been remotely successful in getting
anything to work in R).

Suppose you have a production system that at 4 sequential time steps
generate 640, 825, 580, and 925 units. At each time step, you need to
decide how many of those units need to be 'quality control' (QC) checked
in some fashion, subject to some constraints.

   --> at no point in time can the number of units in the system be >1000
   --> at the end of the production cycle, there can be no units left
   --> 'QC checking' costs money, varying as a function of the time step
-- 35, 55, 50 and 65 for each unit, for each time step in turn.

Objective is to minimize total cost. The total cost objective function
is trivial. Let p1 = number sent out time step 1, p2 number sent out at
time step 3, and so on. So, total cost function we want to minimize is
simply

    cost=(35*p1)+(55*p2)+(50*p3)+(65*p4)

where p1+p2+p3+p4=(640+825+580+925)=2970 (i.e., all the products get
checked). The question is, what number do you send out at each time step
to minimize cost?

Where I get hung up in R is the fact that if I let t(i) be the number of
products at each time step, then

      t1=640,
      t2=t1-p1+825
      t3=t2-p2+580
      t4=t3-p3+925

such that t1+t2+t3+t4=2970 (as it must), with additional constraints being

    p1<=t1, p2<=t2, p3<=t3, p4<=t4, {t1..t4}<=1000, and t4-p4=0.

There may be algebraic ways to reduce the number of functions needed to
describe the constraints, but I can't for the life of me see how I can
create a coefficient matrix (typically, the LHS) since each line of said
matrix, which corresponds to the constraints, needs to be a function of
the unknowns in the objective function -- being, p1, p2, p3 and p4.

In Maple (for example), this is trivial:

   cost:=35*p10+55*p12+50*p14+65*p16;
cnsts:={t10=640,t12=t10-p10+825,t14=t12-p12+580,t16=t14-p14+925,t16-p16=0,p10<=t10,p12<=t12,p14<=t14,p16<=t16,t10<=1000,t12<=1000,t14<=1000,t16<=1000};
       Minimize(cost,cnsts,assume={nonnegative});

which yields (correctly):

p1=640, p2=405, p3=1000, p4=925

for minimized cost of 154800.

Took only a minute to also set this up in Maxima, and using Solver in
Excel. But danged if I can suss out any way to do this in R.

Pointers to the obvious welcome.
[[alternative HTML version deleted]]




--
apl. Prof. PD Dr. Martin Becker, Akad. Oberrat
Lehrstab Statistik
Quantitative Methoden
Fakultät für Empirische Humanwissenschaften und Wirtschaftswissenschaft
Universität des Saarlandes
Campus C3 1, Raum 2.17
66123 Saarbrücken
Deutschland

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] linear programming in R | limits to what it can do, or my mistake?

2024-01-30 Thread Evan Cooch
Question for 'experts' in LP using R (using the lpSolve package, say) -- 
which does not apply to me for the sort of problem I describe below.
I've run any number of LP's using lpSolve in R, but all of them to date 
have objective and constraint functions that both contain the same 
variables. This lets you set up a LHS and RHS matrix/vector that are 
symmetrical.

But, for a problem a student posed in class, I'm stuck with how to do it 
in R, if its even possible (its trivial in Maxima, Maple...even using 
Solver in Excel, but I haven't been remotely successful in getting 
anything to work in R).

Suppose you have a production system that at 4 sequential time steps 
generate 640, 825, 580, and 925 units. At each time step, you need to 
decide how many of those units need to be 'quality control' (QC) checked 
in some fashion, subject to some constraints.

  --> at no point in time can the number of units in the system be >1000
  --> at the end of the production cycle, there can be no units left
  --> 'QC checking' costs money, varying as a function of the time step 
-- 35, 55, 50 and 65 for each unit, for each time step in turn.

Objective is to minimize total cost. The total cost objective function 
is trivial. Let p1 = number sent out time step 1, p2 number sent out at 
time step 3, and so on. So, total cost function we want to minimize is 
simply

   cost=(35*p1)+(55*p2)+(50*p3)+(65*p4)

where p1+p2+p3+p4=(640+825+580+925)=2970 (i.e., all the products get 
checked). The question is, what number do you send out at each time step 
to minimize cost?

Where I get hung up in R is the fact that if I let t(i) be the number of 
products at each time step, then

     t1=640,
     t2=t1-p1+825
     t3=t2-p2+580
     t4=t3-p3+925

such that t1+t2+t3+t4=2970 (as it must), with additional constraints being

   p1<=t1, p2<=t2, p3<=t3, p4<=t4, {t1..t4}<=1000, and t4-p4=0.

There may be algebraic ways to reduce the number of functions needed to 
describe the constraints, but I can't for the life of me see how I can 
create a coefficient matrix (typically, the LHS) since each line of said 
matrix, which corresponds to the constraints, needs to be a function of 
the unknowns in the objective function -- being, p1, p2, p3 and p4.

In Maple (for example), this is trivial:

  cost:=35*p10+55*p12+50*p14+65*p16;
cnsts:={t10=640,t12=t10-p10+825,t14=t12-p12+580,t16=t14-p14+925,t16-p16=0,p10<=t10,p12<=t12,p14<=t14,p16<=t16,t10<=1000,t12<=1000,t14<=1000,t16<=1000};
      Minimize(cost,cnsts,assume={nonnegative});

which yields (correctly):

p1=640, p2=405, p3=1000, p4=925

for minimized cost of 154800.

Took only a minute to also set this up in Maxima, and using Solver in 
Excel. But danged if I can suss out any way to do this in R.

Pointers to the obvious welcome.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] linear programming

2014-03-17 Thread John P. Burkett

On 03/17/2014 07:57 AM, Barbara Rogo wrote:

I have this problem with this form:

min (A*X) under some constraints.

the unknown is X that is a Matrix. I can't use the function "linp" because
in it X is a vector..

How can I do??? Can you help me
If X is a matrix, then A*X could be a matrix or a vector but not a 
scalar.  You will probably need to specify what scalar function of A*X 
is to be minimized.



--
John P. Burkett

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] linear programming

2014-03-17 Thread Barbara Rogo
I have this problem with this form:

min (A*X) under some constraints.

the unknown is X that is a Matrix. I can't use the function "linp" because
in it X is a vector..

How can I do??? Can you help me

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Linear programming problem, RGPLK - "no feasible solution".

2011-10-13 Thread liuevans
Thank you very much for this!  This also solves my original problem.  I can't
remember at what point I assumed the bounds would be written that way.  It
was a costly error.

Regarding the potential bug, I'm going to report it.  R shut down completely
every time I ran the program, but didn't when I edited the file to correct
the "dir" term.

--
View this message in context: 
http://r.789695.n4.nabble.com/Re-Linear-programming-problem-RGPLK-no-feasible-solution-tp3890377p3901297.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Linear programming problem, RGPLK - "no feasible solution".

2011-10-10 Thread Hans W Borchers
Liu Evans, Gareth  liverpool.ac.uk> writes:

> In my post at https://stat.ethz.ch/pipermail/r-help/2011-October/292019.html
> I included an undefined term "ej".  The problem code should be as follows.
> It seems like a simple linear programming problem, but for some reason my
> code is not finding the solution.
> 
> obj <- c(rep(0,3),1)
> 
> col1 <-c(1,0,0,1,0,0,1,-2.330078923,0)
> col2 <-c(0,1,0,0,1,0,1,-2.057855981,0)
> col3 <-c(0,0,1,0,0,1,1,-1.885177032,0)
> col4 <-c(-1,-1,-1,1,1,1,0,0,1)
> 
> mat <- cbind(col1, col2, col3, col4)
> 
> dir <- c(rep("<=", 3), rep(">=", 3), rep("==", 2), ">=")
> 
> rhs <- c(rep(0, 7), 1, 0)
> 
> sol <- Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE,
> bounds = c(-100,100), verbose = TRUE)
> 
> The R output says there is no feasible solution, but e.g.
> (-2.3756786,  0.3297676,  2.0459110, 2.3756786) is feasible.
> 
> The output is
> 
> "GLPK Simplex Optimizer, v4.42
> 9 rows, 4 columns, 19 non-zeros
>   0: obj =  0.0e+000  infeas = 1.000e+000 (2)
> PROBLEM HAS NO FEASIBLE SOLUTION"

Please have a closer look at the help page "?Rglpk_solve_LP". The way to
define the bounds is a bit clumsy, but then it works:

sol <- Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE,
bounds = list(lower=list(ind=1:4, val=rep(-100,4)),
  upper=list(ind=1:4, val=rep(100,4))),
verbose=TRUE)

GLPK Simplex Optimizer, v4.42
9 rows, 4 columns, 19 non-zeros
  0: obj =  -1.0e+02  infeas =  1.626e+03 (2)
*10: obj =   1.0e+02  infeas =  0.000e+00 (0)
*13: obj =   2.247686558e+00  infeas =  0.000e+00 (0)
OPTIMAL SOLUTION FOUND

> sol
$optimum
[1] 2.247687
$solution
[1] -2.247687e+00 -6.446292e-31  2.247687e+00  2.247687e+00

> One other thing, a possible bug - if I run this code with "dir" shorter than
> it should be, R crashes.  My version of R is 2.131.56322.0, and I'm running
> it on Windows 7.  

If you can reproduce that R crashes -- which it shall never do -- inform the
maintainer of this package. On Mac it doesn't crash, it goes into an infinite
loop with "Execution aborted.Error detected in file glplib03.c at line 83".

Regards, Hans Werner

> Regards,
> Gareth

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Linear programming problem, RGPLK - "no feasible solution".

2011-10-10 Thread Liu Evans, Gareth
In my post at https://stat.ethz.ch/pipermail/r-help/2011-October/292019.html I 
included an undefined term "ej".  The problem code should be as follows.  It 
seems like a simple linear programming problem, but for some reason my code is 
not finding the solution.

obj <- c(rep(0,3),1)

col1 <-c(1,0,0,1,0,0,1,-2.330078923,0)
col2 <-c(0,1,0,0,1,0,1,-2.057855981,0)
col3 <-c(0,0,1,0,0,1,1,-1.885177032,0)
col4 <-c(-1,-1,-1,1,1,1,0,0,1)

mat <- cbind(col1, col2, col3, col4)

dir <- c(rep("<=", 3), rep(">=", 3), rep("==", 2), ">=")

rhs <- c(rep(0, 7), 1, 0)

sol <- Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE,
bounds = c(-100,100), verbose = TRUE)


The R output says there is no feasible solution, but e.g. (-2.3756786,  
0.3297676,  2.0459110, 2.3756786) is feasible.

The output is

"GLPK Simplex Optimizer, v4.42
9 rows, 4 columns, 19 non-zeros
  0: obj =  0.0e+000  infeas = 1.000e+000 (2)
PROBLEM HAS NO FEASIBLE SOLUTION"

One other thing, a possible bug - if I run this code with "dir" shorter than it 
should be, R crashes.  My version of R is 2.131.56322.0, and I'm running it on 
Windows 7.  

Regards,
Gareth

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Linear programming problem, RGPLK - "no feasible solution"

2011-10-07 Thread Liu Evans, Gareth
Dear All,

RGLPK says that there is no feasible solution but I think there should be.  In 
a more general setup with more variables, where we know the feasible solution 
analytically, it also says there is no feasible solution.  I have tried 
different values. except the 0s and 1s must stay as they are.  The result has 
always been the same so far, i.e. no feasible solution.


Here is my code:


obj <- c(rep(0,3),1)

col1 <-c(1,0,0,1,0,0,1,-2.330078923,0)
col2 <-c(0,1,0,0,1,0,1,-2.057855981,0)
col3 <-c(0,0,1,0,0,1,1,-1.885177032,0)
col4 <-c(-1,-1,-1,1,1,1,0,0,1)

mat <- cbind(col1, col2, col3, col4)

dir <- c(rep("<=", 3), rep(">=", 3), rep("==", 2), ">=")

rhs <- c(rep(0, 6), ej, 0)

sol <- Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE,
bounds = c(-100,100), verbose = TRUE)

Regards,
Gareth

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Linear Programming.

2008-05-27 Thread Arne Henningsen
Hi Marcus!

On Wednesday 28 May 2008 05:56, Marcus Vinicius wrote:
>  Dear all,
> May anyone explain to me how  I run a linear programming or Data
> Envelopment Analysis (DEA models) into R?

Package "DEA" (on CRAN):
http://cran.r-project.org/web/packages/DEA/index.html

Package "FEAR" (NOT on CRAN):
http://www.clemson.edu/economics/faculty/wilson/Software/FEAR/fear.html

Best wishes,
Arne

> Thanks a lot.
> Best Regards.
> Marcus Vinicius
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented, minimal,
> self-contained, reproducible code.

-- 
Arne Henningsen
http://www.arne-henningsen.name

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Linear Programming.

2008-05-27 Thread Prof Brian Ripley

On my system


help.search("linear program")


gives (slightly edited)

linp(limSolve)  Linear Programming
solveLP(linprog)solve Linear Programming / Optimization
problems
lp.object(lpSolve)  LP (linear programming) object
lpcdd(rcdd) linear programming with exact arithmetic
simplex(boot)   Simplex Method for Linear Programming Problems

which should point you at some relevant packages.

DEA appears to be an OR technique, and we don't see many OR users of R and 
hence very few packages.


On Wed, 28 May 2008, Marcus Vinicius wrote:


Dear all,
May anyone explain to me how  I run a linear programming or Data Envelopment
Analysis (DEA models) into R?
Thanks a lot.
Best Regards.
Marcus Vinicius

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Linear Programming.

2008-05-27 Thread Marcus Vinicius
 Dear all,
May anyone explain to me how  I run a linear programming or Data Envelopment
Analysis (DEA models) into R?
Thanks a lot.
Best Regards.
Marcus Vinicius

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.