V3FIT
ga_sp.f
1  SUBROUTINE ga_sp(fcn, nopt, fvec, best, filename, nfev, iflag,
2  1 max_num_processors, myid)
3  USE ga_mod
4  USE safe_open_mod
5  USE mpi_params, ONLY: master
6  IMPLICIT NONE
7  EXTERNAL fcn
8 
9  INTEGER :: nopt
10  REAL(rprec), DIMENSION(nopt) :: fvec
11 
12  INTEGER :: kount, npossum, ig2sum, istart, istore
13  INTEGER :: ncross, ipick, mate1, mate2, istat
14  INTEGER :: i, j, nfev, iflag, max_num_processors, myid
15  REAL(rprec), INTENT(in) :: best
16  REAL(rprec), SAVE :: fbar, evals
17  CHARACTER(LEN=*) :: filename
18  CHARACTER(LEN=LEN(filename)+10) :: temp
19 c
20 c CALL input
21 c
22 c Perform necessary initialization and READ the ga.restart file.
23  CALL ga_initial(istart,npossum,ig2sum,filename,myid)
24 c
25 c $$$$$ Main generational processing loop. $$$$$
26  kount=0
27  nfit_eval=nfev
28  istore=0
29  iunit_ga_out = 24
30  IF (myid .eq. master) THEN
31  temp = "ga_out." // filename
32  CALL safe_open(iunit_ga_out, istat, trim(temp),
33  1 'unknown', 'formatted')
34  END IF
35 
36 
37  DO 20 i=istart,maxgen+istart-1
38  iflag=-1
39  IF (myid .eq. master) THEN
40  WRITE (6,1111) i
41  WRITE (iunit_ga_out,1111) i
42 c WRITE (iunit_ga_out,1050)
43 c
44 c Evaluate the population, assign fitness, establish the best
45 c individual, and write output information.
46  WRITE(6,*) 'pre ga_evalout', max_num_processors
47  WRITE(6,*) fbar,best,nopt,nfev,max_num_processors,iflag
48  END IF
49  CALL ga_evalout(fbar, best, fcn, nopt, fvec, nfev,
50  > max_num_processors, iflag, myid)
51  istore=istore+1
52  geni(istore) = i
53  genavg(istore)=fbar
54  genmax(istore)=best
55  IF (npopsiz.eq.1 .or. iskip.ne.0) THEN
56  IF (myid .eq. master) CLOSE(iunit_ga_out)
57  CALL ga_restart(i,istart,kount,filename, myid)
58  RETURN
59  END IF
60 c
61 c niching
62  IF (iniche.ne.0) CALL ga_niche(myid)
63 c
64 c selection, crossover and mutation
65  ncross=0
66  ipick=npopsiz
67  DO 45 j=1,npopsiz,nchild
68 c
69 c Perform selection.
70  CALL ga_selectn(ipick,j,mate1,mate2)
71 c
72 c Now perform crossover between the randomly selected pair.
73  CALL crosovr(ncross,j,mate1,mate2)
74  45 CONTINUE
75 
76  IF (myid .eq. master) THEN
77  WRITE(6,1225) ncross
78  WRITE(iunit_ga_out,1225) ncross
79  END IF
80 c
81 c Now perform random mutations. If running micro-GA, skip mutation.
82  IF (microga.eq.0) CALL ga_mutate (myid)
83 c
84 c Write child array back into parent array for new generation. Check
85 c to see IF the best parent was replicated.
86  CALL ga_newgen(npossum,ig2sum,myid)
87 c
88 c Implement micro-GA if enabled.
89  IF (microga.ne.0) CALL ga_micro(i,npossum,ig2sum,myid)
90 c
91 c Write to restart file.
92  CALL ga_restart(i,istart,kount,filename,myid)
93  20 CONTINUE
94 
95 c $$$$$ End of main generational processing loop. $$$$$
96 
97  IF (myid .eq. master) THEN
98  WRITE(iunit_ga_out,3000)
99  DO 100 i=1,maxgen
100  evals = npopsiz*geni(i)
101  WRITE(iunit_ga_out,3100) geni(i),evals,genavg(i),genmax(i)
102  100 CONTINUE
103  CLOSE (iunit_ga_out)
104  END IF
105 
106  1050 FORMAT(1x,' Binary Code',16x,'Parameter Values and Fitness')
107  1111 FORMAT(//,'################# Generation',i5,
108  1 ' #################')
109  1225 FORMAT(/' Number of Crossovers =',i5)
110  3000 FORMAT(2x//'Summary of Output'/
111  + 2x,'Generation Evaluations Avg.Fitness Best Fitness')
112  3100 FORMAT(2x,3(e10.4,4x),e11.5)
113 
114  END SUBROUTINE ga_sp