~ubuntu-branches/ubuntu/raring/scilab/raring-proposed

« back to all changes in this revision

Viewing changes to modules/optimization/demos/neldermead/nmplot_quadratic.variable.sce

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-30 14:42:38 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20120830144238-c1y2og7dbm7m9nig
Tags: 5.4.0-beta-3-1~exp1
* New upstream release
* Update the scirenderer dep
* Get ride of libjhdf5-java dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2
 
// Copyright (C) 2008-2009 - INRIA - Michael Baudin
3
 
// Copyright (C) 2010 - DIGITEO - Allan CORNET
4
 
// Copyright (C) 2011 - DIGITEO - Michael Baudin
5
 
//
6
 
// This file must be used under the terms of the CeCILL.
7
 
// This source file is licensed as described in the file COPYING, which
8
 
// you should have received as part of this distribution.  The terms
9
 
// are also available at
10
 
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11
 
 
12
 
function demo_nmplot_qvariable()
13
 
 
14
 
  filename = 'nmplot_quadratic.variable.sce';
15
 
  dname = get_absolute_file_path(filename);
16
 
 
17
 
  mprintf(_("Illustrates that the variable-shape Nelder-Mead algorithm performs well on a quadratic test case.\n"));
18
 
  mprintf(_("Defining quadratic function ...\n"));
19
 
  function [ y , index ] = quadratic ( x , index )
20
 
    y = x(1)^2 + x(2)^2 - x(1) * x(2);
21
 
  endfunction
22
 
  function y = quadraticC ( x1 , x2 )
23
 
    y = quadratic ( [x1 , x2] , 2 )
24
 
  endfunction
25
 
 
26
 
  mprintf(_("Creating nmplot object...\n"));
27
 
  nm = nmplot_new ();
28
 
  nm = nmplot_configure(nm, "-numberofvariables",2);
29
 
  nm = nmplot_configure(nm, "-function",quadratic);
30
 
  nm = nmplot_configure(nm, "-x0",[2.0 2.0]');
31
 
  nm = nmplot_configure(nm, "-maxiter",100);
32
 
  nm = nmplot_configure(nm, "-maxfunevals",300);
33
 
  nm = nmplot_configure(nm, "-tolxmethod",%f);
34
 
  nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8);
35
 
  nm = nmplot_configure(nm, "-simplex0method","spendley");
36
 
  
37
 
  //
38
 
  // Setup output files
39
 
  //
40
 
  simplexfn = TMPDIR + filesep() + "history.simplex.txt";
41
 
  nm = nmplot_configure(nm, "-simplexfn",simplexfn);
42
 
  
43
 
  //
44
 
  // Perform optimization
45
 
  //
46
 
  mprintf(_("Searching (please wait) ...\n"));
47
 
  nm = nmplot_search(nm);
48
 
  //
49
 
  // Print a summary
50
 
  //
51
 
  exec(fullfile(dname,"nmplot_summary.sci"),-1);
52
 
  nmplot_summary(nm)
53
 
  //
54
 
  // Plot the contours of the cost function and the simplex history
55
 
  mprintf(_("Plotting contour (please wait) ...\n"));
56
 
  nm = nmplot_configure(nm, "-verbose", 0);
57
 
  xmin = -2.0 ; 
58
 
  xmax = 4.0 ; 
59
 
  ymin = -2.0 ; 
60
 
  ymax = 4.0 ; 
61
 
  nx = 50 ; 
62
 
  ny = 50;
63
 
  xdata=linspace(xmin,xmax,nx);
64
 
  ydata=linspace(ymin,ymax,ny);
65
 
  scf()
66
 
  drawlater();
67
 
  contour ( xdata , ydata , quadraticC , [0.1 1.0 2.0 5.0 10.0 15.0 20.0] )
68
 
  nmplot_simplexhistory ( nm );
69
 
  drawnow();
70
 
  
71
 
  // Clean-up
72
 
  deletefile(simplexfn);
73
 
  nm = nmplot_destroy(nm);
74
 
  mprintf(_("End of demo.\n"));
75
 
 
76
 
  //
77
 
  // Load this script into the editor
78
 
  //
79
 
  editor ( dname + filename, "readonly" );
80
 
 
81
 
endfunction
82
 
 
83
 
demo_nmplot_qvariable();
84
 
clear demo_nmplot_qvariable;
85
 
 
 
1
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 
2
// Copyright (C) 2008-2009 - INRIA - Michael Baudin
 
3
// Copyright (C) 2010 - DIGITEO - Allan CORNET
 
4
// Copyright (C) 2011 - DIGITEO - Michael Baudin
 
5
// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
 
6
//
 
7
// This file must be used under the terms of the CeCILL.
 
8
// This source file is licensed as described in the file COPYING, which
 
9
// you should have received as part of this distribution.  The terms
 
10
// are also available at
 
11
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
12
 
 
13
function demo_nmplot_qvariable()
 
14
 
 
15
    filename = 'nmplot_quadratic.variable.sce';
 
16
    dname = get_absolute_file_path(filename);
 
17
 
 
18
    mprintf(_("Illustrates that the variable-shape Nelder-Mead algorithm performs well on a quadratic test case.\n"));
 
19
    mprintf(_("Defining quadratic function ...\n"));
 
20
    function [ y , index ] = quadratic ( x , index )
 
21
        y = x(1)^2 + x(2)^2 - x(1) * x(2);
 
22
    endfunction
 
23
    function y = quadraticC ( x1 , x2 )
 
24
        y = quadratic ( [x1 , x2] , 2 )
 
25
    endfunction
 
26
 
 
27
    mprintf(_("Creating nmplot object...\n"));
 
28
    nm = nmplot_new ();
 
29
    nm = nmplot_configure(nm, "-numberofvariables",2);
 
30
    nm = nmplot_configure(nm, "-function",quadratic);
 
31
    nm = nmplot_configure(nm, "-x0",[2.0 2.0]');
 
32
    nm = nmplot_configure(nm, "-maxiter",100);
 
33
    nm = nmplot_configure(nm, "-maxfunevals",300);
 
34
    nm = nmplot_configure(nm, "-tolxmethod",%f);
 
35
    nm = nmplot_configure(nm, "-tolsimplexizerelative",1.e-8);
 
36
    nm = nmplot_configure(nm, "-simplex0method","spendley");
 
37
 
 
38
    //
 
39
    // Setup output files
 
40
    //
 
41
    simplexfn = TMPDIR + filesep() + "history.simplex.txt";
 
42
    nm = nmplot_configure(nm, "-simplexfn",simplexfn);
 
43
 
 
44
    //
 
45
    // Perform optimization
 
46
    //
 
47
    mprintf(_("Searching (please wait) ...\n"));
 
48
    nm = nmplot_search(nm);
 
49
    //
 
50
    // Print a summary
 
51
    //
 
52
    exec(fullfile(dname,"nmplot_summary.sci"),-1);
 
53
    nmplot_summary(nm)
 
54
    //
 
55
    // Plot the contours of the cost function and the simplex history
 
56
    mprintf(_("Plotting contour (please wait) ...\n"));
 
57
    nm = nmplot_configure(nm, "-verbose", 0);
 
58
    xmin = -2.0 ; 
 
59
    xmax = 4.0 ; 
 
60
    ymin = -2.0 ; 
 
61
    ymax = 4.0 ; 
 
62
    nx = 50 ; 
 
63
    ny = 50;
 
64
    xdata=linspace(xmin,xmax,nx);
 
65
    ydata=linspace(ymin,ymax,ny);
 
66
    scf()
 
67
    drawlater();
 
68
    contour ( xdata , ydata , quadraticC , [0.1 1.0 2.0 5.0 10.0 15.0 20.0] )
 
69
    nmplot_simplexhistory ( nm );
 
70
    drawnow();
 
71
    demo_viewCode(filename);
 
72
 
 
73
    // Clean-up
 
74
    deletefile(simplexfn);
 
75
    nm = nmplot_destroy(nm);
 
76
    mprintf(_("End of demo.\n"));
 
77
endfunction
 
78
 
 
79
demo_nmplot_qvariable();
 
80
clear demo_nmplot_qvariable;
 
81
 
 
82
 
 
83