      program rastrigin

      include "pgapackf.h"
      external evaluate
      integer ctx, maxiter
      double precision evaluate
      character*1 usegray
      common /GLOBAL/ usegray

      print *, 'Do you want to use Gray-coding (y/n): '
      read *, usegray
      print *, 'How many iteration?'
      read *, maxiter
      ctx = PGACreate(PGA_DATATYPE_BINARY, 200, PGA_MINIMIZE)
      call PGASetRandomSeed(ctx, 1)
      call PGASetMaxIter(ctx, maxiter)
      call PGASetUp(ctx)
      call PGARun(ctx, evaluate)
      call PGADestroy(ctx)

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

      sum = 0
      if (usegray .EQ. 'y') then
         do i = 1, 20
            term = PGAGetRealFromGrayCode(ctx, p, pop, i * 10 - 9,
     &           i * 10, -5.12d0, 5.11d0)
            sum = sum + term * term - 10 * cos(2 * PI25DT * term)
         enddo
      else
         do i = 1, 20
            term = PGAGetRealFromBinary(ctx, p, pop, i * 10 - 9,
     &           i * 10, -5.12d0, 5.11d0)
            sum = sum + term * term - 10 * cos(2 * PI25DT * term)
         enddo
      endif
      evaluate = 200 + sum

      end
