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

« back to all changes in this revision

Viewing changes to modules/optimization/demos/neldermead/neldermead_boxpost.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 - Michael Baudin
4
 
// Copyright (C) 2010 - DIGITEO - Allan CORNET
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
 
//
13
 
// nmplot_boxpost.sce --
14
 
//   Show that the Box algorithm is able to reproduce the 
15
 
//   numerical experiment presented in Richardson and Kuester's paper.
16
 
//   Rosenbrock's Post Office
17
 
//
18
 
 
19
 
function demo_boxpost()
20
 
 
21
 
  filename = 'neldermead_boxpost.sce';
22
 
  dname = get_absolute_file_path(filename);
23
 
 
24
 
  mprintf(_("Illustrates Box'' algorithm on Rosenbrock''s Post Office Problem.\n"));
25
 
  mprintf(_("Defining Rosenbrock Post Office function...\n"));
26
 
 
27
 
  //
28
 
  //  Reference:
29
 
  //
30
 
  //    Algorithm 454
31
 
  //    The complex method for constrained
32
 
  //    optimization
33
 
  //    Richardson, Kuester
34
 
  //    1971
35
 
  //
36
 
  //    An automatic method for finding the
37
 
  //    greatest or least value of a function
38
 
  //    Rosenbrock
39
 
  //    1960
40
 
  //
41
 
  //   Richardson and Kuester Results :
42
 
  //   F=3456
43
 
  //   X1 = 24.01
44
 
  //   X2 = 12.00
45
 
  //   X3 = 12.00
46
 
  //   Iterations : 72
47
 
  //
48
 
  //
49
 
 
50
 
  //
51
 
  // fpostoffice --
52
 
  //   Computes the Post Office cost function and 
53
 
  //   inequality constraints.
54
 
  //
55
 
  // Arguments
56
 
  //   x: the point where to compute the function
57
 
  //   index : the stuff to compute
58
 
  //
59
 
  
60
 
  function [ f , c , index ] = fpostoffice ( x , index )
61
 
    f = []
62
 
    c = []
63
 
    if ( index==2 | index==6 ) then
64
 
      f = -x(1) * x(2) * x(3)
65
 
    end
66
 
    
67
 
    if ( index==5 | index==6 ) then
68
 
      c1 = x(1) + 2 * x(2) + 2 * x(3)
69
 
      c2 = 72 - c1
70
 
      c = [c1 c2]
71
 
    end
72
 
  endfunction
73
 
  //
74
 
  // Initialize the random number generator, so that the results are always the
75
 
  // same.
76
 
  //
77
 
  rand("seed" , 0)
78
 
 
79
 
  x0 = [1.0 1.0 1.0].';
80
 
  // Compute f(x0) : should be close to -1
81
 
  fx0 = fpostoffice ( x0 , 2 );
82
 
  mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -1 );
83
 
  [ fx0 , cx0, index ] = fpostoffice ( x0 , 6 );
84
 
  mprintf("Computed Constraints(x0) = [%e %e]\n", ..
85
 
    cx0(1), cx0(2) );
86
 
  mprintf("Expected Constraints(x0) = [%e %e]\n", ..
87
 
    5 , 67 );
88
 
 
89
 
  xopt = [24 12 12].';
90
 
  // Compute f(xopt) : should be 3456
91
 
  fopt = fpostoffice ( xopt );
92
 
  mprintf("Computed fopt = %e (expected = %e)\n", fopt , -3456 );
93
 
 
94
 
  nm = neldermead_new ();
95
 
  nm = neldermead_configure(nm,"-numberofvariables",3);
96
 
  nm = neldermead_configure(nm,"-function",fpostoffice);
97
 
  nm = neldermead_configure(nm,"-x0",x0);
98
 
  nm = neldermead_configure(nm,"-maxiter",300);
99
 
  nm = neldermead_configure(nm,"-maxfunevals",300);
100
 
  nm = neldermead_configure(nm,"-method","box");
101
 
  nm = neldermead_configure(nm,"-verbose",1);
102
 
  logfile = TMPDIR + "/postoffice.txt";
103
 
  nm = neldermead_configure(nm,"-logfile" , logfile );
104
 
  nm = neldermead_configure(nm,"-verbosetermination",1);
105
 
  nm = neldermead_configure(nm,"-boundsmin",[0.0 0.0 0.0]);
106
 
  nm = neldermead_configure(nm,"-boundsmax",[42.0 42.0 42.0]);
107
 
  // Configure like Box
108
 
  nm = neldermead_configure(nm,"-simplex0method","randbounds");
109
 
  nm = neldermead_configure(nm,"-nbineqconst",2);
110
 
  nm = neldermead_configure(nm,"-tolxmethod" , %f );
111
 
  nm = neldermead_configure(nm,"-tolsimplexizemethod",%f);
112
 
  nm = neldermead_configure(nm,"-boxtermination" , %t );
113
 
  nm = neldermead_configure(nm,"-boxtolf" , 0.001 );
114
 
  nm = neldermead_configure(nm,"-boxboundsalpha" , 0.0001 );
115
 
 
116
 
  //
117
 
  // Check that the cost function is correctly connected.
118
 
  //
119
 
  [ nm , result ] = neldermead_function ( nm , x0 );
120
 
  
121
 
  //
122
 
  // Perform optimization
123
 
  //
124
 
  mprintf(_("Searching (please wait) ...\n"));
125
 
  nm = neldermead_search(nm);
126
 
  //
127
 
  // Print a summary
128
 
  //
129
 
  exec(fullfile(dname,"neldermead_summary.sci"),-1);
130
 
  neldermead_summary(nm)
131
 
  mprintf("==========================\n");
132
 
  xcomp = neldermead_get(nm,"-xopt");
133
 
  mprintf("x expected = [%s]\n",strcat(string(xopt)," "));
134
 
  shift = norm(xcomp-xopt)/norm(xopt);
135
 
  mprintf("Shift = %f\n",shift);
136
 
  fcomp = neldermead_get(nm,"-fopt");
137
 
  mprintf("f expected = %f\n",fopt);
138
 
  shift = abs(fcomp-fopt)/abs(fopt);
139
 
  mprintf("Shift = %f\n",shift);
140
 
  nm = neldermead_destroy(nm);
141
 
  deletefile ( logfile )
142
 
  mprintf(_("End of demo.\n"));
143
 
 
144
 
  //
145
 
  // Load this script into the editor
146
 
  //
147
 
  editor ( dname + filename, "readonly" );
148
 
 
149
 
endfunction
150
 
 
151
 
demo_boxpost();
152
 
clear demo_boxpost;
 
 
b'\\ No newline at end of file'
 
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 - Michael Baudin
 
4
// Copyright (C) 2010 - DIGITEO - Allan CORNET
 
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
//
 
14
// nmplot_boxpost.sce --
 
15
//   Show that the Box algorithm is able to reproduce the 
 
16
//   numerical experiment presented in Richardson and Kuester's paper.
 
17
//   Rosenbrock's Post Office
 
18
//
 
19
 
 
20
function demo_boxpost()
 
21
 
 
22
    filename = 'neldermead_boxpost.sce';
 
23
    dname = get_absolute_file_path(filename);
 
24
 
 
25
    mprintf(_("Illustrates Box'' algorithm on Rosenbrock''s Post Office Problem.\n"));
 
26
    mprintf(_("Defining Rosenbrock Post Office function...\n"));
 
27
 
 
28
    //
 
29
    //  Reference:
 
30
    //
 
31
    //    Algorithm 454
 
32
    //    The complex method for constrained
 
33
    //    optimization
 
34
    //    Richardson, Kuester
 
35
    //    1971
 
36
    //
 
37
    //    An automatic method for finding the
 
38
    //    greatest or least value of a function
 
39
    //    Rosenbrock
 
40
    //    1960
 
41
    //
 
42
    //   Richardson and Kuester Results :
 
43
    //   F=3456
 
44
    //   X1 = 24.01
 
45
    //   X2 = 12.00
 
46
    //   X3 = 12.00
 
47
    //   Iterations : 72
 
48
    //
 
49
    //
 
50
 
 
51
    //
 
52
    // fpostoffice --
 
53
    //   Computes the Post Office cost function and 
 
54
    //   inequality constraints.
 
55
    //
 
56
    // Arguments
 
57
    //   x: the point where to compute the function
 
58
    //   index : the stuff to compute
 
59
    //
 
60
 
 
61
    function [ f , c , index ] = fpostoffice ( x , index )
 
62
        f = []
 
63
        c = []
 
64
        if ( index==2 | index==6 ) then
 
65
            f = -x(1) * x(2) * x(3)
 
66
        end
 
67
 
 
68
        if ( index==5 | index==6 ) then
 
69
            c1 = x(1) + 2 * x(2) + 2 * x(3)
 
70
            c2 = 72 - c1
 
71
            c = [c1 c2]
 
72
        end
 
73
    endfunction
 
74
    //
 
75
    // Initialize the random number generator, so that the results are always the
 
76
    // same.
 
77
    //
 
78
    rand("seed" , 0)
 
79
 
 
80
    x0 = [1.0 1.0 1.0].';
 
81
    // Compute f(x0) : should be close to -1
 
82
    fx0 = fpostoffice ( x0 , 2 );
 
83
    mprintf("Computed fx0 = %e (expected = %e)\n",fx0 , -1 );
 
84
    [ fx0 , cx0, index ] = fpostoffice ( x0 , 6 );
 
85
    mprintf("Computed Constraints(x0) = [%e %e]\n", ..
 
86
    cx0(1), cx0(2) );
 
87
    mprintf("Expected Constraints(x0) = [%e %e]\n", ..
 
88
    5 , 67 );
 
89
 
 
90
    xopt = [24 12 12].';
 
91
    // Compute f(xopt) : should be 3456
 
92
    fopt = fpostoffice ( xopt );
 
93
    mprintf("Computed fopt = %e (expected = %e)\n", fopt , -3456 );
 
94
 
 
95
    nm = neldermead_new ();
 
96
    nm = neldermead_configure(nm,"-numberofvariables",3);
 
97
    nm = neldermead_configure(nm,"-function",fpostoffice);
 
98
    nm = neldermead_configure(nm,"-x0",x0);
 
99
    nm = neldermead_configure(nm,"-maxiter",300);
 
100
    nm = neldermead_configure(nm,"-maxfunevals",300);
 
101
    nm = neldermead_configure(nm,"-method","box");
 
102
    nm = neldermead_configure(nm,"-verbose",1);
 
103
    logfile = TMPDIR + "/postoffice.txt";
 
104
    nm = neldermead_configure(nm,"-logfile" , logfile );
 
105
    nm = neldermead_configure(nm,"-verbosetermination",1);
 
106
    nm = neldermead_configure(nm,"-boundsmin",[0.0 0.0 0.0]);
 
107
    nm = neldermead_configure(nm,"-boundsmax",[42.0 42.0 42.0]);
 
108
    // Configure like Box
 
109
    nm = neldermead_configure(nm,"-simplex0method","randbounds");
 
110
    nm = neldermead_configure(nm,"-nbineqconst",2);
 
111
    nm = neldermead_configure(nm,"-tolxmethod" , %f );
 
112
    nm = neldermead_configure(nm,"-tolsimplexizemethod",%f);
 
113
    nm = neldermead_configure(nm,"-boxtermination" , %t );
 
114
    nm = neldermead_configure(nm,"-boxtolf" , 0.001 );
 
115
    nm = neldermead_configure(nm,"-boxboundsalpha" , 0.0001 );
 
116
 
 
117
    //
 
118
    // Check that the cost function is correctly connected.
 
119
    //
 
120
    [ nm , result ] = neldermead_function ( nm , x0 );
 
121
 
 
122
    //
 
123
    // Perform optimization
 
124
    //
 
125
    mprintf(_("Searching (please wait) ...\n"));
 
126
    nm = neldermead_search(nm);
 
127
    //
 
128
    // Print a summary
 
129
    //
 
130
    exec(fullfile(dname,"neldermead_summary.sci"),-1);
 
131
    neldermead_summary(nm)
 
132
    mprintf("==========================\n");
 
133
    xcomp = neldermead_get(nm,"-xopt");
 
134
    mprintf("x expected = [%s]\n",strcat(string(xopt)," "));
 
135
    shift = norm(xcomp-xopt)/norm(xopt);
 
136
    mprintf("Shift = %f\n",shift);
 
137
    fcomp = neldermead_get(nm,"-fopt");
 
138
    mprintf("f expected = %f\n",fopt);
 
139
    shift = abs(fcomp-fopt)/abs(fopt);
 
140
    mprintf("Shift = %f\n",shift);
 
141
    nm = neldermead_destroy(nm);
 
142
    deletefile ( logfile )
 
143
    mprintf(_("End of demo.\n"));
 
144
 
 
145
    //
 
146
    // Load this script into the editor
 
147
    //
 
148
    m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal")
 
149
    if(m == 1)
 
150
        editor ( dname + filename, "readonly" );
 
151
    end 
 
152
endfunction
 
153
 
 
154
demo_boxpost();
 
155
clear demo_boxpost;
 
156
 
 
157
 
 
158
 
 
159
 
 
160
 
 
161
 
 
162
 
 
163
 
 
164