!! module to include cost functions for different applications

module ga_example_function

	implicit none
	
	contains

	!-----------------------------------------------------------------------
	subroutine func(npopsiz,nparam,j,funcval,parent)
	!-----------------------------------------------------------------------
	! This is an N-dimensional version of the multimodal function with
	! decreasing peaks used by Goldberg and Richardson (1987, see ReadMe
	! file for complete reference).  In N dimensions, this function has
	! (nvalley-1)^nparam peaks, but only one global maximum.  It is a
	! reasonably tough problem for the GA, especially for higher dimensions
	! and larger values of nvalley.
	!-----------------------------------------------------------------------

		!! subroutine arguments
		integer, intent(in) :: npopsiz
		integer, intent(in) :: nparam
		integer, intent(in) :: j
		real, intent(out) :: funcval
		real, dimension(nparam,npopsiz), intent(in) :: parent
!		real, dimension(npopsiz,nparam), intent(in) :: parent2
!		integer, dimension(npopsiz,nchrome), intent(in) :: iparent2

		!! local variables
		integer :: i,nvalley
		real :: f1,f2,pi

		nvalley=6
		pi=4.0d0*datan(1.d0)
		funcval=1.0d0
		do i=1,nparam
			f1=(sin(5.1d0*pi*parent(i,j) + 0.5d0))**nvalley
			f2=exp(-4.0d0*log(2.0d0)*((parent(i,j)-0.0667d0)**2)/0.64d0)
			funcval=funcval*f1*f2
		end do

		! As mentioned in the ReadMe file, The arrays have been rearranged
		! to enable a more efficient caching of system memory.  If this causes
		! interface problems with existing functions used with previous 
		! versions of my code, then you can use some temporary arrays to bridge
		! this version with older versions.  I've named the temporary arrays
		! parent2 and iparent2.  If you want to use these arrays, uncomment the
		! dimension statements above as well as the following do loop lines.

		! do i=1,nparam
		!	 parent2(j,i)=parent(i,j)
		! end do
		! do k=1,nchrome
		!	iparent2(j,k)=iparent(k,j)
		! end do

		return
	end subroutine func
	!---------------------------------------------------------------------------

end module ga_example_function
