-
Notifications
You must be signed in to change notification settings - Fork 7
/
sann.f90
569 lines (470 loc) · 16.7 KB
/
sann.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
module SANN
contains
subroutine SA(dataFile)
use MODEL
use FUNC
implicit none
! SANN settings (temperature, cooling rate etc)
real :: T, EPS, RT, FOPT, FP, VMtmp, Ctmp
integer :: NS, NT, IER, MAXEVL, IPRINT, NEPS
! SANN parameter ranges and associated files (dynamically sized)
real, allocatable :: X(:), LB(:), UB(:), C(:), VM(:),&
FSTAR(:),XOPT(:), XP(:)
! more parameter mess yay
integer :: N, NACC,NOBDS, NFCNEV
logical :: MAX, QUIT, QUIT_IT
real :: F, P, PP, RATIO, RNUMBER,&
acceptance_rate,rejection_rate
integer :: NUP, NDOWN, NREJ, NNEW, LNOBDS,&
H, I, J, M, ENOUT, PAROUT
! data
real :: dataFile(:,:,:)
! stuff needed to read the input data files
character(len=200) :: line, filepath, sannPar, modRange
integer :: io, count, nrsites
integer :: nrPar, extPar
! random number generated related declarations
integer :: rand_seed(18), seed_size
integer, allocatable :: seed(:), NACP(:)
sannPar = "./parameters/SANN_parameters.txt"
modRange= "./parameters/model_parameter_ranges.txt"
! ---------------------- IMPORT PARAMETERS -----------------------------
! read first argument, and determine the number of files in the
! sites file
count = command_argument_count()
if (count < 1) then
call exit(0)
else
call get_command_argument(1, filepath)
! read the number of sites in the file
call LinesInFile(filepath,nrsites)
end if
! import the SANN settings: Temperature / cooling rate etc
open(1, file = trim(adjustl(sannPar)))
i = 1
sannpar_loop: do
read(1,'(A)',iostat=io) line
if (io < 0) then
exit sannpar_loop
else if ( index(line, "#") /= 0 ) then
else if ( line == "" ) then
else
select case (i)
case (1)
read(line,*) T
case (2)
read(line,*) EPS
case (3)
read(line,*) RT
case (4)
read(line,*) NS
case (5)
read(line,*) NT
case (6)
read(line,*) MAXEVL
case (7)
read(line,*) VMtmp
case (8)
read(line,*) Ctmp
case (9)
read(line,*) NEPS
case (10)
read(line,*) IPRINT
end select
i = i + 1
end if
end do sannpar_loop
close(1)
! get the number of parameters
call LinesInFile(modRange,nrPar)
! after reading in parameters, determine how many there are
N = nrPar !+ nrsites
! allocate memory to upper / lower boundaries and par vector
allocate(LB(N),UB(N),X(N))
! but also allocate memory to the matrices holding the intermediate
! optimization results which should be of the same size!!
allocate(XP(N),XOPT(N),C(N),VM(N),&
FSTAR(NEPS),NACP(N))
! set step length for the different parameters
do i = 1, N
VM(i) = VMtmp
C(i) = Ctmp
end do
! set all vectors to 0
LB = 0
UB = 0
X = 0
! read all parameters
open(1, file =trim(adjustl(modRange)))
i = 1
parameter_loop: do
read(1,'(A)',iostat=io) line
if (io < 0) then
exit parameter_loop
else if ( index(line, "#") /= 0 ) then
else if ( line == "" ) then
else
read(line,*) lb(i), ub(i)
i = i + 1
end if
end do parameter_loop
close(1)
do i=1,nrPar
X(i) = (LB(i) + UB(i))/2
end do
! ---------------------- IMPORT PARAMETERS - DONE ----------------------
! Print information on the input parameters as defined in the
! SANN_parameters file
write(*,1000) N, T, RT, EPS, NS, NT, NEPS, MAXEVL
1000 format(/,15x,'INITIATING SIMULATED ANNEALING' ,/,&
5x ,'Number of parameters - no obs. operators : ',I3/,&
5x ,'Initial Temperature (T) : ',F8.4/,&
5x ,'Temperature reduction factor (RT) : ',F8.4/,&
5x ,'Error tolerance for termination (EPS) : ',F8.4/,&
5x ,'Number of Cycles (NS) : ',I10/,&
5x ,'Itt. before Temp. reduction (NT) : ',I10/,&
5x ,'Nr. stable function values before exit (NEPS) : ',I10/,&
5x ,'Maximum function evaluations (MAXEVL) : ',I10)
! Generate random seed using machine state (CPU / clock)
call random_seed() ! initialize with system generated seed
call random_seed(size=seed_size) ! find out size of seed
allocate(seed(seed_size))
call random_seed(get=seed) ! get system generated seed
! general flag to quit the routine, set processing
QUIT = .false.
QUIT_IT = .false.
MAX = .false.
ENOUT = 200
PAROUT = 300
! set initial values
NACC = 0
NOBDS = 0
NFCNEV = 0
IER = 99
do I = 1, N
XOPT(I) = X(I) ! copy the start parameter values inot XOPT (optimal parameters)
NACP(I) = 0 ! set number of accepted trials per parameter to 0
end do
do I = 1, NEPS
FSTAR(I) = 1.0D+20 ! set F* to a very high value (to be minimized)
end do
! Give warning when temperature is negative, exit the SANN run
! by setting QUIT to TRUE
if (T .LE. 0.0) then
QUIT = .true.
write(*,'(/,5x,'' THE INITIAL TEMPERATURE IS NOT POSITIVE. CHANGE VALUE!'',/)')
IER = 3
end if
! Give a warning when the parameters are out of bounds, set QUIT
do I = 1, N
if ((X(I) .gt. UB(I)) .or. (X(I) .lt. LB(I))) THEN
QUIT = .true.
call PRT1
end if
end do
XP = X
do I = 1, 5
! Evaluate the function with input X and return value as F
call cost(XP,dataFile,F)
X = XP
end do
if(.not. MAX) F = -F
NFCNEV = NFCNEV + 1
FOPT = F
FSTAR(1) = F
if(IPRINT .eq. 1) call PRT2(MAX,X,F)
! write output to file
open(ENOUT,file="summarizing_parameters.txt")
!-- MAIN LOOP
whileloop: do
NUP = 0
NREJ = 0
NNEW = 0
NDOWN = 0
LNOBDS = 0
!-- should we not start the routine, as certain conditions are not met (see above)
if (QUIT) exit whileloop
tempitterations: do M = 1, NT !-- loop which contains the statements before quenching temperature
updatesteps: do J = 1, NS !-- loop which contains statements before updateing step sizes
parameters: do H = 1, N !-- loop which goes through X parameters and updates them
!-- we update the parameters to be evaluated
!-- uniform random selection within the parameter range
do I = 1, N
!-- for every parameter tweak slightly
if (I .eq. H) then
call random_number(RNUMBER)
XP(I) = X(I) + (RNUMBER*2.-1.) * VM(I)
else
XP(I) = X(I)
end if
!-- if out of bound constrain
if ((XP(I) .lt. LB(I)) .or. (XP(I) .gt. UB(I))) then
call random_number(RNUMBER)
XP(I) = LB(I) + (UB(I) - LB(I))*RNUMBER
LNOBDS = LNOBDS + 1
NOBDS = NOBDS + 1
if(IPRINT .eq. 3) call PRT3(MAX,XP,X,FP,F)
end if
end do
!-- evaluate the cost function with the new parameters
!-- write results to a variable FP
call cost(XP,dataFile,FP)
if (.not. MAX) FP = -FP
NFCNEV = NFCNEV + 1
if(IPRINT .eq. 3) call PRT4(MAX,XP,X,FP,F)
!-- check if the maximum number of function evaluations
!-- has been exceeded, if so flag as last itteration and exit
if (NFCNEV .ge. MAXEVL) then
call PRT5
if (.not. MAX) FOPT = -FOPT
IER = 1
QUIT_IT = .true. !-- quit the loop when maximum eval exceeded, write last
exit tempitterations !-- parameters to file
end if
!-- if the new model error value is lower than the previous one
!-- update parameter array to reflect this change
if(FP .ge. F) then
if(IPRINT .eq. 3) then
write(*,'('' POINT ACCEPTED'')')
end if
X = XP !-- update parameters
F = FP !-- set new error minimum
NACC = NACC + 1 !-- add a successful trial to the counter (global)
NACP(H) = NACP(H) + 1 !-- add a successful trial to the counter for this parameter
NDOWN = NDOWN + 1 !-- mark move as down
!-- if the new value is smaller than ALL previous ones
!-- update parameters to reflect this change (set global optimum)
if (FP .gt. FOPT) then !-- minimized value is lower than lowest on record
if(IPRINT .eq. 3) then
write(*,'('' NEW OPTIMUM'')')
end if
XOPT = XP
FOPT = FP
NNEW = NNEW + 1
end if
!-- this is the METROPOLIS section
!-- which allows random 'uphill' moves
!-- in our 'downhill' optimization...
else
P = exp((FP - F)/T) !-- determine metropolis 'cut'
call random_number(PP) !-- random number
if (PP .lt. P) THEN !-- compare and decide
if (IPRINT .eq. 3) call PRT6(MAX)
X = XP !-- update the parameter even if it is 'not optimal'
F = FP !-- set new minimum / maximum
NACC = NACC + 1 !-- add a successful trial to the counter (global)
NACP(H) = NACP(H) + 1 !-- add a successful trial to the counter for this parameter
NUP = NUP + 1 !-- mark move as going up, away from minimum
else
NREJ = NREJ + 1 !-- if the move is not accepted, do nothing, mark as rejected
if (IPRINT .eq. 3) call PRT7(MAX)
end if
end if
write(ENOUT,*) T,FOPT,X
end do parameters
end do updatesteps
!-- dump everything to a line
if (IPRINT .eq. 5) write(*,*) T,FOPT,F,X
!-- Adjust VM so that approximately half of all evaluations are accepted.
do I = 1, N
RATIO = dfloat(NACP(I)) /dfloat(NS)
acceptance_rate = .5
rejection_rate = 1 - acceptance_rate
if (RATIO .gt. acceptance_rate) then
VM(I) = VM(I)*(1. + C(I) * (RATIO - acceptance_rate)/rejection_rate)
else if (RATIO .lt. rejection_rate) then
VM(I) = VM(I)/(1. + C(I) * (rejection_rate - RATIO)/rejection_rate)
end if
if (VM(I) .gt. (UB(I)-LB(I))) then
VM(I) = UB(I) - LB(I)
end if
end do
if (IPRINT .eq. 2) then
call PRT8(VM,XOPT,X)
end if
!-- reset acceptance values
do I = 1, N
NACP(I) = 0
end do
end do tempitterations
! provide feedback on this temperature itteration
if (IPRINT .eq. 1) then
call PRT9(max,T,XOPT,VM,FOPT,NUP,NDOWN,NREJ,LNOBDS,NNEW)
end if
!-- Check termination criteria.
QUIT = .false.
FSTAR(1) = F
if ((FOPT - FSTAR(1)) .LE. EPS) QUIT = .true.
do I = 1, NEPS
if (abs(F - FSTAR(I)) .gt. EPS) QUIT = .false.
end do
!-- Terminate SA if appropriate.
if (QUIT .or. QUIT_IT) then
X = XOPT
IER = 0
if (.not. max) FOPT = -FOPT
!-- give some feedback on termination criteria
call PRT10(NFCNEV,NACC,NOBDS,T,F)
!-- write everything to file !!!
open(1,file="./parameters/optimized_model_parameters.txt")
write(*,*) "Optimal Parameters:"
do i=1,size(X)
write(*,*) X(i)
write(1,*) X(i)
end do
close(1)
exit whileloop !-- quit processing if criteria met
end if
!-- If termination criteria is not met, prepare for another loop.
T = RT*T !-- adjust temperature
do I = NEPS, 2, -1
FSTAR(I) = FSTAR(I-1)
end do
F = FOPT
X = XOPT
end do whileloop
close(ENOUT)
!-- END MAIN LOOP
end subroutine SA
!-- These are plotting subroutines for verbose output of results
subroutine PRT1
write(*,'(5x,'' THE STARTING VALUE (X) IS OUTSIDE THE BOUNDS '',/,&
5x,'' (LB AND UB). EXECUTION TERMINATED WITHOUT ANY'',/,&
5x,'' OPTIMIZATION. RESPECIFY X, UB OR LB SO THAT '',/,&
5x,'' LB(I) .lt. X(I) .lt. UB(I), I = 1, N. ''/)')
end subroutine PRT1
!
subroutine PRT2(max,X,F)
real :: X(:), F
logical max
write(*,'(5x,'' '')')
call PRTVEC(X,'INITIAL X')
if (max) then
write(*,'(5x,'' INITIAL F: '',/, G25.18,/)') F
else
write(*,'(5x,'' INITIAL F: '',/, G25.18,/)') -F
end if
end subroutine PRT2
subroutine PRT3(max,XP,X,FP,F)
real :: XP(:), X(:), FP, F
logical max
write(*,'(5x,'' '')')
call PRTVEC(X,'CURRENT X')
if (max) then
write(*,'(5x,'' CURRENT F: '',G25.18)') F
else
write(*,'(5x,'' CURRENT F: '',G25.18)') -F
end if
call PRTVEC(XP,'TRIAL X')
write(*,'(5x,'' POINT REJECTED SINCE OUT OF BOUNDS'')')
end subroutine PRT3
subroutine PRT4(max,XP,X,FP,F)
real :: XP(:), X(:), FP, F
logical max
write(*,'(5x,'' '')')
call PRTVEC(X,'CURRENT X')
if (max) then
write(*,'(5x,'' CURRENT F: '',G25.18)') F
call PRTVEC(XP,'TRIAL X')
write(*,'(5x,'' RESULTING F: '',G25.18)') FP
else
write(*,'(5x,'' CURRENT F: '',G25.18)') -F
call PRTVEC(XP,'TRIAL X')
write(*,'(5x,'' RESULTING F: '',G25.18)') -FP
end if
end subroutine PRT4
subroutine PRT5
write(*,'(5x,'' TOO MANY FUNCTION EVALUATIONS; CONSIDER '',/,&
5x,'' INCREASING MAXEVL OR EPS, OR DECREASING '',/,&
5x,'' NT OR RT. THESE RESULTS ARE LIKELY TO BE '',/,&
5x,'' POOR.'',/)')
end subroutine PRT5
subroutine PRT6(max)
logical max
if (max) then
write(*,'(5x,'' THOUGH LOWER, POINT ACCEPTED'')')
else
write(*,'(5x,'' THOUGH HIGHER, POINT ACCEPTED'')')
end if
end subroutine PRT6
subroutine PRT7(max)
logical max
if (max) then
write(*,'('' LOWER POINT REJECTED'')')
else
write(*,'('' HIGHER POINT REJECTED'')')
end if
end subroutine PRT7
subroutine PRT8(VM,XOPT,X)
real :: VM(:), XOPT(:), X(:)
write(*,'(/,5x,'' INTERMEDIATE RESULTS AFTER STEP LENGTH ADJUSTMENT'',/)')
call PRTVEC(VM,'NEW STEP LENGTH (VM)')
call PRTVEC(XOPT,'CURRENT OPTIMAL X')
call PRTVEC(X,'CURRENT X')
write(*,'(5x,'' '')')
end subroutine PRT8
subroutine PRT9(max,T,XOPT,VM,FOPT,NUP,NDOWN,NREJ,LNOBDS,NNEW)
real :: XOPT(:), VM(:), T, FOPT
integer :: NUP, NDOWN, NREJ, LNOBDS, NNEW, TOTMOV
logical :: max
TOTMOV = NUP + NDOWN + NREJ
write(*,'(/,'' INTERMEDIATE RESULTS BEFORE NEXT TEMPERATURE REDUCTION'',/)')
write(*,'('' CURRENT TEMPERATURE : '',G12.5)') T
if (max) then
write(*,'('' MAX FUNCTION VALUE SO FAR : '',G25.18)') FOPT
write(*,'('' TOTAL MOVES : '',I8)') TOTMOV
write(*,'('' UPHILL : '',I8)') NUP
write(*,'('' ACCEPTED DOWNHILL : '',I8)') NDOWN
write(*,'('' REJECTED DOWNHILL : '',I8)') NREJ
write(*,'('' OUT OF BOUNDS TRIALS : '',I8)') LNOBDS
write(*,'('' NEW MAXIMA THIS TEMPERATURE : '',I8)') NNEW
else
write(*,'('' MIN FUNCTION VALUE SO FAR : '',G25.18)') -FOPT
write(*,'('' TOTAL MOVES : '',I8)') TOTMOV
write(*,'('' DOWNHILL : '',I8)') NUP
write(*,'('' ACCEPTED UPHILL : '',I8)') NDOWN
write(*,'('' REJECTED UPHILL : '',I8)') NREJ
write(*,'('' TRIALS OUT OF BOUNDS : '',I8)') LNOBDS
write(*,'('' NEW MINIMA THIS TEMPERATURE : '',I8)') NNEW
end if
call PRTVEC(XOPT,'CURRENT OPTIMAL PARAMETERS')
call PRTVEC(VM,'STEP LENGTH (VM)')
write(*,'('' '')')
end subroutine PRT9
subroutine PRT10(NFCNEV,NACC,NOBDS,T, F)
integer :: NFCNEV, NACC, NOBDS
real :: T, FSTAR
write(*,1001) NFCNEV, NACC, NOBDS, T, F
1001 format(/,10x ,'SANN ACHIEVED TERMINATION CRITERIA. IER = 0.',/,&
5x ,' Number of functions evaluated : ',I10 ,/,&
5x ,' Number of accepted functions : ',I10 ,/,&
5x ,' Number of out of bound functions: ',I10 ,/,&
5x ,' Final Temperature : ',G20.13,/,&
5x ,' Final Model Error : ',G20.13,/)
end subroutine PRT10
subroutine PRTVEC(VECTOR,NAME)
! This subroutine prints the double precision vector named VECTOR.
! Elements 1 thru NCOLS will be printed. NAME is a character variable
! that describes VECTOR. Note that if NAME is given in the call to
! PDA_PRTVEC, it must be enclosed in quotes. If there are more than 10
! elements in VECTOR, 10 elements will be printed on each line.
real :: VECTOR(:)
integer :: NCOLS,I
character *(*) NAME
NCOLS = size(VECTOR)
write(*,1001) NAME
if (NCOLS .gt. 10) then
LINES = INT(NCOLS/10.)
do 100, I = 1, LINES
LL = 10*(I - 1)
write(*,1000) (VECTOR(J),J = 1+LL, 10+LL)
100 continue
write(*,1000) (VECTOR(J),J = 11+LL, NCOLS)
else
write(*,1000) (VECTOR(J),J = 1, NCOLS)
end if
1000 format( 10(G12.5,1X))
1001 format(/,5X,A)
return
end subroutine PRTVEC
end module SANN