【发布时间】:2017-03-13 13:57:32
【问题描述】:
我有一个带有成对索引号及其二进制值的子列表列表。例如:
Variable Value
route.x[0,0] 0
route.x[0,1] 1
route.x[0,2] 0
route.x[0,3] 0
route.x[1,0] 0
route.x[1,1] 0
route.x[1,2] 0
route.x[1,3] 1
route.x[2,0] 0
route.x[2,1] 0
route.x[2,2] 0
route.x[2,3] 0
route.x[3,0] 0
route.x[3,1] 0
route.x[3,2] 1
route.x[3,3] 0
如果route.x[i,j] 的值为1,则创建一个包含该数字的新列表。例如,新列表将是:route = 0 1 3 2
到目前为止,我已经编写了以下代码:
//find optimal route
var route = new List<List<int>>();
for (int j = 0; j < C+1; ++j)
{
if (routeopt.x[0, j] != 1)
continue;
List<int> subroute = new List<int>();
subroute.Add(0);
subroute.Add(j);
route.Add(subroute);
}
此代码的结果是route = 0 1。之后,我使用此代码添加新号码(3 和 2)。
for (int i = 1; i < C+1; ++i)
{
for (int j = 1; j < C+1; j++)
{
if (routeopt.x[i, j] == 1)
{
List<int> targetlist = route.Single(r => r.Contains(i));
targetlist.Add(j);
}
}
}
如果我有一个 route.x[i,j] 在有序数中值为 1,则此代码有效。但是如果它没有排序,例如(我只显示值为 1 的变量):
Variable Value
route.x[0,4] 1
route.x[0,3] 1
route.x[4,1] 1
route.x[1,2] 1
应该是route = 0 3 和route = 0 4 1 2。但它显示了Sequence contains no matching element,因为索引1 不包含在route = 0 3 或route = 0 4 中。如何处理这个问题?谢谢
【问题讨论】: