简体   繁体   中英

Gurobi & C#: Index out of range

I am trying to write a decision variable from my MIP model into the console. I am getting the error

System.ArgumentOutOfRangeException: Index was out of range. 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

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) :

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM