Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Get and set functions

    Posted 11/25/08 08:30 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    Hi

    I've used the callback library for many years and am now switching to using the ILOG CPLEX C++ API.  So far I've defined my LP using IloCplex, added variables and constraints etc.  I'd like some help with modifying the LP.  Could someone please tell me how to change a coefficient in the objective function as well as how to get and set upper and lower bounds on variables.  I'd appreciate knowing where this is documented as well.

    thanks

    Stephen
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Get and set functions

    Posted 11/25/08 07:40 PM

    Originally posted by: SystemAdmin


    [prubin said:]

    [quote author=Stephen.Hill link=topic=712.msg2162#msg2162 date=1227591000]
    Could someone please tell me how to change a coefficient in the objective function


    IloObjective::setLinearCoef

    [quote author=Stephen.Hill link=topic=712.msg2162#msg2162 date=1227591000]as well as how to get

    IloRange::getLB, IloRange::getUB

    [quote author=Stephen.Hill link=topic=712.msg2162#msg2162 date=1227591000]and set

    IloRange::setLB, IloRange::setUB

    [quote author=Stephen.Hill link=topic=712.msg2162#msg2162 date=1227591000]upper and lower bounds on variables. I'd appreciate knowing where this is documented as well.

    C++ API Reference Manual, under the respective classes (IloRange, IloObjective).
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Get and set functions

    Posted 11/26/08 05:22 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    Hi Paul

    Thanks for your help.  I'm still a bit lost and would like to get code fragments from you if you have them.  So far I have the following:

    IloEnv env;

    try{

      IloModel model(env);
      IloNumVarArray variables(env);

    // build network with first case of problem.
      buildModel(model, variables);
     
    // solve the problem.
      IloCplex cplex(model);
      cplex.solve();

    It seems with the callable library one can access variables using variable indexes but with the Concert C++ API I need store the variables in a IloNumVarArray in order to access them.  Does this mean the variables are being stored twice, once in the model and once in the IloNumVarArray?  This seems less efficient that using the index method of the callable lib.  Also if I change bounds in the model then they will be out of synch with the stored IloNumVarArray variables which doesn't seem right.  Or don't I need to store the IloNumVarArray variables as there is some way I can easily access the variables from the model?

    I seem to be missing something with my understanding with IloObjective and IloRange.  I would expect to be able to call a method of IloCplex to get a IloObjective object that stores the objective function.  I believe this is a pointer to the objective in IloCplex so when I use IloObjective::setLinearCoefs this will change the values in IloCplex as well.  Is this correct?  I think my problem is I don't know how to create the IloObjective or IloRange objects in order to work with them.  Or am I totally lost!

    thanks

    Stephen
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Get and set functions

    Posted 11/26/08 05:39 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    If the IloObjective object isn't a pointer (thinking more about it, it's probably not) then I'd expect the code to look something like:

    IloNumVarArray chgVariables;
    IloNumArray chgValues;

                // add some variables and values to the above arrays here

    IloObjective obj;
    obj.setLinearCoefs(chgVariables, chgValues);

                // call a method of IloCplex to incorporate the changes

    But I don't know what that last method call is...

    I appreciate you clearing this up for me.

    Stephen

    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Get and set functions

    Posted 11/26/08 12:29 PM

    Originally posted by: SystemAdmin


    [Didier Vidal said:]

    Stephen,

    Here are some guidelines to helo you find your way through the Concert C++ API.

    1) Handles
    Ilo objects in C++ concerts are handles to pointers. This is made to remove the burden of memory management to the users.
    When you create an Ilo object, it actually allocates a pointer to the 'Environment' (this is why you always need to give the 'env' as argument). And the pointer will not be deleted when the stack that contains the Ilo object is destroyed (say when you return from a function).
    By default, the lifecycle of the objects you create is controlled by the lifecycle of the environment. This avoids many memory leaks problems or memory errors.

    2) Adding a model element to CPLEX.
    The method to add a model element (called an 'extractable') to model is IloModel.add(IloExtractable). An objective is an IloExtractable that can be added to a model.

    3) Finding examples
    To find examples of concert code, an easy way is to go in the example directory of a CPLEX installation (cplexInstallDir/examples/src)
    Then list all examples in C++ (suffix: .cpp)

    Didier.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Get and set functions

    Posted 11/27/08 04:27 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    Hi Didier

    Thanks for your help.  Knowing that I'm working with handles helps a lot.  I'm aware of the CPLEX examples but haven't found one that helps me.  I have an algorithm where, within a loop, I take the last CPLEX solution and use this to change variable objective function coefficients and bounds before calling CPLEX again.  I've set up the LP, solved it the first time etc and just need to know how to do these last two things to finish the job.  I assume I get the objective from model each time then make changes using setLinearCoef() which will change the CPLEX LP.  But I don't know how to get this objective.  Similarly with IloRange for bound changes.  Some code snippets would certainly help.

    thanks

    Stephen


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Get and set functions

    Posted 12/03/08 02:45 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    For others who are interested, I used:

    IloNumVarArray chgVariables(env);
    IloNumArray chgValues(env);

    // set some variables using
    IloNumVar variable = variables[(*arcIt).getVariable()];
    chgVariables.add(variable);
    chgValues.add(weight);

    and

    if (chgVariables.getSize()>0)
    {
      IloEnv env = model.getEnv();
      IloObjective objective(env);
      objective.setLinearCoefs(chgVariables, chgValues);
    }

    to change the objective coefficients.

    To get variable bounds I used:

    int variableIndex = (*headSink).getVariableIndex();
    IloNumVar variable = variables[variableIndex];
    IloRange range(env, variable);
    double capacity = range.getUB();

    This may seem obvious, but its not very well documented in the manuals IMHO.  It still seems odd that I need to store and use a IloNumVarArray variables array to keep track of variables in the problem, and pass it to each of my methods,  rather than get them directly from the model each time.  Storing variables twice seems inefficient.  Or have I missed something?

    Stephen



    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Get and set functions

    Posted 12/08/08 04:01 PM

    Originally posted by: SystemAdmin


    [Didier Vidal said:]

    Does this work ? I'm surprised that you don't add the objective to a model in


    if (chgVariables.getSize()>0)
    {
      IloEnv env = model.getEnv();
      IloObjective objective(env);
      objective.setLinearCoefs(chgVariables, chgValues);
    }


    Also, the following might simplify the query to a variable domain

    variable.getUB();

    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Get and set functions

    Posted 12/09/08 04:45 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    Hi Didier

    I do add the objective to the model when setting up the problem then use the code below to change the objective coefficients later on.  I'm still runtime debugging my code and will make sure this works as expected.  But if you know of a better way of doing what I want to do please let me know.

    Using variable.getUB() to get an upper bound raises a point I may be missing.  As mentioned, for each of my methods I pass the model and variables because there doesn't seem to be a way of getting all variables directly from the model.  Therefore it seems variable details need to be stored in two places, in an IloNumVarray array and within the model.  I'm using an 8-processor CPLEX license and intend to use this code in a branch and cut routine.  Now the variables I'm passing with the model won't change at each child node but bounds certainly will.  So it seems to me that by having the necessity of storing variable details in two places, their attributes are likely to become out of synch.  In this case variable.getUB() is likely to give incorrect results.  Have I missed something?

    thanks

    Stephen
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Get and set functions

    Posted 12/10/08 12:51 PM

    Originally posted by: SystemAdmin


    [Didier Vidal said:]

    As mentioned, for each of my methods I pass the model and variables

    This is the architecture I have seen in many concert application. Actually, the variables are often not directly passed as argument of a function. They are stored in a C++ object (for instance an object that represents a train and stores variables relating to it in a supply chain problem).

    intend to use this code in a branch and cut routine.
    That's not something I have experimented... But I to get information about the variables during the search (not in the static model), you should use the method of the callbacks. For instance, the MIP ControllCallBack offers methods to get pseudoCosts, UBs etc... of variables.

    Didier.
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Get and set functions

    Posted 12/11/08 05:20 AM

    Originally posted by: SystemAdmin


    [Stephen.Hill said:]

    Thanks Didier

    It seems I'm using concert as intended.  My confusion has been why variables need to be stored within the model then only accessed via the stored IloNumVarArray variable handle array.  Why not allow access to variables via their indexes as with the callable library?  I note that for each of the Ilog examples where 2-index variables are used, each variable is defined over the range of both indexes so it seems natural to access variables using handles stored in the IloNumVarArray.  But if variables aren't defined for all index pairs, as is likely to happen for real life problems, then one needs to store the variable index corresponding to each pair with a sentinel value used for when a variable isn't defined.  If I need to use this index to access the IloNumVarArray to get the variable handle then why not do this within the model, as with the callable library, to avoid potential access problems?  Seems odd that this access isn't provided.

    thanks for clearing this up for me



    Stephen
    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Get and set functions

    Posted 12/11/08 12:02 PM

    Originally posted by: SystemAdmin


    [Didier Vidal said:]

    I saw a class called IloLPMatrix that stores a sparse array of variables and coefficients in constraints.  I suppose it can be used to manipulate the constraints and variables in a way that is close to the engine representation. It has getters for both variables and constraints. Concert tries to be independent of the solving engine, in its modeling layer.

    Didier.
    #CPLEXOptimizers
    #DecisionOptimization