forum.alglib.net
http://forum.alglib.net/

ALGLIB and C++
http://forum.alglib.net/viewtopic.php?f=2&t=328
Page 1 of 2

Author:  Vladimir [ Sat Mar 26, 2011 10:21 pm ]
Post subject:  ALGLIB and C++

Hi!
I'm newbie in C++ and I'm trying to write a programm using C++ (not C :)).
I want to do approximation using non-linear Least squares method. But lsfitfit function requires pointer to function in global scope. It's not usefull when you use objects (because pointer to method are not equal with global function). So I cant use callback-method (using static requires pointer to source object) for calculation function value like this:
Code:
class MyClass{
private:
      void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr) {
      ....
      }
public:
      FitCoeff() {
      ...
      lsfitfit(state, &function_cx_1_func);
      ...
      }
};

Maybe made some interface for this function like
Code:
class IFunction(...) {
public:
virtual double getValue(...)=0;
}

and send it to lsfitfit()?
(maybe best solution are present, I'm newbie in c++)

Also is it possible to include in distribution pre-compiled .dlls and .libs? Because adding to project lot of files from src directory are not usefull and filelist are huge.
Thx!

Author:  Sergey.Bochkanov [ Tue Mar 29, 2011 8:12 am ]
Post subject:  Re: ALGLIB and C++

No, I don't think that it is possible to avoid definition of the global function. You may declare it as static (in order to move it to file-only scope), if you want.

About precompiled units - yes, it is possible to make .lib file from ALGLIB. But in any way, according to GPL, you should distribute your project with sources (both your sources and ALGLIB).

Author:  SteSus85 [ Wed Jul 18, 2012 1:19 pm ]
Post subject:  Re: ALGLIB and C++

Hello,
I have exactly the same problem. It is impossible to use global function in my case, because it destroies whole concept of classes. On some forums people recommend to use delegates in this situation, but it looks quite complicate at the moment:(

Stepan.

Author:  SteSus85 [ Wed Jul 18, 2012 7:05 pm ]
Post subject:  Re: ALGLIB and C++

I've solved previous trouble, at least it is compiled (I just call as "func = ClassName.ClassFunction()"). But now I have new problem, algorithm (lsfitfit) makes one step left, then step right, then it goes back to initial value of parameter and then it stops!

Author:  Sergey.Bochkanov [ Thu Jul 19, 2012 5:47 am ]
Post subject:  Re: ALGLIB and C++

Can you post your code here? I will help you to find out explanation, but I need actual code (including function being optimized).

Author:  SteSus85 [ Thu Jul 19, 2012 2:26 pm ]
Post subject:  Re: ALGLIB and C++

Code:
double function_temp(float p1, float var1)
{
   float rho = 0.3f;         // Model Param
   float sigma0 = 0.02f;      // Model Param
   float m0 = 1.4f;          // Model Param
   int k = 6;             // Fixed Param
   float lambda = -0.1f;      // Option Param
   float initPrice = 50.f;      // Option Param
//   float strikePrice   = 65.f; // Option Param
   float interest = 0.00018f;       // Option Param
//   int noOfSum   = 50;          // Sym Param
   int width = 128;           // Sym Param
   int height = 256;           // Sym Param

return (double)clMonteCarloAMSM.runCLKernels2(            
             p1,//gkk,         // Model Param
             b,//b,           // Model Param
             rho,         // Model Param
             sigma0,      // Model Param
             m0,          // Model Param
             k,             // Fixed Param
             lambda,      // Option Param
             initPrice,      // Option Param
             var1,//strikePrice, // Option Param
             interest,       // Option Param
             22,//noOfSum,          // Sym Param
             width,            // Sym Param
             height           // Sym Param
             );
}

Code:
void function_cx_1_func(const real_1d_array &c, const real_1d_array &x, double &func, void *ptr)
{   
   func = function_temp((float)c[0],(float)x[0]);
   std::cout<<" c[0] = " << c[0] << " x[0] = " << x[0] << std::endl;
}

Code:
main(int argc, char * argv[])
{
        real_2d_array x = "[[25],[30],[35],[40],[45],[50],[55],[60],[65],[70],[75]]";
    real_1d_array y = "[24.99959, 20.01935, 15.03914, 10.07982, 5.9340054, 1.814159, 0.3633873, 0.05629168, 0.006411664, 0.000785, 0.00005]";
    real_1d_array c = "[0.99]";
/*   real_2d_array x = "[[-1],[-0.8],[-0.6],[-0.4],[-0.2],[0],[0.2],[0.4],[0.6],[0.8],[1.0]]";
    real_1d_array y = "[0.223130, 0.382893, 0.582748, 0.786628, 0.941765, 1.000000, 0.941765, 0.786628, 0.582748, 0.382893, 0.223130]";
    real_1d_array c = "[0.3]"; */
   real_1d_array bndl = "[0.00002]";
    real_1d_array bndu = "[0.99999]";
    double epsf = 0;
    double epsx = 0.0001;
    ae_int_t maxits = 0;
    ae_int_t info;
    lsfitstate state;
    lsfitreport rep;
    double diffstep = 0.0001;

    //
    // Fitting without weights
    //
    lsfitcreatef(x, y, c, diffstep, state);
    lsfitsetbc(state, bndl, bndu);
    lsfitsetcond(state, epsf, epsx, maxits);
    alglib::lsfitfit(state, function_cx_1_func);
    lsfitresults(state, info, c, rep);
    printf("%s\n", c.tostring(1).c_str()); // EXPECTED: [1.0]
}


The function clMonteCarloAMSM.runCLKernels2(...) is an integral computed by Monte Carlo technique as it is evident from the name.

The plot of the sum([runCLKernels2(gkk_real = 0.95,...,strikePrice_i) - runCLKernels2(gkk,...,,strikePrice_i),i=1..11]^2 w.r.t. gkk (minimization parameter) is:

Image

I've changed something and it makes a lot of steps, but there is a new problem. It doesn't converge to the right value (0.95). I tried different very close initial values: 0.92 -> 0.9, 0.99 -> 1.0, 0.97 -> 1.0

Thank you for your answer.

Author:  Sergey.Bochkanov [ Fri Jul 20, 2012 5:43 am ]
Post subject:  Re: ALGLIB and C++

Are you trying to minimize function which somehow depends on integral computed with Monte-Carlo method? If so, it is very likely that optimizer stops because of noise in the function being optimized. Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise. Conventional optimizers can't be used on such functions. Hmmm... I recomend you to google for "noisy optimization", maybe you will find method suited for your problem.

Author:  SteSus85 [ Fri Jul 20, 2012 3:08 pm ]
Post subject:  Re: ALGLIB and C++

Sergey.Bochkanov wrote:
Are you trying to minimize function which somehow depends on integral computed with Monte-Carlo method?

Yes, the minimized function is an integral computed by Monte Carlo.
Sergey.Bochkanov wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.

I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.
Sergey.Bochkanov wrote:
Hmmm... I recomend you to google for "noisy optimization", maybe you will find method suited for your problem.

Perhaps, I'll do it, thank you for idea.

Author:  Sergey.Bochkanov [ Mon Jul 23, 2012 10:35 am ]
Post subject:  Re: ALGLIB and C++

SteSus85 wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.
I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.

The problem is in how your integral changes when you change integration area or some other parameter. "Exact" integral is smooth with respect to changes in the integrand, Monte Carlo integral is nonsmooth. I suppose that is the source of your problems.

Author:  SteSus85 [ Mon Jul 23, 2012 9:51 pm ]
Post subject:  Re: ALGLIB and C++

Sergey.Bochkanov wrote:
SteSus85 wrote:
Monte-Carlo is not exact method, different values are returned every time you run it, and these values contain a lot of noise.
I use always the same seed for generation pseudo-random numbers for each computation. So if I compute integral two times with the same parameters on different computers I would have exactly the same value always.

The problem is in how your integral changes when you change integration area or some other parameter. "Exact" integral is smooth with respect to changes in the integrand, Monte Carlo integral is nonsmooth. I suppose that is the source of your problems.

Monte-Carlo integral is a sum of integrand-functions. So, if an integrand is smooth then Monte Carlo integral is also smooth. But in my case the integrand is unsmooth in certain point. So, I think you are right, unsmoothness can be a problem. I'll try to make the integrand smooth in that point!

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/