如何在使用Gurobi优化模型后获得约束值

时间:2017-04-28 08:50:03

标签: c# linear-programming gurobi

我想知道模型之后每个索引的约束sumPj的值是否已经优化。这是约束:

for (int m = 0; m < M; m++)                
{
    for (int i = 0; i < C; i++)
    {
       GRBLinExpr sumPj = 0;
        foreach (int p in VehicleList[m])
        {
            sumPj.AddTerm(a[i].Column[p], z[p]);
        }
        master.AddConstr(sumPj <= y[m], "tj" + m + "," + i);
    }
}

我使用master.GetConstrByName("tj" + m + "," + i).Get(GRB.DoubleAttr.Slack)但显示错误的值。

1 个答案:

答案 0 :(得分:0)

我自己找到了。首先,我应该将sumPj作为一个多维数组。

GRBLinExpr[,] sumPj = new GRBLinExpr [M,C];
for (int m = 0; m < M; m++)                       
{

for (int i = 0; i < C; i++)
    {
        sumPj[m,i] = 0;
        foreach (int p in VehicleList[m])
        {
            sumPj[m,i].AddTerm(a[i].Column[p], z[p]);
        }
        master.AddConstr(sumPj[m,i] <= y[m], "tj" + m + "," + i);
    }
}

然后我通过

添加优化后的值
for (int m = 0; m < M; ++m)
    {
      for (int i = 0; i < C; i++)
        {
          Console.WriteLine("C " + m + "," + (i + 1) + " " + sumPj[m,i].Value);
          Constraint[m, i] = sumPj[m,i].Value;
        }
    }