CPLEX 创建依赖于决策变量的数组(矩阵)

CPLEX Creating arrays (matrix) that depend on decision variables

非常感谢您在以下方面提供帮助。

将 n 个起点和 n 个目的地以及相关的 n x n 成本矩阵(整数)作为输入,我必须选择 k 个起点和 k 个目的地(k

我无法根据起点和终点的选择定义创建 k x k 成本矩阵的条件,因此它作为分配问题的输入。我尝试了以下代码:

int n=...;
int k= ...;

range origins = 1..n;
range destin = 1..n;
range k_origins =1..k;
range k_destin = 1..k;

int costs[origins][destin]=...;

dvar boolean x[origins];
dvar boolean y[destin];
dvar boolean z[k_origins][k_destin];

///First I tried to create k x k costs matrix directly:
//dexpr int k_costs[k_origins][k_destin]= costs[i: x[i]==1][j: y[j]==1]; //ERROR: Syntax error: unexpected ":"

//Then I tried to extract first the indexes and then create the k x k matrix, but there is always an error:
    //dexpr int array_origins[k_origins] =  [i: x[i]==1| i in origins]; //ERROR: Boolean[{int}] type cannot be used for dexpr int[k_origins]
    //dexpr {int} array_origins =  {i| i in origins : x[i]==1}; //ERROR: {int} type cannot be used with decision expressions

dexpr int k_costs[i in k_origins][j in k_destin]= costs[i][j]* (x[i]==1)*(y[j]==1);

dexpr float totalcost = sum(i in k_origins, j in k_destin)
    k_costs[i][j] * z[i][j];

minimize totalcost ;
        
subject to
{
sum(i in origins)x[i]==k;
sum(j in destin)y[j]==k;
 
forall(j in k_destin) sum(i in k_origins)z[i][j] == 1;
forall(i in k_origins) sum(j in k_destin)z[i][j] == 1;
};

欢迎任何想法或提示!谢谢

*分配问题的简要说明可以在这里找到https://en.wikipedia.org/wiki/Assignment_problem

https://www.linkedin.com/pulse/how-opl-alex-fleischer/

How to use a decision variable as an index with CPLEX ?

range r=1..5;

float value[r]=[2,3,4.5,1,0];
dvar int i in 1..5;

maximize sum(k in r) value[k]*(k==i);
subject to
{

}

execute
{
writeln("i=",i);
}

or if with decision expressions

range r=1..5;

float value[r]=[2,3,4.5,1,0];
dvar int i in 1..5;

dexpr float cost=sum(k in r) value[k]*(k==i);

minimize cost;
subject to
{

}

execute
{
writeln("i=",i);
}

对于您的示例,您可以使用 COPtimizer:

using CP;

int n=3;
int k= 2;

range origins = 1..n;
range destin = 1..n;
range k_origins =1..k;
range k_destin = 1..k;

int costs[o in origins][d in destin]=ftoi(abs(o-d)) mod 2;

dvar boolean x[origins];
dvar boolean y[destin];
dvar boolean z[k_origins][k_destin];

///First I tried to create k x k costs matrix directly:
//dexpr int k_costs[k_origins][k_destin]= costs[i: x[i]==1][j: y[j]==1]; //ERROR: Syntax error: unexpected ":"

//Then I tried to extract first the indexes and then create the k x k matrix, but there is always an error:
    //dexpr int array_origins[k_origins] =  [i: x[i]==1| i in origins]; //ERROR: Boolean[{int}] type cannot be used for dexpr int[k_origins]
    //dexpr {int} array_origins =  {i| i in origins : x[i]==1}; //ERROR: {int} type cannot be used with decision expressions

dexpr int k_costs[i in k_origins][j in k_destin]= costs[i][j]* (x[i]==1)*(y[j]==1);

dexpr float totalcost = sum(i in k_origins, j in k_destin:i in origins && j in destin)
    k_costs[i][j] * z[i][j];

minimize totalcost ;
        
subject to
{
sum(i in origins)x[i]==k;
sum(j in destin)y[j]==k;
 
forall(j in k_destin) sum(i in k_origins)z[i][j] == 1;
forall(i in k_origins) sum(j in k_destin)z[i][j] == 1;
};

或者如果您想继续使用 MIP

int n=3;
int k= 2;

range origins = 1..n;
range destin = 1..n;
range k_origins =1..k;
range k_destin = 1..k;

int costs[o in origins][d in destin]=ftoi(abs(o-d)) mod 2;

dvar boolean x[origins];
dvar boolean y[destin];
dvar boolean z[k_origins][k_destin];

///First I tried to create k x k costs matrix directly:
//dexpr int k_costs[k_origins][k_destin]= costs[i: x[i]==1][j: y[j]==1]; //ERROR: Syntax error: unexpected ":"

//Then I tried to extract first the indexes and then create the k x k matrix, but there is always an error:
    //dexpr int array_origins[k_origins] =  [i: x[i]==1| i in origins]; //ERROR: Boolean[{int}] type cannot be used for dexpr int[k_origins]
    //dexpr {int} array_origins =  {i| i in origins : x[i]==1}; //ERROR: {int} type cannot be used with decision expressions

dexpr int k_costs[i in k_origins][j in k_destin]= costs[i][j]* ((x[i]==1) &&(y[j]==1));
dvar int k_coststimesz[i in k_origins][j in k_destin];

dexpr float totalcost = sum(i in k_origins, j in k_destin:i in origins && j in destin)
    k_coststimesz[i][j];

minimize totalcost ;
        
subject to
{
forall(i in k_origins, j in k_destin)  (z[i][j]==0) => (k_coststimesz[i][j]==0);
forall(i in k_origins, j in k_destin)  (z[i][j]==1) => (k_coststimesz[i][j]==k_costs[i][j]);
  
sum(i in origins)x[i]==k;
sum(j in destin)y[j]==k;
 
forall(j in k_destin) sum(i in k_origins)z[i][j] == 1;
forall(i in k_origins) sum(j in k_destin)z[i][j] == 1;
};

工作正常