-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDIRsubrout.f
1553 lines (1466 loc) · 61.7 KB
/
DIRsubrout.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
C+-----------------------------------------------------------------------+
C| Program : Direct.f (subfile DIRsubrout.f) |
C| Last modified : 07-16-2001 |
C| Written by : Joerg Gablonsky |
C| Subroutines used by the algorithm DIRECT. |
C+-----------------------------------------------------------------------+
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRChoose |
C| Decide, which is the next sampling point. |
C| Changed 09/25/00 JG |
C| Added maxdiv to call and changed S to size maxdiv. |
C| Changed 01/22/01 JG |
C| Added Ifeasiblef to call to keep track if a feasible point has|
C| been found. |
C| Changed 07/16/01 JG |
C| Changed if statement to prevent run-time errors. | |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRChoose(anchor,S,actdeep,f,fmin,eps,thirds,maxpos,
+ length,maxfunc,maxdeep,maxdiv,n,logfile,dwrit,cheat,kmax,
+ Ifeasiblef)
IMPLICIT None
Double Precision MaxLower
Parameter (MaxLower = 1.D20)
Integer maxfunc,maxdeep,n, maxdiv
Integer Anchor(-1:maxdeep),S(maxdiv,2),length(maxfunc,n)
Integer actdeep,dwrit
Integer DIRGetlevel
Double Precision f(maxfunc,2),fmin,eps,thirds(0:maxfunc)
Integer maxpos,i,j,k,i_,j_,logfile,cheat
Double Precision help2,helplower,helpgreater,kmax
Integer novalue,novaluedeep
Integer Ifeasiblef
helplower = MaxLower
helpgreater = 0.D0
k = 1
if (Ifeasiblef .ge. 1) then
DO 1001,j=0,actdeep
IF (anchor(j) .GT. 0) THEN
S(k,1) = anchor(j)
S(k,2) = DIRGetlevel(S(k,1),length,maxfunc,n)
goto 12
END IF
1001 CONTINUE
12 k = k + 1
maxpos = 1
return
else
DO 10,j=0,actdeep
IF (anchor(j) .GT. 0) THEN
S(k,1) = anchor(j)
S(k,2) = DIRGetlevel(S(k,1),length,maxfunc,n)
k = k + 1
END IF
10 CONTINUE
END IF
novalue = 0
if (anchor(-1) .gt. 0) then
novalue = anchor(-1)
novaluedeep = DIRGetlevel(novalue,length,maxfunc,n)
end if
maxpos = k - 1
DO 11,j=k-1,maxdeep
S(k,1) = 0
11 CONTINUE
DO 40,j=maxpos,1,-1
helplower = Maxlower
helpgreater = 0.D0
j_ = S(j,1)
DO 30,i=1,j-1
i_ = S(i,1)
C+-----------------------------------------------------------------------+
C| JG 07/16/01 Changed IF statement into two to prevent run-time errors |
C| which could occur if the compiler checks the second |
C| expression in an .AND. statement although the first |
C| statement is already not true. |
C+-----------------------------------------------------------------------+
IF ((i_ .GT. 0) .AND. .NOT. (i .EQ. j)) THEN
IF (f(i_,2) .le. 1.D0) THEN
help2 = thirds(S(i,2)) - thirds(S(j,2))
help2 = (f(i_,1) - f(j_,1))/help2
IF (help2 .LE. 0.D0) THEN
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "thirds > 0,help2 <= 0"
END IF
END IF
GOTO 60
END IF
IF (help2 .LT. helplower) THEN
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "helplower = ",help2
END IF
END IF
helplower = help2
END IF
END IF
END IF
30 CONTINUE
DO 31,i=j+1,maxpos
i_ = S(i,1)
C+-----------------------------------------------------------------------+
C| JG 07/16/01 Changed IF statement into two to prevent run-time errors |
C| which could occur if the compiler checks the second |
C| expression in an .AND. statement although the first |
C| statement is already not true. |
C+-----------------------------------------------------------------------+
IF ((i_ .GT. 0) .AND. .NOT. (i .EQ. j)) THEN
IF (f(i_,2) .le. 1.D0) THEN
help2 = thirds(S(i,2)) - thirds(S(j,2))
help2 = (f(i_,1) - f(j_,1))/help2
IF (help2 .LE. 0.D0) THEN
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "thirds < 0,help2 <= 0"
END IF
END IF
GOTO 60
END IF
IF (help2 .GT. helpgreater) THEN
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "helpgreater = ",help2
END IF
END IF
helpgreater = help2
END IF
END IF
END IF
31 CONTINUE
IF ((helplower .GT. Maxlower) .AND.
+ (helpgreater .gt. 0)) THEN
helplower = helpgreater
helpgreater = helpgreater - 1.D0
END IF
IF (helpgreater .LE. helplower) THEN
IF ((cheat .EQ. 1) .AND. (helplower .GT. Kmax)) THEN
helplower = Kmax
END IF
IF ((f(j_,1) - helplower * thirds(S(j,2))) .GT.
+ (fmin - eps*abs(fmin))) THEN
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "> fmin - eps|fmin|"
END IF
END IF
GOTO 60
END IF
ELSE
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,*) "helpgreater > helplower",helpgreater,
+ helplower,helpgreater - helplower
END IF
END IF
GOTO 60
END IF
GOTO 40
60 S(j,1) = 0
40 CONTINUE
if (novalue .gt. 0) then
maxpos = maxpos + 1
S(maxpos,1) = novalue
S(maxpos,2) = novaluedeep
end if
END
C+-----------------------------------------------------------------------+
C| INTEGER Function GetmaxDeep |
C| function to get the maximal length (1/length) of the n-dimensional |
C| rectangle with midpoint pos. |
C| |
C| On Return : |
C| the maximal length |
C| |
C| pos -- the position of the midpoint in the array length |
C| length -- the array with the dimensions |
C| maxfunc -- the leading dimension of length |
C| n -- the dimension of the problem |
C| |
C+-----------------------------------------------------------------------+
INTEGER Function DIRGetmaxDeep(pos,length,maxfunc,n)
IMPLICIT None
INTEGER pos,maxfunc,n,length(maxfunc,n),help,i
help = length(pos,1)
DO 10,i = 2,n
help = min(help, length(pos,i))
10 CONTINUE
DIRGetMaxDeep = help
END
C+-----------------------------------------------------------------------+
C| INTEGER Function DIRGetlevel |
C| Returns the level of the hyperrectangle. Depending on the value of the|
C| global variable JONES. IF JONES equals 0, the level is given by |
C| kN + p, where the rectangle has p sides with a length of|
C| 1/3^(k+1), and N-p sides with a length of 1/3^k. |
C| If JONES equals 1, the level is the power of 1/3 of the length of the |
C| longest side hyperrectangle. |
C| |
C| On Return : |
C| the maximal length |
C| |
C| pos -- the position of the midpoint in the array length |
C| length -- the array with the dimensions |
C| maxfunc -- the leading dimension of length |
C| n -- the dimension of the problem |
C| |
C+-----------------------------------------------------------------------+
INTEGER Function DIRGetlevel(pos,length,maxfunc,n)
IMPLICIT None
INTEGER pos,maxfunc,n,length(maxfunc,n),help,i,p,k
C JG 09/15/00 Added variable JONES (see above)
Integer JONES
COMMON /directcontrol/ JONES
IF (JONES .eq. 0) THEN
help = length(pos,1)
k = help
p = 1
DO 100,i = 2,n
IF (length(pos,i) .LT. k) THEN
k = length(pos,i)
END IF
IF (length(pos,i) .EQ. help) THEN
p = p + 1
END IF
100 CONTINUE
IF (k .EQ. help) THEN
DIRGetLevel = k*n + n-p
ELSE
DIRGetLevel = k*n + p
END IF
ELSE
help = length(pos,1)
DO 10,i = 2,n
IF (length(pos,i) .LT. help) THEN
help = length(pos,i)
END IF
10 CONTINUE
DIRGetLevel = help
END IF
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRDoubleInsert |
C| Routine to make sure that if there are several potential optimal |
C| hyperrectangles of the same level (i.e. hyperrectangles that have|
C| the same level and the same function value at the center), all of|
C| them are divided. This is the way as originally described in |
C| Jones et.al. |
C| JG 07/16/01 Added errorflag to calling sequence. We check if more |
C| we reach the capacity of the array S. If this happens, we |
C| return to the main program with an error. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRDoubleInsert(anchor, S, maxpos, point, f,
+ maxdeep, maxfunc, maxdiv, IError)
IMPLICIT None
Integer maxpos, maxdeep, maxfunc, maxdiv
Integer anchor(-1:maxdeep),S(maxdiv,2)
Integer point(maxfunc)
Double Precision f(maxfunc,2)
C+-----------------------------------------------------------------------+
C| JG 07/16/01 Added flag to prevent run time-errors on some systems. |
C+-----------------------------------------------------------------------+
Integer iflag, IError
Integer oldmaxpos, i, pos, help, actdeep
oldmaxpos = maxpos
DO 10, i = 1,oldmaxpos
IF (S(i,1) .GT. 0) THEN
actdeep = S(i,2)
help = anchor(actdeep)
pos = point(help)
iflag = 0
C+-----------------------------------------------------------------------+
C| JG 07/16/01 Added flag to prevent run time-errors on some systems. On |
C| some systems the second conditions in an AND statement is |
C| evaluated even if the first one is already not true. |
C+-----------------------------------------------------------------------+
DO WHILE ((pos .GT. 0) .AND. (iflag .eq. 0))
if (f(pos,1) - f(help,1) .LE. 1.D-13) then
if (maxpos .lt. maxdiv) then
maxpos = maxpos + 1
S(maxpos,1) = pos
S(maxpos,2) = actdeep
pos = point(pos)
else
C+-----------------------------------------------------------------------+
C| JG 07/16/01 Maximum number of elements possible in S has been reached!|
C+-----------------------------------------------------------------------+
IError = -6
return
end if
else
iflag = 1
end if
END DO
END IF
10 CONTINUE
END
C+-----------------------------------------------------------------------+
C| JG Added 09/25/00 |
C| SUBROUTINE DIRreplaceInf |
C| |
C| Find out if there are infeasible points which are near feasible ones. |
C| If this is the case, replace the function value at the center of the |
C| hyper rectangle by the lowest function value of a nearby function. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRreplaceInf(free,freeold,f,c,thirds,length,anchor,
+ point,c1,c2,maxfunc,maxdeep,maxdim,n,logfile, fmax)
Implicit None
Integer maxfunc, maxdeep, maxdim, n, free, freeold, logfile
Double Precision f(maxfunc,2)
Integer anchor(-1:maxdeep)
Integer point(maxfunc)
Double Precision c1(n),c2(n)
Double Precision c(maxfunc,MaxDim)
Double Precision thirds(0:maxdeep)
Integer length(maxfunc,MaxDim)
Integer LMaxDim
PARAMETER (LMaxDim = 32)
Double Precision sidelength
Double Precision a(LmaxDim),b(LmaxDim),x(LmaxDim)
Integer i,j,k,l, help, Isinbox
Integer DIRgetmaxdeep
C+-----------------------------------------------------------------------+
C| JG 01/22/01 Added variable to keep track of the maximum value found. |
C+-----------------------------------------------------------------------+
Double Precision fmax
DO 10, i = 1,free-1
if (f(i,2) .gt. 0) then
C+-----------------------------------------------------------------------+
C| Get the maximum side length of the hyper rectangle and then set the |
C| new side length to this lengths times the growth factor. |
C+-----------------------------------------------------------------------+
help = DIRgetmaxdeep(i,length,maxfunc,n)
sidelength = thirds(help)*2.D0
C+-----------------------------------------------------------------------+
C| Set the Center and the upper and lower bounds of the rectangles. |
C+-----------------------------------------------------------------------+
do 20, j = 1,n
sidelength = thirds(length(i,j))
a(j) = c(i,j) - sidelength
b(j) = c(i,j) + sidelength
20 continue
C+-----------------------------------------------------------------------+
C| The function value is reset to 'Inf', since it may have been changed |
C| in an earlier iteration and now the feasible point which was close |
C| is not close anymore (since the hyper rectangle surrounding the |
C| current point may have shrunk). |
C+-----------------------------------------------------------------------+
f(i,1) = 1.0E+6
f(i,2) = 2.D0
C+-----------------------------------------------------------------------+
C| Check if any feasible point is near this infeasible point. |
C+-----------------------------------------------------------------------+
DO 30, k = 1,free-1
C+-----------------------------------------------------------------------+
C| If the point k is feasible, check if it is near. |
C+-----------------------------------------------------------------------+
if (f(k,2) .eq. 0) then
C+-----------------------------------------------------------------------+
C| Copy the coordinates of the point k into x. |
C+-----------------------------------------------------------------------+
DO 40, l = 1,n
x(l) = c(k,l)
40 continue
C+-----------------------------------------------------------------------+
C| Check if the point k is near the infeasible point, if so, replace the |
C| value at
C+-----------------------------------------------------------------------+
if (Isinbox(x,a,b,n,Lmaxdim) .eq. 1) then
f(i,1) = min(f(i,1), f(k,1))
f(i,2) = 1.D0
end if
end if
30 continue
if (f(i,2) .eq. 1.0D0) then
f(i,1) = f(i,1) + 1.0E-6*abs(f(i,1))
do 200,l=1,n
x(l)=c(i,l)*c1(l)+c(i,l)*c2(l)
200 continue
CALL DIRResortlist(i,anchor,f,point,length,n,maxfunc,
+ maxdim,maxdeep,logfile)
else
C+-----------------------------------------------------------------------+
C| JG 01/22/01 |
C| Replaced fixed value for infeasible points with maximum value found, |
C| increased by 1. |
C+-----------------------------------------------------------------------+
if (.NOT. (fmax .eq. f(i,1))) then
f(i,1) = max(fmax + 1.0D0,f(i,1))
end if
end if
end if
10 continue
1000 format(20f18.8)
END
C+-----------------------------------------------------------------------+
C| JG Added 09/25/00 |
C| |
C| SUBROUTINE DIRResortlist |
C| |
C| Resort the list so that the infeasible point is in the list with the |
C| replaced value. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRResortlist(replace,anchor,f,point,length,n,maxfunc,
+ maxdim,maxdeep,logfile)
Implicit None
Integer maxfunc, maxdim, maxdeep, n, logfile
Integer replace
Double Precision f(maxfunc,2)
Integer anchor(-1:maxdeep)
Integer point(maxfunc)
Integer length(maxfunc,MaxDim)
Integer start, l, i, pos
Integer DIRgetlevel
C+-----------------------------------------------------------------------+
C| Get the length of the hyper rectangle with infeasible mid point and |
C| Index of the corresponding list. |
C+-----------------------------------------------------------------------+
C JG 09/25/00 Replaced with DIRgetlevel
C l = DIRgetmaxDeep(replace,length,maxfunc,n)
l = DIRgetlevel(replace,length,maxfunc,n)
start = anchor(l)
C+-----------------------------------------------------------------------+
C| If the hyper rectangle with infeasibel midpoint is already the start |
C| of the list, give out message, nothing to do. |
C+-----------------------------------------------------------------------+
if (replace .eq. start) then
C write(logfile,*) 'No resorting of list necessarry, since new ',
C + 'point is already anchor of list .',l
else
C+-----------------------------------------------------------------------+
C| Take the hyper rectangle with infeasible midpoint out of the list. |
C+-----------------------------------------------------------------------+
pos = start
do 10, i = 1,maxfunc
if (point(pos) .eq. replace) then
point(pos) = point(replace)
goto 20
else
pos = point(pos)
end if
if (pos .eq. 0) then
IF (logfile /= 99999) then
write(logfile,*) 'Error in DIRREsortlist: We went ',
+ 'through the whole list and could not find the point to
+ replace!!'
END IF
goto 20
end if
10 continue
C+-----------------------------------------------------------------------+
C| If the anchor of the list has a higher value than the value of a |
C| nearby point, put the infeasible point at the beginning of the list. |
C+-----------------------------------------------------------------------+
20 if (f(start,1) .gt. f(replace,1)) then
anchor(l) = replace
point(replace) = start
C write(logfile,*) 'Point is replacing current anchor for '
C + , 'this list ',l,replace,start
else
C+-----------------------------------------------------------------------+
C| Insert the point into the list according to its (replaced) function |
C| value. |
C+-----------------------------------------------------------------------+
pos = start
do 30, i = 1,maxfunc
C+-----------------------------------------------------------------------+
C| The point has to be added at the end of the list. |
C+-----------------------------------------------------------------------+
if (point(pos) .eq. 0) then
point(replace) = point(pos)
point(pos) = replace
C write(logfile,*) 'Point is added at the end of the '
C + , 'list ',l, replace
goto 40
else
if (f(point(pos),1) .gt. f(replace,1)) then
point(replace) = point(pos)
point(pos) = replace
C write(logfile,*) 'There are points with a higher '
C + ,'f-value in the list ',l,replace, pos
goto 40
end if
pos = point(pos)
end if
30 continue
40 pos = pos
end if
end if
end
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRInsertList |
C| Changed 02-24-2000 |
C| Got rid of the distinction between feasible and infeasible points|
C| I could do this since infeasible points get set to a high |
C| function value, which may be replaced by a function value of a |
C| nearby function at the end of the main loop. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRInsertList(new,anchor,point,f,maxI,
+ length,maxfunc,maxdeep,n,samp)
IMPLICIT None
INTEGER maxfunc,maxdeep,j,maxI,n,samp
INTEGER pos1,pos2,pos,new,deep,anchor(-1:maxdeep)
INTEGER point(maxfunc),length(maxfunc,n)
C JG 09/24/00 Changed this to Getlevel
INTEGER DIRGetlevel
Double Precision f(maxfunc,2)
DO 10,j = 1,maxI
pos1 = new
pos2 = point(pos1)
new = point(pos2)
C JG 09/24/00 Changed this to Getlevel
C deep = DIRGetMaxdeep(pos1,length,maxfunc,n)
deep = DIRGetlevel(pos1,length,maxfunc,n)
IF (anchor(deep) .EQ. 0) THEN
IF (f(pos2,1) .LT. f(pos1,1)) THEN
anchor(deep) = pos2
point(pos2) = pos1
point(pos1) = 0
ELSE
anchor(deep) = pos1
point(pos2) = 0
END IF
ELSE
pos = anchor(deep)
IF (f(pos2,1) .LT. f(pos1,1)) THEN
IF (f(pos2,1) .LT. f(pos,1)) THEN
anchor(deep) = pos2
C JG 08/30/00 Fixed bug. Sorting was not correct when
C f(pos2,1) < f(pos1,1) < f(pos,1)
IF (f(pos1,1) .LT. f(pos,1)) THEN
point(pos2) = pos1
point(pos1) = pos
ELSE
point(pos2) = pos
CALL DIRInsert(pos,pos1,point,f,maxfunc)
END IF
ELSE
CALL DIRInsert(pos,pos2,point,f,maxfunc)
CALL DIRInsert(pos,pos1,point,f,maxfunc)
END IF
ELSE
IF (f(pos1,1) .LT. f(pos,1)) THEN
C JG 08/30/00 Fixed bug. Sorting was not correct when
C f(pos1,1) < f(pos2,1) < f(pos,1)
anchor(deep) = pos1
IF (f(pos,1) .LT. f(pos2,1)) THEN
point(pos1) = pos
CALL DIRInsert(pos,pos2,point,f,maxfunc)
ELSE
point(pos1) = pos2
point(pos2) = pos
END IF
ELSE
CALL DIRInsert(pos,pos1,point,f,maxfunc)
CALL DIRInsert(pos,pos2,point,f,maxfunc)
END IF
END IF
END IF
10 CONTINUE
C JG 09/24/00 Changed this to Getlevel
C deep = DIRGetMaxdeep(samp,length,maxfunc,n)
deep = DIRGetlevel(samp,length,maxfunc,n)
pos = anchor(deep)
IF (f(samp,1) .LT. f(pos,1)) THEN
anchor(deep) = samp
point(samp) = pos
ELSE
CALL DIRInsert(pos,samp,point,f,maxfunc)
END IF
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRInsertList2 (Old way to do it.) |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRInsertList_2(start,j,k,List2,w,maxI,n)
IMPLICIT None
INTEGER start,n,j,k
INTEGER List2(n,2)
Double Precision w(n)
INTEGER pos,i,maxI
pos = start
IF (start .EQ. 0) THEN
List2(j,1) = 0
start = j
GOTO 50
END IF
IF (w(start) .GT. w(j)) THEN
List2(j,1) = start
start = j
ELSE
DO 10,i=1,maxI
IF (List2(pos,1) .EQ. 0) THEN
List2(j,1) = 0
List2(pos,1) = j
GOTO 50
ELSE
IF (w(j) .LT. w(List2(pos,1))) THEN
List2(j,1) = List2(pos,1)
List2(pos,1) = j
GOTO 50
END IF
END IF
pos = List2(pos,1)
10 CONTINUE
END IF
50 List2(j,2) = k
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRSearchmin |
C| Search for the minimum in the list. !
C+-----------------------------------------------------------------------+
SUBROUTINE DIRSearchmin(start,List2,pos,k,n)
IMPLICIT None
Integer start,pos,k,n
INTEGER List2(n,2)
k = start
pos = List2(start,2)
start = List2(start,1)
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRInit |
C| Initialise all needed variables and do the first run of the |
C| algorithm. |
C| Changed 02/24/2000 |
C| Changed fcn Double precision to fcn external! |
C| Changed 09/15/2000 |
C| Added distinction between Jones way to characterize rectangles |
C| and our way. Common variable JONES controls which way we use. |
C| JONES = 0 Jones way (Distance from midpoint to corner) |
C| JONES = 1 Our way (Length of longest side) |
C| Changed 09/24/00 |
C| Added array levels. Levels contain the values to characterize |
C| the hyperrectangles. |
C| Changed 01/22/01 |
C| Added variable fmax to keep track of maximum value found. |
C| Added variable Ifeasiblef to keep track if feasibel point has |
C| been found. |
C| Changed 01/23/01 |
C| Added variable Ierror to keep track of errors. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRInit(f,fcn,c,length,actdeep,point,anchor,free,
+ dwrit,logfile,ArrayI,maxI,List2,w,x,l,u,fmin,minpos,thirds,
+ levels,maxfunc,maxdeep,n,maxor,fmax,Ifeasiblef,IInfeasible,
+ Ierror,
+ iidata, iisize, ddata, idsize, cdata, icsize)
IMPLICIT None
Integer maxfunc,maxdeep,n,maxor
Double Precision f(maxfunc,2),c(maxfunc,maxor),fmin
Double Precision x(n),delta, thirds(0:maxdeep)
Double Precision levels(0:maxdeep)
Integer length(maxfunc,maxor),actdeep,minpos,i,oops,j
Integer point(maxfunc),anchor(-1:maxdeep),free
Integer ArrayI(maxor),maxI,new,dwrit,logfile,List2(maxor,2)
Double Precision w(maxor)
External fcn
Double Precision help2,l(n),u(n)
Double Precision costmin
C+-----------------------------------------------------------------------+
C| JG 01/22/01 Added variable to keep track of the maximum value found. |
C+-----------------------------------------------------------------------+
Double Precision fmax
C+-----------------------------------------------------------------------+
C| JG 01/22/01 Added variable Ifeasiblef to keep track if feasibel point |
C| has been found. |
C| JG 01/23/01 Added variable Ierror to keep track of errors. |
C| JG 03/09/01 Added IInfeasible to keep track if an infeasible point has|
C| been found. |
C+-----------------------------------------------------------------------+
Integer Ifeasiblef, Ierror, IInfeasible
C JG 09/15/00 Added variable JONES (see above)
Integer JONES, help
COMMON /directcontrol/ JONES
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)
fmin = 1.D20
costmin = fmin
C JG 09/15/00 If Jones way of characterising rectangles is used,
C initialise thirds to reflect this.
IF (JONES .eq. 0) THEN
DO 5,j = 0,n-1
w(j+1) = 0.5D0 * dsqrt(n - j + j/9.D0)
5 CONTINUE
help2 = 1.D0
DO 10,i = 1,maxdeep/n
DO 8, j = 0, n-1
levels((i-1)*n+j) = w(j+1) / help2
8 CONTINUE
help2 = help2 * 3.D0
10 CONTINUE
ELSE
C JG 09/15/00 Initialiase levels to contain 1/j
help2 = 3.D0
DO 11,i = 1,maxdeep
levels(i) = 1.D0 / help2
help2 = help2 * 3.D0
11 CONTINUE
levels(0) = 1.D0
ENDIF
help2 = 3.D0
DO 21,i = 1,maxdeep
thirds(i) = 1.D0 / help2
help2 = help2 * 3.D0
21 CONTINUE
thirds(0) = 1.D0
DO 20,i=1,n
c(1,i) = 0.5D0
x(i) = 0.5D0
length(1,i) = 0
20 CONTINUE
CALL DIRinfcn(fcn,x,l,u,n,f(1,1),help,
+ iidata, iisize, ddata, idsize, cdata, icsize)
f(1,2) = help
IInfeasible = help
fmax = f(1,1)
C 09/25/00 Added this
C if (f(1,1) .ge. 1.E+6) then
if (f(1,2) .gt. 0.D0) then
f(1,1) = 1.D6
fmax = f(1,1)
Ifeasiblef = 1
else
Ifeasiblef = 0
end if
C JG 09/25/00 Remove IF
fmin = f(1,1)
costmin = f(1,1)
minpos = 1
actdeep = 2
point(1) = 0
free = 2
delta = thirds(1)
CALL DIRGet_I(length,1,ArrayI,maxI,n,maxfunc)
new = free
CALL DIRSamplepoints(c,ArrayI,delta,1,new,length,
+ dwrit,logfile,f,free,maxI,point,fcn,x,l,
+ fmin,minpos,u,n,
+ maxfunc,maxdeep,oops)
C+-----------------------------------------------------------------------+
C| JG 01/23/01 Added error checking. |
C+-----------------------------------------------------------------------+
IF (oops .GT. 0) THEN
IError = -4
return
END IF
C+-----------------------------------------------------------------------+
C| JG 01/22/01 Added variable to keep track of the maximum value found. |
C| Added variable to keep track if feasible point was found. |
C+-----------------------------------------------------------------------+
CALL DIRSamplef(c,ArrayI,delta,1,new,length,
+ dwrit,logfile,f,free,maxI,point,fcn,x,l,
+ fmin,minpos,u,n,maxfunc,maxdeep,oops,fmax,Ifeasiblef,
+ IInfeasible,
+ iidata, iisize, ddata, idsize, cdata, icsize)
C+-----------------------------------------------------------------------+
C| JG 01/23/01 Added error checking. |
C+-----------------------------------------------------------------------+
IF (oops .GT. 0) THEN
IError = -5
return
END IF
CALL DIRDivide(new,0,length,point,
+ ArrayI,1,List2,w,maxI,f,maxfunc,maxdeep,n)
CALL DIRInsertList(new,anchor,point,f,maxI,length,
+ maxfunc,maxdeep,n,1)
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRDivide |
C| Subroutine to divide the hyper rectangles according to the rules. |
C| Changed 02-24-2000 |
C| Replaced if statement by min (line 367) |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRDivide(new,currentlength,length,point,
+ ArrayI,sample,List2,w,maxI,f,maxfunc,maxdeep,n)
IMPLICIT None
INTEGER start,new,maxfunc,maxdeep,n,sample
INTEGER currentlength,length(maxfunc,n)
INTEGER point(maxfunc)
INTEGER List2(n,2),maxI,ArrayI(n)
Double Precision f(maxfunc,2),w(n)
INTEGER pos,i,j,k,pos2
start = 0
pos = new
DO 10,i=1,maxI
j = ArrayI(i)
w(j) = f(pos,1)
k = pos
pos = point(pos)
w(j) = min(f(pos,1),w(j))
pos = point(pos)
CALL DIRInsertList_2(start,j,k,list2,w,maxI,n)
10 CONTINUE
IF (pos .GT. 0) THEN
Write(*,*) "Error Divide"
STOP
END IF
DO 20,j=1,maxI
CALL DIRSearchmin(start,List2,pos,k,n)
pos2 = start
length(sample,k) = currentlength + 1
DO 30,i=1,maxI-j+1
length(pos,k) = currentlength + 1
pos = point(pos)
length(pos,k) = currentlength + 1
C JG 07/10/01 pos2 = 0 at the end of the 30-loop. Since we end
C the loop now, we do not need to reassign pos and pos2.
if (pos2 .gt. 0) then
pos = List2(pos2,2)
pos2 = List2(pos2,1)
end if
30 CONTINUE
20 CONTINUE
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRSamplepoints |
C| Subroutine to sample the new points. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRSamplepoints(c,ArrayI,delta,sample,start,length,
+ dwrit,logfile,f,free,maxI,point,fcn,x,l,fmin,minpos,
+ u,n,
+ maxfunc,maxdeep,oops)
IMPLICIT None
INTEGER n,maxfunc,maxdeep,oops
INTEGER maxI,ArrayI(n),sample
INTEGER length(maxfunc,n),free,point(maxfunc)
Double Precision c(maxfunc,n),delta,x(n),l(n)
Double Precision u(n),f(maxfunc,2)
Double Precision fmin
INTEGER pos,j,k,dwrit,logfile,minpos
Integer start
External fcn
oops = 0
pos = free
start = free
DO 10,k=1,maxI+maxI
DO 20,j=1,n
length(free,j) = length(sample,j)
c(free,j) = c(sample,j)
20 CONTINUE
pos = free
free = point(free)
IF (free .EQ. 0) THEN
Write(*,1000)
Write(*,1001)
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,1000)
Write(logfile,1001)
END IF
END IF
oops = 1
RETURN
1000 FORMAT("Error, no more free positions !")
1001 FORMAT("Increase maxfunc !")
END IF
10 CONTINUE
point(pos) = 0
pos = start
DO 30,j=1,maxI
c(pos,ArrayI(j)) = c(sample,ArrayI(j)) + delta
pos = point(pos)
c(pos,ArrayI(j)) = c(sample,ArrayI(j)) - delta
pos = point(pos)
30 CONTINUE
IF (pos .GT. 0) THEN
Write(*,2000)
IF (logfile /= 99999) then
IF (dwrit .EQ. 2) THEN
Write(logfile,2000)
END IF
END IF
STOP
2000 FORMAT("Error ! ")
END IF
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRGet_I |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRGet_I(length,pos,ArrayI,maxi,n,maxfunc)
IMPLICIT None
Integer maxfunc,n,maxi,pos
Integer length(maxfunc,n),ArrayI(n),i,help,j
j = 1
help = length(pos,1)
DO 10,i = 2,n
IF (length(pos,i) .LT. help) THEN
help = length(pos,i)
END IF
10 CONTINUE
DO 20,i = 1,n
IF (length(pos,i) .EQ. help) THEN
ArrayI(j) = i
j = j + 1
END IF
20 CONTINUE
maxi = j - 1
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRInitList |
C| Initialise the list. |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRInitList(anchor,free,point,f,maxfunc,maxdeep)
IMPLICIT None
Integer maxdeep,maxfunc
Double Precision f(maxfunc,2)
C f -- values of functions.
Integer anchor(-1:maxdeep)
C anchor -- anchors of lists with deep i
Integer point(maxfunc), free
C point -- lists
C free -- first free position
Integer i
DO 10,i = -1,maxdeep
anchor(i) = 0
10 CONTINUE
DO 20,i = 1,maxfunc
f(i,1) = 0.D0
f(i,2) = 0
point(i) = i + 1
C point(i) = 0
20 CONTINUE
point(maxfunc) = 0
free = 1
END
C+-----------------------------------------------------------------------+
C| SUBROUTINE DIRInsert3 |
C+-----------------------------------------------------------------------+
SUBROUTINE DIRInsert3(pos1,pos2,pos3,deep,anchor,point,free,
+ f,fmin,minpos,maxfunc,maxdeep)
IMPLICIT None
INTEGER maxfunc,maxdeep
INTEGER deep,free,pos1,pos2,pos3
INTEGER anchor(-1:maxdeep),point(maxfunc)
Double Precision f(maxfunc,2),fmin
INTEGER pos,minpos
CALL DIRSort3(pos1,pos2,pos3,f,maxfunc)
IF (anchor(deep) .EQ. 0) THEN
anchor(deep) = pos1
point(pos1) = pos2
point(pos2) = pos3
point(pos3) = 0
ELSE
pos = anchor(deep)
IF (f(pos1,1) .LT. f(pos,1)) THEN
anchor(deep) = pos1
point(pos1) = pos
ELSE
CALL DIRInsert(pos,pos1,point,f,maxfunc)
END IF
CALL DIRInsert(pos,pos2,point,f,maxfunc)
CALL DIRInsert(pos,pos3,point,f,maxfunc)
END IF
IF ((f(pos1,1) .LT. fmin) .and. (f(pos1,2) .eq. 0)) THEN
fmin = f(pos1,1)
minpos = pos1
END IF
END