有条件的和

时间:2019-06-27 17:56:11

标签: conditional-statements cplex

除了j不等于k之外,我想对一个范围(面积)求和。 有人可以帮我吗?

我尝试过:

forall( k in Areas ) 
        sum ( j in Areas: j!=k ) X[k][j] == 1;
Also tried:
forall( k in Areas ) 
        sum ( j in Areas) (j!=k)*X[k][j] == 1;

    int NbAreas = 5;

    range Areas = 1..NbAreas;

    float P[Areas] = [0, 0.3, 0.65, 0.2, 0.1];
    float D[Areas] = [0, 7, 5, 3, 9];
    float FROMTO[Areas][Areas] =    [ 
                                    [0, 2, 5, 1, 3],
                                    [2, 0, 4, 3, 8],
                                    [5, 4, 0, 6, 2],
                                    [1, 3, 6, 0, 7],
                                    [3, 8, 2, 7, 0]];


    dvar int Y[Areas];
    dvar int T[Areas];
    dvar int X[Areas][Areas] in 0..1;


    maximize sum( i in Areas ) P[i] * Y[i];
    subject to {
        forall( k in Areas ) 
            sum ( j in Areas: j!=k) X[k][j] == 1;
        forall( k in Areas)
            sum ( i in Areas: i!=k) X[i][k] == 1;
        forall( i in Areas) forall (j in Areas) T[i] + FROMTO[i][j] -     T[j] - 100*(1-X[i][j]) <= 0;
        T[1] == 0;
        forall( i in Areas: i!=1) T[i] - D[i] - 1000*(1-Y[i]) <= 0;
}

2 个答案:

答案 0 :(得分:1)

我理解你的意思

  

我想对一个范围(面积)求和,除非j是等于(而不是j)   “不等于”)到k。

我还假设您的问题是发布的模型不可行。您应该标记约束,以便冲突优化程序可以运行,然后查看冲突优化程序的结果。如果我这样标记您的约束条件:

maximize sum( i in Areas ) P[i] * Y[i];
subject to {
    forall( k in Areas ) 
        sum1: sum ( j in Areas: j!=k) X[k][j] == 1;
    forall( k in Areas)
        sum2: sum ( i in Areas: i!=k) X[i][k] == 1;
    forall( i in Areas) forall (j in Areas)
        fromto: T[i] + FROMTO[i][j] - T[j] - 100*(1-X[i][j]) <= 0;
    T[1] == 0;
    forall( i in Areas: i!=1)
        limit: T[i] - D[i] - 1000*(1-Y[i]) <= 0;

然后我遇到了这个冲突:

 sum1(1):      X(1)(2) + X(1)(3) + X(1)(4) + X(1)(5)  = 1
 sum1(2):      X(2)(1) + X(2)(3) + X(2)(4) + X(2)(5)  = 1
 sum1(3):      X(3)(1) + X(3)(2) + X(3)(4) + X(3)(5)  = 1
 sum1(4):      X(4)(1) + X(4)(2) + X(4)(3) + X(4)(5)  = 1
 sum1(5):      X(5)(1) + X(5)(2) + X(5)(3) + X(5)(4)  = 1
 fromto(1)(2): 100 X(1)(2) + T(1) - T(2) <= 98
 fromto(1)(3): 100 X(1)(3) + T(1) - T(3) <= 95
 fromto(1)(4): 100 X(1)(4) + T(1) - T(4) <= 99
 fromto(1)(5): 100 X(1)(5) + T(1) - T(5) <= 97
 fromto(2)(1): 100 X(2)(1) - T(1) + T(2) <= 98
 fromto(2)(3): 100 X(2)(3) + T(2) - T(3) <= 96
 fromto(2)(4): 100 X(2)(4) + T(2) - T(4) <= 97
 fromto(2)(5): 100 X(2)(5) + T(2) - T(5) <= 92
 fromto(3)(1): 100 X(3)(1) - T(1) + T(3) <= 95
 fromto(3)(2): 100 X(3)(2) - T(2) + T(3) <= 96
 fromto(3)(4): 100 X(3)(4) + T(3) - T(4) <= 94
 fromto(3)(5): 100 X(3)(5) + T(3) - T(5) <= 98
 fromto(4)(1): 100 X(4)(1) - T(1) + T(4) <= 99
 fromto(4)(2): 100 X(4)(2) - T(2) + T(4) <= 97
 fromto(4)(3): 100 X(4)(3) - T(3) + T(4) <= 94
 fromto(4)(5): 100 X(4)(5) + T(4) - T(5) <= 93
 fromto(5)(1): 100 X(5)(1) - T(1) + T(5) <= 97
 fromto(5)(2): 100 X(5)(2) - T(2) + T(5) <= 92
 fromto(5)(3): 100 X(5)(3) - T(3) + T(5) <= 98
 fromto(5)(4): 100 X(5)(4) - T(4) + T(5) <= 93

因此,看来您在FROMTOT中的数据无法提供可行的解决方案。

答案 1 :(得分:0)

您写了“除非j不等于k”。

所以不是

forall( k in Areas)
        sum ( i in Areas: i!=k) X[i][k] == 1;

我会写

forall( k in Areas)
        sum ( i in Areas: i==k) X[i][k] == 1;