Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How I can remove some constraints in cplex using java

    Posted 04/19/16 12:58 PM

    Originally posted by: nr-mz


    Dear IBM,

     

    Actually, I am using Cplex in java. In my code, I a going to optimize an objective function subject to some constrains. Then I need to remove some of the constraints and add another set of them and then optimize the second model.

     

    My cplex object is named solver.

    solver= new IloCplex();

     

    My constraints are defined like folowing:

     

     

     IloLinearNumExpr expr1 = solver.linearNumExpr(); 

    .....

      solver.addEq(expr1, 1);

     

     IloLinearNumExpr expr2 = solver.linearNumExpr(); 

    .....

      solver.addEq(expr2, 1);

     

     IloLinearNumExpr expr3 = solver.linearNumExpr(); 

    .....

      solver.addEq(expr3, 1);

    .

    .

    .

    After adding all the constraints I solve my model by using the commands as it follows

     

    solver.addMinimize(obj);

     solver.solve();

     

    Then I want to start optimizing my second model. I would like to remove just some of constraint (for example just expr1 ) and optimize again my model again. But actually I wonder how I can do that. Any help of you will be useful for  me.

     

    Thank you so much for your attention.

     

    Best regards

     

     

     

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How I can remove some constraints in cplex using java

    Posted 04/19/16 01:15 PM

    addEq() returns the constraint it added. You can remove it using IloCplex.remove(). Like so

    IloConstraint c = solver.addEq(...);
    ...
    solver.solve();
    ...
    solver.remove(c);

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 02:47 AM

    Originally posted by: nr-mz


    Thank you so much for your response.. But actually after finishing the solution procedure ( after the command solver.solve() ), when I add solver.remove( c), it points out an error saying: createlocal variable 'object', create local filed 'object', create parameter 'object' ,... In fact, when I add solver.remove (object), I cannot select c as an object argument for remove.. Could you please guide me on it?

     

    Thank you so much for your kind attention


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 03:03 AM

    Sorry, I have no clue what you are talking about. This sounds like an error message from an IDE or what?

    In any case, it sounds like a Java programming error not anything related to CPLEX.

    My wild guess is that 'c' is not in the same scope as the solver.remove(...) statement. Can you double-check that?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 04:31 AM

    Originally posted by: nr-mz


    Sorry, I was not clear...

     

    Actually, in my model, I need to optimize an objective function subject to some constrains. Then, I need to remove some of the constraints and add another set of the constraints and then optimize the new model. So for that, as you proposed me, I name the constraints, which I wish remove them . for example I do like in following:

     

     IloLinearNumExpr expr1 = solver.linearNumExpr(); 

    .....

     IloConstraint  c = solver.addEq(expr1, 1);

     

    After that, I solve the the model by using the following command:

     

    solver.addMinimize(obj);

    solver.solve();

     

    Now, I wish to remove constraint c, and add new constraints and then solve my new model. So, directly after solver.solve(); I add this command:   solver.remove(c);

    But when I add solver.remove(c); I find an error message that appears on this command line in my java environment. when I click on the error, a dialogue window opens which propose to me to create local variable  or, create local filed , or create parameter . That is to say, it acts on a way that c is not defined in my code !! but as you know wehave added befor c as an object of iloconstraints. So, now I wonder what I can do to overcome this error.

     

    Thank you so much for your kind attention.

     

    Best regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 04:47 AM

    Like I said, it looks like a Java programming error and the most likely reason is that 'c' is not in the current scope. Did you check that 'c' is in the current scope?

    Maybe you can post the full code?


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 08:49 AM

    Originally posted by: nr-mz


    Yeah. I paste some important parts of my code.

     

    My constraint:

     for(int i=0; i<20;i++) {

          // The activity should be executed between its earliest start time and latest star time.
          IloLinearNumExpr expr1 = solver.linearNumExpr(); 
         
          for (int k=earliestStart; k<=latestStart; k++){
            expr1.addTerm(1, variables.Z_activityStartDate[i][k]);
          }
          IloConstraint  c =solver.addEq(expr1, 1);

     

    .

    .

    .

    other ocnstraint

    }

    objective function:

            for(int i=0; i<20;i++) {
         
             IloLinearNumExpr obj1 = solver.linearNumExpr();
        
              obj1.addTerm(1, variables.S_StartDateOfActvity[i]);
              
              IloObjective objfun1 = solver.addMinimize(obj1);  
             
              solver.solve();
              
              if (solver.solve()){
                System.out.println("MIIIIIIIIIIIIIIINNNNNNNNN start is solved  "+solver.getObjValue());
        
              }
              
              else{
                System.out.println("PREPROCESS FOR MIN START NOT SOLVEDDDDDDDDDDDDD");
              }
              
              solver.remove(objfun1);
        
            }

    After solving this model and before starting the next one, I add this command to remove 'c':

    solver.remove(c);

     

    But I have an error message,as I wrote you above...

     

    Thank you so much for your attention

    Best regards,

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 09:26 AM

    Well, it is exactly like I suspected: 'c' is no longer in scope when you call solver.remove(c). You need to fix this. This is a pure (Java) programming error and is completely unrelated to CPLEX. You may want to read about this in some Java tutorial or so.

    Also, from your code I suspect that you want to remove more than just the one instance of expr1==1. Instead you probably want to remove all of these constraints (you have one for each i in 0..19)? In that case you of course have to store references to each of the constraints you want to remove later and then remove all of them.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 09:38 AM

    Originally posted by: nr-mz


    Yeah, I want to remove all. So could you please help me how I can store refrences? and what should i use in coding for that?

    Could you please help me what I can remove a set of constraints when I am using cplex in java?

     

    Thank you so much


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: How I can remove some constraints in cplex using java

    Posted 04/20/16 09:59 AM

    If you have so much trouble writing a Java program then maybe you should try using CPLEX through OPL?

    Like I said before, your problem is a pure Java programming problem and not really related to CPLEX. So I just show an untested code snippet how I would go about implementing this. I hope this helps

    Collection<IloConstraint> c = new Vector<Constraint>();
    for (int i = 0; i < 20; ++i) {
        ...
        c.add(solver.addEq(expr1, 1));
    }
    ...
    solver.solve();
    ...
    solver.remove(c.toArray(new IloConstraint[0]));
    

     


    #CPLEXOptimizers
    #DecisionOptimization