Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.5.0 - Português

Change language to:
English - Français - 日本語 - Русский

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Ajuda do Scilab >> Otimização e Simulação > Optimization base > optimbase_outputcmd

optimbase_outputcmd

Calls back user's output command.

Calling Sequence

stop = optimbase_outputcmd(opt, state, data)

Argument

opt

The object of TOPTIM type (tlist).

state

A 1-by-1 matrix of strings, the current state of the algorithm.

Available values are "init, "iter", "done".

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.

stop

A 1-by-1 matrix of booleans.

stop is %t if the optimization algorithm must be stopped.

stop is %f if the optimization algorithm must continue.

Description

The optimbase_outputcmd function calls back user's output command. The -outputcommand option allwos to configure a command which is called back at the start of the optimization, at each iteration and at the end of the optimization.

It might happen that the output function requires additional arguments to be evaluated. In this case, we can use the following feature. The outputcmd argument can also be the list (outf, a1, a2, ...). In this case outf, the first element in the list, must be a function and must have the header :

stop = outf(state, data, a1, a2, ....)

where the input arguments a1, a2, ... are automatically appended at the end of the calling sequence.

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

Report an issue
<< optimbase_new Optimization base optimbase_outstruct >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Fri Apr 11 14:18:13 CEST 2014