Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Matrices in CPLEX

    Posted Sun October 03, 2010 11:49 PM

    Originally posted by: BerkUstun


    I'm currently coding on C++ along with the CPLEX API and have recently discovered that there are matrix arrays in CPLEX (i.e. IloNumArray2) but there is no documentation on them.

    I am wondering how I can use matrices within CPLEX.

    1. Can I get away with using matrices to set up constraints without a for loop (i.e. .add(Ax = 0) where c is an A is a IloNumArray2 and x is a IloNumVarArray?

    2. Can I add two matrices together (i.e. C = A + B) or multiply them by scalars (i.e. C = 2*A). Here, A, B and C are IloNumArrays2 (actually, it would also help to know whether I can do this for vectors)

    3. Can I append a matrix? If so, how?

    Anything helps!
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Matrices in CPLEX

    Posted Mon October 04, 2010 03:14 AM

    Originally posted by: SystemAdmin


    Answer to question 1: I don't think so. However, if you have to do that frequently it should be easy to write a function that does that.

    Answer to question 2: I don't think so either. But it is again easy to define you own operator+ for that purpose if you want to that a lot (untested):
    struct SizeMismatchException {
    };
     
    IloNumArray2 operator+(IloNumArray2 const& a, IloNumArray2 const& b)
    {
       if (a.getSize() != b.getSize())
          throw SizeMismatchException();
     
       IloNumArray2 c(a.getEnv());
     
       for (IloInt i = 0; i < a.getSize(); ++i) {
          IloNumArray const& arow = a[i];
          IloNumArray const& brow = b[i];
          if (arow.getSize() != brow.getSize()) {
             for (IloInt j = 0; j < c.getSize(); ++j)
                c[i].end();
             c.end();
             throw SizeMismatchException();
          }
          IloNumArray row(c.getEnv());
          for (IloInt j = 0; j < arow.getSize(); ++j) {
             row.add(arow[j] + brow[j]);
          }
          c.add(row);
       }
     
       return c;
    }
    

    A similar operator can be defined for two-dimensional arrays of variables or for vectors.

    Answer to question 3: IloNumArray2 is just an IloArray of IloNumArray instances. You can easily append new rows or append elements to rows:
    IloNumArray2 A(env);
     
    // Append a row:
    // 1. create the new row.
    IloNumArray row(env);
    ...
    // 2. append the new row.
    A.add(row);
     
    // Append an element to row i:
    A[i].add(3.58);
    


    Be careful, IloNumArray2 implements a dense matrix. If you get large dimensions you may run out of memory. You may want to consider using sparse matrix implementations.
    #CPLEXOptimizers
    #DecisionOptimization