C     ******************************************************************
C     ******************************************************************

      program packing

C     PARAMETERS
      integer ncirmax
      parameter ( ncirmax  =         100 )

C     COMMON SCALARS
      integer probid,nrec,r,ncir,
      double precision dx,dy,xmin,xmax,ymin,ymax
      double precision circle(ncirmax,3)

C     COMMON BLOCKS
      common /packdata/ dx,dy,xmin,xmax,ymin,ymax,probid,nrec,r
      common /packcircledata/ circle,ncir

C     LOCAL SCALARS
      integer lower,upper
      real tottime
      double precision f

C     LOCAL ARRAYS
      real dum(2),time(2)

      write(*,100)

      write(*,200)
      read(*,*) probid
      write(*,300)
      read(*,*) lower
      write(*,400)
      read(*,*) upper
      write(*,*)

      tottime = 0.0

      do nrec = lower,upper

         time(1) = etime(dum)

         call algencanma(probid,nrec,f)
      
         time(2) = etime(dum)
         tottime = tottime + time(2) - time(1)

         if( f .le. 1.0d-08 ) then
            if( nrec .gt. 1) then
               write(*,500) ' A packing with ',nrec,
     +                      ' rectangles was found. Time = ',tottime
            else
               write(*,500) ' A packing with ', 1,
     +                      ' rectangle was found.  Time = ',tottime
            end if
         else
            if( nrec .gt. 1) then
               write(*,500) ' A packing with ',nrec,
     +                      ' rectangles was not found. Time = ',tottime
            else
               write(*,500) ' A packing with ', 1,
     +                      ' rectangle was not found.  Time = ',tottime
            end if
         end if

      end do

 100  format(/,' Welcome to IGENPACK !!!',/
     +       /,' This program uses IGENCAN and the strategy described',
     +         ' in:',/
     +       /,' E. G. Birgin and R. D. Lobato, Orthogonal packing of',
     +       /,' rectangles within isotropic convex regions,',
     +       /,' submitted, 2009.',/,
     +       /,' to solve the problem of packing rectangles within',
     +       /,' arbitrary isotropic convex regions.',/)

 200  format(' Enter the problem number (between 1 and 16): '$)
 300  format(' Enter the minimum number of rectangles to be packed: '$)
 400  format(' Enter the maximum number of rectangles to be packed: '$)
 500  format(A,I3,A,F8.2)

      end

      subroutine algencanma(probid,num_rec,f)

      implicit none

#include "dim.par"

C     SCALAR ARGUMENTS
      integer num_rec,probid
      double precision f

C     LOCAL SCALARS
      logical checkder
      integer inform,iprint,m,n,ncomp,nbv,niv
      double precision cnorm,epsfeas,epsopt,nlpsupn,snorm

C     LOCAL ARRAYS
      logical coded(10),equatn(mmax),linear(mmax)
      double precision l(nmax),lambda(mmax),u(nmax),x(nmax)
      integer variableTypes(nmax)

C     EXTERNAL SUBROUTINES
      external algencan,param

C     SET UP PROBLEM DATA

      call param(epsfeas,epsopt,iprint,ncomp)

      call inip(n,x,l,u,m,lambda,equatn,linear,coded,checkder,nbv,niv,
     +variableTypes,probid,num_rec)

      call algencan(epsfeas,epsopt,iprint,ncomp,n,x,l,u,m,lambda,equatn,
     +linear,coded,checkder,f,cnorm,snorm,nlpsupn,inform,nbv,niv,
     +variableTypes)

      call drawsol(n,x,'solution.mp')

      call endp(n,x,l,u,m,lambda,equatn,linear)

      end

C     ******************************************************************
C     ******************************************************************

      subroutine param(epsfeas,epsopt,iprint,ncomp)

C     SCALAR ARGUMENTS
      integer iprint,ncomp
      double precision epsfeas,epsopt

      epsfeas  = 1.0d-08
      epsopt   = 1.0d-08

      iprint   = 0
      ncomp    = 6

      end

