Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Callback function in OPL language

  • 1.  Callback function in OPL language

    Posted 08/20/19 11:25 AM

    Originally posted by: A.Omidi


    Dear community team,

     

    I'm trying to solve a MIP model. I will need to know the number of cuts which is used by the solver (for instance Gomory cuts). In the Cplex with the concert technology, it is possible by using callbacks function. I was wondering if, is it possible to use callbacks via OPL?

     

    Regards

    Omidi


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Callback function in OPL language

    Posted 08/21/19 04:31 AM

    Originally posted by: DavidGravot


    I don't think this is possible through OplScript

    I suggest you keep your opl model and runs it into your favorite language, go to folder  ...\IBM\ILOG\CPLEX_Studio129\opl\examples\opl_interfaces to browse examples such as Mulprod.java 

     

    package mulprod;
    
    import ilog.concert.*;
    import ilog.cplex.*;
    import ilog.opl.*;
    
    public class Mulprod
    {
        static final String DATADIR = ".";
    
        static public void main(String[] args) throws Exception
        {
          int status = 127;    
          try {
            IloOplFactory.setDebugMode(true);
            IloOplFactory oplF = new IloOplFactory();
            IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
            IloOplModelSource modelSource = oplF.createOplModelSource(DATADIR
                    + "/mulprod.mod");
            IloOplSettings settings = oplF.createOplSettings(errHandler);
            IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
            IloCplex cplex = oplF.createCplex();
            cplex.setOut(null);
            IloOplModel opl = oplF.createOplModel(def, cplex);
            IloOplDataSource dataSource = oplF.createOplDataSource(DATADIR
                    + "/mulprod.dat");
            opl.addDataSource(dataSource);
            opl.generate();
            if (cplex.solve())
            {
                System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
                opl.postProcess();
                opl.printSolution(System.out);
            }
            else
            {
                System.out.println("No solution!");
            }
            oplF.end();
                    status = 0;
          } catch (IloOplException ex) {
            System.err.println("### OPL exception: " + ex.getMessage());
            ex.printStackTrace();
            status = 2;
          } catch (IloException ex) {
            System.err.println("### CONCERT exception: " + ex.getMessage());
            ex.printStackTrace();
            status = 3;
          } catch (Exception ex) {
            System.err.println("### UNEXPECTED UNKNOWN ERROR ...");
            ex.printStackTrace();
            status = 4;
          }
          System.exit(status);
        }
    }
    

    Then, instead of calling cplex.solve() , you may create your callback there and pass it as an argument

    The callback will have to monitor your mathematical variables, that can be extracted from the IloOplModel through API such as 

    IloNumVarMap cuts = masterOpl.getElement("Cut").asNumVarMap();
    

    as shown in the Cutstock_change.java sample

    Basically, each data / variable of your .mod file can be queried by its name, then cast in the ad-hoc type (you may check getType on an element to figure out accurately what type it is, depending on how is build your collection : arrays, sets, tuple set …)

     

    Once you get familiar with these APIs, it is relatively simple to mix both of best worlds : OPL to build an easy-to-write-read model , and your favorite language (C++ / .NET / JAVA / SCALA …) to access advanced features of CPLEX / CPO that are not part of the OPL script

     

    David


    #DecisionOptimization
    #OPLusingCPLEXOptimizer