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
------------------------------
Original Message:
Sent: Mon February 08, 2021 06:40 AM
From: Christiane Bracchi
Subject: A question regarding "Black-box expressions in CP Optimizer 20.1"
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
Original Message:
Sent: Thu February 04, 2021 06:47 AM
From: Mohamed Awad
Subject: A question regarding "Black-box expressions in CP Optimizer 20.1"
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