summaryrefslogtreecommitdiff
path: root/modules/optimization/demos/neldermead/nmplot_rosenbrock.sce
blob: 7501008d7fe8b7bfc3eac4e0ac3e9697d5499947 (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
// 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) 2011 - DIGITEO - Michael Baudin
// 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_nmplot_rosenvar()

    filename = "nmplot_rosenbrock.sce";
    dname = get_absolute_file_path(filename);
    mprintf("Illustrates the Nelder-Mead algorithm on Rosenbrock test case.\n");
    mprintf("Defining Rosenbrock function...\n");
    function [ y , index ] = rosenbrock ( x , index )
        y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
    endfunction
    function [ y , index ] = rosenbrockC ( x1 , x2 )
        y = rosenbrock ( [x1 , x2] , 2 )
    endfunction

    mprintf("Creating nmplot object...\n");
    nm = nmplot_new ();
    nm = nmplot_configure(nm,"-numberofvariables",2);
    nm = nmplot_configure(nm,"-function",rosenbrock);
    nm = nmplot_configure(nm,"-x0",[-1.2 1.0]');
    nm = nmplot_configure(nm,"-maxiter",200);
    nm = nmplot_configure(nm,"-maxfunevals",300);
    nm = nmplot_configure(nm,"-tolfunrelative",10*%eps);
    nm = nmplot_configure(nm,"-tolxrelative",10*%eps);
    //
    // Setup output files
    //
    simplexfn = TMPDIR + filesep() + "history.simplex.txt";
    nm = nmplot_configure(nm,"-simplexfn",simplexfn);
    //
    // Perform optimization
    //
    mprintf("Searching (please wait)...\n");
    nm = nmplot_search(nm);
    //
    // Print a summary
    //
    exec(fullfile(dname,"nmplot_summary.sci"),-1);
    nmplot_summary(nm)
    //
    // Plot the contours of the cost function and the simplex history
    mprintf("Plotting contour (please wait)...\n");
    xmin = -2.0;
    xmax = 2.0 ;
    ymin = -1.0 ;
    ymax = 2.5 ;
    nx = 50 ;
    ny = 50;
    xdata=linspace(xmin,xmax,nx);
    ydata=linspace(ymin,ymax,ny);
    scf();
    drawlater();
    contour ( xdata , ydata , rosenbrockC , [2 10 100 500 1000 2000] )
    nmplot_simplexhistory ( nm );
    drawnow();
    demo_viewCode(filename);
    //
    // Cleanup
    deletefile(simplexfn);
    nm = nmplot_destroy(nm);
    mprintf("End of demo.\n");
endfunction

demo_nmplot_rosenvar()
clear demo_nmplot_rosenvar;