2013-05-02 391 views
8

在constrOptim函數中是否有一個簡單的方法來設置theta,ui,ci以下約束條件?constrOptim中的設置約束

c1<x1<=c2 
x1+1<x2<=c2+1 
x2+1<x3<=c2+2 
x3+1<x4<=c2+3 

我考慮過使用simplex,但它只需要3個約束。

由於

回答

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) 
+0

謝謝文森特。這很有幫助。我想我需要回到高中...笑... – earthlink 2013-05-02 23:37:15

+2

對於在這個問題誰絆倒的用戶,注意合理區域'UI%*%THETA> ci'按本[鏈接](HTTPS: //stat.ethz.ch/pipermail/r-devel/2010-June/057730.html) – earthlink 2013-05-03 15:56:02

+0

@文森特-zoonekynd您翻譯OPS約束'C1 crsh 2014-09-18 14:54:51