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

Pinned topic Calculating arc Cosine

‏2012-11-22T10:28:01Z |
Hello,
I am trying to calculate the arc cosine of values but
I think i cannot do it directly. please help me with a way to do it.

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

    Re: Calculating arc Cosine

    ‏2012-11-22T18:06:13Z  in response to SystemAdmin
    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
    • SystemAdmin
      SystemAdmin
      378 Posts
      ACCEPTED ANSWER

      Re: Calculating arc Cosine

      ‏2012-11-23T07:12:06Z  in response to ol
      Dear Olivier,
      Thank you for your response.

      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
        ol
        33 Posts
        ACCEPTED ANSWER

        Re: Calculating arc Cosine

        ‏2012-11-23T11:03:50Z  in response to SystemAdmin
        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; 
        }
        
    • SystemAdmin
      SystemAdmin
      378 Posts
      ACCEPTED ANSWER

      Re: Calculating arc Cosine

      ‏2012-11-23T13:30:03Z  in response to ol
      Thank you very much,
      since I am using OPL, I will try Taylor series.