Originally posted by: ol
Hello,
here is an example of what you could do if you use the C++ API; you may want to improve it a little bit, for example, the floating point errors when computing a cosine is rougly bounded with the addition of
-epr,+eps, and for simplifying I forced the angle to be between 0 and pi, but that is the idea.
If you are using OPL or java API, you may simply use a taylor serie for the arc cosine (or several taylor series, one around 0, one around 0.2,... with ifThen expressions if you want a better precision).
Regrds,
Olivier
#include <ilcp/cpext.h> #include <ilcp/ilosolver.h> #include <math.h> ILOSTLBEGIN #define PI 3.14159265358979323846
class MyIlcArccosI :
public IlcConstraintI
{
private: IlcFloatVar _x; IlcFloatVar _y;
public: MyIlcArccosI(IloCP s, IlcFloatVar x, IlcFloatVar y);
void post();
void propagate();
}; MyIlcArccosI::MyIlcArccosI(IloCP s, IlcFloatVar x, IlcFloatVar y): IlcConstraintI(s), _x(x), _y(y)
{
}
void MyIlcArccosI::post()
{ _x.whenRange(
this); _y.whenRange(
this);
}
void MyIlcArccosI::propagate()
{ _x.setRange(0,PI); _y.setRange(-1,1); IlcFloat eps=0.0000001; _x.setRange(acos(_y.getMax())-eps, acos(_y.getMin())+eps); _y.setRange(cos(_x.getMax())-eps,cos(_x.getMin())+eps);
} IlcConstraint MyIlcArccos(IloCP cp, IlcFloatVar x, IlcFloatVar y)
{
return
new (cp.getHeap()) MyIlcArccosI(cp, x, y);
}
//x == arccos(y) ILOCPCONSTRAINTWRAPPER2(MyIloArccos, cp, IloNumVar, x, IloNumVar, y)
{ use(cp, x); use(cp, y);
return MyIlcArccos(cp, cp.getFloatVar(x), cp.getFloatVar(y));
}
void Model(IloEnv env)
{ IloNumVar x(env, -33, 44); IloNumVar y(env, -100, 23.44); IloModel mdl(env); mdl.add(IloMaximize(env, x)); mdl.add(x); mdl.add(y); IloExtractable ct1 = MyIloArccos(env,x,y); mdl.add(ct1); IloCP cp(mdl); cp.solve(); cout <<
"Arccos : " << cp.domain(x) <<
" " << cp.domain(y) << endl; cp.end();
}
int main(
int argc,
const
char * argv[])
{ IloEnv env;
try
{ Model(env);
}
catch (IloException & ex)
{ env.out() <<
"Caught: " << ex << endl;
} env.end();
return 0;
}
#ConstraintProgramming-General#DecisionOptimization