program optvorma

  use iso_c_binding, only: c_ptr,c_loc,c_f_pointer
  use optvor, only: pdata_type,evalfg,proj,approxg,drawsol,InA,compscale
  
  implicit none
	
  ! LOCAL SCALARS
  integer :: allocerr,fcnt,i,inform,iprint,iter,itrial,j,maxfc,maxit,maxnv,n, &
       nsites,ntrials,spginfo
  real(kind=8) :: area,epsopt,f,fbest,finish,ftarget,gpsupn,marg,scale,seed,start
  type(pdata_type), target :: pdata

  ! LOCAL ARRAYS
  real(kind=8) :: p(2)
  real(kind=8), allocatable :: g(:),x(:),x0(:),vol(:)
  character(len=80) :: filename

  ! FUNCTIONS
  real(kind=8) :: drand
  
  ! Problem data
  
  ! Set sites

  write(*,*) 'Enter nsites>0: '
  read(*,*) nsites

  ! Set disjoint convex polygons Aj whose union define A 
  
  pdata%npols = 16

  allocate(pdata%nvpols(pdata%npols),stat=allocerr)
  if ( allocerr .ne. 0 ) then
     write(*,*) 'Allocation error.'
     stop
  end if

  pdata%nvpols(1:pdata%npols) = (/ 7, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7 /)

  maxnv = maxval( pdata%nvpols(1:pdata%npols) )

  allocate(pdata%poledges(pdata%npols,maxnv),pdata%xpol(pdata%npols,maxnv), &
       pdata%ypol(pdata%npols,maxnv),stat=allocerr)
  if ( allocerr .ne. 0 ) then
     write(*,*) 'Allocation error.'
     stop
  end if

  pdata%xpol( 1,1:pdata%nvpols(1))  = (/ -0.1d0, 8.2d0, 8.2d0, 6.92d0, 1.0d0, -0.1d0, -0.1d0 /)
  pdata%ypol( 1,1:pdata%nvpols(1))  = (/ 0.0d0, 0.0d0, 0.62d0, 0.76d0, 0.8d0, 0.6d0, 0.0d0 /)

  pdata%xpol( 2,1:pdata%nvpols(2))  = (/ 1.0d0, 6.92d0, 5.86d0, 2.0d0, 1.0d0 /)
  pdata%ypol( 2,1:pdata%nvpols(2))  = (/ 0.8d0, 0.76d0, 1.32d0, 1.5d0, 0.8d0 /)
  
  pdata%xpol( 3,1:pdata%nvpols(3))  = (/ 2.0d0, 5.86d0, 5.24d0, 3.5d0, 2.0d0 /)
  pdata%ypol( 3,1:pdata%nvpols(3))  = (/ 1.5d0, 1.32d0, 2.65d0, 4.36d0, 1.5d0 /)

  pdata%xpol( 4,1:pdata%nvpols(4))  = (/ 5.24d0, 5.58d0, 3.5d0, 5.24d0 /)
  pdata%ypol( 4,1:pdata%nvpols(4))  = (/ 2.65d0, 4.36d0, 4.36d0, 2.65d0 /)
  
  pdata%xpol( 5,1:pdata%nvpols(5))  = (/ 3.5d0, 5.58d0, 7.58d0, 5.5d0, 3.5d0 /)
  pdata%ypol( 5,1:pdata%nvpols(5))  = (/ 4.36d0, 4.36d0, 9.0d0, 9.0d0, 4.36d0 /)
       
  pdata%xpol( 6,1:pdata%nvpols(6))  = (/ 5.5d0, 7.58d0, 8.4d0, 6.32d0, 5.5d0 /)
  pdata%ypol( 6,1:pdata%nvpols(6))  = (/ 9.0d0, 9.0d0, 10.91d0, 10.91d0, 9.0d0 /)
        
  pdata%xpol( 7,1:pdata%nvpols(7))  = (/ 6.32d0, 8.4d0, 14.02d0, 11.94d0, 6.32d0 /)
  pdata%ypol( 7,1:pdata%nvpols(7))  = (/ 10.91d0, 10.91d0, 23.95d0, 23.95d0, 10.91d0 /)
          
  pdata%xpol( 8,1:pdata%nvpols(8))  = (/ 11.94d0, 18.72d0, 15.89d0, 14.79d0, 11.94d0 /)
  pdata%ypol( 8,1:pdata%nvpols(8))  = (/ 23.95d0, 23.95d0, 30.56d0, 30.56d0, 23.95d0 /)
          
  pdata%xpol( 9,1:pdata%nvpols(9))  = (/ 19.6d0, 24.3d0, 18.72d0, 14.02d0, 19.6d0 /)
  pdata%ypol( 9,1:pdata%nvpols(9))  = (/ 10.91d0, 10.91d0, 23.95d0, 23.95d0, 10.91d0 /)
          
  pdata%xpol(10,1:pdata%nvpols(10)) = (/ 7.58d0, 20.42d0, 19.6d0, 8.4d0, 7.58d0 /)
  pdata%ypol(10,1:pdata%nvpols(10)) = (/ 9.0d0, 9.0d0, 10.91d0, 10.91d0, 9.0d0 /)
  
  pdata%xpol(11,1:pdata%nvpols(11)) = (/ 20.42d0, 25.12d0, 24.3d0, 19.6d0, 20.42d0 /)
  pdata%ypol(11,1:pdata%nvpols(11)) = (/ 9.0d0, 9.0d0, 10.91d0, 10.91d0, 9.0d0 /)
        
  pdata%xpol(12,1:pdata%nvpols(12)) = (/ 22.06d0, 26.54d0, 25.12d0, 20.42d0, 22.06d0 /)
  pdata%ypol(12,1:pdata%nvpols(12)) = (/ 5.15d0, 6.0d0, 9.0d0, 9.0d0, 5.15d0 /)
  
  pdata%xpol(13,1:pdata%nvpols(13)) = (/ 22.46d0, 28.53d0, 26.54d0, 22.06d0, 22.46d0 /)
  pdata%ypol(13,1:pdata%nvpols(13)) = (/ 2.26d0, 2.3d0, 6.0d0, 5.15d0, 2.26d0 /)            
  
  pdata%xpol(14,1:pdata%nvpols(14)) = (/ 22.05d0, 29.6d0, 28.53d0, 22.46d0, 22.05d0 /)
  pdata%ypol(14,1:pdata%nvpols(14)) = (/ 1.2d0, 1.22d0, 2.3d0, 2.26d0, 1.2d0 /)
      
  pdata%xpol(15,1:pdata%nvpols(15)) = (/ 21.24d0, 30.79d0, 29.6d0, 22.05d0, 21.24d0 /)
  pdata%ypol(15,1:pdata%nvpols(15)) = (/ 0.82d0, 0.74d0, 1.22d0, 1.2d0, 0.82d0 /)  
    
  pdata%xpol(16,1:pdata%nvpols(16)) = (/ 19.13d0, 32.15d0, 32.15d0, 30.79d0, 21.24d0, 19.13d0, 19.13d0 /)
  pdata%ypol(16,1:pdata%nvpols(16)) = (/ 0.0d0, 0.0d0, 0.6d0, 0.74d0, 0.82d0, 0.6d0, 0.0d0 /)
      
  pdata%poledges( 1,1:pdata%nvpols(1)-1)  = (/ 0, 0, 0, 1, 0, 0 /)
  pdata%poledges( 2,1:pdata%nvpols(2)-1)  = (/ 1, 0, 1, 0 /)
  pdata%poledges( 3,1:pdata%nvpols(3)-1)  = (/ 1, 0, 1, 0 /)
  pdata%poledges( 4,1:pdata%nvpols(4)-1)  = (/ 0, 1, 1 /)
  pdata%poledges( 5,1:pdata%nvpols(5)-1)  = (/ 1, 0, 1, 0 /)
  pdata%poledges( 6,1:pdata%nvpols(6)-1)  = (/ 1, 1, 1, 0 /)
  pdata%poledges( 7,1:pdata%nvpols(7)-1)  = (/ 1, 0, 1, 0 /)
  pdata%poledges( 8,1:pdata%nvpols(8)-1)  = (/ 1, 0, 0, 0 /)
  pdata%poledges( 9,1:pdata%nvpols(9)-1)  = (/ 1, 0, 1, 0 /)
  pdata%poledges(10,1:pdata%nvpols(10)-1) = (/ 0, 1, 0, 1 /)
  pdata%poledges(11,1:pdata%nvpols(11)-1) = (/ 1, 0, 1, 1 /)
  pdata%poledges(12,1:pdata%nvpols(12)-1) = (/ 1, 0, 1, 0 /)
  pdata%poledges(13,1:pdata%nvpols(13)-1) = (/ 1, 0, 1, 0 /)
  pdata%poledges(14,1:pdata%nvpols(14)-1) = (/ 1, 0, 1, 0 /)
  pdata%poledges(15,1:pdata%nvpols(15)-1) = (/ 1, 0, 1, 0 /)
  pdata%poledges(16,1:pdata%nvpols(16)-1) = (/ 0, 0, 0, 1, 0, 0 /)

  ! Scale A so cells have ideal area near unity
  
  call compscale(nsites,scale,area,c_loc(pdata))

  pdata%volA = area
  
  do j = 1,pdata%npols
     pdata%xpol(j,1:pdata%nvpols(j)) = scale * pdata%xpol(j,1:pdata%nvpols(j))
     pdata%ypol(j,1:pdata%nvpols(j)) = scale * pdata%ypol(j,1:pdata%nvpols(j))
  end do
  
  pdata%xmin = minval( pdata%xpol(1,1:pdata%nvpols(1)) )
  pdata%xmax = maxval( pdata%xpol(1,1:pdata%nvpols(1)) )
  pdata%ymin = minval( pdata%ypol(1,1:pdata%nvpols(1)) )
  pdata%ymax = maxval( pdata%ypol(1,1:pdata%nvpols(1)) )
  do j = 2,pdata%npols
     pdata%xmin = min( pdata%xmin, minval( pdata%xpol(j,1:pdata%nvpols(j)) ) )
     pdata%xmax = max( pdata%xmax, maxval( pdata%xpol(j,1:pdata%nvpols(j)) ) )
     pdata%ymin = min( pdata%ymin, minval( pdata%ypol(j,1:pdata%nvpols(j)) ) )
     pdata%ymax = max( pdata%ymax, maxval( pdata%ypol(j,1:pdata%nvpols(j)) ) )
  end do
  
  marg = 0.1d0 * max( 1.0d0, abs( pdata%xmin ), abs( pdata%xmax ), abs( pdata%ymin), abs( pdata%ymax ) )

  pdata%xmin = pdata%xmin - marg
  pdata%xmax = pdata%xmax + marg
  pdata%ymin = pdata%ymin - marg
  pdata%ymax = pdata%ymax + marg
  
  pdata%c(1:2) = 0.5d0 * (/ pdata%xmax + pdata%xmin, pdata%ymax + pdata%ymin /)
  pdata%rmax = norm2(pdata%c - (/ pdata%xmin, pdata%ymin /))
  pdata%drawscale = 0.15d0 / scale

  ! Goal function data

  pdata%Jweight(0:5) = 0.0d0
  pdata%Jweight(0)  = 10.0d0
  pdata%Jweight(1)  =  1.0d0
  
  pdata%J0delta = 0.1d0
  
  ! Optimize
  
  write(*,*) 'Enter ntrials>0: '
  read(*,*) ntrials
  
  n = 2 * nsites
  
  allocate(x(n),x0(n),g(n),vol(nsites),stat=allocerr)
  if ( allocerr .ne. 0 ) then
     write(*,*) 'Allocation error.'
     stop
  end if
        
  fbest = huge( 1.0d0 )
  
  do itrial = 1,ntrials
     seed = 123456.0d0 * itrial

     write(*,*) 'Processing trial itrial = ',itrial
     
     do i = 1,nsites
        p(1) = pdata%xmin + ( pdata%xmax - pdata%xmin ) * drand(seed)
        p(2) = pdata%ymin + ( pdata%ymax - pdata%ymin ) * drand(seed)

        do while ( .not. InA(p,c_loc(pdata)) )
           p(1) = pdata%xmin + ( pdata%xmax - pdata%xmin ) * drand(seed)
           p(2) = pdata%ymin + ( pdata%ymax - pdata%ymin ) * drand(seed)
        end do
       
        x(2*i-1) = p(1)
        x(2*i)   = p(2)
     end do

     x0(1:n) = x(1:n)
     
     epsopt  = 1.0d-08
     ftarget = 1.0d-08
     maxit = 10000
     maxfc = 10000
     iprint = 0
  
     call cpu_time(start)
  
     call spg(n,x,epsopt,ftarget,maxit,maxfc,iprint,f,gpsupn,iter,fcnt,spginfo,inform, &
          evalfg,proj,c_loc(pdata))
  
     call cpu_time(finish)

     write(*,*) 'Statistics: f = ',f,' gpsupn = ',gpsupn,' it = ',iter,' fcnt = ',fcnt, &
          ' spginfo = ',spginfo,' time = ',finish-start
     
     if ( f .lt. fbest ) then
        fbest = f

        write(*,*) 'Solution was improved. New best f = ',fbest

        write(filename,"(A10,I0,A3)") 'fig-a-ini-',nsites,'.mp'
        call drawsol(n,x0,filename,c_loc(pdata))
  
        write(filename,"(A10,I0,A3)") 'fig-a-end-',nsites,'.mp'
        call drawsol(n,x,filename,c_loc(pdata))

        open(unit=10,file='tabline.txt')   
        write(10,1000) scale,pdata%volA,pdata%npols,nsites,itrial,f,gpsupn,iter,fcnt,spginfo,finish-start
        close(10)
        
        if ( fbest .le. ftarget ) then
           write(*,*) 'A global minimizer has been found!'
           exit
        end if
     end if
  end do
  
  deallocate(x,x0,g,vol,stat=allocerr)
  if ( allocerr .ne. 0 ) then
     write(*,*) 'Deallocation error.'
     stop
  end if
  
  stop

1000 format(1P,E12.5,1X,1P,E12.5,1X,I3,1X,I6,1X,I6,1X,1P,E12.5,1X,1P,E7.1,2(1X,I6),1X,I2,0P,F14.6)
  
end program optvorma
