Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Different models in Cplex Java

    Posted 03/09/10 04:31 PM

    Originally posted by: mrr_1010


    Hi,
    I am sorry, I posted this thread somewhere else, but I cannot find it back (I am new to the forum...). I want to define different models within the same cplex instance, in the context of master/subproblem, so that one model only solves part of the whole problem. I find this very easy in AMPL/GAMS, with the model command, but I am not quite sure which class to use in Java.
    Many thanks!
    m.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Different models in Cplex Java

    Posted 03/09/10 11:31 PM

    Originally posted by: SystemAdmin


    > mrr_1010 wrote:
    > I am sorry, I posted this thread somewhere else, but I cannot find it back (I am new to the forum...). I want to define different models within the same cplex instance, in the context of master/subproblem, so that one model only solves part of the whole problem. I find this very easy in AMPL/GAMS, with the model command, but I am not quite sure which class to use in Java.

    IloCplex masterProblem = new IloCplex();
    IloCplex subProblem = new IloCplex();

    Then add model content to each instance of IloCplex and solve away.

    /Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Different models in Cplex Java

    Posted 03/10/10 09:11 AM

    Originally posted by: mrr_1010


    Hi Paul,

    Many thanks for your answer. I have been trying it out, and the problem I have is that I do not quite understand how the masterProblem and the subProblem "share" information, as they are separate instances. I would like some variables of the master to be parameters of the subProblem, and viceversa. Do I just have to pass them to an array at every iteration that acts as the parameter? Is there a better way to do this?

    In a similar set-up, I am not sure I understand how to add the same variable to two different models. Do I just have to create two different variables and pass the value from one model to the other if I want it as initial guess? For example, I encounter the following problem if trying to do it step by step (I have arrays)

    IloCplex base = new IloCplex();
    IloCplex integer = new IloCplex();
    IloNumVar][[] ut = new IloIntVar[S][H][K]; // variable in both models
    for (int h = 0; h < H; h++) {
    for (int k = 0; k < K; k++) {
    for (int s = 0; s < S; s++) {
    ut[s][h][k] = integer.intVar(0, 1); // initialize "ut" for integer and add
    integer.add(ut[s][h][k]);
    ut[s][h][k] = base.intVar(0, 1); // **** with this line, solver seems to get lost for integer model;
    // without this line, solver crashes if solving base model
    // as base does not initialize "ut"...
    base.add(ut[s][h][k]);
    }
    }
    }

    As a solution, I created a model with all the common variables (base), exported it and imported it into the integer model ---if I just did IloCplex integer = base; then any modification in integer would apply to base. However I am not yet sure how they share the info in such case.

    Many thanks,
    m.
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Different models in Cplex Java

    Posted 03/10/10 11:46 AM

    Originally posted by: SystemAdmin


    > mrr_1010 wrote:
    > Hi Paul,
    >
    > Many thanks for your answer. I have been trying it out, and the problem I have is that I do not quite understand how the masterProblem and the subProblem "share" information, as they are separate instances. I would like some variables of the master to be parameters of the subProblem, and viceversa. Do I just have to pass them to an array at every iteration that acts as the parameter? Is there a better way to do this?

    That's about it. After you solve the master problem, you extract whatever portion of the solution is relevant (as doubles, integers or maybe booleans, depending on the types of the master problem variables) and pass that to the subproblem as values for some of the subproblem parameters. Then solve the subproblem, extract whatever info you need, and pass it as parameter values to the master problem, etc., ad nauseum.
    >
    > In a similar set-up, I am not sure I understand how to add the same variable to two different models. Do I just have to create two different variables and pass the value from one model to the other if I want it as initial guess? For example, I encounter the following problem if trying to do it step by step (I have arrays)
    >
    > IloCplex base = new IloCplex();
    > IloCplex integer = new IloCplex();
    > IloNumVar][[] ut = new IloIntVar[S][H][K]; // variable in both models
    > for (int h = 0; h < H; h++) {
    > for (int k = 0; k < K; k++) {
    > for (int s = 0; s < S; s++) {
    > ut[s][h][k] = integer.intVar(0, 1); // initialize "ut" for integer and add
    > integer.add(ut[s][h][k]);
    > ut[s][h][k] = base.intVar(0, 1); // **** with this line, solver seems to get lost for integer model;
    > // without this line, solver crashes if solving base model
    > // as base does not initialize "ut"...
    > base.add(ut[s][h][k]);
    > }
    > }
    > }
    >
    > As a solution, I created a model with all the common variables (base), exported it and imported it into the integer model ---if I just did IloCplex integer = base; then any modification in integer would apply to base. However I am not yet sure how they share the info in such case.
    >

    You cannot share model objects between two models using the Java API. On the other hand, I don't see much reason to do so. I'm not sure what info you're trying to share, so I can't be more specific.

    /Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Different models in Cplex Java

    Posted 03/10/10 12:08 PM

    Originally posted by: mrr_1010


    Hi Paul,

    Many thanks, this is very useful!

    For the example I gave, I was trying to compare a relaxed model and a more constrained model, that are very similar except for some added variables and constraints to the latter -- so cplex potentially could use info of one solution to find the solution of the other (that is why I was trying to make them "share" objects).

    It seems to me after your feedback that the easiest way to do this in Java, differently than the master/subproblem setting, is to create the same model and add or remove variables and constraints as needed when switching between the two problems. If I solve the relaxed problem first, this way cplex would use the info contained in the solution. (?)

    Many thanks!
    m.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Different models in Cplex Java

    Posted 03/10/10 02:29 PM

    Originally posted by: SystemAdmin


    > mrr_1010 wrote:
    > For the example I gave, I was trying to compare a relaxed model and a more constrained model, that are very similar except for some added variables and constraints to the latter -- so cplex potentially could use info of one solution to find the solution of the other (that is why I was trying to make them "share" objects).
    >
    > It seems to me after your feedback that the easiest way to do this in Java, differently than the master/subproblem setting, is to create the same model and add or remove variables and constraints as needed when switching between the two problems. If I solve the relaxed problem first, this way cplex would use the info contained in the solution. (?)

    Yes. I would create one instance of IloCplex with the "core" variables/constraints, then add/remove model objects as needed. If you're going to compare just once, the obvious possibility is to solve the relaxed model first, then add the additional features to get the more constrained model and solve again. CPLEX will try to make use of the solution to the first model when solving the second, although how much use it makes of the first solution will depend on what changes you made, whether it's an LP or an IP, etc.

    If for some reason you need to go back and forth between two versions of the same model, you can add and remove constraints and variables repeatedly (as long as the constraints are assigned to named instances of IloRange you can add them back without recreating them). My preference, though, would be to pile everything in at the outset and then change the bounds on the fly to turn specific variables/constraints on or off. I think fiddling with bounds in CPLEX is faster than making structural modifications to the model.

    /Paul
    #CPLEXOptimizers
    #DecisionOptimization