      program dejong
      include "pgapackf.h"
      external dejong1, dejong2, dejong3, dejong4, dejong5
      integer len, testnum, ctx, maxiter
      double precision dejong1, dejong2, dejong3, dejong4, dejong5
      character*1 usegray
      common /GLOBAL/ usegray

      usegray = 'n'
      print *, 'Do you want to use Gray-coding (y/n)? '
      read *, usegray
      print *, 'Which test do you want to run (1 - 5)? '
      read *, testnum
      goto (10, 20, 30, 40, 50), testnum
 10   len = 30
      goto 90
 20   len = 24
      goto 90
 30   len = 50
      goto 90
 40   len = 240
      goto 90
 50   len = 34
      goto 90
 90   continue
      ctx = PGACreate(PGA_DATATYPE_BINARY, len, PGA_MINIMIZE)
      call PGASetRandomSeed(ctx, 1)
      print *, 'How many iterations?'
      read *, maxiter
      call PGASetMaxIter(ctx, maxiter)
      call PGASetUp(ctx)
      goto (110, 120, 130, 140, 150), testnum
 110  call PGARun(ctx, dejong1)
      goto 190
 120  call PGARun(ctx, dejong2)
      goto 190
 130  call PGARun(ctx, dejong3)
      goto 190
 140  call PGARun(ctx, dejong4)
      goto 190
 150  call PGARun(ctx, dejong5)
      goto 190
 190  continue
      call PGADestroy(ctx)

      stop
      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function dejong1(ctx, p, pop)
      include "pgapackf.h"
      integer ctx, p, pop, i
      double precision term, sum
      character*1 usegray
      common /GLOBAL/ usegray

      sum = 0
      if (usegray .eq. 'y') then
         do i = 1,30,10
            term = PGAGetRealFromGrayCode(ctx, p, pop, i, i + 9,
     &           -5.12d0, 5.11d0)
            sum = sum + (term * term)
         enddo
      else
         do i = 1,30,10
            term = PGAGetRealFromBinary(ctx, p, pop, i, i + 9,
     &           -5.12d0, 5.11d0)
            sum = sum + (term * term)
         enddo
      endif
      dejong1 = sum

      end

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function dejong2(ctx, p, pop)
      include "pgapackf.h"
      integer ctx, p, pop
      double precision x1, x2, p1, p2
      character*1 usegray
      common /GLOBAL/ usegray

      if (usegray .eq. 'y') then
         x1 = PGAGetRealFromGrayCode(ctx, p, pop, 1, 12, -2.048d0,
     &        2.047d0)
         x2 = PGAGetRealFromGrayCode(ctx, p, pop, 13, 24, -2.048d0,
     &        2.047d0)
      else
         x1 = PGAGetRealFromBinary(ctx, p, pop, 1, 12, -2.048d0,
     &        2.047d0)
         x2 = PGAGetRealFromBinary(ctx, p, pop, 13, 24, -2.048d0,
     &        2.047d0)
      endif

      p1 = x1 * x1 - x2
      p2 = 1 - x1
      dejong2 = 100 * p1 * p1 + p2 * p2

      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function dejong3(ctx, p, pop)
      include "pgapackf.h"
      external floor
      integer ctx, p, pop, i
      double precision sum, floor
      character*1 usegray
      common /GLOBAL/ usegray

      sum = 0
      if (usegray .eq. 'y') then
         do i = 1,50,10
            sum = sum + floor(PGAGetRealFromGrayCode(ctx, p, pop, i,
     &           i + 9, -5.12d0, 5.11d0))
         enddo
      else
         do i = 1,50,10
            sum = sum + floor(PGAGetRealFromBinary(ctx, p, pop, i,
     &           i + 9, -5.12d0, 5.11d0))
         enddo
      endif

      dejong3 = sum

      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function dejong4(ctx, p, pop)
      include "pgapackf.h"
      integer ctx, p, pop, i
      double precision term, sum
      character*1 usegray
      common /GLOBAL/ usegray

      sum = 0
      if (usegray .eq. 'y') then
         do i = 1,30
            term = PGAGetRealFromGrayCode(ctx, p, pop, i * 8 - 7,
     &           i * 8, -1.28d0, 1.27d0)
            sum = sum + (i * term * term * term * term)
         enddo
      else
         do i = 1,30
            term = PGAGetRealFromBinary(ctx, p, pop, i * 8 - 7,
     &           i * 8, -1.28d0, 1.27d0)
            sum = sum + (i * term * term * term * term)
         enddo
      endif
      dejong4 = sum + PGARandomGaussian(ctx, 0d0, 1d0)

      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function dejong5(ctx, p, pop)
      include "pgapackf.h"
      integer ctx, p, pop, i, j
      double precision sum_over_j, sum_over_i, term
      character*1 usegray
      common /GLOBAL/ usegray
      integer A(2,25)
      data A          /-32, -32,   -16, -32,     0, -32,    16, -32,
     &                  32, -32,   -32, -16,   -16, -16,     0, -16,
     &                  16, -16,    32, -16,   -32,   0,   -16,   0,
     &                   0,   0,    16,   0,    32,   0,   -32,  16,
     &                 -16,  16,     0,  16,    16,  16,    32,  16,
     &                 -32,  32,   -16,  32,     0,  32,    16,  32,
     &                  32,  32/

      sum_over_j = 0d0
      if (usegray .eq. 'y') then
         do j = 1, 25
            sum_over_i = 0
            do i = 1, 2
               term = PGAGetRealFromGrayCode(ctx, p, pop, i * 17 - 16,
     &              i * 17, -65.536D0, 65.535D0)
               sum_over_i = sum_over_i + ((term - A(i, j)) ** 6)
            enddo
            sum_over_j = sum_over_j + (1D0 / (j + sum_over_i))
         enddo
      else
         do j = 1, 25
            sum_over_i = 0
            do i = 1, 2
               term = PGAGetRealFromBinary(ctx, p, pop, i * 17 - 16,
     &              i * 17, -65.536D0, 65.535D0)
               sum_over_i = sum_over_i + ((term - A(i, j)) ** 6)
            enddo
            sum_over_j = sum_over_j + (1D0 / (j + sum_over_i))
         enddo
      endif
      dejong5 = 1D0 / (2D-3 + sum_over_j)

      end
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      double precision function floor(x)
      double precision x, y

      y = int(x)
      if (x .lt. 0 .and. x .ne. y) then
         y = y - 1
      endif
      floor = y

      end
