-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIRDFN.f90
1847 lines (1545 loc) · 48.8 KB
/
DIRDFN.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
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
!============================================================================================
! DIRDFN - DIRECT with DFN local search for general constrained Nonlinear Programming
! Copyright (C) 2016 G.Di Pillo, G.Liuzzi, S.Lucidi, V.Piccialli, F.Rinaldi
!
! This program is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
!
! G.Di Pillo, G.Liuzzi, S.Lucidi, V.Piccialli, F.Rinaldi, A DIRECT-type approach for
! derivative-free constrained global optimization. Computational Optimization and
! Applications, 2016
!============================================================================================
subroutine dirdfn(n, me, mi, xbest, lb0, ub0, nomefun, iprint, maxint,maxnf, fglob, tolglob)
use mod_bounds
use mod_box
use mod_suddividi
use mod_mem
use mod_best
use int_elim
use test_ott
use vincoli
implicit none
! input parameters
integer :: n,me,mi,iprint,maxint
integer*8 :: maxnf
real*8 :: lb0(n), ub0(n)
character*40 :: nomefun
real*8 :: fglob, tolglob
real*8 :: lbb(n), ubb(n), yaux(n)
real*8 :: xbest(n), fbest, ftemp, mindist, maxdist
real*8 :: xbestold(n), gcor(n)
real*8 :: mindiam,maxdiam,maxL
real :: rr,time_begin, time_end, timetot
integer :: imin, jmin
integer :: seed(2), i, j, ng
integer :: h, nint, stessopunto, numripartenze
integer*8 :: nminloc, nf
logical :: trovato, rangen, soglia, flag_minloc
real*8 :: c
real*8 :: hp(me), gp(mi), violmax, violmax2, fbestprova, fminfeasprova
!**********************************************************************************************
memerror = .false.
rangen = .false.
soglia = .false.
write(*,*)
write(*,*)
write(*,*) ' DIRDFN, now solving: ',nomefun
write(*,*) ' maxnf: ',maxnf
write(*,*) ' maxint: ',maxint
write(*,*) ' print_level: ',iprint
write(*,*)
write(*,*)
allocate(lb(n),ub(n),xtemp(n),ytemp(n),xbar(n),lbs(n),ubs(n))
allocate(vetf1(n),vetf2(n),vetg1(n),vetg2(n),xsud(n),ysud(n),mask(n))
allocate(constr(mi+me),eps(mi+me), epsiniz(mi+me),constr_old(mi+me))
allocate(xminfeas(n),xbestamm(n))
xbestamm = 1.d0
n1=n
me1=me
mi1=mi
lbs = 0.d0
ubs = 1.d0
do i = 1,n
lbb(i)=lb0(i)
ubb(i)=ub0(i)
enddo
xbest = (ubb+lbb)/2.d0
xbar = (ubs+lbs)/2.d0
fattore = 1.0d0
ampiezza = 1.0d0
stessopunto = 0
numripartenze = 0
nint_elim = 0
call fun_viol(n,mi,me,xbest,minfeas)
call funob(n,xbest,fminfeas)
fbest = minfeas
xminfeas = xbest
if(minfeas <= tolfeas) then
bestamm = fminfeas
xbestamm = xbest
fbest = fminfeas
else
bestamm = 1.d+30
endif
nftot = 1
ninttot = 0
nminloc = 0
h = 0
imin = 0
jmin = 0
call cpu_time(time_begin)
trovato = .false.
xbestold = xbest
call nuovoglobale(n,lbb,ubb,xbest,fbest,nf,ng,fglob,tolglob,maxint,maxnf,iprint, &
trovato, nint,mindiam,maxdiam,maxL,flag_minloc)
nftot = nftot + nf
ninttot = ninttot + nint
nminloc = nminloc + ng
!xbest = xminfeas
!call sd_box(n,xbest,fbest,lbb,ubb,1.d-5,500000,nf,-1,imin, 2)
if(iprint > 2) write(*,*) 'computing violmax'
violmax=0.0d0
call funob(n, xminfeas,fminfeasprova)
call fun_viol(n,mi,me,xminfeas,violmax)
if(bestamm < 1.d+30) then
if(iprint > 2) write(*,*) 'computing violmax2',xbestamm
violmax2=0.0d0
call funob(n, xbestamm,fbestprova)
call fun_viol(n,mi,me,xbestamm,violmax2)
else
violmax2 = 1.d+30
fbestprova = 1.d+30
endif
if(minfeas > 1.d+30) minfeas = 1.d+30
if(violmax2> 1.d+30) violmax2= 1.d+30
call cpu_time(time_end)
timetot = time_end - time_begin
write(*,*)
write(*,*) '---------- sommario risultati -----------'
write(*,*) ' cpu_time = ',timetot, ' seconds'
write(*,*) ' nf = ',nftot
write(*,*) ' ng = ',ng
write(*,*) 'profondita = ',h
write(*,*) ' fbest = ',fbest
write(*,*) ' fviolbest = ',fminfeasprova
write(*,*) ' violbest = ',violmax
write(*,*) ' xbest = ',xminfeas
write(*,*) ' xbestamm = ',xbestamm
write(*,*) ' violamm = ',violmax2
write(*,*) '-----------------------------------------'
! write(1,*)
! write(1,*) '---------- sommario risultati -----------'
! write(1,*) ' cpu_time = ',timetot, ' secondi'
! write(1,*) ' nf = ',nftot
! write(1,*) ' ng = ',ng
! write(1,*) 'profondita = ',h
! write(1,*) ' fbest = ',fbest
! write(1,*) ' xbest = ',xbest
! write(1,*) '-----------------------------------------'
xbest = xbestamm
write(3,950) nomefun, n, mi, me, timetot, bestamm, violmax2, fminfeas, minfeas, nftot, nminloc, ninttot, mindiam,maxdiam
deallocate(lb,ub,xtemp,ytemp,xbar,lbs,ubs)
deallocate(vetf1,vetf2,xsud,ysud,mask,vetg1,vetg2)
deallocate(constr,eps, epsiniz,constr_old)
deallocate(xminfeas,xbestamm)
100 format(a40)
800 FORMAT(a20,' & ',i4,' & \bf', es16.8,4(' & ',i10),' & ',i3,' & ', es10.2,' & ', es10.2,' & ', es10.2,'\\')
801 FORMAT(a20,' & ',i4,' & ', es16.8,4(' & ',i10),' & ',i3,' & ', es10.2,' & ', es10.2,' & ', es10.2, es10.2,'\\')
802 FORMAT(a20,' & ',i4,' & * ', es16.8,4(' & ',i10),' & ',i3,' & ', es10.2,' & ', es10.2,' & ', es10.2, es10.2,'\\')
900 FORMAT(a20,3(' & ',i4),8(' & ', es16.8),3(' & ',i10),' & ', es10.2,' & ', es10.2,'\\')
950 FORMAT(a20,3(' & ',i4),5(' & ', es16.8),3(' & ',i10),' & ', es10.2,' & ', es10.2,'\\')
end subroutine dirdfn
subroutine funct(n,x,f)
implicit none
integer n
real*8 x(n),f
call funct_pen(n,x,f)
return
end subroutine funct
subroutine funct_pen(n,x,f)
use vincoli
use mod_bounds
use mod_best
implicit none
integer n,i
real*8 x(n),f,fob,fmax
!!!!!!!!!!!!!!!!!!!!!!!!!!!
integer :: me, mi
!!!!!!!!!!!!!!!!!!!!!!!!!!!
me=me1
mi=mi1
call funct_vinc(n,mi, me, x,fob,constr)
fmax = 0.d0
viol = 0.d0
do i = 1,mi
fmax = fmax + (max(0.d0,constr(i)/eps(i)))
viol=viol+max(0.d0,constr(i))
enddo
do i = 1, me
fmax = fmax + (abs(constr(mi+i)/eps(mi+i)))
viol=viol+abs(constr(mi+i))
enddo
if(viol < minfeas) then
minfeas = viol
fminfeas = fob
xminfeas = x
elseif((viol <= minfeas).and.(fob < fminfeas)) then
minfeas = viol
fminfeas = fob
xminfeas = x
endif
if((viol <= tolfeas).and.(fob < bestamm)) then
bestamm = fob
xbestamm = x
endif
f = fob + fmax
return
end subroutine funct_pen
subroutine funct_vinc(n, m, p, x, fob, constr)
implicit none
integer :: n, m, p
double precision :: x(n), fob, constr(m+p)
call funob(n, x, fob)
if (m >0 ) call fconstrin(n,m,x,constr(1:m))
if (p >0 ) call fconstreq(n,p,x,constr(m+1:m+p))
return
end subroutine funct_vinc
subroutine fun_viol(n, m, p, x, viol)
use vincoli, only : constr
use mod_bounds
implicit none
integer :: n, m, p, i
double precision :: x(n), viol, fob
!!!!!!!!!!!!!!!!!!!!!!!!!!!
integer :: me, mi
!!!!!!!!!!!!!!!!!!!!!!!!!!!
me=me1
mi=mi1
call funct_vinc(n,mi, me, x,fob,constr)
viol = 0.d0
do i = 1,mi
viol=viol+max(0.d0,constr(i))
enddo
do i = 1, me
viol=viol+abs(constr(mi+i))
enddo
return
end subroutine fun_viol
subroutine nuovoglobale(n,lbb,ubb,xbest,fbest,nf,nminloc,fglob,tolglob,maxint,maxnf,iprint, &
trovato,nint,mindiam,maxdiam,maxL,flag_minloc)
use mod_bounds
use mod_type
use mod_box
use mod_suddividi
use mod_mem
use int_elim
use mod_best
use vincoli, only : constr
implicit none
interface
subroutine aggiorna_struttura(root,Ltilde,fdir,nint)
use mod_type
implicit none
type(colonna),pointer :: root
real*8 :: Ltilde,fdir
integer :: nint
end subroutine aggiorna_struttura
subroutine genera_partizione(root,n,tol,iprint)
use mod_type
implicit none
type(colonna),pointer :: root
integer :: n,iprint
real*8 :: tol
end subroutine genera_partizione
subroutine elimina_colonna(currcol,num)
use mod_type
implicit none
type(colonna),pointer :: currcol
integer :: num
end subroutine elimina_colonna
subroutine ricintervallo_dx(root,convexhull,nconv,iprint,Ltilde,eps,eps_cvx,fmin)
use mod_type
use mod_mem
implicit none
type(colonna),pointer :: root
type(vertice), pointer :: convexhull
real*8 :: Ltilde, eps, eps_cvx
real*8 :: fmin
integer :: iprint, nconv
end subroutine ricintervallo_dx
subroutine ricintervallo(root,convexhull,nconv,iprint,maxL,eps_cvx)
use mod_type
implicit none
type(colonna),pointer :: root
type(vertice),pointer :: convexhull
integer :: nconv,iprint
real*8 :: maxL, eps_cvx
end subroutine ricintervallo
subroutine ricintervalloFob(convexhull,nconv,maxdiam,iprint)
use mod_type
use mod_mem
implicit none
type(vertice), pointer :: convexhull
real*8 :: maxdiam
integer :: nconv, iprint
end subroutine ricintervalloFob
subroutine riduciconvexhull(convexhull,nelim,eps,toldiam,fmin)
use mod_type
implicit none
type(vertice),pointer :: convexhull
integer :: nelim
real*8 :: eps, fmin, toldiam
end subroutine riduciconvexhull
subroutine dealloca_struct(root)
use mod_type
implicit none
type(colonna), pointer :: root
end subroutine dealloca_struct
subroutine suddividi(currch,root,n,nf,nint,xdir,fdir,xdir_unscaled,maxL,Ltilde,cont,tol)
use mod_type
use mod_suddividi
implicit none
type(vertice), pointer :: currch
type(colonna), pointer :: root
integer :: n, nint, cont
integer*8 :: nf
real*8 :: xdir(n), fdir, xdir_unscaled(n)
real*8 :: maxL,Ltilde,tol
end subroutine suddividi
subroutine stampa_intervalli(root)
use mod_type
implicit none
type(colonna), pointer :: root
end subroutine stampa_intervalli
subroutine find_colonna(root,int,tol,curcol)
use mod_type
implicit none
type(colonna), pointer :: root, curcol
type(intervallo), pointer :: int
real*8 :: tol
end subroutine find_colonna
end interface
type(colonna), pointer :: root, currcol, tempcol
type(intervallo), target :: primo
type(intervallo), pointer :: curr
type(vertice), pointer :: convexhull, currch, currch1, currchmax
type(vertice), pointer :: filter
type(fpunt) :: ottimo
external :: funct
integer :: n, iprint
integer*8 :: maxnf, nf
integer :: ng, nint, num, i, nelim, nconv, k ,ktot, maxiter, numnf
integer :: maxint, iexit, nminloc, cont, maxcont
logical :: halt, direct_puro, trovato, lista_vuota, minric !minric=true fatte min loc per ric
logical :: flag_minloc, vicino
real*8 :: norma, maxdiam, mindiam, toldiam, basdiam, fglob, tolglob
real*8 :: eps, eps_cvx, alfa_stop
real*8 :: lbb(n),ubb(n)
real*8 :: xbest(n), fbest, ftemp, tmpder, minder, maxL, Ltilde, gg(n), bl(n), bu(n), blsd(n), busd(n)
real*8 :: xdir(n), xx(n), ff, fdir, tol, xdir_unscaled(n), fminloc, xtemp_sc(n)
integer :: seed(2), istop, n_agg_strut
real :: rr
!!!!!!!!!!!!!!!!!!!!!!!!!!!
integer :: me, mi
!!!!!!!!!!!!!!!!!!!!!!!!!!!
me=me1
mi=mi1
! parametri ed inizializzazione
flag_minloc = .false.
cont = 0
maxcont = 0
tol = 1.d-12
toldiam = 1.0d-10
basdiam = 1.d-3*sqrt(dble(n))/sqrt(dot_product(ubb-lbb,ubb-lbb))
basdiam = -1.0d60
eps = 1.d-4
eps_cvx = 0.d0
minder = 0.0d0
maxL = 0.0d0
Ltilde = 1.d30 !1.d+3*sqrt(dot_product(ubb-lbb,ubb-lbb))
halt = .false.
trovato = .false.
lista_vuota = .false.
memerror = .false.
minric = .false.
alfa_stop = 1.d-4
nf = 0
lb = lbb
ub = ubb
allocate(root)
nullify(root%next)
nullify(root%pred)
allocate(root%int)
call alloca_intervallo(root%int,n)
root%int%cent = 0.5d0
root%int%dimen = 1.d0
root%int%maxdim = 1.d0
root%int%der = 0.d0
root%int%xbars = 0.5d0
root%int%lbs = 0.d0
root%int%ubs = 1.d0
root%int%diam = norma(n,root%int%dimen)/2.d0
root%diam = norma(n,root%int%dimen)/2.d0
root%int%flagloc = .false.
root%int%flagdiv = .true.
root%int%flagcon = .false.
root%int%flagopt = .true.
root%int%id = 1
nullify(root%int%next)
nullify(root%int%pred)
call unscalevars(n,root%int%cent,ytemp,xbar,lbs,ubs)
call unscalevars_direct(n,ytemp,xtemp)
ytemp = xtemp
call funob(n,xtemp,root%int%fob)
call fun_viol(n,mi,me,xtemp,root%int%fint)
nf = 1
nint = 1
fdir = root%int%fint
xdir = root%int%cent
ng = 0
nconv = 1
nelim = 0
nminloc = 0
n_agg_strut = 100
xdir_unscaled = xbest
if(iprint > 1) then
call stampa_ottimo(n,xbest,fbest,nint,nf)
endif
direct_puro = .true.
do while (.not.halt)
cont = cont + 1
maxcont = maxcont + 1
currcol => root
do while (associated(currcol))
if(currcol%diam < basdiam) then
!------------------------------------------------
! Elimina tutta la colonna corrispondente
!------------------------------------------------
call elimina_colonna(currcol,num)
nint_elim=nint_elim+num
nint = nint - num
if(.not.associated(currcol%next)) then
lista_vuota = .true.
deallocate(currcol)
nullify(currcol)
nullify(root)
exit
else
currcol => currcol%next
deallocate(root)
root => currcol
nullify(root%pred)
endif
else
if(.not.associated(currcol%next)) then
maxdiam = currcol%diam
! non ci vuole un exit?
! exit
endif
currcol => currcol%next
endif
enddo
!------------------------------------------------------------------------------
if(associated(root)) then
nullify(root%pred)
if((.not.associated(root%int)).and.(.not.associated(root%next))) then
if(iprint > 0) write(*,*) 'root associated'
lista_vuota = .true.
endif
else
if(iprint > 0) write(*,*) 'root not associated'
lista_vuota = .true.
endif
if(lista_vuota) then
halt = .true.
write(*,*) 'empty data structure (1)'
exit
endif
!write(*,*) 'diam root:',root%diam
mindiam = root%diam
!if(iprint > 0) pause
!--------------------------
! stopping criterion
!--------------------------
trovato = ((fbest-fglob)/dmax1(1.0d0,abs(fglob)) < tolglob)
if((nint > maxint) .or. trovato .or. (nf > maxnf)) then
halt = .true.
if(iprint > 0) then
write(*,*) 'stop condition satisfied'
write(*,*) ' num. hyperint = ',nint,' maxint = ', maxint
write(*,*) ' num. f.evals = ',nf, ' maxnf = ', maxnf
endif
cycle
endif
!---------------------------------------
! ric. intervallo potenzialmente ottimo
!---------------------------------------
if(iprint.ge.2) write(*,*) 'start search for potentially optimal hyperint'
!call ricintervallo_dx(root,convexhull,nconv,-1,Ltilde,eps,eps_cvx,fdir)
call ricintervallo(root,convexhull,nconv,-1,Ltilde,eps_cvx)
call ricintervalloFob(convexhull,nconv,maxdiam,-1)
if (memerror) then
write(*,*) ' fine memoria disponibile'
halt = .true.
endif
if(iprint.ge.2) write(*,*) ' end search for potentially optimal hyperint'
!----------------------------------------------
! riduci il convex hull con il criterio su
! fmin
!----------------------------------------------
if(iprint.ge.2) write(*,*) 'start convex hull reduction'
call riduciconvexhull(convexhull,nelim,eps,toldiam,fdir)
if(iprint.ge.2) write(*,*) ' end convex hull reduction'
currch => convexhull
if(iprint >= 2) write(*,*) 'nconv = ',nconv,' nelim = ',nelim
do i = 1,nelim
currch => currch%next
enddo
!-----------------------------------------------
! rimuovo i rimanenti intervalli sul convexhull
! dalla struttura dati per inserirli in seguito
! nella posizione corretta
!-----------------------------------------------
if(iprint > 2) then
write(*,*) 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
write(*,*) ' id diam viol fob'
endif
currch1 => currch
do while (associated(currch1))
if(iprint > 2) then
write(*,*) currch1%int%id, currch1%int%diam, currch1%int%fint, currch1%int%fob
endif
call find_colonna(root,currch1%int,tol,tempcol)
if(tempcol%int%id == currch1%int%id) then
if(associated(currch1%int%next)) then
tempcol%int => currch1%int%next
nullify(tempcol%int%pred)
else
!write(*,*) 'la colonna si e'' svuotata quindi la elimino'
!--------------------------------
! la colonna si e' svuotata
! quindi la elimino
!--------------------------------
if((.not.associated(tempcol%pred)) .and. (.not.associated(tempcol%next))) then
!--------------------------------
! E' l'unica colonna
!--------------------------------
nullify(tempcol)
deallocate(root)
nullify(root)
!write(*,*) 'la col e'' unica e la elimino',associated(root)
exit
elseif(.not.associated(tempcol%pred)) then
!write(*,*) 'la col e'' la prima ma non unica'
!--------------------------------
! E' la prima ma non l'unica
!--------------------------------
tempcol => root
root => root%next
deallocate(tempcol)
nullify(root%pred)
elseif(.not.associated(tempcol%next)) then
!--------------------------------
! E' l'ultima ma non l'unica
!--------------------------------
!write(*,*) 'la col e'' l''ultima ma non unica',tempcol%int%id,root%int%id
nullify(tempcol%pred%next)
deallocate(tempcol)
else
!--------------------------------
! E' in mezzo
!--------------------------------
!write(*,*) 'la col e'' in mezzo',tempcol%int%id,root%int%id
tempcol%pred%next => tempcol%next
tempcol%next%pred => tempcol%pred
deallocate(tempcol)
endif
endif
else
write(*,*) 'FATAL ERROR!! first is not *the* first',tempcol%int%id,currch1%int%id
stop
endif
currch1 => currch1%next
enddo
if(iprint > 2) then
write(*,*) 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
endif
if(associated(currch)) then
!write(*,*) 'currch e'' associato dopo eliminazione'
minder = 0.d0
maxiter = 0
currch1 => currch
do while (associated(currch1))
if(nf > maxnf) exit
if((.not.currch1%int%flagloc).and.(currch1%int%flagcon)) then
call unscalevars(n, currch1%int%cent, ytemp, xbar, lbs, ubs)
call unscalevars_direct(n,ytemp,xtemp)
if(iprint.ge.2) then
call funob(n,xtemp,tempfob)
call fun_viol(n,mi,me,xtemp,tempfeas)
write(*,*) ' '
write(*,*) ' -----------------------------------------------------------------------------'
write(*,*) ' begin local minimization --- viol =',tempfeas, ' f.o. =',tempfob
write(*,*) ' -----------------------------------------------------------------------------'
write(*,*) ' '
endif
numnf = 0
call sd_box(n,xtemp,fminloc,lb,ub,1.d-5,min(500000,maxnf-nf),numnf,-1,istop, 2)
maxiter=maxiter+1
nf = nf + numnf
nminloc = nminloc + 1
call funob(n,xtemp,tempfob)
call fun_viol(n,mi,me,xtemp,tempfeas)
if(iprint.ge.2) then
write(*,*) ' '
write(*,*) ' -----------------------------------------------------------------------------'
write(*,*) ' end local minimization --- viol =',tempfeas, ' f.o. =',tempfob, ' fpen =',fminloc
write(*,*) ' --- minfeas =',minfeas, ' fminfeas =',fminfeas,' bestamm =',bestamm
write(*,*) ' --------------------------------------------------------------------------------'
write(*,*) ' '
endif
if(tempfeas < minfeas) then
minfeas = tempfeas
fminfeas = tempfob
xminfeas = xtemp
elseif((tempfeas <= minfeas).and.(tempfob < fminfeas)) then
minfeas = tempfeas
fminfeas = tempfob
xminfeas = xtemp
write(*,*) 'update minfeas ',fminfeas,minfeas
endif
if((tempfeas <= tolfeas).and.(tempfob < bestamm)) then
bestamm = tempfob
xbestamm = xtemp
write(*,*) 'update bestamm ',bestamm,tempfeas
endif
if(min(fminfeas,bestamm) < fbest) then
if(fminfeas < bestamm) then
fbest = fminfeas
xbest = xminfeas
if(iprint > 1) call stampa_ottimo(n,xbest,fbest,nint, nf)
else
fbest = bestamm
xbest = xbestamm
if(iprint > 1) call stampa_ottimo(n,xbest,fbest,nint, nf)
endif
endif
currch1%int%flagloc = .true.
endif
currch1 => currch1%next
enddo
if(iprint.ge.2) write(*,*) ' tot number of executed local minimizations =',maxiter
else !----- convex hull vuoto
toldiam = 1.d-1*toldiam
endif
if(iprint >= 2) write(*,*) 'main loop: after execute'
if(associated(root)) then
nullify(root%pred)
if((.not.associated(root%int)).and.(.not.associated(root%next))) lista_vuota = .true.
else
lista_vuota = .true.
endif
if(lista_vuota) then
!write(*,*) 'empty data structure'
lista_vuota = .false.
endif
if(iprint.ge.2) write(*,*) 'begin subdivide'
do while (associated(currch).and. (.not. memerror))
!----------------------------------------------------
! suddivisione dell'intervallo potenzialmente ottimo
!----------------------------------------------------
if(currch%int%flagdiv) call suddividi(currch,root,n,nf,nint,xdir,fdir,xdir_unscaled,maxL,Ltilde,cont,tol)
if(cont == 0) maxcont = 0
if (memerror) then
write(*,*)'out of memory, exit after subdivide'
halt = .true.
exit
endif
currch => currch%next
enddo
if(iprint.ge.2) write(*,*) ' end subdivide'
if(iprint >= 2) write(*,*) 'main loop: after subdivide'
!-------------------------------------------
! dealloca la lista che memorizza gli
! intervalli sul convex hull
!-------------------------------------------
currch => convexhull
do while (associated(currch))
!if(associated(currch%int)) currch%int%flagcon = .false.
currch => currch%next
nullify(convexhull%int)
nullify(convexhull%next)
deallocate(convexhull)
convexhull => currch
enddo
!write(*,*) '----- ',num_el_L
if(associated(root)) then
nullify(root%pred)
if((.not.associated(root%int)).and.(.not.associated(root%next))) lista_vuota = .true.
else
lista_vuota = .true.
endif
if(lista_vuota) then
halt = .true.
write(*,*) 'lista vuota'
exit
endif
call funct_vinc(n,mi,me,xbestamm,tempfob,constr)
call fun_viol(n,mi,me,xbestamm,tempfeas)
if(iprint > 0) then
write(*,800) tempfob,tempfeas,fminfeas,fdir,nf,nelim,nconv-nelim,mindiam,maxdiam
endif
800 FORMAT(' bamm=',es11.4,' volm=',es11.4,' fobm=',es11.4,' fDIR=',es11.4,' nf=',i11, &
' nelim=',i11,' ncnv-nelim=',i8,' diam=',es11.4,' DIAM=',es11.4)
810 FORMAT(' DIMEN=',es11.4,' nmaxdim=',I11, ' nminloc=',I11, ' nint=',i11)
830 FORMAT(' fbest=',es11.4)
!call stampa_intervalli(root)
enddo
call dealloca_struct(root)
return
end subroutine nuovoglobale
subroutine stampa_ottimo(n,x,f, nint, nf)
use mod_box
implicit none
integer :: n, nint
integer :: nf
integer*8 :: nf_app
real*8 :: x(n), f
integer :: i
!open(12,file='best.txt',status='unknown',access='append')
nf_app=nf
write(*,110) f,ninttot+nint, nftot+nf_app
!write(12,110) f,ninttot+nint, nftot+nf_app
!close(12)
return
110 format(1x,'fbest = ',es17.10,' nint = ',i20,' nftot = ',i20)
end subroutine stampa_ottimo
subroutine riduciconvexhull(convexhull,nelim,eps,toldiam,fmin)
use mod_type
implicit none
type(vertice),pointer :: convexhull
type(vertice),pointer :: currch
integer :: nelim, nugua
real*8 :: eps, toldiam, fmin, L
logical :: halt
nelim = 0
nugua = 0
halt = .false.
currch => convexhull
do while (.not.halt)
!write(*,*) '1'
if(associated(currch)) then
if(currch%int%diam < toldiam) then
nelim = nelim + 1
currch => currch%next
cycle
endif
else
halt = .true.
exit
endif
!write(*,*) '2'
if(associated(currch%next)) then
!write(*,*) '3'
if((currch%next%int%diam - currch%int%diam) > 0.d0) then
!write(*,*) '4'
L = (currch%next%int%fint - currch%int%fint) / (currch%next%int%diam - currch%int%diam)
!if( currch%int%fint - L*currch%int%diam > fmin - eps*(1.d0 + abs(fmin))) then
!write(*,*) currch%int%diam, toldiam
!write(*,*) currch%int%fint,L,currch%int%diam,fmin,eps,abs(fmin)
!write(*,*) currch%int%fint - L*currch%int%diam , fmin-eps*abs(fmin)
if( currch%int%fint - L*currch%int%diam > fmin - eps*max(abs(fmin),1.d-6) ) then
!if(associated(currch%next)) then
nelim = nelim + 1 + nugua
nugua = 0
!endif
currch => currch%next
!currch => convexhull
!convexhull => convexhull%next
!nullify(currch%int)
!nullify(currch%next)
!deallocate(currch)
else
halt = .true.
endif
else
nugua = nugua + 1
currch => currch%next
endif
else
halt = .true.
endif
enddo
!read(*,*)
nullify(currch)
return
end subroutine riduciconvexhull
subroutine ricintervallo_dx(root,convexhull,nconv,iprint,Ltilde,eps,eps_cvx,fmin)
use mod_type
use mod_mem
implicit none
type(colonna),pointer :: root, currcol, tempcol, ultimacol
type(vertice), pointer :: convexhull, currch
type(intervallo), pointer :: primo
real*8 :: Ltilde, stimaL, fh, dh, eps, eps_cvx, maxL, maxLtemp
real*8 :: maxcos, coseno, norma, minfunc, maxdiam, fmin
logical :: halt
integer :: nconv, iprint, sv
!search the column with max diameter
ultimacol => root
do while (associated(ultimacol%next))
ultimacol => ultimacol%next
enddo
!ultimacol points to the column with maximum diameter
!record the first interval (top, right in the CVX HULL)
primo => ultimacol%int
allocate(convexhull,stat=sv)
if(sv.ne.0) then
memerror = .true.
return
endif
convexhull%int => primo
primo%flagcon = .true.
currch => convexhull
currcol => root
if(iprint > 0) then
write(*,*) 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
write(*,*) ' id diam fint'
write(*,*) convexhull%int%id, convexhull%int%diam, convexhull%int%fint
!write(24,*) 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
!write(24,*) ' diam fint',fmin
!write(24,*) convexhull%int%diam, convexhull%int%fint
endif
nconv = 1
maxL = -1.d+10
currcol => ultimacol