是否有一种简单的方法可以为constrOptim函数中的以下约束设置theta,ui,ci?
c1<x1<=c2
x1+1<x2<=c2+1
x2+1<x3<=c2+2
x3+1<x4<=c2+3
我考虑过使用单纯形,但只需要3个约束。
由于
答案 0 :(得分:18)
只需以所需格式重写约束ui %*% theta >= ci
。
# Initial formulation of the constraints
c1 <= x1
x1 <= c2
x1+1 <= x2
x2 <= c2+1
x2+1 <= x3
x3 <= c2+2
x3+1 <= x4
x4 <= c2+3
# Rewrite them
x1 >= c1
- x1 >= -c2
- x1 + x2 >= 1
- x2 >= -c2 - 1
- x2 + x3 >= 1
- x3 >= -c2 - 2
- x3 + x4 >= 1
- x4 >= -c2 - 3
# In matrix form
ui <- matrix(c(
1, 0, 0, 0,
-1, 0, 0, 0,
-1, 1, 0, 0,
0, -1, 0, 0,
0, -1, 1, 0,
0, 0, -1, 0,
0, 0, -1, 1,
0, 0, 0, -1
),
ncol = 4,
byrow = TRUE
)
ci <- c( c1, -c2, 1, -c2-1, 1, -c2-2, 1, -c2-3 )