如何在 R 中没有错误消息的情况下输入约束符号

How do I input signs for constraints without an error message in R

我想将 "signs" 包含在我关于 LP 问题的约束中。

例如,

最大化 120X1 + 230X2 + 410X3 + 100X4

580X1 + 600X2 + 800X3 + 300X4 ≤ 1000

0≤X1≤10

6≤X2≤10

5≤X3≤12

0≤X4≤10

代码如下,

库(lpSolve)

cvec <- c(120,230,410,100) #OF Coefficients  
Amat <- rbind(c(580,600,800,300),**c(x<6,x>10)**,c(15),c(22),c(10))  
bvec <- c(10000,40,15,22,10)   #RHS values

res <- solveLP(cvec, Amat, bvec, maximum = TRUE, const.dir = c("<=","<=","<=","<=","<="),solve.dual=FALSE, verbose = 4)

print(res)

The output shows : Matrix A must have as many rows as constraints (=elements of vector b) and as many columns as variables (=elements of vector c).

请注意,580X1 + 600X2 + 800X3 + 300X4 ≤ 1000 使模型不可行。这是一个非常简单的教科书 LP 求解器,似乎处理得不是很优雅。

> library(linprog)
> cvec <- c(120,230,410,100) # obj coefficients  
> Amat <- rbind(
+ #    x1  x2  x3  x4    
+   c(580,600,800,300),    # 580X1 + 600X2 + 800X3 + 300X4 ≤ 10000
+   c(  1,  0,  0,  0),    # x1 <= 10
+   c(  0,  1,  0,  0),    # x2 >= 6
+   c(  0,  1,  0,  0),    # X2 <= 10
+   c(  0,  0,  1,  0),    # x3 >= 5
+   c(  0,  0,  1,  0),    # x3 <= 12
+   c(  0,  0,  0,  1))    # x4 <= 10
> bvec <- c(10000,10,6,10,5,12,10)  # RHS values
> dir  <- c("<=","<=",">=","<=",">=","<=","<=")
> res <- solveLP(cvec, bvec, Amat, maximum = TRUE, const.dir = dir, verbose = 1)
> print(res)


Results of Linear Programming / Linear Optimization

Objective function (Maximum): 4660 

Iterations in phase 1: 2
Iterations in phase 2: 1
Solution
  opt
1   0
2   6
3   8
4   0

Basic Variables
    opt
2     6
3     8
S 2  10
S 4   4
S 5   3
S 6   4
S 7  10

Constraints
  actual dir  bvec free    dual dual.reg
1  10000  <= 10000    0  0.5125     2400
2      0  <=    10   10  0.0000       10
3      6  >=     6    0 77.5000        4
4      6  <=    10    4  0.0000        4
5      8  >=     5    3  0.0000        3
6      8  <=    12    4  0.0000        4
7      0  <=    10   10  0.0000       10

All Variables (including slack variables)
    opt cvec    min.c    max.c      marg   marg.reg
1     0  120     -Inf 297.2500 -177.2500    4.13793
2     6  230     -Inf 307.5000        NA         NA
3     8  410  306.667      Inf        NA         NA
4     0  100     -Inf 153.7500  -53.7500    8.00000
S 1   0    0     -Inf   0.5125   -0.5125 2400.00000
S 2  10    0 -177.250      Inf    0.0000         NA
S 3   0    0     -Inf  77.5000  -77.5000    4.00000
S 4   4    0  -77.500      Inf    0.0000         NA
S 5   3    0 -103.333      Inf    0.0000         NA
S 6   4    0       NA 103.3333    0.0000         NA
S 7  10    0  -53.750      Inf    0.0000         NA

> 

周围有更好的工具和求解器。