c 
c   This is a FORTRAN test program for PGAPack.  The objective is to maximize
c   the number of 1-bits in a chromosome.  To test the integration of a user
c   supplied local search heuristic, suggest a heuristic that randomly
c   selects a bit and sets it to one
c
      include "pgapackf.h"

      integer ctx
      double precision evaluate
      external evaluate

      ctx = PGACreate(PGA_DATATYPE_BINARY, 100, PGA_MAXIMIZE)
      call PGASetRandomSeed    ( ctx, 1)
      call PGASetMaxIter       ( ctx, 50)
      call PGASetUp            ( ctx                       )
      call PGARun              ( ctx, evaluate             )
      call PGADestroy          ( ctx                       )
      
      stop
      end

c
c     user defined evaluation function
c
      double precision function evaluate ( ctx, p, pop )
      include "pgapackf.h"

      integer ctx, p, pop
      integer i, bit, nbits, stringlen

      stringlen = PGAGetStringLen(ctx)
      nbits = 0

      do i=1, stringlen
         bit = PGAGetBinaryAllele(ctx, p, pop, i)
         if ( bit .eq. 1 ) then 
            nbits = nbits + 1
         endif
      enddo

      evaluate = dble(nbits)
     
      return
      end
