Decision Optimization

 View Only
  • 1.  How to get access to the coefficient matrix in a QCQP model that already been built in the C++ API?

    Posted Wed January 05, 2022 01:56 PM
    Hi Everyone,

    I am solving a Quadratic Programming Problem with Quadratic Constraints (QCQP) with CPLEX using C++ API. Currently, I am struggling with getting access to the coefficient matrix in a model that has already been built. The objective function of the model is quadratic, and the constraints include Second-order cone programming (SOCP) constraints, Quadratic constraints, and linear constraints.
    And I have the following ideas:
    1) Use IloModel::Iterator to traverse the variables, constraints, and objectives in the model.
    2) Use IloExtractable to judge their types.
    I wonder if this idea is correct, and Can anyone provide a piece of C + + code for reference?

    Thanks in advance!

    ------------------------------
    Musheng Li
    ------------------------------

    #DecisionOptimization


  • 2.  RE: How to get access to the coefficient matrix in a QCQP model that already been built in the C++ API?

    Posted Mon January 10, 2022 05:21 AM
    Hello, I think it would be something like
    void iterate(IloExpr::QuadIterator it){
            while (it.ok()) {
                    IloNum c = it.getCoef();
                    IloNumVar v1 = it.getVar1();
                    IloNumVar v2 = it.getVar2();
                    std::cout << c <<  "*" << v1 << "*" << v2 << "\n";
                    ++it;
            }
    }
    void iterate(IloModel model){
            for (IloModel::Iterator it(model); it.ok(); ++it) {
                    IloExtractable ext = *it;
                    if (ext.isObjective()) {
                            IloObjective o = ext.asObjective();
                            IloExpr::QuadIterator it = o.getQuadIterator();
                            std::cout << "  ===== start obj ====\n";
                            iterate(it);
                            std::cout << "  ===== end obj ====\n";
                    }
                    else if (ext.isConstraint()) {
                            IloConstraint c = ext.asConstraint();
                            if (c.getImpl()->isType(IloRangeI::GetTypeInfo())){
                                    IloRange r = (IloRangeI*)c.getImpl();
                                    IloExpr::QuadIterator it = r.getQuadIterator();
                                    std::cout << "  ===== start range ====\n";
                                    iterate(it);
                                    std::cout << "  ===== end range ====\n";
                            }
                    }
            }
    }
    ​


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



  • 3.  RE: How to get access to the coefficient matrix in a QCQP model that already been built in the C++ API?

    Posted Tue January 11, 2022 10:24 PM
    Thanks a lot for your reply, your code is inspiring to me, and I've figured it out.
    But now I have another problem. When I try to read the constant term in the objective function of a MIQP problem, I.e. miqpex1.lp, the getConstant() function fails to read the value. In order to prove the correctness of the code, I use it to read the constant term of a SOCP model, that is ilosocpex1.lp, the program outputs the correct value 0. I checked a lot of official documents, but still can't figure out why.

    See the program in obj_test.cpp
    Please refer to miqpex1.lp for the MIQP model
    See ilosocpex1.lp for the SOCP model

    A Example of the function call:
    obj_test miqpex1.lp

    Looking forward to your reply ​

    ------------------------------
    Musheng Li
    ------------------------------



  • 4.  RE: How to get access to the coefficient matrix in a QCQP model that already been built in the C++ API?

    Posted Wed January 12, 2022 07:29 AM
    It's a limitation (or a bug) in concert, which checks if the objective is complex in term of expressions or not (imbricated expressions can be hard to normalize), so an exception is raised.
    We will look to improve this check in a next version.
    Meanwhile, a code which should be safe is as follows:
    if (e.isObjective()) {
       IloObjective obj = e.asObjective();
       IloObjectiveI* o = obj.getImpl();
       IloNumLinTermI* e = o->getExpr();
       if (e->getQuad() && e->getFirstGeneral() == e->getLastGeneral()) {
    	std::cout << "quad objective\n";
    	std::cout << "Obj " << e->getNumConstant() << "\n";
       }
       else {
    	std::cout << "standard obj\n";
    	std::cout << "Obj " << obj.getConstant() << "\n";
       }
    }​


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



  • 5.  RE: How to get access to the coefficient matrix in a QCQP model that already been built in the C++ API?

    Posted Thu January 13, 2022 05:44 AM
    Thanks for your prompt reply, the code that you provide works well for me, thanks again!

    ------------------------------
    Musheng Li
    ------------------------------