      program perf

C     PARAMETERS
      integer nprobmax,nmethmax
      parameter ( nprobmax = 1032, nmethmax = 6 )

C     LOCAL SCALARS
      integer i,j,nprob,nmeth,di1,di2,di3,di4,di5,di6,di7,di8,iguais,
     +        diffmin,bothinf,orifeas,otrfeas
      double precision fmin,mmin,tau,epsfeas,dd1,dd2,dd3,dd4,dd5,dd6,
     +        dd7,dd8
      character * 8 probname1,probname2

C     LOCAL ARRAYS
      integer innit(nmethmax),sca(nmethmax),scb(nmethmax),
     +        cont0(nmethmax),cont1(nmethmax),cont2(nmethmax),
     +        cont9(nmethmax)
      double precision f(nmethmax),cnorm(nmethmax),cputim(nmethmax),
     +        table(nprobmax,nmethmax),rho(nmethmax)

C     CONSTANTS

      epsfeas = 1.0d-08

C     INPUT DATA

      nmeth =    2
      nprob =  733

      open(10,file='algencan-ori-table.out')
      open(20,file='algencan-otr-table.out')

C     READ INPUT TABLES

      cont0(1) = 0
      cont0(2) = 0
      cont1(1) = 0
      cont1(2) = 0
      cont2(1) = 0
      cont2(2) = 0
      cont9(1) = 0
      cont9(2) = 0

      iguais   = 0
      diffmin  = 0
      bothinf  = 0
      orifeas  = 0 
      otrfeas  = 0

      do i = 1,nprob

C         READ LINES

          read(10,*) probname1,f(1),cnorm(1),dd1,dd2,dd3,dd4,dd5,dd6,
     +    dd7,dd8,sca(1),scb(1),di1,di2,di3,innit(1),di4,di5,di6,di7,
     +    di8,cputim(1)

          read(20,*) probname2,f(2),cnorm(2),dd1,dd2,dd3,dd4,dd5,dd6,
     +    dd7,dd8,sca(2),scb(2),di1,di2,di3,innit(2),di4,di5,di6,di7,
     +    di8,cputim(2)

          if ( probname1 .ne. probname2 ) then
               write(*,*) 'Error in the input file ',probname1,probname2
               stop
          end if

          if ( sca(1) .ne. 0 ) then
              write(*,*) 'Method 1 aborted problem ',probname1
          else
              if ( scb(1) .eq. 0 ) cont0(1) = cont0(1) + 1
              if ( scb(1) .eq. 1 ) cont1(1) = cont1(1) + 1
              if ( scb(1) .eq. 2 ) cont2(1) = cont2(1) + 1
              if ( scb(1) .eq. 9 ) cont9(1) = cont9(1) + 1
          end if

          if ( sca(2) .ne. 0 ) then
              write(*,*) 'Method 2 aborted problem ',probname2
          else
              if ( scb(2) .eq. 0 ) cont0(2) = cont0(2) + 1
              if ( scb(2) .eq. 1 ) cont1(2) = cont1(2) + 1
              if ( scb(2) .eq. 2 ) cont2(2) = cont2(2) + 1
              if ( scb(2) .eq. 9 ) cont9(2) = cont9(2) + 1
          end if

          if ( max( cnorm(1), cnorm(2) ) .le. epsfeas ) then

              fmin = min( f(1), f(2) )

              if ((f(1).le.-1.0d+20.and.f(2).le.-1.0d+20) .or. 
     +            (f(1).le.fmin+max(1.0d-10,1.0d-06*abs(fmin)).and.
     +             f(2).le.fmin+max(1.0d-10,1.0d-06*abs(fmin)))) then

                  iguais = iguais + 1

                  if ( scb(1) .eq. scb(2) .and.
     +                 max( innit(1),innit(2) ) .le. 2.0d0 *
     +                 min( innit(1),innit(2) ) ) then
C                     write(*,200) 'Same solution, similar performance',
C    +                probname1,f(1),cnorm(1),scb(1),f(2),cnorm(2),
C    +                scb(2)
                  else
                      write(*,300) 'Same solution, diff performance',
     +                probname1,f(1),cnorm(1),scb(1),innit(1),cputim(1),
     +                f(2),cnorm(2),scb(2),innit(2),cputim(2)
                  end if

              else
                  diffmin = diffmin + 1
                  if ( f(1) .lt. f(2) ) then
                      write(*,200) 'algencan better min',probname1,
     +                f(1),cnorm(1),scb(1),f(2),cnorm(2),scb(2)
                  else
                      write(*,200) 'algencan-OTR better min',probname1,
     +                f(1),cnorm(1),scb(1),f(2),cnorm(2),scb(2)
                  end if
              end if

          else if ( min( cnorm(1), cnorm(2) ) .gt. epsfeas ) then
              bothinf = bothinf + 1
              write(*,200) 'Both infeasibles',probname1,
     +        f(1),cnorm(1),scb(1),f(2),cnorm(2),scb(2)

          else if ( cnorm(1) .le. epsfeas .and.
     +              cnorm(2) .gt. epsfeas ) then
              orifeas = orifeas + 1
              write(*,200) 'Only algencan feas',probname1,
     +        f(1),cnorm(1),scb(1),f(2),cnorm(2),scb(2)

          else
              otrfeas = otrfeas + 1
              write(*,200) 'Only algencan-OTR feas',probname1,
     +        f(1),cnorm(1),scb(1),f(2),cnorm(2),scb(2)
          end if

C         COMPUTE FMIN

          fmin = 1.0d+99
          do j = 1,nmeth
              if ( cnorm(j) .le. epsfeas ) then
                  fmin = min( fmin, f(j) )
              end if
          end do

C         SET THE OUTPUT TABLE

          do j = 1,nmeth
              if ( cnorm(j) .gt. epsfeas ) then
                  table(i,j) = 1.0d+99
              else
                  if ( f(j) .le. -1.0d+20 .or. f(j) .le. fmin + 
     +                 max( 1.0d-10, 1.0d-06 * abs( fmin ) ) ) then
                      table(i,j) = float( max( 1, innit(j) ) )
c                     table(i,j) = max( 0.01d0, cputim(j) )
                  else
                      table(i,j) = 1.0d+99
                  end if
              end if
          end do

      end do

      close(10)
      close(20)

C     COMPUTE THE PERFORMANCE RATIOS

      do i = 1,nprob

          mmin = 1.0d+99
          do j = 1,nmeth
              mmin = min ( mmin, table(i,j) )
          end do

          if ( mmin .lt. 1.0d+99 ) then
              do j = 1,nmeth
                  table(i,j) = table(i,j) / mmin
              end do
          end if

      end do

      
C     COMPUTE PERFORMANCE OF THE SOLVER

      open(70,file='perf-loc.txt')

      tau = 1.0d0
 10   if ( tau .le. 10001.0d0 ) then

          do j = 1,nmeth

              rho(j) = 0
              do i = 1,nprob
                  if ( table(i,j) .le. tau ) then              
                      rho(j) = rho(j) + 1
                  end if
              end do

          end do

          write(70,100) tau,(rho(j)/nprob,j=1,nmeth)

          if ( abs( tau -     1.0d0 ) .le. 5.0d-02 .or. 
     +         abs( tau -     1.1d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.2d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.3d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.4d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.5d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.6d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.7d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.8d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     1.9d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     2.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     3.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     4.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -     5.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    10.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    15.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    20.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    30.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    40.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -    50.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -   100.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -   500.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -  1000.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau -  1500.0d0 ) .le. 5.0d-02 .or.
     +         abs( tau - 10000.0d0 ) .le. 5.0d-02 ) then
              write(*,100) tau,(rho(j)/nprob,j=1,nmeth)
          end if

          tau = tau + 0.1d0
          go to 10
      end if

      close(70)

      write(*,*) 'Conts M1 : ',cont0(1),cont1(1),cont2(1),cont9(1)
      write(*,*) 'Conts M2 : ',cont0(2),cont1(2),cont2(2),cont9(2)
      write(*,*) 'iguais   : ',iguais 
      write(*,*) 'diffmin  : ',diffmin
      write(*,*) 'bothinf  : ',bothinf
      write(*,*) 'orifeas  : ',orifeas
      write(*,*) 'otrfeas  : ',otrfeas

 100  format(F8.2,6(1X,F10.6))
 200  format(1X,A25,1X,A8,2(1X,1P,D24.16,1X,D7.1,1X,I1))
 300  format(1X,A25,1X,A8,
     +       2(1X,1P,D24.16,1X,D7.1,1X,I1,1X,I16,1X,0P,F8.2))

      end
