Topic
• 4 replies
• Latest Post - ‏2012-11-23T13:30:03Z by SystemAdmin
378 Posts

# Pinned topic Calculating arc Cosine

‏2012-11-22T10:28:01Z |
Hello,
I am trying to calculate the arc cosine of values but

Thank you.
Updated on 2012-11-23T13:30:03Z at 2012-11-23T13:30:03Z by SystemAdmin
• ol
51 Posts

#### Re: Calculating arc Cosine

‏2012-11-22T18:06:13Z
Hello,
do you mean computing the arc cosine of an IloNumExpr?

In the general case, you need to define your own constraint in the C++ API, at the Ilc level and then wrap it to the Ilo level with the macro ILOCPCONSTRAINTWRAPPER.

Depending on your need, you may be able to simply use the relation cosinus(a)=adjacent / hypotenuse. If you have integers variables, you may also be able to use element constraint for example with tabulated values. Can you give some precisions on your exact need?

Regards,
Olivier
378 Posts

#### Re: Calculating arc Cosine

‏2012-11-23T07:12:06Z
• ol
• ‏2012-11-22T18:06:13Z
Hello,
do you mean computing the arc cosine of an IloNumExpr?

In the general case, you need to define your own constraint in the C++ API, at the Ilc level and then wrap it to the Ilo level with the macro ILOCPCONSTRAINTWRAPPER.

Depending on your need, you may be able to simply use the relation cosinus(a)=adjacent / hypotenuse. If you have integers variables, you may also be able to use element constraint for example with tabulated values. Can you give some precisions on your exact need?

Regards,
Olivier
Dear Olivier,

As for your suggested method, I am not familiar with using C++ API. Could you please explain further.
The problem that I am trying to calculate the angle (decision variable) between two vectors during the optimization run. While it is not possible to use trigonometric functions, the question is whether there is another way to calculate the angle. Using tabulated values seems difficult, since the angle is derived from calculating the arcosine of the vectors dotproduct and they are floating numbers.
Is there a way to calculate the angle similar to how the mathematical function is calculated by software.

Best Wishes
• ol
51 Posts

#### Re: Calculating arc Cosine

‏2012-11-23T11:03:50Z
Dear Olivier,

As for your suggested method, I am not familiar with using C++ API. Could you please explain further.
The problem that I am trying to calculate the angle (decision variable) between two vectors during the optimization run. While it is not possible to use trigonometric functions, the question is whether there is another way to calculate the angle. Using tabulated values seems difficult, since the angle is derived from calculating the arcosine of the vectors dotproduct and they are floating numbers.
Is there a way to calculate the angle similar to how the mathematical function is calculated by software.

Best Wishes
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;
}```
```
378 Posts

#### Re: Calculating arc Cosine

‏2012-11-23T13:30:03Z
• ol
• ‏2012-11-22T18:06:13Z
Hello,
do you mean computing the arc cosine of an IloNumExpr?

In the general case, you need to define your own constraint in the C++ API, at the Ilc level and then wrap it to the Ilo level with the macro ILOCPCONSTRAINTWRAPPER.

Depending on your need, you may be able to simply use the relation cosinus(a)=adjacent / hypotenuse. If you have integers variables, you may also be able to use element constraint for example with tabulated values. Can you give some precisions on your exact need?

Regards,
Olivier
Thank you very much,
since I am using OPL, I will try Taylor series.