我想知道模型之后每个索引的约束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)
但显示错误的值。
答案 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;
}
}