Decision Optimization

 View Only
Expand all | Collapse all

Solvefixed in OPL to get dual for MIP

  • 1.  Solvefixed in OPL to get dual for MIP

    Posted Mon October 16, 2017 09:18 AM

    In RFE we have a very valid wish

    Cplex.solveFixed() for OPL Script

    https://www.ibm.com/developerworks/rfe/execute?use_case=viewRfe&CR_ID=96227

    But in the meantime, let s have a workaround and do that manually in scripting:

    1) solve it to get the solution.

    2) solve the corresponding LP with fixed integer variables

    3) get dual costs

    dvar int x in 0..10;
    dvar float y in 0..10;
     
    minimize x+y;
    subject to {
      ctx :   
        x >= 1/2;
      cty:
        y>=1/2;
    }
     
    main {
      var status = 0;
      thisOplModel.generate();
      if (cplex.solve()) {
        writeln("Integer Model");   
        writeln("OBJECTIVE: ",cplex.getObjValue());   
        writeln("dual CTX:",thisOplModel.ctx.dual);
        writeln("dual CTY:",thisOplModel.cty.dual);
      }
      var xvalue=thisOplModel.x.solutionValue;
     
      thisOplModel.convertAllIntVars();
      thisOplModel.x.UB=xvalue;
      thisOplModel.x.LB=xvalue;
      if (cplex.solve()) {
        writeln("Relaxed Model");   
        writeln("OBJECTIVE: ",cplex.getObjValue());  
        writeln("dual CTX:",thisOplModel.ctx.dual);
        writeln("dual CTY:",thisOplModel.cty.dual);
      }
     
    }

    gives

    Integer Model
    OBJECTIVE: 1.5
    dual CTX:undefined
    dual CTY:undefined
    Relaxed Model
    OBJECTIVE: 1.5
    dual CTX:0
    dual CTY:1

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Solvefixed in OPL to get dual for MIP

    Posted Mon October 16, 2017 09:41 AM

    Originally posted by: ouaigooo


    Thanks Alex,

    Is there a way to iterate all int vars of model (in order to fix them) ?

    Something like for (var x in thisOplModel.getVariables()) if (x.isIntVar), ...

    It would be cool if it could work without specifying explicitely the variable names and their dimensions

    kr -- lucien


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  RE: Re: Solvefixed in OPL to get dual for MIP

    Posted Wed August 24, 2022 08:38 AM
    Edited by System Fri January 20, 2023 04:24 PM
    Now we can use solveFixed in scripting:

    dvar int x in 0..10;
    dvar float y in 0..10;
     
    minimize x+y;
    subject to {
      ctx :   
        x >= 1/2;
      cty:
        y>=1/2;
    }
     
    main {
      var status = 0;
      thisOplModel.generate();
      cplex.solve();
      writeln("Solve");
      writeln("x=",thisOplModel.x);
      writeln("y=",thisOplModel.y);
      writeln("OBJECTIVE: ",cplex.getObjValue()); 
      writeln("SolveFixed");
      if (cplex.solveFixed()) {
        writeln("x=",thisOplModel.x);
        writeln("y=",thisOplModel.y);  
        writeln("OBJECTIVE: ",cplex.getObjValue());   
        writeln("dual CTX:",thisOplModel.ctx.dual);
        writeln("dual CTY:",thisOplModel.cty.dual);
      }
      
     
    } ​


    which gives

    Solve
    x=1
    y=0.5
    OBJECTIVE: 1.5
    SolveFixed
    x=1
    y=0.5
    OBJECTIVE: 1.5
    dual CTX:0
    dual CTY:1​


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 4.  RE: Re: Solvefixed in OPL to get dual for MIP

    Posted Wed August 24, 2022 08:40 AM
    Now with solveFixed we can simply write

    dvar int x in 0..10;
    dvar float y in 0..10;
     
    minimize x+y;
    subject to {
      ctx :   
        x >= 1/2;
      cty:
        y>=1/2;
    }
     
    main {
      var status = 0;
      thisOplModel.generate();
      cplex.solve();
      writeln("Solve");
      writeln("x=",thisOplModel.x);
      writeln("y=",thisOplModel.y);
      writeln("OBJECTIVE: ",cplex.getObjValue()); 
      writeln("SolveFixed");
      if (cplex.solveFixed()) {
        writeln("x=",thisOplModel.x);
        writeln("y=",thisOplModel.y);  
        writeln("OBJECTIVE: ",cplex.getObjValue());   
        writeln("dual CTX:",thisOplModel.ctx.dual);
        writeln("dual CTY:",thisOplModel.cty.dual);
      }
      
     
    } ​

    which gives

    Solve
    x=1
    y=0.5
    OBJECTIVE: 1.5
    SolveFixed
    x=1
    y=0.5
    OBJECTIVE: 1.5
    dual CTX:0
    dual CTY:1


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------