Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Solve a linear programming in canonical form

    Posted Tue August 16, 2022 02:07 PM
    I have a canonical problem:

    min c'x
    s.t. Ax <= b

    It's a large problem using a sparse matrix. 
    What's the easiest way to formulate it in cplex? Since it's already in canonical form, I was hoping for a low level function that can accept these vars instead of using the modeling interface.

    ------------------------------
    Zohar Levi
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Solve a linear programming in canonical form

    Posted Wed August 17, 2022 04:20 AM
    You may have a look at the docplex Python package. 

    It provides some ways to create a model from a sparse matrix (numpy)
    See
     http://docs.cplex.org.cn/mp/docplex.mp.advmodel.html?highlight=advmodel and the methods matrix_constraints.

    You can also use a transformers from docplex: https://ibmdecisionoptimization.github.io/docplex-doc/2.21.207/mp/docplex.mp.sktrans.transformers.html?highlight=cplextransformer#docplex.mp.sktrans.transformers.CplexTransformerBase
    and an example here: https://stackoverflow.com/questions/50714397/cplex-with-python-api-how-to-make-model-formulating-faster/50716526#50716526

    ------------------------------
    Vincent Beraudier
    ------------------------------



  • 3.  RE: Solve a linear programming in canonical form

    Posted Wed August 17, 2022 06:44 AM
    I'm sorry, somehow I forgot to mention that I'm using c++, and if it helps, I'm using Eigen vars.

    I think I unconsciously perceived cplex as having a built-in cpp in it :)

    ------------------------------
    Zohar Levi
    ------------------------------



  • 4.  RE: Solve a linear programming in canonical form

    Posted Wed August 17, 2022 10:56 AM
    We don't provide such construction in C++.
    you will have to build the CPLEX model yourself. The code should look like this, I think:
    SparseMatrix<double> mat = ...;
    double[] d = ...;
    
    IloModel model(env);
    IloNumVarArray vars(env, mat.cols());
    
    // to help build the constraint expression for each row.
    IloNumVarArray selected_vars(env);
    IloNumArray selected_coefs(env);
    
    for (int k=0; k<mat.outerSize(); ++k){
      selected_vars.clear();
      selected_coefs.clear();
      for (SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
      {
        selected_coefs.add( it.value() );
        selected_vars.add( it.col() );
      }
      model.add( IloScalProd(selected_vars, selected_coefs) <= d[k] );
    }
    selected_vars.end();
    selected_coefs.end();​


    ------------------------------
    Vincent Beraudier
    ------------------------------



  • 5.  RE: Solve a linear programming in canonical form

    Posted Wed August 17, 2022 06:17 PM
    That should do, thanks.

    ------------------------------
    Zohar Levi
    ------------------------------