Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Problem with std::vector and IloNumArray

  • 1.  Problem with std::vector and IloNumArray

    Posted Wed June 13, 2012 03:41 PM

    Originally posted by: SystemAdmin


    As far as I know there is no matrix support for cplex in c++. So I was trying to implement it myself and I can't figure out what is going on or if it is a bug.

    I have the following code

    IloEnv env;

    std::vector< IloNumArray > T(2, IloNumArray(env) );
    T[0].add(7.1); T[0].add(1.1);
    T[1].add(8.1); T[1].add(1.2);

    for( unsigned int i=0;i<T.size();++i)
    {
    env.out() << T[i] << endl;
    }

    As for as I can tell this should print
    7.1 1.1
    8.1 1.2
    But it doesn't it prints
    7.1 1.1 8.1 1.2
    7.1 1.1 8.1 1.2

    What is going on here? Why doesn't using the overloaded operator from std::vector move to the next element of T and then add elements there? Is IloNumArray not a class? Why does it concat on the previous vector element?
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Problem with std::vector and IloNumArray

    Posted Thu June 14, 2012 08:44 AM

    Originally posted by: SystemAdmin


    Instead of
    std::vector< IloNumArray > T(2, IloNumArray(env) );
    

    try
    std::vector< IloNumArray > T;
    T.push_back(IloNumArray(env));
    T.push_back(IloNumArray(env));
    

    The vector constructor you used invokes the copy constructor of IloNumArray. However, IloNumArray is just a handle (you can think of it as a pointer to the real array). So you are creating new handles that still point to the same array, not new arrays.

    In concert there are classes IloNumArray2, IloNumArray3 and IloNumArray4 to support higher-dimensional arrays.

    I have also posted several times in this Forum that one could use recursive templates to define higher dimensional arrays in a nice and easy way. If I remember correctly I have also posted example code at least once. You may find that by searching this Forum.
    #CPLEXOptimizers
    #DecisionOptimization