Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Conversion from IloNumArray to IloNum

    Posted 10/25/13 09:12 AM

    Originally posted by: MariusGi


    Hello, I have a problem. I want to fill the IloNumArray with data from file. Would appreciate your help.
    ----------
    ifstream is;
    .......
    ---------- 
    double qq,t;
    int m=30;

    for(int i=0;i<m;i++){
         is>>qq;   
         IloNumArray q(env,1);
         q[i]=IloNumArray(env,1,qq); //error no suitable conversion function from "IloNumArray" to "IloNum" exists
      }

    Although if I use a matrix it works.

    ---------

    typedef IloArray<IloNumArray> NumMatrix;
    typedef IloArray<NumMatrix> Matrix;
    ---------

    int l=20,n=20;

    Matrix T(env,l);
    for(int i=0;i<l;i++){
         T[i]=NumMatrix(env,n);
          for(int j=0;j<n;j++){
                T[i][j]=IloNumArray(env,1);
                is>>t;
                T[i][j]=IloNumArray(env,1,t);
          }

     }  


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Conversion from IloNumArray to IloNum

    Posted 10/26/13 04:36 PM

    Are you trying to read all the date into a single numeric vector (one dimension), or into a two- or three-dimensional array?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Conversion from IloNumArray to IloNum

    Posted 10/27/13 05:34 AM

    Originally posted by: MariusGi


    Yes I want to read it to a single numeric vector (one dimension).


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Conversion from IloNumArray to IloNum

    Posted 10/27/13 04:29 PM

    It is still unclear to me what exactly you want to achieve. Read everything into q?

    In any case, there is an overload for operator>> that reads from std::istream into IloNumArray. Assume that file arrayinput.txt contains this

    [  1.0, 2.0, 3.0 ]

    Then this code

    #include <fstream>
    #include <iostream>
    #include <ilcplex/ilocplex.h>

    int
    main(void)
    {
       IloEnv env;
       IloNumArray a(env);
       std::ifstream is("arrayinput.txt");

       is >> a;
       for (IloInt i = 0; i < a.getSize(); ++i)
          std::cout << a[i] << std::endl;

       env.end();
       return 0;
    }

    prints

    1
    2
    3

    So if you can write your data file like the one above, then input of an array is very simple. Otherwise something like this can do the trick:

    IloNumArray q(env);
    for (int i = 0; i < m; ++i) {
       double qq;
       is >> qq;
       q.add(qq);
    }


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Conversion from IloNumArray to IloNum

    Posted 10/28/13 04:55 AM

    Originally posted by: MariusGi


    Thank you !
    That's what I wanted.
    How about for two dimension vector(matrix) ? This is my code for it but its incorrect since every element is a vector here and I cannot do arithmetic operations with it.

     

    typedef IloArray<IloNumArray> NumMatrix;
    typedef IloArray<NumMatrix> Matrix;

    int l=20,n=20;

    Matrix T(env,l);
    for(int i=0;i<l;i++){
         T[i]=NumMatrix(env,n);
          for(int j=0;j<n;j++){
                T[i][j]=IloNumArray(env,1);
                is>>t;
                T[i][j]=IloNumArray(env,1,t);
          }

     }  

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Conversion from IloNumArray to IloNum

    Posted 10/28/13 05:02 AM

    Please take a look at the examples/src/cpp/facility.cpp example and its corresponding examples/data/facility.dat data file. The example reads two vectors and a matrix from that file. From the example it should be pretty clear how to handle input into vectors and matrices.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Conversion from IloNumArray to IloNum

    Posted 10/28/13 08:06 AM

    Originally posted by: MariusGi


    Seems I created a matrix with one row. Still don't know how to create with n rows

    typedef IloArray<IloNumArray> NumMatrix;

    NumMatrix W(env,l*m);
     for(int j=0;j<l*m;j++){
     is>>w;
     W[j]=IloNumArray(env,1,w);
     }

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Conversion from IloNumArray to IloNum

    Posted 10/29/13 08:01 AM

    What is 'w' and how does your input file look like? In general, if you want to create a matrix row-by-row then you do it like this:

    NumMatrix W(env);
    for (int i = 0; i < rows; ++i) {
       IloNumArray row(env);
       // Set/read values for for row
       ...
       W.add(row);
    }


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Conversion from IloNumArray to IloNum

    Posted 10/30/13 05:24 AM

    Originally posted by: MariusGi


    My w is 
    double w; 
    my input file is .txt with data for vectors and matrixes. I will attach that file.
    I want this to be a 20x30 matrix (first matrix in file). Seems I did it right. Thanx again for the help !

     

     NumMatrix W(env);
     for(int i=0;i<20;++i){
     IloNumArray row(env);
     for(int j=0;j<30;j++){
     is>>w;
     row.add(w);
     }
     W.add(row);    
     }  
     cout<<W<<endl;


     


    #CPLEXOptimizers
    #DecisionOptimization