Decision Optimization

 View Only
  • 1.  A question regarding "Black-box expressions in CP Optimizer 20.1"

    Posted Thu February 04, 2021 06:48 AM

    Hi,

    I would appreciate if you could help me with a question regarding "Black-box expressions in CP Optimizer 20.1". I built a large scheduling model using OPL and now, I would like to insert the objective function from external source. I can build the external objective function expression in C++ however, it is very hard to re-build the scheduling model in C++. My question is can I call the OPL model from C++ or is there a way to translate my OPL model as C++ model.

     

    Thanks in advance

    Mohamed



    ------------------------------
    Mohamed Awad
    ------------------------------

    #DecisionOptimization


  • 2.  RE: A question regarding "Black-box expressions in CP Optimizer 20.1"

    Posted Thu February 04, 2021 01:00 PM
    Hello Mohamed,
    Sure you can call your OPL model from C++.
    You might you a look to the distributed sample oplrunsample.
    I hope this helps,
    Chris

    ------------------------------
    Christiane Bracchi
    ------------------------------



  • 3.  RE: A question regarding "Black-box expressions in CP Optimizer 20.1"

    Posted Mon February 08, 2021 06:41 AM
    Hello Mohamed,
    Here is a tiny C++ sample which implements the use of a CPO Black-box expression with an OPL model.
    This example is intentionally very simple and not very interesting itself, its only interest being to show how to link the OPL model and the black-box expression.
    I hope this helps,
    Chris

    ------------------------------
    Christiane Bracchi
    ------------------------------



  • 4.  RE: A question regarding "Black-box expressions in CP Optimizer 20.1"

    Posted Wed February 17, 2021 12:29 PM
    It seems that the attached file has disappeared.
    So I copy it here.

    #include <ilcp/cpext.h>
    #include <ilopl/iloopl.h>
    #include <sstream>
    ILOSTLBEGIN
    
    static char* getModelText();
    
    ILOBLACKBOX1(MySum, IloIntVarArray, array) {
      IloNum result = 0.0;
      for (IloInt i=0; i<array.getSize(); i++) {
        result += getValue(array[i]);
      }
      returnValue(result);
    }
    
    int main(int argc,char* argv[]) {
      IloEnv env;
      int status = 127;
      try {
        IloOplErrorHandler handler(env,cout);
        std::istringstream in( getModelText() );
        IloOplModelSource modelSource(env,in,"sample");
        IloOplSettings settings(env,handler);
        IloOplModelDefinition def(modelSource,settings);
        IloCP cp(env);
        IloOplModel opl(def,cp);
        opl.generate();
        IloOplElement x = opl.getElement("x");
        IloIntVarMap map = x.asIntVarMap();
        IloIntVarArray vars = map.asNewIntVarArray();
        cp.getModel().add(IloMinimize(env, MySum(env, vars)));
        if ( cp.solve()) {
          opl.printSolution(cout);
          status = 0;
        } else {
          cout << "No solution!" << endl;
          status = 1;
        }
      } catch (IloOplException & e) {
        cout << "### OPL exception: " << e.getMessage() << endl;
      } catch( IloException & e ) {
        cout << "### exception: ";
        e.print(cout);
        status = 2;
      } catch (...) {
        cout << "### UNEXPECTED ERROR ..." << endl;
        status = 3;
      }
      env.end();
      cout << endl << "--Press <Enter> to exit--" << endl;
      getchar();
      return status;
    }
    
    static char* getModelText() {
      return (char*)"\
    using CP; \
    dvar int x[1..5] in 1..10; \
    subject to { \
     allDifferent(x); \
    }; \
    ";
    }
    ​


    ------------------------------
    Christiane Bracchi
    ------------------------------



  • 5.  RE: A question regarding "Black-box expressions in CP Optimizer 20.1"

    Posted Tue March 02, 2021 09:46 AM
    Dear Chris,

    Thanks a million for your kind support.

    Best wishes,
    Mohamed

    ------------------------------
    Mohamed Awad
    ------------------------------