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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008-2009 - INRIA - Michael Baudin
//
// 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-en.txt
//
// 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 bugmes();quit;end
endfunction
//
// assert_equal --
// Returns 1 if the two real matrices computed and expected are equal.
// Arguments
// computed, expected : the two matrices to compare
// epsilon : a small number
//
function flag = assert_equal ( computed , expected )
if computed==expected then
flag = 1;
else
flag = 0;
end
if flag <> 1 then bugmes();quit;end
endfunction
function [ y , index ] = rosenbrock ( x , index )
y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
endfunction
//
// myoutputcmd --
// This command is called back by the Optimization
// algorithm.
// Arguments
// state : the current state of the algorithm
// "init", "iter", "done"
// data : the data at the current state
// This is a tlist with the following entries:
// * x : the optimal vector of parameters
// * fval : the minimum function value
// * iteration : the number of iterations performed
// * funccount : the number of function evaluations
//
function myoutputcmd ( state , data )
global _OUTPUCMDFLAG_
// Unload the array, just to make sure that the minimum is there
iter = data.iteration
fc = data.funccount
fval = data.fval
x = data.x
_OUTPUCMDFLAG_ = 1
endfunction
global _OUTPUCMDFLAG_
_OUTPUCMDFLAG_ = 0
_OUTPUCMDFLAG_ =
0.
//
// myoutputcmd2 --
// This command is called back by the Optimization
// algorithm.
// Arguments
// state : the current state of the algorithm
// "init", "iter", "done"
// data : the data at the current state
// This is a tlist with the following entries:
// * x : the optimal vector of parameters
// * fval : the minimum function value
// * iteration : the number of iterations performed
// * funccount : the number of function evaluations
// myobj : a user-defined data structure
//
function myoutputcmd2 ( state , data , myobj )
global _OUTPUCMDFLAG_
// Unload the array, just to make sure that the minimum is there
iter = data.iteration
fc = data.funccount
fval = data.fval
x = data.x
_OUTPUCMDFLAG_ = myobj.myarg
endfunction
global _OUTPUCMDFLAG_
_OUTPUCMDFLAG_ = 0
_OUTPUCMDFLAG_ =
0.
myobj = tlist(["T_MYSTUFF","myarg"]);
myobj.myarg = 12;
//
// In this case, the mydata variable is passed
// explicitely by the optimization class.
// So the actual name "mydata" does not matter
// and whatever variable name can be used.
//
function [ y , index , mydata ] = rosenbrock2 ( x , index , mydata )
a = mydata.a
y = 100*(x(2)-x(1)^2)^2 + ( a - x(1))^2;
endfunction
//
// Test with an additional argument
//
mystuff = tlist(["T_MYSTUFF","a"]);
mystuff.a = 12.0;
//
// Test nearly all features of the optimization "abstract" class
//
opt = optimbase_new ();
// Check number of variables
opt = optimbase_configure(opt,"-numberofvariables",2);
nbvar = optimbase_cget(opt,"-numberofvariables");
assert_equal ( nbvar , 2 );
// Check cost function without additionnal argument
opt = optimbase_configure(opt,"-function",rosenbrock);
[this,f , index ] = optimbase_function ( opt , [0.0 0.0] , 2 );
assert_close ( f , 1.0 , %eps );
// Check cost function with additionnal argument
opt = optimbase_configure(opt,"-function",rosenbrock2);
opt = optimbase_configure(opt,"-costfargument",mystuff);
[this,f, index ] = optimbase_function ( opt , [0.0 0.0] , 2 );
assert_close ( f , 144.0 , %eps );
// Check initial guess
opt = optimbase_configure(opt,"-x0",[-1.2 1.0]');
x0 = optimbase_cget(opt,"-x0");
assert_close ( x0 , [-1.2 1.0]' , %eps);
// Check maxiter
opt = optimbase_configure(opt,"-maxiter",200);
maxiter = optimbase_cget(opt,"-maxiter");
assert_equal ( maxiter , 200);
// Check maxfunevals
opt = optimbase_configure(opt,"-maxfunevals",200);
maxfunevals = optimbase_cget(opt,"-maxfunevals");
assert_equal ( maxfunevals , 200);
// Check tolfunrelative
opt = optimbase_configure(opt,"-tolfunrelative",10*%eps);
tolfunrelative = optimbase_cget(opt,"-tolfunrelative");
assert_equal ( tolfunrelative , 10*%eps );
// Check tolxrelative
opt = optimbase_configure(opt,"-tolxrelative",10*%eps);
tolxrelative = optimbase_cget(opt,"-tolxrelative");
assert_equal ( tolxrelative , 10*%eps );
// Check verbose
opt = optimbase_configure(opt,"-verbose",1);
verbose = optimbase_cget(opt,"-verbose");
assert_equal ( verbose , 1 );
opt = optimbase_configure(opt,"-verbose",0);
// Check verbose termination
opt = optimbase_configure(opt,"-verbosetermination",1);
verbosetermination = optimbase_cget(opt,"-verbosetermination");
assert_equal ( verbosetermination , 1 );
opt = optimbase_configure(opt,"-verbosetermination",0);
// Check optimum
opt = optimbase_set(opt,"-xopt",[1.0 1.0]);
xopt = optimbase_get(opt,"-xopt");
assert_close ( xopt , [1.0 1.0], %eps );
// Check function value at optimum
opt = optimbase_set(opt,"-fopt",1.0);
fopt = optimbase_get(opt,"-fopt");
assert_close ( fopt , 1.0 , %eps );
// Check status
opt = optimbase_set(opt,"-status","maxiter");
status = optimbase_get(opt,"-status");
assert_equal ( status , "maxiter" );
// Log a message
opt = optimbase_configure(opt,"-verbose",1);
opt = optimbase_log ( opt , "My interesting message" );
My interesting message
opt = optimbase_configure(opt,"-verbose",0);
// Log a message relative to the stopping rule
opt = optimbase_configure(opt,"-verbosetermination",1);
opt = optimbase_stoplog ( opt , "My interesting stop message" );
opt = optimbase_configure(opt,"-verbosetermination",0);
// Check output command without additionnal argument
opt = optimbase_configure(opt,"-outputcommand",myoutputcmd);
brutedata = optimbase_outstruct ( opt );
mydata = tlist(["T_MYDATA",...
"x","fval","iteration","funccount",...
"myspecialdata"]);
mydata.x = brutedata.x;
mydata.fval = brutedata.fval;
mydata.iteration = brutedata.iteration;
mydata.funccount = brutedata.funccount;
mydata.myspecialdata = "yahoo !";
optimbase_outputcmd ( opt , "init" , mydata );
assert_equal ( _OUTPUCMDFLAG_ , 1 );
// Check output command with additionnal argument
opt = optimbase_configure(opt,"-outputcommand",myoutputcmd2);
opt = optimbase_configure(opt,"-outputcommandarg",myobj);
brutedata = optimbase_outstruct ( opt );
mydata = tlist(["T_MYDATA",...
"x","fval","iteration","funccount",...
"myspecialdata"]);
mydata.x = brutedata.x;
mydata.fval = brutedata.fval;
mydata.iteration = brutedata.iteration;
mydata.funccount = brutedata.funccount;
mydata.myspecialdata = "yahoo !";
optimbase_outputcmd ( opt , "init" , mydata );
assert_equal ( _OUTPUCMDFLAG_ , 12. );
// Check incriter
opt = optimbase_incriter ( opt );
iter = optimbase_get ( opt , "-iterations");
assert_equal ( iter , 1 );
// Check history storing with xopt
opt = optimbase_configure ( opt , "-storehistory" , %t );
opt = optimbase_histset ( opt , 1 , "-xopt" , [1.0 1.0]' );
x0 = optimbase_histget ( opt , 1 , "-xopt" );
assert_close ( x0 , [1.0 1.0]', %eps );
// Check history storing with fopt
opt = optimbase_configure ( opt , "-storehistory" , %t );
opt = optimbase_histset ( opt , 1 , "-fopt" , 1.0 );
f0 = optimbase_histget ( opt , 1 , "-fopt" );
assert_close ( f0 , 1.0, %eps );
// Check display
optimbase_display ( opt );
Optimization Object
Number of variables : 2
Initial Guess : [-1.2 1]
Initial Function Value :
Number of Inequality Constraints :0
Bounds Mininimum : []
Bounds Maxinimum :[]
Optimum Parameters : [1 1]
Optimum Function Value :1.000000e+00
Number of iterations : 1
Maximum number of iterations : 200
Number function evaluations : 0
Maximum number of function evaluations : 200
Termination Method on function value : F
Termination Absolute Tolerance on function value : 0
Termination Relative Tolerance on function value : 2.220D-15
Termination Method on x : T
Termination Absolute Tolerance on x : 0
Termination Relative Tolerance on x : 2.220D-15
Optimization Status : maxiter
Verbose logging : 0
Verbose Termination : 0
Verbose Log File :
Verbose Log File Startup Up: F
Store History : T
// Check the boundsmin, boundsmax and nbineqconst
opt = optimbase_configure ( opt , "-boundsmin" , [-5.0 -5.0] );
boundsmin = optimbase_cget ( opt , "-boundsmin" );
assert_equal ( boundsmin , [-5.0 -5.0] );
opt = optimbase_configure ( opt , "-boundsmax" , [5.0 5.0] );
boundsmax = optimbase_cget ( opt , "-boundsmax" );
assert_equal ( boundsmax , [5.0 5.0] );
opt = optimbase_configure ( opt , "-nbineqconst" , 3 );
nbineqconst = optimbase_cget ( opt , "-nbineqconst" );
assert_equal ( nbineqconst , 3 );
// Cleanup
opt = optimbase_destroy(opt);
//
// Test error cases
//
opt = optimbase_new ();
//
// Test wrong initial guess
//
cmd = "optimbase_configure(opt,''-x0'',[-1.2 1.0])";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_configure: The x0 vector is expected to be a column matrix, but current shape is 1 x 2";
assert_equal ( computed , expected );
//
// Test wrong -tolxmethod
//
cmd = "optimbase_configure(opt,''-tolxmethod'',''foo'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "assert_typeboolean: Expected boolean but got string instead";
assert_equal ( computed , expected );
//
// Test wrong -tolfunmethod
//
cmd = "optimbase_configure(opt,''-tolfunmethod'',''foo'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "assert_typeboolean: Expected boolean but got string instead";
assert_equal ( computed , expected );
// Cleanup
opt = optimbase_destroy(opt);
//
// Test outstruct when no -outputcommand is defined
//
opt = optimbase_new ();
cmd = "data = optimbase_outstruct ( opt )";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_outstruct: No output command is defined.";
assert_equal ( computed , expected );
// Cleanup
opt = optimbase_destroy(opt);
//
// Test optimbase_cget with unknown key
//
opt = optimbase_new ();
cmd = "value = optimbase_cget (opt,''foo'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_cget: Unknown key foo";
assert_equal ( computed , expected );
// Cleanup
opt = optimbase_destroy(opt);
//
// Test optimbase_get with unknown key
//
opt = optimbase_new ();
cmd = "value = optimbase_get (opt,''foo'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_get: Unknown key foo";
assert_equal ( computed , expected );
// Cleanup
opt = optimbase_destroy(opt);
//
// Test various errors
//
opt = optimbase_new ();
// Test -historyxopt when there is no history
cmd = "value = optimbase_get (opt,''-historyxopt'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_get: History disabled ; enable -storehistory option.";
assert_equal ( computed , expected );
// Test -historyfopt when there is no history
cmd = "value = optimbase_get (opt,''-historyfopt'')";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_get: History disabled ; enable -storehistory option.";
assert_equal ( computed , expected );
// Test optimbase_function when there is no function
cmd = "[ opt , f , index ] = optimbase_function ( opt , [] , %t )";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_function: Empty function (use -function option).";
assert_equal ( computed , expected );
// Test optimbase_histget ( this , iter , key ) when there is no history
cmd = "optimbase_histget ( opt , 1 , ''-xopt'' )";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_histget: History disabled ; turn on -storehistory option.";
assert_equal ( computed , expected );
// Test optimbase_histget ( this , iter , key ) with negative iteration
opt = optimbase_configure ( opt , "-storehistory" , %t );
cmd = "optimbase_histget ( opt , -1 , ''-xopt'' )";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimbase_histget: Negative iteration index -1 is not allowed.";
assert_equal ( computed , expected );
// Cleanup
opt = optimbase_destroy(opt);
|