-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownhillSimplex.f90
374 lines (275 loc) · 11.2 KB
/
DownhillSimplex.f90
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
370
371
372
373
! As the derivative of the object function is not available, the optimiser must need only
! evaluations of the function to be minimized. this makes selection of an algorithm simpler,
! although potential benefits of methods that use the derivative are not available.
! The downhill simplex method is robust but potentially slow. Given that the optimization of this
! particular object function does not involve lots of variables it is unlikely a faster or more complicated
! (i.e. direction set method or PSO) algorithm is needed. It is short and self-contained.
MODULE DownhillSimplex
IMPLICIT NONE
PRIVATE
PUBLIC :: RunNelderMead
NAMELIST /opt/ c_reflect, c_expand, c_contract, c_shrink, threshold, maxSteps
REAL(8) :: c_reflect, c_expand, c_contract, c_shrink, threshold
INTEGER :: maxSteps
CONTAINS
!*
! needs to take an object function as an argument to pass to the nelder-mead optimizer
! right now is just importing a fixed object function from module ObjectFunctions
SUBROUTINE RunNelderMead(guess,lambda,logUnit,printDetails)
! #DES: Driver for Nelder-Mead optimizer, checks sanity of input, creates guess and informs the log.
! guess is the point around which the simplex should be built
! lambda is the characteristic length scale in each dimension
IMPLICIT NONE
REAL(8), INTENT(IN) :: guess(:), lambda(:)
INTEGER, INTENT(IN) :: logUnit
LOGICAL, INTENT(IN) :: printDetails
REAL(8) :: simplex(SIZE(guess)+1,SIZE(guess))
REAL(8) :: optimum(SIZE(guess)), optValue
LOGICAL :: converged = .FALSE.
INTEGER :: i
WRITE(logUnit,'(A)') "* Initializing Nelder-Mead Downhill Simplex *"
WRITE(logUnit,*)
WRITE(logUnit,'(A24,I6)') "Parameter Space Size = ", SIZE(guess)
WRITE(logUnit,'(A24,I6)') "Points in Simplex = ", SIZE(guess) + 1
CALL ProcessNameList(logUnit)
CALL GenerateSimplex(guess,lambda,logUnit,simplex)
CALL NelderMead(simplex,logUnit,printDetails,optimum,optValue,converged)
IF (converged .EQV. .TRUE.) THEN
WRITE(logUnit,'(A)') "Downhill Simplex Converged"
ELSE
WRITE(logUnit,'(A)') "Downhill Simplex Not Converged"
ENDIF
WRITE(logUnit,'(A,F15.8,A)',ADVANCE="NO") "Best value located = ", optValue, " at:"
DO i = 1, SIZE(optimum)
WRITE(logUnit,'(F15.8,1X)',ADVANCE='NO') optimum(i)
ENDDO
WRITE(logUnit,*)
WRITE(logUnit,*)
END SUBROUTINE RunNelderMead
!*
SUBROUTINE GenerateSimplex(guess,lambda,logUnit,simplex)
USE EVBParameters, ONLY : objectFunction
IMPLICIT NONE
REAL(8), INTENT(IN) :: guess(:), lambda(:)
INTEGER, INTENT(IN) :: logUnit
REAL(8), INTENT(OUT) :: simplex(SIZE(guess)+1,SIZE(guess))
REAL(8) :: functionValues(SIZE(simplex,1)+1) ! object function evaluated at each point
INTEGER :: m, n, i
n = SIZE(guess)
m = n + 1
!generate a simplex from the input point - should be done in own routine/function
simplex(:,:) = 0.0d0
functionValues(:) = 0.0d0
simplex(1,:) = guess(:)
functionValues(1) = objectFunction(simplex(1,:))
DO i = 2, m
simplex(i,:) = guess(:)
simplex(i,i-1) = simplex(i,i-1) + lambda(i-1)
functionValues(i) = objectFunction(simplex(i,:))
ENDDO
WRITE(logUnit,'(A16)') "Initial simplex:"
CALL WriteSimplex(simplex,functionValues,logUnit)
END SUBROUTINE GenerateSimplex
!*
SUBROUTINE NelderMead(initialSimplex,logUnit,debug,optimum,optValue,converged)
USE EVBParameters, ONLY : objectFunction
IMPLICIT NONE
REAL(8), INTENT(IN) :: initialSimplex(:,:) ! m x n, each row is an n-dimensional vector
INTEGER, INTENT(IN) :: logUnit
LOGICAL, INTENT(IN) :: debug
REAL(8), INTENT(OUT) :: optimum(SIZE(initialSimplex,2))
REAL(8), INTENT(OUT) :: optValue
LOGICAL, INTENT(OUT) :: converged
REAL(8) :: simplex(SIZE(initialSimplex,1),SIZE(initialSimplex,2))
REAL(8) :: functionValues(SIZE(initialSimplex,1)+1) ! object function evaluated at each point
INTEGER :: step, n, m, i
REAL(8) :: f_reflect, f_expand, f_contract
REAL(8) :: reflectedPoint(SIZE(initialSimplex,2)), expandedPoint(SIZE(initialSimplex,2))
REAL(8) :: contractedPoint(SIZE(initialSimplex,2)), centroidPoint(SIZE(initialSimplex,2))
converged = .FALSE.
optimum(:) = 0.0d0
optValue = 0.0d0
simplex(:,:) = initialSimplex(:,:)
n = SIZE(simplex,2)
m = n + 1
DO i = 1, m
functionValues(i) = objectFunction(simplex(i,:))
ENDDO
!repeat until convergence or excessive compute time
DO step = 1, maxSteps
CALL BubbleSort(simplex,functionValues) ! sort ascending (f(x1) smallest, f(n+1) largest)
IF (debug .EQV. .TRUE.) WRITE(logUnit,'(A5,I0.4,A8,F10.4)') "STEP ", step, " f(x) = ", functionValues(1)
IF (functionValues(1) < threshold) THEN ! convergence test
converged = .TRUE.
optimum(:) = simplex(1,:)
optValue = functionValues(1)
RETURN
ENDIF
centroidPoint(:) = centroid(simplex(1:n,:))
!reflect
reflectedPoint(:) = centroidPoint(:) + (c_reflect * (centroidPoint(:)-simplex(m,:)))
f_reflect = objectFunction(reflectedPoint)
IF ((f_reflect >= functionValues(1)) .AND. (f_reflect < functionValues(n))) THEN
IF (debug .EQV. .TRUE.) WRITE(logUnit,*) "REFLECTING"
simplex(m,:) = reflectedPoint(:)
functionValues(m) = f_reflect
CYCLE
ELSE IF (f_reflect < functionValues(1)) THEN
!expand
expandedPoint(:) = centroidPoint(:) + (c_expand * (reflectedPoint(:) - centroidPoint(:)))
f_expand = objectFunction(expandedPoint)
IF (f_expand < f_reflect) THEN
IF (debug .EQV. .TRUE.) WRITE(logUnit,*) "EXPANDING"
simplex(m,:) = expandedPoint(:)
functionValues(m) = f_expand
ELSE
IF (debug .EQV. .TRUE.) WRITE(logUnit,*) "REFLECTING"
simplex(m,:) = reflectedPoint(:)
functionValues(m) = f_reflect
ENDIF
CYCLE
ELSE
!contract
contractedPoint(:) = centroidPoint(:) + (c_contract * (simplex(m,:) - centroidPoint(:)))
f_contract = objectFunction(contractedPoint)
IF (f_contract < functionValues(m)) THEN
IF (debug .EQV. .TRUE.) WRITE(logUnit,*) "CONTRACTING"
simplex(m,:) = contractedPoint(:)
functionValues(m) = f_contract
CYCLE
ELSE
!shrink
IF (debug .EQV. .TRUE.) WRITE(logUnit,*) "SHRINKING"
DO i = 2, m
simplex(i,:) = simplex(1,:) + c_shrink * (simplex(i,:) - simplex(1,:))
ENDDO
ENDIF
ENDIF
ENDDO
! If optimization fails, return best value located
optimum(:) = simplex(1,:)
optValue = functionValues(1)
RETURN
END SUBROUTINE NelderMead
!*
SUBROUTINE BubbleSort(simplex,functionValues)
! #DES: Basic bubble sort algorithm, can be replaced if necessary.
IMPLICIT NONE
REAL(8) :: simplex(:,:), functionValues(:)
reaL(8) :: f_temp, x_temp(SIZE(simplex,2))
INTEGER :: i, j, m
m = SIZE(simplex,1)
DO i = 1, m !loop over vectors
DO j = m, i+1, -1
IF (functionValues(j-1) > functionValues(j)) THEN
f_temp = functionValues(j-1)
functionValues(j-1) = functionValues(j)
functionValues(j) = f_temp
x_temp(:) = simplex(j-1,:)
simplex(j-1,:) = simplex(j,:)
simplex(j,:) = x_temp(:)
ENDIF
ENDDO
ENDDO
END SUBROUTINE BubbleSort
!*
FUNCTION centroid(input)
IMPLICIT NONE
REAL(8), INTENT(IN) :: input(:,:) !each row a vector
REAL(8) :: centroid(SIZE(input,2))
INTEGER :: i, j, m, n
m = SIZE(input,1)
n = SIZE(input,2)
centroid(:) = 0.0d0
DO i = 1, m
DO j = 1, n
centroid(j) = centroid(j) + input(i,j)
ENDDO
ENDDO
centroid(:) = centroid(:) / SIZE(input,2)
END FUNCTION centroid
!*
SUBROUTINE WriteSimplex(simplex,functionValues,unit)
! #DES: Write the provided simplex coords/values to unit.
IMPLICIT NONE
INTEGER, INTENT(IN) :: unit
REAL(8), INTENT(IN) :: simplex(:,:), functionValues(:)
INTEGER :: i, j
DO i = 1, SIZE(simplex,1)
WRITE(unit,'(I0.3,1X)',ADVANCE='NO') i
DO j = 1, SIZE(simplex,2)
WRITE(unit,'(F10.3,1X)',ADVANCE='NO') simplex(i,j)
ENDDO
WRITE(unit,'(F10.3)') functionValues(i)
ENDDO
WRITE(unit,*)
END SUBROUTINE WriteSimplex
!*
SUBROUTINE ProcessNameList(logUnit)
IMPLICIT NONE
INTEGER, INTENT(IN) :: logUnit
CALL ReadNameList(logUnit)
CALL CheckNameList()
CALL PrintNameList(logUnit)
END SUBROUTINE ProcessNameList
!*
SUBROUTINE ReadNameList(logUnit)
! #DES: Open, read and close the namelist file opt.nml
USE FileIO, ONLY : OpenFile, CloseFile
IMPLICIT NONE
INTEGER, INTENT(IN) :: logUnit
INTEGER, PARAMETER :: nmlUnit = 20
LOGICAL :: fileRead
CALL OpenFile(nmlUnit,"opt.nml","read",success=fileRead)
!standard algorithm parameter values
c_reflect = 1.0d0
c_expand = 2.0d0
c_contract = 0.5d0
c_shrink = 0.5d0
threshold = 1.0d-10
maxSteps = 100
IF (fileRead .EQV. .TRUE.) THEN
WRITE(logUnit,'(A)') "Optimization parameters will be read from opt.nml"
READ(nmlUnit,NML=opt)
CALL CloseFile(nmlUnit)
ELSE
WRITE(logUnit,'(A)') "Namelist opt.nml could not be read - default optimization parameters will be used"
ENDIF
END SUBROUTINE ReadNameList
!*
SUBROUTINE CheckNameList()
! There must be additional constraints on these values - expand > 1, 0 < contract < 1 CHECK
IMPLICIT NONE
IF (threshold < 0.0d0) STOP "Illegal (-ve) value of threshold in DownhillSimplex:CheckNameList"
IF (c_reflect < 0.0d0) STOP "Illegal (-ve) value of c_reflect in DownhillSimplex:CheckNameList"
IF (c_expand < 0.0d0) STOP "Illegal (-ve) value of c_expand in DownhillSimplex:CheckNameList"
IF (c_contract < 0.0d0) STOP "Illegal (-ve) value of c_contract in DownhillSimplex:CheckNameList"
IF (c_shrink < 0.0d0) STOP "Illegal (-ve) value of c_shrink in DownhillSimplex:CheckNameList"
IF (maxSteps < 1) STOP "Illegal (-ve) value of maxSteps in DownhillSimplex:CheckNameList"
END SUBROUTINE CheckNameList
!*
SUBROUTINE PrintNameList(logUnit)
IMPLICIT NONE
INTEGER, INTENT(IN) :: logUnit
WRITE(logUnit,'(A)') "* Optimization Parameters *"
WRITE(logUnit,*) ""
WRITE(logUnit,'(A,F6.2)') "Reflection Coefficient (alpha) = ", c_reflect
WRITE(logUnit,'(A,F6.2)') "Expansion Coefficient (beta) = ", c_expand
WRITE(logUnit,'(A,F6.2)') "Contraction Coefficient (gamma) = ", c_contract
WRITE(logUnit,'(A,F6.2)') "Shrinking Coefficient (sigma) = ", c_shrink
WRITE(logUnit,'(A,F6.2)') "Convergence Threshold = ", threshold
WRITE(logUnit,'(A,I6)') "Maximum Steps = ", maxSteps
WRITE(logUnit,*)
END SUBROUTINE PrintNameList
END MODULE DownhillSimplex
! SUBROUTINE WriteVector(input,unit)
! ! #DES: Print a vector to a single line on the provided unit.
! IMPLICIT NONE
! INTEGER, INTENT(IN) :: unit
! REAL(8), INTENT(IN) :: input(:)
! INTEGER :: i
! DO i = 1, SIZE(input)
! WRITE(unit,'(F6.2)',ADVANCE='NO') input(i)
! ENDDO
! WRITE(unit,*)
! END SUBROUTINE WriteVector