V3FIT
ga_initial.f
1  SUBROUTINE ga_initial(istart,npossum,ig2sum,filename,myid)
2 c#######################################################################
3 c
4 c This subroutine sets up the program by generating the g0, g1 and
5 c ig2 arrays, and counting the number of chromosomes required for the
6 c specified input. The subroutine also initializes the random number
7 c generator, parent and iparent arrays (reads the ga.restart file).
8  USE ga_mod
9  USE safe_open_mod
10  USE mpi_params, ONLY: master
11  USE mpi_inc
12  IMPLICIT NONE
13 #if defined(MPI_OPT)
14  INTEGER :: ierr
15 #endif
16  INTEGER :: istart, npossum, ig2sum, myid
17  INTEGER :: i, j, k, l, itemp, istat
18  CHARACTER(LEN=100) :: filename
19  CHARACTER(LEN=200) :: temp
20  REAL(rprec) :: rand
21 c
22 c
23  DO i=1,nparam
24  g0(i)=par_min(i)
25  pardel(i)=par_max(i)-par_min(i)
26  itemp=2**nposibl(i)
27  g1(i)=pardel(i)/(itemp-1)
28  END DO
29 
30  DO i=1,nparam
31  ig2(i)=nposibl(i)
32  END DO
33 c
34 c Count the total number of chromosomes (bits) required
35  nchrome=0
36  npossum=0
37  ig2sum=0
38  DO 9 i=1,nparam
39  nchrome=nchrome+ig2(i)
40  npossum=npossum+2**nposibl(i)
41  ig2sum=ig2sum+(2**ig2(i))
42  9 CONTINUE
43  IF (nchrome.gt.nchrmax) THEN
44  IF (myid .eq. master) THEN
45  WRITE(6,1800) nchrome
46  WRITE(iunit_ga_out,1800) nchrome
47  CLOSE(iunit_ga_out)
48  END IF
49  stop
50  END IF
51 c
52  IF (npossum.lt.ig2sum .and. microga.ne.0
53  1 .and. myid.eq.master) THEN
54  WRITE(6,2100)
55  WRITE(iunit_ga_out,2100)
56  END IF
57 c
58 c Initialize random number generator
59  CALL ran3(idum,rand)
60 c
61  IF(irestrt.eq.0) THEN
62 c Initialize the random distribution of parameters in the individual
63 c parents when irestrt=0.
64  istart=1
65  DO 10 i=1,npopsiz
66  DO 15 j=1,nchrome
67  CALL ran3(1,rand)
68  iparent(j,i)=1
69  IF(rand.lt.0.5d0) iparent(j,i)=0
70  15 CONTINUE
71  10 CONTINUE
72  IF (npossum.lt.ig2sum) CALL ga_possibl(parent,iparent,myid)
73 c insert unique individual
74  IF (unique_ind .gt. 0) THEN
75  DO i=1, nparam
76  CALL ga_code(unique_ind, i, parent, iparent)
77  END DO
78  END IF
79 
80  ELSE
81 c If irestrt.ne.0, READ from restart file.
82  IF (myid .eq. master) THEN
83  iunit_ga_restart = 25
84  temp = "../ga_restart." // filename
85  CALL safe_open(iunit_ga_restart, istat,
86  1 trim(temp), 'unknown', 'formatted')
87  READ (iunit_ga_restart,*) istart,npopsiz
88  DO j=1,npopsiz
89  READ(iunit_ga_restart,*) k,(iparent(l,j),l=1,nchrome)
90  END DO
91  CLOSE (iunit_ga_restart)
92  END IF
93 #if defined(MPI_OPT)
94  CALL mpi_bcast(istart, 1, mpi_integer, master,
95  1 mpi_comm_world, ierr)
96  CALL mpi_bcast(npopsiz, 1, mpi_integer, master,
97  1 mpi_comm_world, ierr)
98  DO l = 1, nchrome
99  IF (myid .eq. master) fitness = iparent(l,:)
100  CALL mpi_bcast(fitness, indmax, mpi_real8, master,
101  1 mpi_comm_world, ierr)
102  IF (myid .ne. master) iparent(l,:) = fitness
103  END DO
104 #endif
105  END IF
106 c
107  IF(irestrt.ne.0) CALL ran3(idum-istart,rand)
108 c
109  1800 FORMAT(1x,'ERROR: nchrome > nchrmax. Set nchrmax = ',i6)
110  2000 FORMAT(1x,'ERROR: you have a parameter with a number of '/
111  + 1x,' possibilities > 2**30! if you really desire this,'/
112  + 1x,' change the do loop 7 statement and recompile.'//
113  + 1x,' you may also need to alter the code to work with'/
114  + 1x,' real numbers rather than integer numbers; fortran'/
115  + 1x,' does not like to compute 2**j when j>30.')
116  2100 FORMAT(1x,'WARNING: for some cases, a considerable performance'/
117  + 1x,' reduction has been observed when running a non-'/
118  + 1x,' optimal number of bits with the micro-GA.'/
119  + 1x,' If possible, use values for nposibl of 2**n,'/
120  + 1x,' e.g. 2, 4, 8, 16, 32, 64, etc. See ReadMe file.')
121 c
122 
123  END SUBROUTINE ga_initial
mpi_inc
Umbrella module avoid multiple inlcudes of the mpif.h header.
Definition: mpi_inc.f:11