[英]Gurobi & C#: Index out of range
I am trying to write a decision variable from my MIP model into the console. 我正在尝试将MIP模型中的决策变量写入控制台。 I am getting the error
我收到错误
System.ArgumentOutOfRangeException: Index was out of range.
System.ArgumentOutOfRangeException:索引超出范围。 Must be non-negative and less than the size of the collection.
必须为非负数并且小于集合的大小。
How can I fix this? 我怎样才能解决这个问题?
using System;
using System.Collections.Generic;
using System.Linq;
using Gurobi;
if (status == GRB.Status.OPTIMAL)
{
List<List<List<int>>> X_ijk_list = new List<List<List<int>>>();
Console.WriteLine("X_ijk:");
for (int k = 0; k < n_machines; ++k)
{
Console.WriteLine("Maschine" + k);
X_ijk_list.Add(new List<List<int>>());
for (int i = 0; i < n_jobs; ++i)
{
X_ijk_list[i].Add(new List<int>());
for (int j = 0; j < n_tasks_job[i]; ++j)
{
X_ijk_list[i][j].Add(Convert.ToInt32(X_ijk[i, j, k].Get(GRB.DoubleAttr.X))); // error here
Console.Write(X_ijk_list[i][j][k]);
Console.Write(";");
}
Console.WriteLine();
}
}
You are mixing your loop variables. 您正在混合循环变量。 Outer loop is
k
, then i
then j
, so 外环是
k
,那么i
就是j
,所以
X_ijk_list[i][j].Add(Convert.ToInt32(X_ijk[i, j, k].Get(GRB.DoubleAttr.X))); // error here
should be 应该
X_ijk_list[k][i].Add(Convert.ToInt32(X_ijk[i, j, k].Get(GRB.DoubleAttr.X))); // error here
and same for the 和相同的
X_ijk_list[k].Add(new List<int>());
A reproducible code with hardcoded n_*
variables and dummy 42
instead of X_ijk[i, j, k].Get(GRB.DoubleAttr.X)
: 具有硬编码的
n_*
变量和伪42
而不是X_ijk[i, j, k].Get(GRB.DoubleAttr.X)
复制代码:
void Main()
{
var n_machines = 5;
var n_jobs = 5;
var n_tasks_job = new int[] { 5, 5, 5, 5, 5 };
List<List<List<int>>> X_ijk_list = new List<List<List<int>>>();
Console.WriteLine("X_ijk:");
for (int k = 0; k < n_machines; ++k)
{
Console.WriteLine("Maschine" + k);
X_ijk_list.Add(new List<List<int>>());
for (int i = 0; i < n_jobs; ++i)
{
X_ijk_list[k].Add(new List<int>());
for (int j = 0; j < n_tasks_job[i]; ++j)
{
//X_ijk_list[i][j].Add(Convert.ToInt32(X_ijk[i, j, k].Get(GRB.DoubleAttr.X))); // error here
X_ijk_list[k][i].Add(42); // dummy data
Console.Write(X_ijk_list[k][i][j]);
Console.Write(";");
}
Console.WriteLine();
}
}
}
produces 产生
X_ijk:
Maschine0
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
Maschine1
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
Maschine2
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
Maschine3
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
Maschine4
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
42;42;42;42;42;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.