      program schwefel

      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 iterations?'
      read *, maxiter
      ctx = PGACreate(PGA_DATATYPE_BINARY, 100, 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
      character*1 usegray
      common /GLOBAL/ usegray

      sum = 0
      if (usegray .EQ. 'y') then
         do i = 1, 10
            term = PGAGetRealFromGrayCode(ctx, p, pop, i * 10 - 9,
     &           i * 10, -512d0, 511d0)
            sum = sum - term * dsin(dsqrt(dabs(term)))
         enddo
      else
         do i = 1, 10
            term = PGAGetRealFromBinary(ctx, p, pop, i * 10 - 9,
     &           i * 10, -512d0, 511d0)
            sum = sum - term * dsin(dsqrt(dabs(term)))
         enddo
      endif
      evaluate = sum

      end
