Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

r - Solving simultaneous equations/inequalities

I need to solve a mix of equations/inequalities:

a1 + a2 + a3 >= 50
b1 + b2 + b3 <= 40
c1 + c2 + c3 <= 10
a1 + b1 + c1  = 50
a2 + b2 + c2  = 35
a3 + b3 + c3  = 15

There are nine variables and six equations/inequalities, but I will be zeroing three of the variables at a time (I will have to check all combinations).

I tried a couple of packages in R (limSolve, matlib) with no success.

#The matrix representation:
X1 <- c(1,1,1,0,0,0,0,0,0)
X2 <- c(0,0,0,1,1,1,0,0,0)
X3 <- c(0,0,0,0,0,0,1,1,1)
Y1 <- c(1,0,0,1,0,0,1,0,0)
Y2 <- c(0,1,0,0,1,0,0,1,0)
Y3 <- c(0,0,1,0,0,1,0,0,1)
A1 <- matrix(c(X1,X2,X3,Y1,Y2,Y3),c(9,9))
A2 <- t(A1); colnames(A2) <- c("a1","a2","a3","b1","b2","b3","c1","c2","c3")
b <- c(50,40,10,50,35,15)

Any help will be appreciated.

question from:https://stackoverflow.com/questions/65841855/solving-simultaneous-equations-inequalities

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be solved using linear programming. There is nothing to optimize, you are just searching for a feasible solution.

library(lpSolve)

res=lp(
  "min",
  rep(0,ncol(A2)),
  A2,
  c(">=","<=","<=","==","==","=="),
  b
)

res$solution

[1] 50 35 15  0  0  0  0  0  0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...