# optimbase_outstruct

Returns a data structure with type T_OPTDATA.

## Calling Sequence

                data = optimbase_outstruct(opt)

## Argument

opt

  The object of TOPTIM type (tlist).

data

  A data structure with type T_OPTDATA containing at least the following fields.

  x

    The current optimum.

  fval

    The current function value.

  iteration

    The current iteration index.

  funccount

    The number of function evaluations.

## Description

The `optimbase_outstruct` function returns a data structure with T_OPTDATA type. This data structure contains basic optimization fields.

The output argument `data` is designed to be the input of the `optimbase_outputcmd` function which, in turn, calls back the output function. This data structure may be enriched by children (specialized) optimization methods.

## Example

    function [f,index] = fun(x, index)
        f = 2*x - 4;
    endfunction

    function stop = myoutputcmd(state, data)
        iter = data.iteration
        if ( state == "init" ) then
            mprintf ( "=================================\n");
            mprintf ( "Initialization\n");
        elseif ( state == "done" ) then
            mprintf ( "=================================\n");
            mprintf ( "End of Optimization\n");
        end
        fc = data.funccount
        fval = data.fval
        x = data.x
        mprintf ( "Iter. #%3d, Feval #%3d, Fval = %.1e, x = %s \n" , ..
        iter, fc, fval, strcat(string(x)," "));
        stop = %f
    endfunction

    a = -5;
    b = 5;
    x0 = (a+b)/2;;

    // Creates the object
    opt = optimbase_new();

    // Configures the object
    opt = optimbase_configure(opt,"-numberofvariables",2);
    opt = optimbase_configure(opt, "-x0", x0);
    opt = optimbase_configure(opt, "-tolxrelative", 10*%eps);
    opt = optimbase_configure(opt, "-maxiter", 100);
    opt = optimbase_configure(opt, "-function", fun);
    opt = optimbase_configure(opt,"-outputcommand", myoutputcmd);

    function x = Dicho(opt,a,b)
        xk = optimbase_cget(opt, "-x0");
        [opt, fx0,index] = optimbase_function (opt , xk , 4);
        opt = optimbase_set ( opt , "-fx0" , fx0 );
        opt = optimbase_set ( opt , "-xopt" , x0 );
        opt = optimbase_set ( opt , "-fopt" , fx0 );

        // OutputCmd
        brutedata = optimbase_outstruct ( opt );
        data = tlist(["T_OPTBDATA",...
        "x","fval","iteration","funccount"]);
        data.x = brutedata.x;
        data.fval = brutedata.fval;
        data.iteration = brutedata.iteration;
        data.funccount = brutedata.funccount;
        stop = optimbase_outputcmd (opt , "init" , data );

        while optimbase_get(opt, "-iterations") <optimbase_cget(opt, "-maxiter")
            [opt, f,index] = optimbase_function(opt, xk, 4);
            [opt, g, index] = optimbase_function(opt, a, 4);
            if g*f <=0 then
               b = xk;
            else
                a = xk;
            end
            x = (a+b)/2;
            opt = optimbase_incriter(opt);
            opt = optimbase_set ( opt , "-xopt" , x );
            opt = optimbase_set ( opt , "-fopt" , f );
            temp = abs(x-xk);

            if temp < optimbase_cget(opt, "-tolxrelative")
                // OutputCmd
                brutedata = optimbase_outstruct ( opt );
                data = tlist(["T_OPTBDATA",...
                "x","fval","iteration","funccount"]);
                data.x = brutedata.x;
                data.fval = brutedata.fval;
                data.iteration = brutedata.iteration;
                data.funccount = brutedata.funccount;
                stop = optimbase_outputcmd (opt , "done" , data );
                break
            end
            xk = x;

            // OutputCmd
            brutedata = optimbase_outstruct ( opt );
            data = tlist(["T_OPTBDATA",...
            "x","fval","iteration","funccount"]);
            data.x = brutedata.x;
            data.fval = brutedata.fval;
            data.iteration = brutedata.iteration;
            data.funccount = brutedata.funccount;
            stop = optimbase_outputcmd (opt , "iter" , data );
        end

    endfunction
    x = Dicho(opt,a,b)

## See Also

- optimbase_outputcmd
- optimbase_function