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,compvols
  
  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 = 22
  
  allocate(pdata%nvpols(pdata%npols),stat=allocerr)
  if ( allocerr .ne. 0 ) then
     write(*,*) 'Allocation error.'
     stop
  end if

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

  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.0d0, 0.0d0, 2.49d0, 3.0d0, 3.0d0, 0.0d0   /) 
  pdata%ypol( 1,1:pdata%nvpols(1))  = (/ 0.0d0, -3.44d0, -3.44d0, -3.0d0, 0.0d0, 0.0d0  /)
  
  pdata%xpol( 2,1:pdata%nvpols(2))  = (/ 0.0d0, 0.0d0, 1.58d0, 2.49d0, 2.49d0, 0.0d0 /)
  pdata%ypol( 2,1:pdata%nvpols(2))  = (/ -3.44d0, -4.5d0, -4.5d0, -3.74d0, -3.44d0, -3.44d0 /)  
  
  pdata%xpol( 3,1:pdata%nvpols(3))  = (/ 0.0d0, 0.0d0, 1.58d0, 1.58d0, 0.0d0 /)
  pdata%ypol( 3,1:pdata%nvpols(3))  = (/ -4.5d0, -4.79d0, -4.79d0, -4.5d0, -4.5d0 /)  
  
  pdata%xpol( 4,1:pdata%nvpols(4))  = (/ 0.0d0, 0.0d0, 1.87d0, 2.0d0, 2.0d0, 1.58d0, 0.0d0 /)
  pdata%ypol( 4,1:pdata%nvpols(4))  = (/ -4.79d0, -5.48d0, -5.48d0, -5.4d0, -5.14d0, -4.79d0, -4.79d0 /) 
  
  pdata%xpol( 5,1:pdata%nvpols(5))  = (/ 0.0d0, 0.0d0, 1.87d0, 1.87d0, 0.0d0 /)
  pdata%ypol( 5,1:pdata%nvpols(5))  = (/ -5.48d0, -5.86d0, -5.86d0, -5.48d0, -5.48d0 /) 
  
  pdata%xpol( 6,1:pdata%nvpols(6))  = (/ 0.0d0, 0.0d0, 2.26d0, 2.42d0, 2.42d0, 1.87d0, 0.0d0 /)
  pdata%ypol( 6,1:pdata%nvpols(6))  = (/ -5.86d0, -6.9d0, -6.9d0, -6.76d0, -6.51d0, -5.86d0, -5.86d0 /) 
  
  pdata%xpol( 7,1:pdata%nvpols(7))  = (/ 0.0d0, 0.0d0, 2.26d0, 2.26d0, 0.0d0 /)
  pdata%ypol( 7,1:pdata%nvpols(7))  = (/ -6.9d0, -7.22d0, -7.22d0, -6.9d0, -6.9d0 /) 
  
  pdata%xpol( 8,1:pdata%nvpols(8))  = (/ 0.0d0, 0.0d0, 2.1d0, 2.43d0, 2.43d0, 2.26d0, 0.0d0 /)
  pdata%ypol( 8,1:pdata%nvpols(8))  = (/ -7.22d0, -7.98d0, -7.98d0, -7.65d0, -7.4d0, -7.22d0, -7.22d0 /) 
  
  pdata%xpol( 9,1:pdata%nvpols(9))  = (/ 0.0d0, 0.0d0, 2.1d0, 2.1d0, 0.0d0 /)
  pdata%ypol( 9,1:pdata%nvpols(9))  = (/ -7.98d0, -8.2d0, -8.2d0, -7.98d0, -7.98d0 /)   

  pdata%xpol(10,1:pdata%nvpols(10)) = (/ 0.0d0, 0.0d0, 2.26d0, 2.43d0, 2.43d0, 2.1d0, 0.0d0 /)
  pdata%ypol(10,1:pdata%nvpols(10)) = (/ -8.2d0, -8.87d0, -8.87d0, -8.74d0, -8.49d0, -8.2d0, -8.2d0 /)   
    
  pdata%xpol(11,1:pdata%nvpols(11)) = (/ 0.0d0, 0.0d0, 2.26d0, 2.26d0, 0.0d0 /)
  pdata%ypol(11,1:pdata%nvpols(11)) = (/ -8.87d0, -9.17d0, -9.17d0, -8.87d0, -8.87d0 /)   
  
  pdata%xpol(12,1:pdata%nvpols(12)) = (/ 0.0d0, 0.0d0, 1.87d0, 2.43d0, 2.43d0, 2.26d0, 0.0d0 /)
  pdata%ypol(12,1:pdata%nvpols(12)) = (/ -9.17d0, -10.15d0, -10.15d0, -9.62d0, -9.28d0, -9.17d0, -9.17d0 /)
  
  pdata%xpol(13,1:pdata%nvpols(13)) = (/ 0.0d0, 0.0d0, 0.37d0, 0.94d0, 1.87d0, 1.87d0, 0.0d0 /)
  pdata%ypol(13,1:pdata%nvpols(13)) = (/ -10.15d0, -10.5d0, -10.9d0, -10.9d0, -10.35d0, -10.15d0, -10.15d0 /)  
  
  pdata%xpol(14,1:pdata%nvpols(14)) = (/ 0.94d0, 1.29d0, 1.86d0, 2.26d0, 1.87d0, 0.94d0   /) 
  pdata%ypol(14,1:pdata%nvpols(14)) = (/ -10.9d0, -11.35d0, -11.12d0, -10.7d0, -10.35d0, -10.9d0 /)     
  
  pdata%xpol(15,1:pdata%nvpols(15)) = (/ 0.85d0, 0.58d0, -0.51d0, -3.0d0, -3.6d0, -3.0d0, 0.0d0, 0.85d0   /) 
  pdata%ypol(15,1:pdata%nvpols(15)) = (/ 6.06d0, 6.68d0, 6.53d0, 6.0d0, 3.5d0, 0.7d0, 0.0d0, 6.06d0 /)   
      
  pdata%xpol(16,1:pdata%nvpols(16)) = (/ 1.5d0, 0.85d0, 0.0d0, 3.0d0, 1.5d0 /)
  pdata%ypol(16,1:pdata%nvpols(16)) = (/ 5.86d0, 6.06d0, 0.0d0, 0.0d0, 5.86d0 /)  
  
  pdata%xpol(17,1:pdata%nvpols(17)) = (/ 1.5d0, 3.0d0, 2.15d0, 1.5d0 /)
  pdata%ypol(17,1:pdata%nvpols(17)) = (/ 5.86d0, 0.0d0, 6.06d0, 5.86d0 /)

  pdata%xpol(18,1:pdata%nvpols(18)) = (/ 2.15d0, 3.0d0, 6.0d0, 6.6d0, 6.0d0, 3.51d0, 2.42d0, 2.15d0  /) 
  pdata%ypol(18,1:pdata%nvpols(18)) = (/ 6.06d0, 0.0d0, 0.7d0, 3.35d0, 6.0d0, 6.53d0, 6.68d0, 6.06d0 /)

  pdata%xpol(19,1:pdata%nvpols(19)) = (/ 0.58d0, 0.85d0, 0.69d0, 0.0d0, -0.51d0, 0.58d0   /) 
  pdata%ypol(19,1:pdata%nvpols(19)) = (/ 6.68d0, 7.3d0, 8.16d0, 7.62d0, 6.53d0, 6.68d0 /)
  
  pdata%xpol(20,1:pdata%nvpols(20)) = (/ 0.85d0, 1.5d0, 1.5d0, 0.69d0, 0.85d0 /)
  pdata%ypol(20,1:pdata%nvpols(20)) = (/  7.3d0, 7.5d0, 8.5d0, 8.16d0,  7.3d0 /) 
  
  pdata%xpol(21,1:pdata%nvpols(21)) = (/ 1.5d0, 2.15d0, 2.31d0, 1.5d0, 1.5d0 /)
  pdata%ypol(21,1:pdata%nvpols(21)) = (/ 7.5d0, 7.3d0, 8.16d0, 8.5d0, 7.5d0 /)     
  
  pdata%xpol(22,1:pdata%nvpols(22)) = (/ 2.42d0, 3.51d0, 3.0d0, 2.31d0, 2.15d0, 2.42d0   /) 
  pdata%ypol(22,1:pdata%nvpols(22)) = (/ 6.68d0, 6.53d0, 7.62d0, 8.16d0, 7.3d0, 6.68d0 /)
  
  pdata%poledges( 1,1:pdata%nvpols(1)-1)  = (/ 0, 1, 0, 0, 1 /) 
  pdata%poledges( 2,1:pdata%nvpols(2)-1)  = (/ 0, 1, 0, 0, 1 /) 
  pdata%poledges( 3,1:pdata%nvpols(3)-1)  = (/ 0, 1, 0, 1 /)
  pdata%poledges( 4,1:pdata%nvpols(4)-1)  = (/ 0, 1, 0, 0, 0, 1 /)
  pdata%poledges( 5,1:pdata%nvpols(5)-1)  = (/ 0, 1, 0, 1 /)
  pdata%poledges( 6,1:pdata%nvpols(6)-1)  = (/ 0, 1, 0, 0, 0, 1 /)
  pdata%poledges( 7,1:pdata%nvpols(7)-1)  = (/ 0, 1, 0, 1 /)
  pdata%poledges( 8,1:pdata%nvpols(8)-1)  = (/ 0, 1, 0, 0, 0, 1 /)
  pdata%poledges( 9,1:pdata%nvpols(9)-1)  = (/ 0, 1, 0, 1 /)
  pdata%poledges(10,1:pdata%nvpols(10)-1) = (/ 0, 1, 0, 0, 0, 1 /)
  pdata%poledges(11,1:pdata%nvpols(11)-1) = (/ 0, 1, 0, 1 /)
  pdata%poledges(12,1:pdata%nvpols(12)-1) = (/ 0, 1, 0, 0, 0, 1 /)
  pdata%poledges(13,1:pdata%nvpols(13)-1) = (/ 0, 0, 0, 1, 0, 1 /)
  pdata%poledges(14,1:pdata%nvpols(14)-1) = (/ 0, 0, 0, 0, 1 /)
  pdata%poledges(15,1:pdata%nvpols(15)-1) = (/ 0, 1, 0, 0, 0, 0, 1 /)
  pdata%poledges(16,1:pdata%nvpols(16)-1) = (/ 0, 1, 1, 1 /)
  pdata%poledges(17,1:pdata%nvpols(17)-1) = (/ 1, 1, 0 /)
  pdata%poledges(18,1:pdata%nvpols(18)-1) = (/ 1, 0, 0, 0, 0, 1, 0 /)
  pdata%poledges(19,1:pdata%nvpols(19)-1) = (/ 0, 1, 0, 0, 1 /)
  pdata%poledges(20,1:pdata%nvpols(20)-1) = (/ 0, 1, 0, 1 /)
  pdata%poledges(21,1:pdata%nvpols(21)-1) = (/ 0, 1, 0, 1 /)
  pdata%poledges(22,1:pdata%nvpols(22)-1) = (/ 1, 0, 0, 1, 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.35d0 / scale

  ! 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,"(A12,I0,A3)") 'fig-key-ini-',nsites,'.mp'
        call drawsol(n,x0,filename,c_loc(pdata))
  
        write(filename,"(A12,I0,A3)") 'fig-key-end-',nsites,'.mp'
        call drawsol(n,x,filename,c_loc(pdata))

        call compvols(n,x,vol,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, &
             pdata%volA / nsites, &
             count( vol(1:nsites) .lt. sqrt( epsilon( 1.0d0 ) ) ), &
             maxval( abs( vol(1:nsites) * nsites / pdata%volA - 1.0d0 ), vol(1:nsites) .ge. sqrt( epsilon( 1.0d0 ) ) )
        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,1X,1P,E12.5,1X,I6,1X,1P,E12.5)
  
end program optvorma
