1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
3 // Copyright (C) 2011 - DIGITEO - Michael Baudin
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 // <-- CLI SHELL MODE -->
12 // Here, the cost function is OK
14 function [ y , index ] = rosenbrockOk ( x , index )
15 y = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
17 opt = optimbase_new ();
18 opt = optimbase_configure(opt,"-numberofvariables",2);
19 opt = optimbase_configure(opt,"-x0",[1.1 1.1]');
20 opt = optimbase_configure(opt,"-function",rosenbrockOk);
21 opt = optimbase_checkcostfun(opt);
22 opt = optimbase_destroy(opt);
24 // Here, the cost function is not callable
26 function [ y , index ] = rosenbrock2 ( x , index )
29 opt = optimbase_new ();
30 opt = optimbase_configure(opt,"-numberofvariables",2);
31 opt = optimbase_configure(opt,"-x0",[1.1 1.1]');
32 opt = optimbase_configure(opt,"-function",rosenbrock2);
33 cmd = "opt = optimbase_checkcostfun(opt);";
34 lclmsg = "%s: Cannot evaluate cost function with ""%s"": %s";
35 scimsg = msprintf(_("Undefined variable: %s\n"), "fdsmklqfjdsf");
36 assert_checkerror(cmd,lclmsg,[],"optimbase_checkcostfun","[f,index]=costf(x0,1)", scimsg);
37 opt = optimbase_destroy(opt);
39 // Here, the cost function is callable, but returns a matrix,
40 // instead of a scalar.
42 function [ y , index ] = rosenbrock3 ( x , index )
45 opt = optimbase_new ();
46 opt = optimbase_configure(opt,"-numberofvariables",2);
47 opt = optimbase_configure(opt,"-x0",[1.1 1.1]');
48 opt = optimbase_configure(opt,"-function",rosenbrock3);
49 cmd = "opt = optimbase_checkcostfun(opt);";
50 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d rows, instead of %d.",[],"optimbase_checkcostfun","f",2,10,1);
51 opt = optimbase_destroy(opt);
53 // Test with good non linear constraints
55 function [ f , c , index ] = optimtestcase ( x , index )
58 if ( index == 2 | index == 6 ) then
59 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ...
60 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4)
62 if ( index == 5 | index == 6 ) then
63 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ...
64 - x(1) + x(2) - x(3) + x(4) + 8
65 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ...
67 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ...
72 opt = optimbase_new ();
73 opt = optimbase_configure(opt,"-numberofvariables",4);
74 opt = optimbase_configure(opt,"-function",optimtestcase);
75 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
76 opt = optimbase_configure(opt,"-nbineqconst",3);
77 opt = optimbase_checkcostfun(opt);
78 opt = optimbase_destroy(opt);
80 // Test a wrong configuration: we forgot to configure "-nbineqconst"
82 opt = optimbase_new ();
83 opt = optimbase_configure(opt,"-numberofvariables",4);
84 opt = optimbase_configure(opt,"-function",optimtestcase);
85 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
86 cmd="opt = optimbase_checkcostfun(opt)";
87 errmsg = "%s: The matrix %s from costf(x0,%d) has %d rows, instead of %d.";
88 assert_checkerror(cmd,errmsg,[], "optimbase_checkcostfun" , "index" , 1 , 0 , 1);
89 opt = optimbase_destroy(opt);
91 // Test with wrong non linear constraints f(x0,2) is not a row vector
93 function [ f , c , index ] = optimtestcase2 ( x , index )
96 if ( index == 2 | index == 6 ) then
97 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ...
98 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4)
100 if ( index == 5 | index == 6 ) then
101 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ...
102 - x(1) + x(2) - x(3) + x(4) + 8
103 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ...
105 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ...
110 opt = optimbase_new ();
111 opt = optimbase_configure(opt,"-numberofvariables",4);
112 opt = optimbase_configure(opt,"-function",optimtestcase2);
113 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
114 opt = optimbase_configure(opt,"-nbineqconst",3);
115 cmd = "opt = optimbase_checkcostfun(opt);";
116 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d rows, instead of %d.",[],"optimbase_checkcostfun","c",5,3,1);
117 opt = optimbase_destroy(opt);
119 // Test with wrong non linear constraints f(x0,2) is a row vector with 5 components instead of 3
121 function [ f , c , index ] = optimtestcase3 ( x , index )
124 if ( index == 2 | index == 6 ) then
125 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ...
126 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4)
128 if ( index == 5 | index == 6 ) then
129 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ...
130 - x(1) + x(2) - x(3) + x(4) + 8
131 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ...
133 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ...
135 c = [c1 c2 c3 0.0 0.0]
138 opt = optimbase_new ();
139 opt = optimbase_configure(opt,"-numberofvariables",4);
140 opt = optimbase_configure(opt,"-function",optimtestcase3);
141 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
142 opt = optimbase_configure(opt,"-nbineqconst",3);
143 cmd = "opt = optimbase_checkcostfun(opt);";
144 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d columns, instead of %d.",[],"optimbase_checkcostfun","c",5,5,3);
145 opt = optimbase_destroy(opt);
147 // Test with wrong non linear constraints f(x0,3) is a column vector
149 function [ f , c , index ] = optimtestcase4 ( x , index )
152 if ( index == 2 | index == 6 ) then
153 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ...
154 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4)
156 if ( index == 5 | index == 6 ) then
157 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ...
158 - x(1) + x(2) - x(3) + x(4) + 8
159 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ...
161 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ...
166 opt = optimbase_new ();
167 opt = optimbase_configure(opt,"-numberofvariables",4);
168 opt = optimbase_configure(opt,"-function",optimtestcase4);
169 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
170 opt = optimbase_configure(opt,"-nbineqconst",3);
171 cmd = "opt = optimbase_checkcostfun(opt);";
172 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d rows, instead of %d.",[],"optimbase_checkcostfun","c",5,3,1);
173 opt = optimbase_destroy(opt);
175 // Test with wrong non linear constraints f(x0,3) is a row vector with 5 columns instead of 4
177 function [ f , c , index ] = optimtestcase5 ( x , index )
180 if ( index == 2 | index == 6 ) then
181 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ...
182 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4)
184 if ( index == 5 | index == 6 ) then
185 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ...
186 - x(1) + x(2) - x(3) + x(4) + 8
187 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ...
189 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ...
194 opt = optimbase_new ();
195 opt = optimbase_configure(opt,"-numberofvariables",4);
196 opt = optimbase_configure(opt,"-function",optimtestcase5);
197 opt = optimbase_configure(opt,"-x0",[0.0 0.0 0.0 0.0]');
198 opt = optimbase_configure(opt,"-nbineqconst",3);
199 cmd = "opt = optimbase_checkcostfun(opt);";
200 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d columns, instead of %d.",[],"optimbase_checkcostfun","c",5,4,3);
201 opt = optimbase_destroy(opt);
203 // Test with correct rosenbrock function
205 function [ f , g , index ] = rosenbrock ( x , index )
206 f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
207 g(1,1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
208 g(1,2) = 200. * ( x(2) - x(1)**2 )
210 opt = optimbase_new ();
211 opt = optimbase_configure(opt,"-numberofvariables",2);
212 opt = optimbase_configure(opt,"-function", rosenbrock );
213 opt = optimbase_configure(opt,"-withderivatives",%t);
214 opt = optimbase_configure(opt,"-x0",[-1.2 1.0].');
215 opt = optimbase_checkcostfun(opt);
216 opt = optimbase_destroy(opt);
218 // Test with not correct rosenbrock function : g is a column vector instead of row vector
220 function [ f , g , index ] = rosenbrock4 ( x , index )
221 f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
222 g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
223 g(2) = 200. * ( x(2) - x(1)**2 )
225 opt = optimbase_new ();
226 opt = optimbase_configure(opt,"-numberofvariables",2);
227 opt = optimbase_configure(opt,"-function", rosenbrock4 );
228 opt = optimbase_configure(opt,"-withderivatives",%t);
229 opt = optimbase_configure(opt,"-x0",[-1.2 1.0].');
230 cmd = "opt = optimbase_checkcostfun(opt);";
231 assert_checkerror(cmd,"%s: The matrix %s from costf(x0,%d) has %d rows, instead of %d.",[],"optimbase_checkcostfun","g",3,2,1);
232 opt = optimbase_destroy(opt);