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
|
// =============================================================================
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008 - INRIA - Michael Baudin
// Copyright (C) 2009 - DIGITEO - Michael Baudin
//
// This file is distributed under the same license as the Scilab package.
// =============================================================================
// <-- JVM NOT MANDATORY -->
// <-- ENGLISH IMPOSED -->
// optim.tst --
// Test the optim command with the Rosenbrock test case
// in the case where the cost function is provided as a Fortran
// routine and use the "in" option.
//
// Note : the following source code was copied from optimization/sci_gateway/fortran/Ex-optim.f
// Thus, the "genros" function from Ex-optim.f is not needed anymore.
//
//
// Define a fortran source code and compile it (fortran compiler required)
//
ilib_verbose(0);
// Move into the temporary directory to create the temporary files there
cur_dir = pwd();
chdir(TMPDIR);
fortransource=[' subroutine rosenf(ind,n,x,f,g,izs,rzs,dzs)'
'C -------------------------------------------'
'c (DLL Digital Visual Fortran)'
'c On Windows , we need to import common nird from scilab'
'cDEC$ IF DEFINED (FORDLL)'
'cDEC$ ATTRIBUTES DLLIMPORT:: /nird/'
'cDEC$ ENDIF'
'C -------------------------------------------'
'c Example of cost function given by a subroutine'
'c if n<=2 returns ind=0'
'c f.bonnans, oct 86'
' implicit double precision (a-h,o-z)'
' real rzs(1)'
' double precision dzs(*)'
' dimension x(n),g(n),izs(*)'
' common/nird/nizs,nrzs,ndzs'
' if (n.lt.3) then'
' ind=0'
' return'
' endif'
' if(ind.eq.10) then'
' nizs=2'
' nrzs=1'
' ndzs=2'
' return'
' endif'
' if(ind.eq.11) then'
' izs(1)=5'
' izs(2)=10'
' dzs(2)=100.0d+0'
' return'
' endif'
' if(ind.eq.2)go to 5'
' if(ind.eq.3)go to 20'
' if(ind.eq.4)go to 5'
' ind=-1'
' return'
'5 f=1.0d+0'
' do 10 i=2,n'
' im1=i-1'
'10 f=f + dzs(2)*(x(i)-x(im1)**2)**2 + (1.0d+0-x(i))**2'
' if(ind.eq.2)return'
'20 g(1)=-4.0d+0*dzs(2)*(x(2)-x(1)**2)*x(1)'
' nm1=n-1'
' do 30 i=2,nm1'
' im1=i-1'
' ip1=i+1'
' g(i)=2.0d+0*dzs(2)*(x(i)-x(im1)**2)'
'30 g(i)=g(i) -4.0d+0*dzs(2)*(x(ip1)-x(i)**2)*x(i) - '
' & 2.0d+0*(1.0d+0-x(i))'
' g(n)=2.0d+0*dzs(2)*(x(n)-x(nm1)**2) - 2.0d+0*(1.0d+0-x(n))'
' return'
' end'];
mputl(fortransource,'rosenf.f');
// compile the Fortran code
libpath=ilib_for_link('rosenf','rosenf.c',[],'f');
// incremental linking
linkid=link(libpath,'rosenf','f');
chdir(cur_dir);
//
// Define some constants
//
Leps=8.e-5;
bs=10.*ones(1,5);
bi=-bs;
x0=0.12*bs;
epsx=1.e-15*x0;
xopt=.1*bs;
//
// Solve the problem
//
[f,x,g]=optim('rosenf',x0,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'gc','in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
//
[f,x,g]=optim('rosenf',x0,'nd','in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'qn',1,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'gc',1,50,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
//
[f,x,g]=optim('rosenf',x0,'nd',1,50,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x1,g] =optim('rosenf',x0, 'ar',100,6,'in');
[f,x,g,to]=optim('rosenf',x0, 'ar',100,3,'in');
[f,x,g,to]=optim('rosenf',x ,to,'ar',100,3,'in');
if norm(x-x1)/norm(x-xopt) > 0.1 then bugmes();quit;end
[f,x1,g]=optim('rosenf','b',bi,bs,x0,'ar',100,6,'in');
[f,x,g,to]=optim('rosenf','b',bi,bs,x0,'ar',100,3,'in');
[f,x,g] =optim('rosenf','b',bi,bs,x,to,'ar',100,3,'in');
if norm(x-x1)/norm(x-xopt) > 0.1 then bugmes();quit;end
//
// Test all possible stop criteria settings
//
[f,x,g]=optim('rosenf',x0,'ar','in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'ar',100,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'ar',100,100,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'ar',100,100,%eps,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'ar',100,100,%eps,%eps,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'ar',100,100,10.*%eps,%eps,epsx,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',x0,'gc','ar',100,100,%eps,%eps,epsx,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
//
[f,x,g]=optim('rosenf','b',bi,bs,x0,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf','b',bi,bs,x0,'gc','in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x]=optim('rosenf','b',bi,bs,x0,'ar',100,100,1.d-8,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g]=optim('rosenf',..
'b',bi,bs,x0,'gc','ar',100,100,%eps,%eps,epsx,'in');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,to,td]=optim('rosenf',x0,'in','sd');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,ti]=optim('rosenf',x0,'gc','in','si');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,to,ti,td]=optim('rosenf',x0,to,'in','si','sd');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,td]=optim('rosenf',..
'b',bi,bs,x0,'gc','ar',100,100,%eps,%eps,epsx,'in','sd');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,ti]=optim('rosenf',x0,'gc','ar',100,100,%eps,'in','si');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,ti,td]=optim('rosenf',..
x0,'gc','ar',100,100,%eps,'in','si','sd');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
[f,x,g,ti,td]=optim('rosenf',..
x0,'gc','ar',100,100,%eps,'in','ti',ti,'td',td,'si','sd');
if abs(f-1+norm(x-xopt) ) > Leps then bugmes();quit;end
// Clean-up
ulink(linkid);
|