簡體   English   中英

如何將非線性約束轉換為新的線性約束?

[英]How to convert non linear constraint to new linear constraints?

我已經將非線性表達式 [1]: https://i.stack.imgur.com/MzzSO.png轉換為線性方程式 11、12 和 13。但是當我運行我的代碼時,我得到了這些錯誤:“約束尺寸可變的維度不支持標記,請改用命名約束”,“CPLEX 無法提取表達式”,“元素“cons12”未定義”和“元素“cons12”的初始化表達式無效”。 你能幫幫我嗎,我該怎么辦? 提前致謝。

using CPLEX;

//Total nodes number. 
range Nodes = 1..9;
//{int} Nodes = {1,2,3,4,5,6,7,8,9};
//................................................................................

//Total links number

//two_directed
tuple edge{
  int node_out;
  int node_in;
          };
 {edge} L with node_out, node_in in Nodes = {<1,3>, <3,1>, <2,3>, <3,2>, <3,4>, <4,3>, <3,5>, 
                                             <5,3>, <3,6>, <6,3>, <4,5>, <5,4>, <4,6>, <6,4>, 
                                             <4,8>, <8,4>, <5,6>, <6,5>, <6,7>, <7,6>, <6,9>, 
                                             <9,6>};
  {edge} Lout[Nodes] = [{<1,3>},//node1
                 {<2,3>},//node2
                 {<3,1>, <3,2>, <3,4>, <3,5>, <3,6>},//node3
                 {<4,3>, <4,5>, <4,6>, <4,8>},//node4
                 {<5,3>, <5,4>, <5,6>},//node5
                 {<6,3>, <6,4>, <6,5>, <6,7>, <6,9>},//node6
                 {<7,6>},//node7
                 {<8,4>},//node8
                 {<9,6>}];//node9 
 //Flows
 tuple cflow{
   int origin;
   int destination;
           }
 {cflow} F with origin,destination in Nodes = {<1,2>, <1,3>, <1,4>, <1,5>, <1,6>, <1,7>, 
                      <1,8>, <1,9>, <2,1>, <2,3>, <2,4>, <2,5>, <2,6>, <2,7>, <2,8>, <2,9>, 
                      <3,1>, <3,2>, <3,4>, <3,5>, <3,6>, <3,7>, <3,8>, <3,9>, 
                      <4,1>, <4,2>, <4,3>, <4,5>, <4,6>, <4,7>, <4,8>, <4,9>, 
                      <5,1>, <5,2>, <5,3>, <5,4>, <5,6>, <5,7>, <5,8>, <5,9>,
                      <6,1>, <6,2>, <6,3>, <6,4>, <6,5>, <6,7>, <6,8>, <6,9>, <7,1>, <7,2>};
 
float landa_f[f in F]=[0.86, 0.3, 0.75, 0.23, 0.32, 0.4, 0.5, 0.6, 0.22, 0.14, 
                       0.23, 0.42, 0.33, 0.5, 0.62, 0.36, 0.42, 0.35, 0.2, 0.16,
                       0.33, 0.9, 0.41, 0.51, 0.61, 0.33, 0.42, 0.51, 0.87, 0.96,
                       0.31, 0.55, 0.91, 0.36, 0.32, 0.72, 0.76, 0.32, 0.45, 0.64,
                       0.38, 0.71, 0.43, 0.55, 0.53, 0.9, 0.58, 0.97, 0.5, 0.33 ];
 
   {string} V = {"IDS", "DPI", "NAT", "Proxy", "Firewall"};
 
  //MAIN DECISION VARIABLES
  dvar int I[v in V][n in Nodes][f in F][j in 1..2] in 0..1; //denotes that an NF instance v 
  hosted at node n is used by the j-th service on the service chain of flow f.

  dvar int IL[l in L][f in F][j in 1..2][n in Nodes] in 0..1;//denotes that link l is used by 
  flow f to route from the j-th to (j + 1)-th NF service, hosted at node nj and nj+1.

  dvar int Y[v in V][n in Nodes];

  //Decision variables related  with non linear equations
  dvar int z[l in L][f in F][j in 1..2][n in Nodes][v in V] in 0..1;

subject to{
     //convert non_linear_equations to new linear constraints
     forall (f in F, j in 1..2, v in V) 
     cons11: sum( l in Lout[item(Routes[f],j-1)] ) z[l][f][j][item(Routes[f],j-1)][v] == 1;
                        
   forall (f in F, j in 1..2, l in Lout[item(Routes[f],j-1)], v in V) {
    
    cons12: 3 * z[l][f][j][item(Routes[f],j-1)][v] <= ( IL[l][f][j][item(Routes[f],j-1)] + 
                 I[v][item(Routes[f],j-1)][f][j] );     
    cons13:   z[l][f][j][item(Routes[f],j-1)][v] >= ( IL[l][f][j][item(Routes[f],j-1)] + 
                 I[v][item(Routes[f],j-1)][f][j] ) - 2;  }
       }

的確

dvar int x[1..2][1..2];

{int} s[1..2]=[{1,2},{1}];

subject to
{
  forall(i in 1..2) forall(j in s[i]) ct:x[i][j]<=0;
}       

execute
{
  writeln(ct[1][1].UB);
}  

不起作用,但如果你寫

dvar float x[1..2][1..2];



{int} s[1..2]=[{1,2},{1}];

subject to
{
  forall(i in 1..2) forall(j in 1..2:j in s[i]) ct:x[i][j]<=0;
}   

execute
{
  writeln(ct[1][1].UB);
}

然后它工作正常

 range r=1..2;

dvar float x[1..2][1..2];

constraint ct[r][r];

{int} s[1..2]=[{1,2},{1}];

subject to
{
  forall(i in 1..2,j in 1..2)  
  ct[i][j]= if (j in s[i]) x[1][i]<=0;
}   

execute
{
  writeln(ct[1][1].UB);
}   

也很好用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM