-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.f
314 lines (301 loc) · 13.9 KB
/
main.f
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
C+-----------------------------------------------------------------------+
C| Program : main.f |
C| Last modified : 07-16-2001 |
C| Written by : Owen Esslinger, Joerg Gablonsky, Alton Patrick |
C| The main program to run DIRECT on test problems. |
C+-----------------------------------------------------------------------+
PROGRAM main
IMPLICIT NONE
INTEGER Maxdim
PARAMETER (Maxdim = 128)
integer i, problem
external myfunc
C+----------------------------------------------------------------------+
C| DIRECT specific variables. |
C+----------------------------------------------------------------------+
Double Precision DIReps, DIRf
Integer DIRmaxf, DIRmaxT
Integer DIRalg
Integer IError, logfile
Double Precision fglobal, fglper, volper, sigmaper
double precision u(Maxdim), l(Maxdim)
integer n
Double Precision DIRx(Maxdim)
C+-----------------------------------------------------------------------+
C| Variables to pass user defined data to the function to be optimized. |
C+-----------------------------------------------------------------------+
INTEGER iisize, idsize, icsize
Parameter (iisize = 300)
Parameter (idsize = 300)
Parameter (icsize = 30)
INTEGER iidata(iisize)
Double Precision ddata(idsize)
Character*40 cdata(icsize)
Integer resultfile
double precision per2
real dtime,diff,tarray(2)
C+----------------------------------------------------------------------+
C| Define and open the logfile and the resultfile, where we store the |
C| results of the run. We save the problem number, the number of |
C| function evaluations needed, the time used by DIRECT, the percent |
C| error and a flag signaling if we used DIRECT or DIRECT-l in the |
C| resultfile. |
C+----------------------------------------------------------------------+
logfile = 2
resultfile = 29
open(logfile, file='direct.out')
open(resultfile, file='result.out')
CALL mainheader(logfile)
C+----------------------------------------------------------------------+
C| Read in the problem specific data and the parameters for DIRECT. |
C+----------------------------------------------------------------------+
CALL inputdata(n, u, l, logfile, DIReps, DIRmaxf,
+ DIRmaxT, problem, Maxdim, DIRalg, fglobal,
+ fglper, volper, sigmaper,
+ iidata, iisize, ddata, idsize, cdata, icsize)
C+----------------------------------------------------------------------+
C| Initialize and start the timing. |
C+----------------------------------------------------------------------+
tarray(1) = 0.D0
tarray(2) = 0.D0
diff=dtime(tarray)
C+----------------------------------------------------------------------+
C| If the budget is lower zero, multiply the absolute value of the |
C| budget by the dimension of the problem. |
C+----------------------------------------------------------------------+
if (DIRmaxf .lt. 0) then
DIRmaxf = -DIRmaxf*n
endif
C+----------------------------------------------------------------------+
C| For some problems, we need to store some specific data in the user |
C| variables. |
C+----------------------------------------------------------------------+
if ((problem .ge. 5) .and. (problem .LE. 7)) then
iidata(1) = 5
if (problem .EQ. 6) THEN
iidata(1) = 7
else if (problem .EQ. 7) THEN
iidata(1) = 10
END IF
else if ((problem .ge. 8) .and. (problem .LE. 9)) then
n = 3
iidata(1) = 4
if (problem .EQ. 9) THEN
iidata(1) = 4
n = 6
END IF
end if
C+----------------------------------------------------------------------+
C| Call the optimization method. |
C+----------------------------------------------------------------------+
CALL DIRect(myfunc, DIRx, n, DIReps, DIRmaxf, DIRmaxT,
+ DIRf, l, u, DIRalg, Ierror, logfile,
+ fglobal, fglper, volper, sigmaper,
+ iidata, iisize, ddata, idsize, cdata, icsize)
C+----------------------------------------------------------------------+
C| Give out the results of the optimization. |
C+----------------------------------------------------------------------+
Write(*,100)
Write(*,110) IError
Write(*,120) (DIRx(i),i=1,n)
Write(*,130) DIRf
Write(*,140) DIRmaxf
Write(logfile,100)
Write(logfile,110) IError
Write(logfile,120) (DIRx(i),i=1,n)
Write(logfile,130) DIRf
Write(logfile,140) DIRmaxf
diff=dtime(tarray)
write(*,150) diff
write(logfile,150) diff
C+----------------------------------------------------------------------+
C| Calculate the percent error. |
C+----------------------------------------------------------------------+
per2 = DIRf - fglobal
if (fglobal .eq. 0.D0) then
per2 = per2*100.D0
else
per2 = per2/ abs(fglobal)*100.D0
end if
C+----------------------------------------------------------------------+
C| Save the results in the extra resultfile for use with Matlab etc. |
C+----------------------------------------------------------------------+
write(resultfile,200) problem, DIRmaxf, diff, per2, DIRalg
C+----------------------------------------------------------------------+
C| Close the logfile and rsultfile. |
C+----------------------------------------------------------------------+
close(logfile)
close(resultfile)
100 FORMAT('-------------- Final result ------------------')
110 FORMAT('DIRECT termination flag : ',I3)
120 FORMAT('DIRECT minimal point : ',20f12.7)
130 FORMAT('DIRECT minimal value : ',f12.7)
140 FORMAT('DIRECT number of f-eval : ',I5)
150 FORMAT('Time needed : ',e10.4,' seconds.')
200 FORMAT(I3, ' ', I6, ' ', f12.4, ' ', e10.5, ' ',I6)
end
C+--------------------------------------------------------------+
C| Subroutine to read the values of the internal variables |
C| and the data for the interpolator. |
C+--------------------------------------------------------------+
subroutine inputdata(n, u, l, logfile, DIReps, DIRmaxf,
+ DIRmaxT, problem, Maxdim, DIRalg, fglobal,
+ fglper, volper, sigmaper,
+ iidata, iisize, ddata, idsize, cdata, icsize)
implicit none
Integer file, Maxdim, intproblem
parameter(file = 31)
Integer logfile,DOIFFCO
C+----------------------------------------------------------------------+
C| DIRECT specific variables. |
C+----------------------------------------------------------------------+
Double Precision DIReps
Integer DIRmaxf, DIRmaxT
Double Precision fglobal, fglper, volper, sigmaper
Integer DIRalg
C+-----------------------------------------------------------------------+
C| Variables to pass user defined data to the function to be optimized. |
C+-----------------------------------------------------------------------+
INTEGER iisize, idsize, icsize
INTEGER iidata(iisize)
Double Precision ddata(idsize)
Character*40 cdata(icsize)
C+----------------------------------------------------------------------+
C| General variables for the problem. |
C+----------------------------------------------------------------------+
integer n, i
integer problem
Double Precision l(maxdim), u(maxdim)
C+----------------------------------------------------------------------+
C| Variables to store the different file names. |
C+----------------------------------------------------------------------+
character DIRectinit*20
character problemdata*20
C+----------------------------------------------------------------------+
C| Read file names of the different files used in this run. |
C+----------------------------------------------------------------------+
open(unit = file, file='ini/main.ini')
read(file, 154) DIRectinit
read(file, 153) intproblem
read(file, 153) DOIFFCO
close(unit = file)
problem = intproblem
C+----------------------------------------------------------------------+
C| Store the problem number in the last entry of iidata. |
C+----------------------------------------------------------------------+
iidata(iisize) = problem
write(*,2000)
write(*,2010) DIRectinit
write(logfile,2000)
write(logfile,2010) DIRectinit
open(unit = file, file='ini/problems.ini')
read(file,153)
do 40,i = 1,problem+1
read(file, 154) problemdata
40 continue
close(unit = file)
write(*,2020) problemdata
write(*,2030) problem
write(logfile,2020) problemdata
write(logfile,2030) problem
C+----------------------------------------------------------------------+
C| Read DIRECT variables from DIRinit.ini |
C+----------------------------------------------------------------------+
open(unit = file, file = 'ini/'//DIRectinit)
read(file, 151) DIReps
read(file, 150) DIRmaxf
read(file, 150) DIRmaxT
read(file, 150) DIRalg
C+----------------------------------------------------------------------+
C| Read in the percent error when DIRECT should stop. If the optimal |
C| function value is not known (that is, when a real optimization is |
C| done), set this value to 0 and fglobal to -1.D100. This ensures that |
C| the percentage condition cannot be satiesfied. |
C+----------------------------------------------------------------------+
read(file, 151) fglper
C+----------------------------------------------------------------------+
C| Read in the percentage of the volume that the hyperrectangle which |
C| assumes fmin at its center needs to have to stop. Set this value to |
C| 0.D0 if you don't want to use this stopping criteria. |
C+----------------------------------------------------------------------+
read(file, 151) volper
C+----------------------------------------------------------------------+
C| Read in the bound on the measure that the hyperrectangle which |
C| assumes fmin at its center needs to have to stop. Set this value to |
C| 0.D0 if you don't want to use this stopping criteria. |
C+----------------------------------------------------------------------+
read(file, 151) sigmaper
close(unit = file)
C+----------------------------------------------------------------------+
C| Read problem specifics from problem data file. |
C+----------------------------------------------------------------------+
open(unit = file, file = 'problem/'//problemdata)
C+----------------------------------------------------------------------+
C| Read in the problem name. This name is used in the initial output |
C| from DIRECT. |
C+----------------------------------------------------------------------+
read(file,152) cdata(1)
read(file, 150) n
C+----------------------------------------------------------------------+
C| Read in the (know) optimal function value. Note that this value is |
C| generally not know, but for the test problems it is. If this value is|
C| unknown, set fglobal to -1.D100 and fglper (see above) to 0. |
C+----------------------------------------------------------------------+
read(file, 151) fglobal
do 1000, i = 1,n
read(file, 151) l(i)
1000 continue
do 1005, i = 1,n
read(file, 151) u(i)
1005 continue
close(unit = file)
150 FORMAT(I10)
151 FORMAT(F20.10)
152 FORMAT(A40)
153 FORMAT(I20)
154 FORMAT(A20)
2000 FORMAT('Name of ini-directory : ini/')
2010 FORMAT('Name of DIRect.ini file : ',A20)
2020 FORMAT('Name of problemdata file : ',A20)
2030 FORMAT('Testproblem used : ',I4)
end
C+----------------------------------------------------------------------+
C| Give out a header for the main program. |
C+----------------------------------------------------------------------+
SUBROUTINE mainheader(logfile)
IMPLICIT None
Integer logfile
write(*,100)
write(*,110)
write(*,120)
write(*,130)
write(*,140)
write(*,150)
write(*,160)
write(*,170)
write(*,180)
write(*,190)
write(*,200)
write(logfile,100)
write(logfile,110)
write(logfile,120)
write(logfile,130)
write(logfile,140)
write(logfile,150)
write(logfile,160)
write(logfile,170)
write(logfile,180)
write(logfile,190)
write(logfile,200)
100 FORMAT('+----------------------------------------+')
110 FORMAT('| Example Program for DIRECT |')
120 FORMAT('| This program uses DIRECT to optimize |')
130 FORMAT('| testfunctions. Which testfunction is |')
140 FORMAT('| optimized and what parameters are used |')
150 FORMAT('| is controlled by the files in ini/. |')
160 FORMAT('| |')
170 FORMAT('| Owen Esslinger, Joerg Gablonsky, |')
180 FORMAT('| Alton Patrick |')
190 FORMAT('| 04/15/2001 |')
200 FORMAT('+----------------------------------------+')
end