Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Changing Objective Function - OPL

    Posted Thu November 18, 2010 02:12 PM

    Originally posted by: bengu


    Hi All,

    I have a model and need to change the objective function during the iterations. For, eg. during the iteration from script, for the first time it should be minimize x+y, then the next time it should be maximize 2x+2y and so on. Obviously, the trivial thing to do is to create .mod files for each of these and use them doing iterations, is there a better way to do it ?. Pls advice.

    Thanks alot.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Changing Objective Function - OPL

    Posted Fri November 19, 2010 10:58 AM

    Originally posted by: SystemAdmin


    Hi,

    let me give you an example:

    dvar int x;
    dvar int y;
     
     
    minimize x+y;
    subject to
    {
      5<=x<=15;
      5<=y<=15;
      }   
     
    execute
    {
     writeln("x=",x," y=",y); 
    }  
     
    main
    {
      thisOplModel.generate();
      cplex.solve();
      thisOplModel.postProcess();
      
      cplex.setObjCoef(thisOplModel.x,-2);
      cplex.setObjCoef(thisOplModel.y,-2);
      cplex.solve();
      thisOplModel.postProcess();
      
    }
    


    which gives

    x=5 y=5
    x=15 y=15

    Alex
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Changing Objective Function - OPL

    Posted Fri November 19, 2010 04:36 PM

    Originally posted by: bengu


    Thanks alot. This really helps.

    Just an extension to this, what if the objective is quite different, one is linear and the other is quadratic ?. The big picture is that this model is a sub-problem, where the constraints remain the same, but the objective differs (linear, quadratic).so, is there a way to do this ?. Pls advice.

    Thanks alot.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Changing Objective Function - OPL

    Posted Fri November 19, 2010 09:46 PM

    Originally posted by: SystemAdmin


    From script you can just change objective co-efficients as Alex showed. You can change objective completely by making external java call. Here is an example based on earlier example by Alex:
    
    dvar 
    
    int x; dvar 
    
    int y;   minimize x+y; subject to 
    { 5<=x<=15; 5<=y<=15; 
    } execute 
    { writeln(
    "x=",x,
    " y=",y); 
    } main 
    { IloOplImportJava(
    "Test.jar");   thisOplModel.generate(); cplex.solve(); thisOplModel.postProcess(); IloOplCallJava(
    "com.opl.sample.Test", 
    "ChangeObjective", 
    "", thisOplModel); cplex.solve(); thisOplModel.postProcess(); 
    }   On Java Side: 
    
    package com.opl.sample;   
    
    import ilog.concert.IloException; 
    
    import ilog.concert.IloIntVar; 
    
    import ilog.concert.IloNumExpr; 
    
    import ilog.cplex.IloCplex; 
    
    import ilog.opl.IloOplModel;   
    
    public 
    
    class Test 
    { 
    
    public 
    
    static 
    
    void ChangeObjective(IloOplModel model) 
    { 
    
    try 
    { IloCplex cplex = model.getCplex(); IloIntVar x = model.getElement(
    "x").asIntVar(); IloIntVar y = model.getElement(
    "y").asIntVar(); IloNumExpr expr = cplex.linearNumExpr(); expr = cplex.diff(expr, cplex.prod(x, 2)); expr = cplex.diff(expr, cplex.prod(y, 2)); cplex.remove(cplex.getObjective()); cplex.addMinimize(expr); 
    } 
    
    catch(IloException ex) 
    { ex.printStackTrace(); 
    } 
    }   
    }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Changing Objective Function - OPL

    Posted Sat November 20, 2010 12:56 AM

    Originally posted by: bengu


    This is quite interesting. This is very helpful. Thanks alot.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Changing Objective Function - OPL

    Posted Wed April 29, 2015 02:30 PM

    Originally posted by: mathygirl


    When I implement this, I get the following error message:

    ilog.concert.IloException: MultipleObjException: IloCplex can not handle multiple objectives

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Changing Objective Function - OPL

    Posted Fri May 01, 2015 11:54 AM

    Hi,

    the example I gave 5 years ago still works in 12.6.1

    dvar int x;
    dvar int y;
     
     
    minimize x+y;
    subject to
    {
      5<=x<=15;
      5<=y<=15;
      }   
     
    execute
    {
     writeln("x=",x," y=",y);
    }  
     
    main
    {
      thisOplModel.generate();
      cplex.solve();
      thisOplModel.postProcess();
     
      cplex.setObjCoef(thisOplModel.x,-2);
      cplex.setObjCoef(thisOplModel.y,-2);
      cplex.solve();
      thisOplModel.postProcess();
     
    }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer