Deleted vectorized computation feature. Deleted neldermead_contour. Fixed the demos.
[scilab.git] / scilab / modules / optimization / demos / neldermead / nmplot_quadratic.fixed.sce
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10
11 mprintf("Illustrates that the fixed-shape Spendley et al. algorithm performs well on a quadratic test case.\n");
12 mprintf("Defining quadratic function...\n");
13 function y = quadratic (x)
14   y = x(1)^2 + x(2)^2 - x(1) * x(2);
15 endfunction
16
17 mprintf("Creating nmplot object...\n");
18 nm = nmplot_new ();
19 nm = nmplot_configure(nm,"-numberofvariables",2);
20 nm = nmplot_configure(nm,"-function",quadratic);
21 nm = nmplot_configure(nm,"-x0",[2.0 2.0]');
22 nm = nmplot_configure(nm,"-maxiter",100);
23 nm = nmplot_configure(nm,"-maxfunevals",300);
24 nm = nmplot_configure(nm,"-tolxmethod","disabled");
25 nm = nmplot_configure(nm,"-tolsimplexizerelative",1.e-8);
26 nm = nmplot_configure(nm,"-simplex0method","spendley");
27 nm = nmplot_configure(nm,"-method","fixed");
28 nm = nmplot_configure(nm,"-verbose",1);
29 nm = nmplot_configure(nm,"-verbosetermination",0);
30 //
31 // Setup output files
32 //
33 nm = nmplot_configure(nm,"-simplexfn","rosenbrock.fixed.history.simplex.txt");
34 nm = nmplot_configure(nm,"-fbarfn","rosenbrock.fixed.history.fbar.txt");
35 nm = nmplot_configure(nm,"-foptfn","rosenbrock.fixed.history.fopt.txt");
36 nm = nmplot_configure(nm,"-sigmafn","rosenbrock.fixed.history.sigma.txt");
37 //
38 // Perform optimization
39 //
40 mprintf("Searching for minimum...\n");
41 nm = nmplot_search(nm);
42 nmplot_display(nm);
43 // Plot various histories
44 mprintf("Plotting history of fbar...\n");
45 f = scf();
46 nmplot_historyplot ( nm , "rosenbrock.fixed.history.fbar.txt" , ...
47   mytitle = "Function Value Average" , myxlabel = "Iterations" );
48 mprintf("Plotting history of fopt...\n");
49 f = scf();
50 nmplot_historyplot ( nm , "rosenbrock.fixed.history.fopt.txt" , ...
51   mytitle = "Logarithm Minimum Function Value" , myxlabel = "Iterations" );
52 f.children.log_flags = "nln";
53 newticks = tlist(["ticks","locations","labels"]);
54 newticks.labels = ["1.e-20" "1.e-10" "1.e-1"];
55 newticks.locations = [1.e-20 1.e-10 1.e-1];
56 f.children.y_ticks = newticks;
57 f.children.children(1).children.mark_mode = "on";
58 f.children.children(1).children.mark_style = 9;
59 mprintf("Plotting history of sigma...\n");
60 f = scf();
61 nmplot_historyplot ( nm , "rosenbrock.fixed.history.sigma.txt" , ...
62   mytitle = "Logarithm Maximum Oriented length" , myxlabel = "Iterations" );
63 f.children.log_flags = "nln";
64 f.children.y_ticks = newticks;
65 f.children.children(1).children.mark_mode = "on";
66 f.children.children(1).children.mark_style = 9;
67 // Plot the contours of the cost function and the simplex history
68 mprintf("Plotting contour...\n");
69 nm = nmplot_configure(nm,"-verbose",0);
70 [nm , xdata , ydata , zdata ] = nmplot_contour ( nm , xmin = -2.0 , xmax = 4.0 , ymin = -2.0 , ymax = 4.0 , nx = 100 , ny = 100 );
71 f = scf();
72 drawlater();
73 contour ( xdata , ydata , zdata , [0.1 1.0 2.0 5.0 10.0 15.0 20.0] )
74 nmplot_simplexhistory ( nm );
75 drawnow();
76 // Clean-up
77 deletefile("rosenbrock.fixed.history.simplex.txt");
78 deletefile("rosenbrock.fixed.history.fbar.txt");
79 deletefile("rosenbrock.fixed.history.fopt.txt");
80 deletefile("rosenbrock.fixed.history.sigma.txt");
81 nm = nmplot_destroy(nm);
82
83