Topic
  • 4 replies
  • Latest Post - ‏2013-02-15T11:46:07Z by SystemAdmin
SystemAdmin
SystemAdmin
7929 Posts

Pinned topic Quadratic expression in objective function, java cplex

‏2013-02-15T09:02:38Z |
Hello!

I'm solving a minimization problem using cplex java. I declare the objective function as linearNumExpression, but one of the terms requires the multiplication of two decision variables, so it becomes quadratic. My problem is that if I decplare the objective function as quadratic the rest of the terms that are linear are not working. Have you got any idea how I can overcome this problem? In other words how to use linear and quadratic terms together in the objective function?

Thanks
Updated on 2013-02-15T11:46:07Z at 2013-02-15T11:46:07Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Quadratic expression in objective function, java cplex

    ‏2013-02-15T09:09:31Z  
    You can use IloNumExpr instead of IloLinearNumExpr. The former allows any kind of terms in it. Or collect the quadratic terms in an instance of IloQuadNumExpr and in the end use cplex.sum() to add the linear expression and the quadratic expression.
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Quadratic expression in objective function, java cplex

    ‏2013-02-15T09:26:59Z  
    You can use IloNumExpr instead of IloLinearNumExpr. The former allows any kind of terms in it. Or collect the quadratic terms in an instance of IloQuadNumExpr and in the end use cplex.sum() to add the linear expression and the quadratic expression.
    Thanks for the quick answer!
    If I use IloNumExpr I cannot use the addTerm. When you say to use cplex.sum() how exactly do you mean it? I provide you with the relevant part of my code

    IloLinearNumExpr obj_func = cplex.linearNumExpr();

    for(i = 0; i < numVs; i++)
    {
    for(j=0; j<numPs; j++)
    {
    obj_func.addTerm((0.2*tDriving[i][j]), y[i][j]); // y is a decision variable, tDriving is a double java variable
    }
    }

    for(j=0; j<numPs; j++)
    {

    for(i=0;i<numVs; i++)
    {
    IloQuadNumExpr p = cplex.quadNumExpr();
    for(int ii=0; ii<numEVs; ii++)
    {
    for (k=0; k<100; k++)
    {
    p.addTerm(1, n[j][k]ii,y[i][j]); // n is the second decision variable
    }
    }
    obj_func.add(p);
    }
    }
    cplex.addMinimize(obj_func);
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Quadratic expression in objective function, java cplex

    ‏2013-02-15T10:39:37Z  
    Thanks for the quick answer!
    If I use IloNumExpr I cannot use the addTerm. When you say to use cplex.sum() how exactly do you mean it? I provide you with the relevant part of my code

    IloLinearNumExpr obj_func = cplex.linearNumExpr();

    for(i = 0; i < numVs; i++)
    {
    for(j=0; j<numPs; j++)
    {
    obj_func.addTerm((0.2*tDriving[i][j]), y[i][j]); // y is a decision variable, tDriving is a double java variable
    }
    }

    for(j=0; j<numPs; j++)
    {

    for(i=0;i<numVs; i++)
    {
    IloQuadNumExpr p = cplex.quadNumExpr();
    for(int ii=0; ii<numEVs; ii++)
    {
    for (k=0; k<100; k++)
    {
    p.addTerm(1, n[j][k]ii,y[i][j]); // n is the second decision variable
    }
    }
    obj_func.add(p);
    }
    }
    cplex.addMinimize(obj_func);
    I think this should work:
    IloLinearNumExpr lin = cplex.linearNumExpr();
     
    for(i = 0; i < numVs; i++) {
      for(j=0; j<numPs; j++) {
        lin.addTerm((0.2*tDriving[i][j]), y[i][j]);
      }
    }
     
    IloQuadNumExpr quad = cplex.quadNumExpr();
    for(j=0; j<numPs; j++) {
      for(i=0;i<numVs; i++) {
        for(int ii=0; ii<numEVs; ii++) {
          for (k=0; k<100; k++) {
            quad.addTerm(1, n[j][k]ii,y[i][j]);
          }
        }
      }
    }
    cplex.addMinimize(cplex.sum(lin, quad));
    

    Please also refer to the QPex1.java, QPex2.java, and QPex3.java examples in the CPLEX distribution. They illustrate how to compose a quadratic objective function.
    Updated on 2014-03-24T22:39:53Z at 2014-03-24T22:39:53Z by iron-man
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: Quadratic expression in objective function, java cplex

    ‏2013-02-15T11:46:07Z  
    I think this should work:
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloLinearNumExpr lin = cplex.linearNumExpr(); for(i = 0; i < numVs; i++) { for(j=0; j<numPs; j++) { lin.addTerm((0.2*tDriving[i][j]), y[i][j]); } } IloQuadNumExpr quad = cplex.quadNumExpr(); for(j=0; j<numPs; j++) { for(i=0;i<numVs; i++) { for(int ii=0; ii<numEVs; ii++) { for (k=0; k<100; k++) { quad.addTerm(1, n[j][k]ii,y[i][j]); } } } } cplex.addMinimize(cplex.sum(lin, quad)); </pre>
    Please also refer to the QPex1.java, QPex2.java, and QPex3.java examples in the CPLEX distribution. They illustrate how to compose a quadratic objective function.
    Thanks a lot Daniel! This works!