Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Calculating arc Cosine

    Posted 11/22/12 05:28 AM

    Originally posted by: SystemAdmin


    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.
    #ConstraintProgramming-General
    #DecisionOptimization


  • 2.  Re: Calculating arc Cosine

    Posted 11/22/12 01:06 PM

    Originally posted by: ol


    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
    #ConstraintProgramming-General
    #DecisionOptimization


  • 3.  Re: Calculating arc Cosine

    Posted 11/23/12 02:12 AM

    Originally posted by: SystemAdmin


    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
    #ConstraintProgramming-General
    #DecisionOptimization


  • 4.  Re: Calculating arc Cosine

    Posted 11/23/12 06:04 AM

    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


  • 5.  Re: Calculating arc Cosine

    Posted 11/23/12 08:30 AM

    Originally posted by: SystemAdmin


    Thank you very much,
    since I am using OPL, I will try Taylor series.
    #ConstraintProgramming-General
    #DecisionOptimization