0
proc iml;  
start f_prob(beta) global(one_m_one, pone_m_one);

p = nrow(one_m_one);
td = j(p,3,0.);
a = 1;
do i = 1 to p;
    td[i,1] = exp((one_m_one[i,1])*(beta[1]) + (one_m_one[i,2])*(beta[2]) + (one_m_one[i,3])*(beta[3]) + (one_m_one[i,4])*(beta[4]) + (one_m_one[i,5])*(beta[5]) + (one_m_one[i,6])*(beta[6]) + (one_m_one[i,7])*(beta[7]) + (one_m_one[i,8])*(beta[8]) + (one_m_one[i,9])*(beta[9]) + (one_m_one[i,10])*(beta[10]));
    do j = a to 11+a;
        td[i,2] = td[i,2] + exp((pone_m_one[j,1])*(beta[1]) + (pone_m_one[j,2])*(beta[2]) + (pone_m_one[j,3])*(beta[3]) + (pone_m_one[j,4])*(beta[4]) + (pone_m_one[j,5])*(beta[5]) + (pone_m_one[j,6])*(beta[6]) + (pone_m_one[j,7])*(beta[7]) + (pone_m_one[j,8])*(beta[8]) + (pone_m_one[j,9])*(beta[9]) + (pone_m_one[j,10])*(beta[10]));
    end;
    a = a + 12;
end;
td[,3] = td[,1]/td[,2];
f = 1;
do i = 1 to p;
    f = f*td[i,3];
end;
return(f);

finish f_prob;

/* Set up the constraints: sum(x)=0 */
/*     x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 SIGN VALUE */
con = {.  .  .  .  .  .  .  .  .  .   .    .,  /* specify lower bounds */
       .  .  .  .  .  .  .  .  .  .   .    .,  /* specify upper bounds */
       1  1  1  1  1  1  1  1  1  1   0    0}; /* constraints */


beta0 = j(1,10,0);
optn = {1,4};

call nlpnra(rc, result, "f_prob", beta0, optn) blc=con;

嗨,我正在尝试优化具有 10 个参数的函数 f,其中所有 10 个参数的约束总和为零。

谁能建议我如何编写最后一部分的代码,以便我可以优化 f 并获得我想要的结果?提前致谢。

4

1 回答 1

0

该文档提供了如何指定线性约束矩阵的示例。对于您的示例,请使用 3 x 12 矩阵。

  • 在第一行(第 1:10 列)放置参数的任何下限约束。
  • 在第二行(第 1:10 列)放置参数的任何上限约束。
  • 在第三行,将所有的放在 1:10 列中。在第 11 列中输入 0 表示等号。在第 12 列放 0 表示约束的值。

代码如下所示:

/* Set up the constraints: sum(x)=0 */
/*     x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 SIGN VALUE */
con = {.  .  .  .  .  .  .  .  .  .   .    .,  /* specify lower bounds */
       .  .  .  .  .  .  .  .  .  .   .    .,  /* specify upper bounds */
       1  1  1  1  1  1  1  1  1  1   0    0}; /* constraints */
call nlpnra(rc, result, "f_prob", beta, optn) blc=con;

最后一行指定矩阵表达式 c*x = 0 的系数,其中 c = {1 1 ... 1} 包含第三行的系数。

于 2017-01-05T20:29:06.580 回答