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
|
// 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 = rosenbrock (x)
y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
endfunction
//
// Test optimsimplex_new, optimsimplex_destroy, optimsimplex_setn, optimsimplex_setx, optimsimplex_setfv,
// optimsimplex_getn, optimsimplex_getx, optimsimplex_getfv
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn(s1,2);
s1 = optimsimplex_setnbve(s1,3);
s1 = optimsimplex_setx(s1,1,[0.0 0.0]);
s1 = optimsimplex_setfv(s1,1,12.0);
s1 = optimsimplex_setx(s1,2,[1.0 0.0]);
s1 = optimsimplex_setfv(s1,2,13.0);
s1 = optimsimplex_setx(s1,3,[0.0 2.0]);
s1 = optimsimplex_setfv(s1,3,14.0);
// Now check the data
computed = optimsimplex_getn(s1);
assert_equal ( computed , 2 );
computed = optimsimplex_getx( s1 , 1);
assert_equal ( computed , [0.0 0.0] );
computed = optimsimplex_getx( s1 , 2);
assert_equal ( computed , [1.0 0.0] );
computed = optimsimplex_getx( s1 , 3);
assert_equal ( computed , [0.0 2.0] );
computed = optimsimplex_getfv( s1 , 1);
assert_equal ( computed , 12.0 );
computed = optimsimplex_getfv( s1 , 2);
assert_equal ( computed , 13.0 );
computed = optimsimplex_getfv( s1 , 3);
assert_equal ( computed , 14.0 );
// Print the simplex
optimsimplex_print ( s1 );
Dimension : 2
Number of vertices : 3
Vertex #1/3 : fv=1.200000e+01, x=0.000000e+00 0.000000e+00
Vertex #2/3 : fv=1.300000e+01, x=1.000000e+00 0.000000e+00
Vertex #3/3 : fv=1.400000e+01, x=0.000000e+00 2.000000e+00
// We are done !
s1 = optimsimplex_destroy(s1);
//
// Test optimsimplex_setallx, optimsimplex_setallfv, optimsimplex_getallx, optimsimplex_getallfv.
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn(s1,2);
s1 = optimsimplex_setnbve(s1,3);
s1 = optimsimplex_setx(s1,1,[0.0 0.0]);
s1 = optimsimplex_setfv(s1,1,12.0);
s1 = optimsimplex_setx(s1,2,[1.0 0.0]);
s1 = optimsimplex_setfv(s1,2,13.0);
s1 = optimsimplex_setx(s1,3,[0.0 2.0]);
s1 = optimsimplex_setfv(s1,3,14.0);
// Now check the data
computed = optimsimplex_getallx (s1);
expected = [
0.0 0.0
1.0 0.0
0.0 2.0];
assert_equal ( computed , expected );
computed = optimsimplex_getallfv (s1);
assert_equal ( computed , [12.0 13.0 14.0]' );
// setallx, setallfv
newsimplex = [
1.0 2.0
3.0 4.0
5.0 6.0];
s1 = optimsimplex_setallx ( s1 , newsimplex );
computed = optimsimplex_getallx (s1);
assert_equal ( computed , newsimplex );
s1 = optimsimplex_setallfv ( s1 , [3.0 4.0 5.0]' );
computed = optimsimplex_getallfv (s1);
assert_equal ( computed , [3.0 4.0 5.0]' );
s1 = optimsimplex_destroy(s1);
//
// Test optimsimplex_setve, optimsimplex_getve
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn ( s1 , 2 );
s1 = optimsimplex_setnbve(s1,3);
s1 = optimsimplex_setve ( s1 , 1 , 13.0 , [0.0 0.0] );
s1 = optimsimplex_setve ( s1 , 2 , 14.0 , [1.0 0.0] );
s1 = optimsimplex_setve ( s1 , 3 , 15.0 , [0.0 2.0] );
computed = optimsimplex_getallx ( s1 );
expected = [
0.0 0.0
1.0 0.0
0.0 2.0];
assert_equal ( computed , expected );
computed = optimsimplex_getallfv ( s1 );
assert_equal ( computed , [13.0 14.0 15.0]' );
ve1 = optimsimplex_getve ( s1 , 1 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [0.0 0.0] );
assert_equal ( ve1.fv , 13.0 );
ve2 = optimsimplex_getve ( s1 , 2 );
assert_equal ( ve2.n , 2 );
assert_equal ( ve2.x , [1.0 0.0] );
assert_equal ( ve2.fv , 14.0 );
ve3 = optimsimplex_getve ( s1 , 3 );
assert_equal ( ve3.n , 2 );
assert_equal ( ve3.x , [0.0 2.0] );
assert_equal ( ve3.fv , 15.0 );
s1 = optimsimplex_destroy(s1);
//
// optimsimplex_getall, optimsimplex_setall
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn ( s1 , 2 );
s1 = optimsimplex_setnbve(s1,3);
s1 = optimsimplex_setve ( s1 , 1 , 13.0 , [0.0 0.0] );
s1 = optimsimplex_setve ( s1 , 2 , 14.0 , [1.0 0.0] );
s1 = optimsimplex_setve ( s1 , 3 , 15.0 , [0.0 2.0] );
computed = optimsimplex_getall ( s1 );
expected = [
13. 0. 0.
14. 1. 0.
15. 0. 2.
];
assert_equal ( computed , expected );
simplex = [
10.0 1.0 2.0
11.0 3.0 4.0
12.0 5.0 6.0
];
s1 = optimsimplex_setall ( s1 , simplex );
ve1 = optimsimplex_getve ( s1 , 1 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [1.0 2.0] );
assert_equal ( ve1.fv , 10.0 );
ve1 = optimsimplex_getve ( s1 , 2 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [3.0 4.0] );
assert_equal ( ve1.fv , 11.0 );
ve1 = optimsimplex_getve ( s1 , 3 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [5.0 6.0] );
assert_equal ( ve1.fv , 12.0 );
s1 = optimsimplex_destroy(s1);
//
// Test setall with wrong simplex :
// there are 3 parameters => 4 columns
// but there is only 3 vertices, while the minimum would be 4.
//
s1 = optimsimplex_new ();
simplex = [
10.0 1.0 2.0 12.0
11.0 3.0 4.0 12.0
12.0 5.0 6.0 12.0
];
cmd = "s1 = optimsimplex_setall ( s1 , simplex );";
execstr(cmd,"errcatch");
computed = lasterror();
expected = "optimsimplex_setall: The number of vertices (i.e. the number of rows) is 3 which is smaller than the number of columns 4 (i.e. n+1).";
assert_equal ( computed , expected );
s1 = optimsimplex_destroy(s1);
//
// optimsimplex_getall, optimsimplex_setall with 5 vertices
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn ( s1 , 2 );
s1 = optimsimplex_setnbve(s1, 5 );
s1 = optimsimplex_setve ( s1 , 1 , 13.0 , [0.0 0.0] );
s1 = optimsimplex_setve ( s1 , 2 , 14.0 , [1.0 0.0] );
s1 = optimsimplex_setve ( s1 , 3 , 15.0 , [0.0 2.0] );
s1 = optimsimplex_setve ( s1 , 4 , 16.0 , [1.0 3.0] );
s1 = optimsimplex_setve ( s1 , 5 , 17.0 , [2.0 4.0] );
computed = optimsimplex_getall ( s1 );
expected = [
13. 0. 0.
14. 1. 0.
15. 0. 2.
16. 1. 3.
17. 2. 4.
];
assert_equal ( computed , expected );
simplex = [
10.0 1.0 2.0
11.0 3.0 4.0
12.0 5.0 6.0
13.0 6.0 7.0
14.0 7.0 8.0
];
s1 = optimsimplex_setall ( s1 , simplex );
ve1 = optimsimplex_getve ( s1 , 1 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [1.0 2.0] );
assert_equal ( ve1.fv , 10.0 );
ve1 = optimsimplex_getve ( s1 , 2 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [3.0 4.0] );
assert_equal ( ve1.fv , 11.0 );
ve1 = optimsimplex_getve ( s1 , 3 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [5.0 6.0] );
assert_equal ( ve1.fv , 12.0 );
ve1 = optimsimplex_getve ( s1 , 4 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [6.0 7.0] );
assert_equal ( ve1.fv , 13.0 );
ve1 = optimsimplex_getve ( s1 , 5 );
assert_equal ( ve1.n , 2 );
assert_equal ( ve1.x , [7.0 8.0] );
assert_equal ( ve1.fv , 14.0 );
s1 = optimsimplex_destroy(s1);
//
// Test optimsimplex_setallx, optimsimplex_setallfv, optimsimplex_getallx, optimsimplex_getallfv with 5 vertices
//
s1 = optimsimplex_new ();
s1 = optimsimplex_setn(s1,2);
s1 = optimsimplex_setnbve(s1,5);
s1 = optimsimplex_setx ( s1 , 1 , [0.0 0.0]);
s1 = optimsimplex_setfv ( s1 , 1 , 12.0);
s1 = optimsimplex_setx ( s1 , 2 , [1.0 0.0]);
s1 = optimsimplex_setfv ( s1 , 2 , 13.0);
s1 = optimsimplex_setx ( s1 , 3 , [0.0 2.0]);
s1 = optimsimplex_setfv ( s1 , 3 , 14.0);
s1 = optimsimplex_setx ( s1 , 4 , [1.0 3.0]);
s1 = optimsimplex_setfv ( s1 , 4 , 15.0);
s1 = optimsimplex_setx ( s1 , 5 , [2.0 4.0]);
s1 = optimsimplex_setfv ( s1 , 5 , 16.0);
// Now check the data
computed = optimsimplex_getallx (s1);
expected = [
0. 0.
1. 0.
0. 2.
1. 3.
2. 4.
];
assert_equal ( computed , expected );
computed = optimsimplex_getallfv (s1);
assert_equal ( computed , [12.0 13.0 14.0 15.0 16.0]' );
// setallx, setallfv
newsimplex = [
1. 2.
3. 4.
5. 6.
7. 8.
9. 10.
];
s1 = optimsimplex_setallx ( s1 , newsimplex );
computed = optimsimplex_getallx (s1);
assert_equal ( computed , newsimplex );
s1 = optimsimplex_setallfv ( s1 , [3.0 4.0 5.0 6.0 7.0]' );
computed = optimsimplex_getallfv (s1);
assert_equal ( computed , [3.0 4.0 5.0 6.0 7.0]' );
s1 = optimsimplex_destroy(s1);
|