Topic
  • 4 replies
  • Latest Post - ‏2013-04-23T18:04:53Z by PeteCacioppi
PeteCacioppi
PeteCacioppi
8 Posts

Pinned topic CPXgetobj analog for Java API

‏2013-04-21T02:44:12Z |

This page is in general helpful

http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r2/index.jsp

 

But the "Accesses objective coefficient values" block for the Java API is a little unclear. If I call IloObjective.getExpr then I get an interface of type IloNumExpr. Where do I go from there to query the actual coefficient value? 

I want to iterate over all the variables and find all their objective coefficients. This is dead easy with the C API, it's confusing me a bit with the Java.

 

Thanks

Pete

 

  • DanielJunglas
    DanielJunglas
    1555 Posts

    Re: CPXgetobj analog for Java API

    ‏2013-04-21T11:09:23Z  

    I agree, this is non-obvious in Concert technology :-(

    You have to cast the IloNumExpr to one of IloLinearNumExpr, IloQuadNumExpr or IloLQNumExpr. These interfaces provide functions linearIterator() and/or quadIterator() that return an iterator suitable for iterating over the objective function.

  • PeteCacioppi
    PeteCacioppi
    8 Posts

    Re: CPXgetobj analog for Java API

    ‏2013-04-22T20:05:18Z  

    I agree, this is non-obvious in Concert technology :-(

    You have to cast the IloNumExpr to one of IloLinearNumExpr, IloQuadNumExpr or IloLQNumExpr. These interfaces provide functions linearIterator() and/or quadIterator() that return an iterator suitable for iterating over the objective function.

    Thanks! That should be close enough for me to take it from there.

     

    Since i have your attention, could you please review the following.

     

    My task is

    • Read an .lp (and possibly .prm) from the filesystem
    • Validate that it is a min linear objective function where exactly two variables have non zero coeffiicents
      • Call these pVar and sVar
    • Add a new variable (call it z)
    • Add two new constraints that say scaledPVar * pVar <= z; scaledSVar * sVar <= z;
    • Set the obj coefficients for all variables to zero, except for new variable z, which is set to 1.

    I am familiar with the C API and know that I can do all these steps with the C API. Thanks to your help I think I now have all the tools to do this with Java.   That said, I you think this task can't be done with the Java API it would save me some time to be forewarned. 

     

    Thanks for any help

    Pete

  • DanielJunglas
    DanielJunglas
    1555 Posts

    Re: CPXgetobj analog for Java API

    ‏2013-04-23T09:50:21Z  

    Thanks! That should be close enough for me to take it from there.

     

    Since i have your attention, could you please review the following.

     

    My task is

    • Read an .lp (and possibly .prm) from the filesystem
    • Validate that it is a min linear objective function where exactly two variables have non zero coeffiicents
      • Call these pVar and sVar
    • Add a new variable (call it z)
    • Add two new constraints that say scaledPVar * pVar <= z; scaledSVar * sVar <= z;
    • Set the obj coefficients for all variables to zero, except for new variable z, which is set to 1.

    I am familiar with the C API and know that I can do all these steps with the C API. Thanks to your help I think I now have all the tools to do this with Java.   That said, I you think this task can't be done with the Java API it would save me some time to be forewarned. 

     

    Thanks for any help

    Pete

    What you are trying to do can definitely be done in Java and should actually be pretty simple. In the last step, when you want to clear the objective for all but z the easiest thing is probably to remove the old objective and add a new one. Some thing like this (untested)

    cplex.remove(cplex.getObjective());
    cplex.addMinimize(z);

  • PeteCacioppi
    PeteCacioppi
    8 Posts

    Re: CPXgetobj analog for Java API

    ‏2013-04-23T18:04:53Z  

    What you are trying to do can definitely be done in Java and should actually be pretty simple. In the last step, when you want to clear the objective for all but z the easiest thing is probably to remove the old objective and add a new one. Some thing like this (untested)

    cplex.remove(cplex.getObjective());
    cplex.addMinimize(z);

    Thanks a bunch! I was actually told this was something that the C API covered well but the Java API didn't. It appears this was a miscommunication (probably my fault, my initial requirement spec too vague).  

    I hope you don't mind if I come back and post a Java snippet when I have it running. (Probably 4 weeks). I think this is a nice demo project to illustrate the themes of this page. http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r2/index.jsp and it fits in with a public domain algorithm I am going to describe in a developerWorks article.