File: optim_scilabscript.tst

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (212 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// =============================================================================
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008 - INRIA - Michael Baudin
// Copyright (C) 2009 - INRIA - Michael Baudin
//
//  This file is distributed under the same license as the Scilab package.
// =============================================================================

// <-- JVM NOT MANDATORY -->
// <-- ENGLISH IMPOSED -->
// <-- NO CHECK REF -->
//
// Do not check ref, because imp option create a output messages
// which contains lots of floating point values which may
// be slightly different across platforms, without being bugs.
//
// optim_script.tst --
//   Test the optim command with the Rosenbrock test case 
//   in the case where the cost function is a Scilab function.
//   The expected solution is x=(1,...,1) where f(x)=0
//
//
// assert_close --
//   Returns 1 if the two real matrices computed and expected are close,
//   i.e. if the relative distance between computed and expected is lesser than epsilon.
// Arguments
//   computed, expected : the two matrices to compare
//   epsilon : a small number
//
function flag = assert_close ( computed, expected, epsilon )
  if expected==0.0 then
    shift = norm(computed-expected);
  else
    shift = norm(computed-expected)/norm(expected);
  end
  if shift < epsilon then
    flag = 1;
  else
    flag = 0;
  end
  if flag <> 1 then pause,end
endfunction

Leps=1.e-3;
bs=[5 5];
bi=-bs;
x0=[-1.2 1];
xopt=[1 1];
fopt = 0.0;
gopt = 0.0;
function [ f , g , ind ] = rosenbrock ( x , ind )
  if ind == 2 | ind == 4 then
    f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
  end
  if ind == 2 | ind == 4 then
    g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
    g(2) = 200. * ( x(2) - x(1)**2 )
  end
endfunction
//
// Configure the test so that verbose message do not interrupt the test.
lines(0);
//
// 1. Test unconstrained BFGS
// Test without arguments
[f,x,g,tr]=optim(rosenbrock,x0);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function
[f,x,g]=optim(rosenbrock,x0,'qn','ar',50);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with hot-restart array tr
[f,x,g,tr]=optim(rosenbrock,x0,'qn','ar',50);
[f,x,g]=optim(rosenbrock,x0,tr);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function, maximum number of iterations
[f,x,g]=optim(rosenbrock,x0,'qn','ar', 50 , 100 );
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with various imp levels
// imp = 0 : No messages
[f,x,g]=optim(rosenbrock,x0,'qn',imp=0);
// imp = 1 : one line at start, one line at end
[f,x,g]=optim(rosenbrock,x0,'qn',imp=1);
// imp = 2 : one line by iteration
[f,x,g]=optim(rosenbrock,x0,'qn',imp=2);
// imp = 3 : one line by iteration + one line by line search
[f,x,g]=optim(rosenbrock,x0,'qn',imp=3);
// Negative imp : call back the cost function with ind = 0 each -imp iteration
[f,x,g]=optim(rosenbrock,x0,'qn',imp=-5);
//
// 2. Test unconstrained Limited Memory BFGS
[f,x,g]=optim(rosenbrock,x0,'gc');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
[f,x,g]=optim(rosenbrock,x0,'gc','ar',50);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with various imp levels
// imp = 0 : No messages
[f,x,g]=optim(rosenbrock,x0,'gc',imp=0);
// imp = 1,2 : one line at start, one line at end
[f,x,g]=optim(rosenbrock,x0,'gc',imp=1);
// imp = 3 : one line by iteration
[f,x,g]=optim(rosenbrock,x0,'gc',imp=2);
// imp = 4 : one line by iteration + one line by line search
[f,x,g]=optim(rosenbrock,x0,'gc',imp=3);
//
// 3. Test unconstrained non-differentiable method
[f,x,g]=optim(rosenbrock,x0,'nd');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function
[f,x,g]=optim(rosenbrock,x0,'nd','ar',100);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function, maximum number of iterations
[f,x,g]=optim(rosenbrock,x0,'nd','ar', 100 , 100 );
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with various imp levels
// imp = 0 : No messages
[f,x,g]=optim(rosenbrock,x0,'nd',imp=0);
// imp = 1,2 : one line at start, one line at end
[f,x,g]=optim(rosenbrock,x0,'nd',imp=1);
// imp = 3 : one line by iteration
[f,x,g]=optim(rosenbrock,x0,'nd',imp=2);
// imp = 4 : one line by iteration + one line by line search
[f,x,g]=optim(rosenbrock,x0,'nd',imp=3);

//
// 4. Test bound-constrained BFGS
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn','ar',50);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function, maximum number of iterations
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn','ar', 100 , 100 );
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with various imp levels
// imp = 0 : No messages
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn',imp=0);
// imp = 1,2 : one line at start, one line at end
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn',imp=1);
// imp = 3 : one line by iteration
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn',imp=2);
// imp = 4 : one line by iteration + one line by line search
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'qn',imp=3);
//
// 5. Test bound-constrained Limited Memory BFGS
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc','ar',100);
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with maximum number of call to cost function, maximum number of iterations
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc','ar', 100 , 100 );
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
// Test with various imp levels
// imp = 0 : No messages
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc',imp=0);
// imp = 1,2 : one line at start, one line at end
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc',imp=1);
// imp = 3 : one line by iteration
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc',imp=2);
// imp = 4 : one line by iteration + one line by line search
[f,x,g]=optim(rosenbrock,'b',bi,bs,x0,'gc',imp=3);
//
// 6. Test "sd", "si", "td", "ti" options
[f,x,g,td]=optim(rosenbrock,x0,'sd');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
[f,x,g,ti]=optim(rosenbrock,x0,'si');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
[f,x,g,ti,td]=optim(rosenbrock,x0,'si','sd');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );
[f,x,g,ti,td]=optim(rosenbrock,x0,"ti",[2 2 2],"td",[1.1 1.1 1.1],'si','sd');
assert_close ( x , xopt , Leps );
assert_close ( f , fopt , Leps );
assert_close ( g , gopt , Leps );