summaryrefslogtreecommitdiff
path: root/modules/optimization/demos/neldermead/neldermead_outputcmd.sce
blob: 6882b07d170d75f11bad31882114a576e8c10de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008-2009 - INRIA - Michael Baudin
// Copyright (C) 2010 - DIGITEO - Allan CORNET
// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt


function demo_outputcmd()

    function [ y , index ] = rosenbrock ( x , index )
        y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
    endfunction


    //
    // myoutputcmd --
    //  This command is called back by the Nelder-Mead
    //  algorithm.
    // Arguments
    //  state : the current state of the algorithm
    //    "init", "iter", "done"
    //  data : the data at the current state
    //    This is a tlist with the following entries:
    //    * x : the optimal vector of parameters
    //    * fval : the minimum function value
    //    * simplex : the simplex, as a simplex object
    //    * iteration : the number of iterations performed
    //    * funccount : the number of function evaluations
    // stop : set to %t to interrupt the algorithm
    //

    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
        simplex = data.simplex
        // Simplex is a data structure, which can be managed
        // by the optimsimplex class.
        ssize = optimsimplex_size ( simplex )
        mprintf ( "Iteration #%d, Feval #%d, Fval = %e, x = %s, Size = %e\n", iter, fc, fval, strcat(string(x)," "), ssize);
        stop = %f
    endfunction


    nm = neldermead_new ();
    nm = neldermead_configure(nm,"-numberofvariables",2);
    nm = neldermead_configure(nm,"-function",rosenbrock);
    nm = neldermead_configure(nm,"-x0",[-1.2 1.0]');
    nm = neldermead_configure(nm,"-maxiter",200);
    nm = neldermead_configure(nm,"-maxfunevals",350);
    nm = neldermead_configure(nm,"-tolfunrelative",10*%eps);
    nm = neldermead_configure(nm,"-tolxrelative",10*%eps);
    nm = neldermead_configure(nm,"-outputcommand",myoutputcmd);
    nm = neldermead_search(nm);
    nm = neldermead_destroy(nm);
    mprintf(_("End of demo.\n"));

    //
    // Load this script into the editor
    //
    m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal")
    if(m == 1)
        filename = "neldermead_outputcmd.sce";
        dname = get_absolute_file_path(filename);
        editor ( dname + filename, "readonly" );
    end
endfunction

demo_outputcmd()
clear demo_outputcmd;