简体   繁体   中英

How to change 1 element in a matrix in Cplex

 int demand1[T][I]=[[0,17,4,7,10],
    [0,39,7,3,9],
    [0,15,13,10,10],
    [0,2,19,5,2],
    [0,7,13,4,16],
    [0,5,5,1,10],
    [0,15,13,12,7],
    [0,15,14,10,10]]

forall(i in Z, t in T)
  if (demand1[t][i] <= 5){
    demand1[t][i]==0;

}

The intention of this code should be, if there is one element that's greater then 0 and smaller then 5, it should be put to 0

You can use the ternary operator or a discontinuous piecewise linear functionrange T=1..8; range I=1..5;

int demand0[T][I]=[[0,17,4,7,10],
    [0,39,7,3,9],
    [0,15,13,10,10],
    [0,2,19,5,2],
    [0,7,13,4,16],
    [0,5,5,1,10],
    [0,15,13,12,7],
    [0,15,14,10,10]];
    
int demand1[t in T][i in I]=(0<=demand0[t][i]<=5)?0:demand0[t][i]; 

pwlFunction f=piecewise{1->0;0->6;6->6;1};   
int demand1b[t in T][i in I]=ftoi(f(demand0[t][i]));

execute
{
  writeln(demand1);
  writeln(demand1b)
}

assert forall(t in T,i in I) as:demand1[t][i]==demand1b[t][i];

works fine and gives

[[0 17 0 7 10]
         [0 39 7 0 9]
         [0 15 13 10 10]
         [0 0 19 0 0]
         [0 7 13 0 16]
         [0 0 0 0 10]
         [0 15 13 12 7]
         [0 15 14 10 10]]
 [[0 17 0 7 10]
         [0 39 7 0 9]
         [0 15 13 10 10]
         [0 0 19 0 0]
         [0 7 13 0 16]
         [0 0 0 0 10]
         [0 15 13 12 7]
         [0 15 14 10 10]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM