forum.alglib.net

ALGLIB forum
It is currently Thu Mar 28, 2024 12:00 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Levenberg Max. State.NeedF and NeedFiJ deprecated?
PostPosted: Mon Nov 13, 2017 1:48 pm 
Offline

Joined: Wed Mar 20, 2013 11:21 am
Posts: 4
Hi everyone:

I used old AlgLib versions (when source code was provided), where Levenberg functions were used like this:

Code:

minlm_fj2 example

var
    State : MinLMState;
    Rep : MinLMReport;
    I : AlglibInteger;
    S : TReal1DArray;
    X : TReal1DArray;
    Y : TReal1DArray;
    FI : Double;
    N : AlglibInteger;
    M : AlglibInteger;
begin
   
    //
    // Example of solving polynomial approximation task using FJ scheme.
    //
    // Data points:
    //     xi are random numbers from [-1,+1],
    //
    // Function being fitted:
    //     yi = exp(xi) - sin(xi) - x^3/3
    //
    // Function being minimized:
    //     F(a,b,c) =
    //         (a + b*x0 + c*x0^2 - y0)^2 +
    //         (a + b*x1 + c*x1^2 - y1)^2 + ...
    //
    N := 3;
    SetLength(S, N);
    I:=0;
    while I<=N-1 do
    begin
        S[I] := RandomReal-0.5;
        Inc(I);
    end;
    M := 100;
    SetLength(X, M);
    SetLength(Y, M);
    I:=0;
    while I<=M-1 do
    begin
        X[I] := AP_Double(2*I)/(M-1)-1;
        Y[I] := Exp(X[I])-Sin(X[I])-X[I]*X[I]*X[I]/3;
        Inc(I);
    end;
   
    //
    // Now S stores starting point, X and Y store points being fitted.
    //
    MinLMCreateFJ(N, M, S, State);
    MinLMSetCond(State, 0.0, 0.0, 0.001, 0);
    while MinLMIteration(State) do
    begin
        if State.NeedF then
        begin
            State.F := 0;
        end;
        I:=0;
        while I<=M-1 do
        begin
           
            //
            // "a" is stored in State.X[0]
            // "b" - State.X[1]
            // "c" - State.X[2]
            //
            FI := State.X[0]+State.X[1]*X[I]+State.X[2]*AP_Sqr(X[I])-Y[I];
            if State.NeedF then
            begin
               
                //
                // F is equal to sum of fi squared.
                //
                State.F := State.F+AP_Sqr(FI);
            end;
            if State.NeedFiJ then
            begin
               
                //
                // Fi
                //
                State.Fi[I] := FI;
               
                //
                // dFi/da
                //
                State.J[I,0] := 1;
               
                //
                // dFi/db
                //
                State.J[I,1] := X[I];
               
                //
                // dFi/dc
                //
                State.J[I,2] := AP_Sqr(X[I]);
            end;
            Inc(I);
        end;
    end;
    MinLMResults(State, S, Rep);
   
    //
    // output results
    //
    Write(Format('A = %4.2f'#13#10'',[
        S[0]]));
    Write(Format('B = %4.2f'#13#10'',[
        S[1]]));
    Write(Format('C = %4.2f'#13#10'',[
        S[2]]));
    Write(Format('TerminationType = %0d (should be 2 - stopping when step is small enough)'#13#10'',[
        Rep.TerminationType]));
end.


However, in dll version I can?t use this code anymore, since the State.NeedF or State.NeedFiJ are deprecated (not recognized).
How could I get the same results with new versions? I guess I should use minlmoptimize function, but I don?t know how to do that since examples provided are not suitable for my needs.

Thank you in advance


Top
 Profile  
 
 Post subject: Re: Levenberg Max. State.NeedF and NeedFiJ deprecated?
PostPosted: Tue Nov 14, 2017 8:48 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 903
1. Yes, you can get same results with new version - although you can't access optimizer internals like in old unsupported ALGLIB version. Can you tell me what is wrong with examples and why you can't apply them to your task?

From what I see, it is pretty standard optimization process which is well covered by examples in manual.

2. Just for your information. If you want to perform polynomial fitting, it may be easier to use http://www.alglib.net/translator/man/manual.delphi.html#sub_polynomialfit followed by conversion from barycentic representation into power basis.


Top
 Profile  
 
 Post subject: Re: Levenberg Max. State.NeedF and NeedFiJ deprecated?
PostPosted: Thu Nov 16, 2017 8:18 am 
Offline

Joined: Wed Mar 20, 2013 11:21 am
Posts: 4
Thanks for the reply!

I already managed to get it using the minlmoptimize(state, function1_fvec, function1_jac, nil, MyMatrixWithValues).
I just had to create the function1_fvec and function1_jac (with its derivatives) and pass them my own Matrix.

I am not trying to perform polynomial fitting, but finding "peaks" in a wave. So I need Levenberg because I am just looking for gaussians or gaussians group.
(The example I provided in the first post is one sample included in old alglib versions, but the structure was similar to the one I needed)

Now everything is working as expected.

Thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group