Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Question about IloCsvReader

    Posted Mon December 14, 2015 05:00 PM

    Originally posted by: Olivia.w


    I am very, very new of using cplex with c++. I am trying to solve a problem using the column generation algorithm. But firstly I need to input data from a .csv file like this:

    I have write something using IloCsvReader:

    #include <ilcplex/ilocplex.h>

    #include <ilconcert/ilocsvreader.h>

     

    ILOSTLBEGIN

     

    #define RC_EPS 1.0e-6 // optimality tolerance

     

    //function of data reading

    void readArray (IloIntArray ordername, char const *file)

    {

        ordername.clear();

        IloCsvReader reader;

        try {

            reader = IloCsvReader(ordername.getEnv(),file);

            IloInt const lines = reader.getNumberOfItems();

            

            for (IloInt i=0; i < lines; ++i)

            {

                IloCsvLine const line = reader.getLineByNumber(i);

                

                ordername.add(IloIntArray(ordername.getEnv()));

                

                ordername[i]=line.getIntByPosition(4);

            }

            reader.end();

            

        } catch (...){

            reader.end();

            throw;

        }

    }

     

    int main(int argc, const char **argv) {

        

        char const *filename = "/Users/wy/Documents/Programs/PD151201/RSS3_2014-10-24.csv";

            try {

                IloEnv env;

            //************* Read Parameters *************//

            //orders

                IloIntArray ordername(env);

            readArray(ordername, filename);         

            for (IloInt i=0; i< ordername.getSize(); ++i)

            {

                cout<< ordername[i]<<endl;

            }

     

    env.end();

            }

            catch (IloException& ex) {

                cerr << "Error: " << ex << endl;

            }

            catch (...) {

                cerr << "Error" << endl;

            }

        return 0;

    }

     

    While debug it says 

     

    X& IloArray::operator[] (IloInt i) : Out of bounds operation: index superior to size of array

    Assertion failed: ((i < _impl->getSize()) || (ILOSTD(cerr) << "X& IloArray::operator[] (IloInt i) : Out of bounds operation: index superior to size of array" << ILOSTD(endl), ilo_stop_assert())), function operator[], file /Applications/IBM_CPLEX/ILOG/CPLEX_Studio125/concert/include/ilconcert/iloenv.h, line 2204.

    Program ended with exit code: 9

     

    I have no idea what is wrong... Maybe I just did not understand the class at all...Could somebody please tell me what should I do...Thank you!


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Question about IloCsvReader

    Posted Tue December 15, 2015 03:31 AM

    The problem is this line of code which most likely does not do what you want:

    ordername.add(IloIntArray(ordername.getEnv()));
    

    This line results in the following actions:

    1. IloIntArray(ordername.getEnv()) creates a new empty instance of IloIntArray.
    2. ordername.add(...) adds all elements from the newly created (and empty) array to ordername. See also the reference documentation.

    Since adding an empty array to an empty array is a nop, ordername is still an empty array after that. Thus trying to set element 0 in that array will fail in the very first iteration of the loop since that element does not exist.

    I think what you meant to write was

    ordername.add(0);
    ordername[i] = line.getIntByPosition(4);
    

    or even shorter

    ordername.add(line.getIntByPosition(4));
    

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Question about IloCsvReader

    Posted Tue December 15, 2015 10:29 AM

    Originally posted by: Olivia.w


    Hi, Daniel, 

     

     

    Thank you very much for your detailed reply.  I think it will solve my problem.

    I didn't notice that  IloIntArray(ordername.getEnv()) creates empty array. 

      ordername.add(line.getIntByPosition(4)) seems like what I want.

    Thank you so much!!! 

     


    #CPLEXOptimizers
    #DecisionOptimization