Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to use .txt file to input data into a 3D array?

    Posted 10/26/15 08:08 AM

    Originally posted by: YewenGu


    I am new with c++ and trying to solve some lp in c++ with cplex. One of my data is 3D. I know how to input 1D or 2D data with .txt or .dat file. But not sure how to do it with 3D.
     
    For example, if I have 3D (2*2*3) data like below in .txt file.
     
    1 0
    0 1
     
    1 1
    0 0
     
    0 1
    1 0
     
    I guess after I created a 3D array by using IloNumArray3, I need to use ifstream and for loop to input the three 2D matrix individually and loop on the third dimension?
     
    Can any give some hint or may be a small example based on my case? Just want to see how to do the for loop with ifstream. Or maybe you have a better way to handle 3D data. Thanks.

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How to use .txt file to input data into a 3D array?

    Posted 10/26/15 10:35 AM

    If you format your data file in a certain way then you can directly use operator>> which is overloaded for std::istream and IloNumArray3:

    #include <ilcplex/ilocplex.h>
    #include <iostream>
    
    int
    main(void)
    {
       IloEnv env;
       IloNumArray3 data(env);
       std::ifstream in("data.txt");
       in >> data;
       for (IloInt i = 0; i < data.getSize(); ++i)
          for (IloInt j = 0; j < data[i].getSize(); ++j)
             for (IloInt k = 0; k < data[i][j].getSize(); ++k)
                std::cout << i << ", " << ", " << j << ", " << k << ": "
                          << data[i][j][k] << std::endl;
       in.close();
       env.end();
       return 0;
    }
    

    For this to work your data file should look something like this:

    [[[1, 0],
      [0, 1]],
     [[1, 1],
      [0, 0]],
     [[0, 1],
      [1, 0]]]

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How to use .txt file to input data into a 3D array?

    Posted 10/27/15 05:56 AM

    Originally posted by: YewenGu


    Again you helped me Daniel. Really appreciate it. I tried, it works. Super!!


    #CPLEXOptimizers
    #DecisionOptimization