      PROGRAM HPSERUN
      ALLOCATABLE::Q(:,:),na(:),xi(:),nxi(:),NB(:),C(:)
      ALLOCATABLE::NCSIZE(:),MEMBER(:,:),NCMEM(:)
      COMMON/PARAM/NOPT,N,k,mtc
      logical mtc
      CHARACTER*80 FNAME,GNAME,INFORM
      ires=1
5010  WRITE(*,*) ' MAIN MENU '
      WRITE(*,*) '    1 UNWEIGHTED GRADIENT WITHIN ROWS AND COLUMNS '
      write(*,*) '      number of strict consistencies minus '
      write(*,*) '      inconsistencies ABOVE the main diagonal'
      write(*,*) '    2 number of consistencies MINUS INCONSISTENCIES'
      write(*,*) '      WITHIN ROWS left and right of main diagonal'
      WRITE(*,*) '    3 DEFAYS CRITERION '
      WRITE(*,*) '      SUM OF SQUARES OF PROXIMITY SUMS TO MAIN '
      WRITE(*,*) '      DIAGONAL MINUS FROM MAIN DIAGONAL WITHIN ROWS'
      WRITE(*,*) '    4 PROXIMITY SUM '
      write(*,*) '      above main diagonal'
      WRITE(*,*) '    5 WEIGHTED GRADIENT WITHIN ROWS AND COLUMNS '
      write(*,*) '      weighted by positive or negative differences'
      write(*,*) '    6 greenberg pattern'
      write(*,*) '      same as 1 but reversed within the column'
      write(*,*) '      comparisons'
      WRITE(*,*) '    8 GREENBERG PATTERN'
      WRITE(*,*) '      SAME AS 5 BUT REVERSED WITH THE COLUMN'
      WRITE(*,*) '      COMPARISONS'
      WRITE(*,*) ' CHOICE? '
      READ(*,1) NOPT
      WRITE(*,552)
      IF((NOPT.EQ.1).OR.(NOPT.EQ.5)) WRITE(*,*) '1 FOR WITHIN ROW COMPAR
     &ISONS ONLY, 2 FOR WITHIN COLUMN COMPARISONS ONLY,
     &3 FOR BOTH COMPARISONS'
      IF((NOPT.EQ.1).OR.(NOPT.EQ.5)) READ(*,*) NCOMP
1     FORMAT(BN,I6)
      GO TO (5000,5000,5000,5000,5000,5000,5010,5000) NOPT
      GO TO 5010
5000  WRITE(*,*) ' NUMBER OF ROWS/COLUMNS IN THE INPUT MATRIX '
      READ(*,1) N
      WRITE(*,552)
      WRITE(*,*) 'NUMBER OF INITIAL OBJECT CLASSES TO BE SEQUENCED?'
      READ(*,*) NCLAS
      ALLOCATE (Q(N,N),NA(N),NB(N),XI(2**NCLAS),NXI(2**NCLAS),C(N),
     &MEMBER(N,N),NCMEM(N),NCSIZE(N),STAT=IERR)
      IF(IERR.NE.0) WRITE(*,*) 'ARRAY ALLOCATION ERROR'
      IF(IERR.NE.0) STOP
      IF(IERR.EQ.0) WRITE(*,*) 'ARRAY ALLOCATION SUCCESSFUL'
      WRITE(*,*) ' FILE NAME FOR THE INPUT MATRIX '
      READ(*,'(A)') FNAME
      WRITE(*,552)
      WRITE(*,*) ' FILE NAME FOR THE OUTPUT '
      READ(*,'(A)') GNAME
      WRITE(*,552)
      WRITE(*,550)
      WRITE(*,*) FNAME
      WRITE(*,*) GNAME
550   FORMAT(' INPUT AND OUTPUT FILES ARE ')
505   WRITE(*,*) ' IS THE INPUT MATRIX SYMMETRIC AND READ AS A LOWER'
      WRITE(*,*) ' TRIANGULAR MATRIX WITHOUT DIAGONAL ENTRIES OR AS'
      WRITE(*,*) ' A COMPLETE SQUARE MATRIX '
      WRITE(*,*) '    1 LOWER TRIANGULAR '
      WRITE(*,*) '    2 COMPLETE SQUARE '
      WRITE(*,*) '    3 COMPLETE SQUARE AND SKEW-SYMMETRIC'
      WRITE(*,*) '    4 COMPLETE SQUARE AND SKEW-SYMMETRIC'
      WRITE(*,*) '      BUT USE SIGN INFORMATION ONLY'
      WRITE(*,*) ' CHOICE? '
      READ(*,1) NSYM
      WRITE(*,552)
      GO TO (504,504,504,504) NSYM
      GO TO 505
504   WRITE(*,*) ' PROVIDE FORMAT FOR THE INPUT MATRIX '
      READ(*,'(A)') INFORM
      WRITE(*,552)
      WRITE(*,551)
      WRITE(*,*) INFORM
551   FORMAT(' INPUT FORMAT IS ')
      OPEN(1,FILE=FNAME)
      OPEN(2,FILE=GNAME,STATUS='NEW')
      WRITE(2,7799) NOPT
7799  FORMAT(' OPTIMIZATION MENU OPTION ',I4)
      WRITE(2,552)
      IF(((NOPT.EQ.1).OR.(NOPT.EQ.5)).AND.(NCOMP.EQ.1)) WRITE(2,*)
     &'WITHIN ROW COMPARISONS ONLY'
      IF(((NOPT.EQ.1).OR.(NOPT.EQ.5)).AND.(NCOMP.EQ.2)) WRITE(2,*)
     &'WITHIN COLUMN COMPARISONS ONLY'
      IF(((NOPT.EQ.1).OR.(NOPT.EQ.5)).AND.(NCOMP.EQ.3)) WRITE(2,*)
     &'BOTH WITHIN ROW AND COLUMN COMPARISONS USED'
      WRITE(2,552)
      WRITE(2,*) 'INPUT MATRIX '
      IF(NSYM.GE.2) GO TO 1001
      DO 1002 I=2,N
      ISTOP=I-1
      READ(1,INFORM) (Q(I,J),J=1,ISTOP)
      WRITE(2,552)
      WRITE(2,6000) (Q(I,J),J=1,ISTOP)
1002  CONTINUE
      DO 1004 I=1,N
      Q(I,I)=0.0
      DO 1004 J=I+1,N
      Q(I,J)=Q(J,I)
1004  CONTINUE
      WRITE(2,552)
      GO TO 7779
1001  DO 1006 I=1,N
      READ(1,INFORM) (Q(I,J),J=1,N)
      WRITE(2,552)
      WRITE(2,6000) (Q(I,J),J=1,N)
      Q(I,I)=0.0
1006  CONTINUE
6000  FORMAT(2X,6F13.3)
      IF(NSYM.NE.4) GO TO 7779
      DO 9020 I=1,N
      DO 9020 J=1,N
      IF(Q(I,J).GT.0.0) Q(I,J)=1.0
      IF(Q(I,J).LT.0.0) Q(I,J)=-1.0
9020  CONTINUE
7779  WRITE(2,552)
      WRITE(*,*) 'OBJECT CLASSES TO BE SEQUENCED'
      WRITE(*,*) '1 THE SAME AS THE NUMBER OF OBJECTS'
      WRITE(*,*) '2 OBJECT CLASS MEMBERSHIP TO BE INPUT'
      READ(*,*) NOBCL
      IF(NOBCL.EQ.2) GO TO 4000
      DO 4001 I=1,N
      NCSIZE(I)=1
4001  MEMBER(I,1)=I
      GO TO 4002
4000  WRITE(*,*) 'INPUT CLASS MEMBERSHIP(SEPARATED BY BLANKS)'
      WRITE(*,*) 'WITH ZEROS INDICATING A DELETED OBJECT'
      READ(*,*) (NCMEM(I),I=1,N)
4003  FORMAT(40I2)
      DO 4004 I=1,N
4004  NCSIZE(I)=0
      DO 4005 I=1,N
      DO 4006 J=1,N
      IF(NCMEM(I).EQ.J) NCSIZE(J)=NCSIZE(J)+1
      IF(NCMEM(I).EQ.J) MEMBER(J,NCSIZE(J))=I
4006  CONTINUE
4005  CONTINUE
4002  WRITE(2,*) 'CLASS NUMBER / OBJECT MEMBERSHIP IN CLASS'
      DO 4009 I=1,NCLAS
4009  WRITE(2,4008) I,(MEMBER(I,J),J=1,NCSIZE(I))
4008  FORMAT(I3,10X,20I3)
6500  WRITE(*,*) ' PROGRAM RUNNING '
      WRITE(*,552)
      Z=-1.0e+20
      NZ=0
      NSUM=(2**NCLAS)-1
      DO 2 I=1,NSUM
      xi(i)=z
2     nxi(i)=nz
      DO 560 I=1,NCLAS
      INDEX=2**(I-1)
      xi(index)=0.0
560   nxi(index)=i
      IF((NOPT.EQ.3).OR.(NOPT.EQ.7)) GO TO 561
564   K=0
4     K=K+1
      WRITE(*,3012) K
3012  FORMAT(' PROCESSING SUBSETS OF SIZE ',I4)
      IF(K.EQ.NCLAS) GO TO 5
      MTC=.FALSE.
6     go to 30
30    IF(MTC) GO TO 40
      M2=0
      nH=K
      GO TO 50
40    IF(M2.LT.(NCLAS-nH)) nH=0
      nH=nH+1
      M2=NA(K+1-nH)
50    DO 51 J=1,nH
51    NA(K+J-nH)=M2+J
      MTC=NA(1).NE.(NCLAS-K+1)
      go to 6050
6050  INDEX=0
      DO 10 I=1,K
10    INDEX=INDEX+2**(NA(I)-1)
      JJ=1
      DO 7 I=1,NCLAS
      DO 8 J=1,K
      IF(NA(J).EQ.I) GO TO 7
8     CONTINUE
      NB(JJ)=I
      JJ=JJ+1
7     CONTINUE
      NK=NCLAS-K
      DO 9 JJ=1,NK
      J1=NB(JJ)
      SUM=0.0
      GO TO (13,19,500,1000,5005,9000,500,9000) NOPT
1000  DO 11 I=1,K
      DO 11 L1=1,NCSIZE(NA(I))
      DO 11 L2=1,NCSIZE(J1)
      SUM=SUM+Q(MEMBER(NA(I),L1),MEMBER(J1,L2))
11    CONTINUE
      GO TO 14
9000  DO 1500 I=1,K
      DO 1500 L1=1,NCSIZE(NA(I))
      I1=NA(I)
      DO 1600 J=1,NK
      DO 1600 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      DO 1600 L3=1,NCSIZE(J1)
      IF(J2.EQ.J1) GO TO 1600
      IF((NOPT.EQ.6).AND.(Q(MEMBER(I1,L1),MEMBER(J1,L3)).LT.  
     &Q(MEMBER(I1,L1),MEMBER(J2,L2)))) SUM=SUM+1.0
      if((NOPT.EQ.6).AND.(Q(MEMBER(I1,L1),MEMBER(J1,L3)).GT.
     &Q(MEMBER(I1,L1),MEMBER(J2,L2)))) SUM=SUM-1.0
      IF(NOPT.EQ.8) SUM=SUM+Q(MEMBER(I1,L1),MEMBER(J2,L2))-
     &Q(MEMBER(I1,L1),MEMBER(J1,L3))
1600  CONTINUE
1500  CONTINUE
      DO 1700 J=1,NK
      DO 1700 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      IF(J2.EQ.J1) GO TO 1700
      DO 1800 I=1,K
      DO 1800 L1=1,NCSIZE(NA(I))
      I1=NA(I)
      DO 1800 L3=1,NCSIZE(J1)
      IF((NOPT.EQ.6).AND.(Q(MEMBER(J1,L3),MEMBER(J2,L2)).GT.
     &Q(MEMBER(I1,L1),MEMBER(J2,L2)))) SUM=SUM+1.0
      IF((NOPT.EQ.6).AND.(Q(MEMBER(J1,L3),MEMBER(J2,L2)).LT.
     &Q(MEMBER(I1,L1),MEMBER(J2,L2)))) SUM=SUM-1.0
      IF(NOPT.EQ.8) SUM=SUM+Q(MEMBER(J1,L3),MEMBER(J2,L2))-
     &Q(MEMBER(I1,L1),MEMBER(J2,L2))
1800    CONTINUE
1700    CONTINUE
      GO TO 14
13    IF(NCOMP.EQ.2) GO TO 8000
      DO 15 I=1,K
      DO 15 L1=1,NCSIZE(NA(I))
      I1=NA(I)
      DO 16 J=1,NK
      DO 16 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      DO 16 L3=1,NCSIZE(J1)
      IF(J2.EQ.J1) GO TO 16
      IF(Q(MEMBER(I1,L1),MEMBER(J1,L3)).LT.  
     &Q(MEMBER(I1,L1),MEMBER(J2,L2))) SUM=SUM+1.0
      IF(Q(MEMBER(I1,L1),MEMBER(J1,L3)).GT.  
     &Q(MEMBER(I1,L1),MEMBER(J2,L2))) SUM=SUM-1.0
16    CONTINUE
15    CONTINUE
8000  IF(NCOMP.EQ.1) GO TO 14
      DO 17 J=1,NK
      DO 17 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      IF(J2.EQ.J1) GO TO 17
      DO 18 I=1,K
      DO 18 L1=1,NCSIZE(NA(I))
      I1=NA(I)
      DO 18 L3=1,NCSIZE(J1)
      IF(Q(MEMBER(J1,L3),MEMBER(J2,L2)).LT.  
     &Q(MEMBER(I1,L1),MEMBER(J2,L2))) SUM=SUM+1.0
      IF(Q(MEMBER(J1,L3),MEMBER(J2,L2)).GT.  
     &Q(MEMBER(I1,L1),MEMBER(J2,L2))) SUM=SUM-1.0
18    CONTINUE
17    CONTINUE
      GO TO 14
5005  IF(NCOMP.EQ.2) GO TO 8001
      DO 715 I=1,K
      DO 715 L1=1,NCSIZE(NA(I))
      I1=NA(I)
      DO 716 J=1,NK
      DO 716 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      DO 716 L3=1,NCSIZE(J1)
      IF(J2.EQ.J1) GO TO 716
      SUM=SUM+Q(MEMBER(I1,L1),MEMBER(J2,L2)) -
     &Q(MEMBER(I1,L1),MEMBER(J1,L3))
716    CONTINUE
715    CONTINUE
8001  IF(NCOMP.EQ.1) GO TO 14
      DO 717 J=1,NK
      DO 717 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      IF(J2.EQ.J1) GO TO 717
      DO 718 I=1,K
      DO 718 L1=1,NCSIZE(NA(I))
      DO 718 L3=1,NCSIZE(J1)
      I1=NA(I)
      SUM=SUM+Q(MEMBER(I1,L1),MEMBER(J2,L2)) -
     &Q(MEMBER(J1,L3),MEMBER(J2,L2))
718    CONTINUE
717    CONTINUE
      GO TO 14
19    DO 21 J=1,NK
      DO 21 L2=1,NCSIZE(NB(J))
      J2=NB(J)
      IF(J2.EQ.J1) GO TO 21
      DO 22 I=1,K
      DO 22 L1=1,NCSIZE(NA(I))
      DO 22 L3=1,NCSIZE(J1)
      I1=NA(I)
      IF(Q(MEMBER(J1,L3),MEMBER(I1,L1)).LT.
     &Q(MEMBER(J1,L3),MEMBER(J2,L2))) SUM=SUM+1.0
      IF(Q(MEMBER(J1,L3),MEMBER(I1,L1)).GT.  
     &Q(MEMBER(J1,L3),MEMBER(J2,L2))) SUM=SUM-1.0
22    CONTINUE
21    CONTINUE
      GO TO 14
500   DO 501 I=1,K
      DO 501 L1=1,NCSIZE(NA(I))
      DO 501 L3=1,NCSIZE(J1)
      I1=NA(I)
501   SUM=SUM+Q(MEMBER(J1,L3),MEMBER(I1,L1))
      DO 502 I=1,NK
      DO 502 L2=1,NCSIZE(NB(I))
      DO 502 L3=1,NCSIZE(J1)
      I1=NB(I)
      IF(I1.EQ.J1) GO TO 502
      SUM=SUM-Q(MEMBER(J1,L3),MEMBER(I1,L2))
502   CONTINUE
      IF(NOPT.EQ.3) SUM=(SUM*SUM)/FLOAT(NCSIZE(J1))
      GO TO 14
14    s=xi(index)
      TEMP=S+SUM
      INDEXT=INDEX+2**(J1-1)
      cp=xi(indext)
      IF(TEMP.LT.CP) GO TO 9
      xi(indext)=temp
      nxi(indext)=j1
9     CONTINUE
      IF(MTC) GO TO 6
      GO TO 4
5     smax=xi(nsum)
      nt=nxi(nsum)
      WRITE(2,552)
      WRITE(2,*) ' CUMULATIVE OBJECTIVE FUNCTION     PERMUTATION IN REVE
     &RSE ORDER '
      WRITE(2,1010) SMAX,NT
      NA(NCLAS)=NT
1010  FORMAT(9X,F13.3,13X,I12)
      NN=NCLAS-1
      INDEX=NSUM
      DO 12 I=1,NN
      INDEX=INDEX-(2**(NT-1))
      nt=nxi(index)
      s=xi(index)
      NA(NCLAS-I)=NT
12    WRITE(2,1010) S,NT
      GO TO (2000,8005,2002,2003,5006,8004,2002,8004) NOPT
561   DO 562 I=1,NCLAS
      RSUM=0.0
      DO 563 J=1,NCLAS
      DO 563 L2=1,NCSIZE(J)
      DO 563 L1=1,NCSIZE(I)
      IF(I.EQ.J) GO TO 563
      RSUM=RSUM+Q(MEMBER(I,L1),MEMBER(J,L2))
563   CONTINUE
      IF(NOPT.EQ.3) RSUM=(RSUM*RSUM)/FLOAT(NCSIZE(I))
3011  INDEX=2**(I-1)
      xi(index)=rsum
562   CONTINUE
      GO TO 564
5006  WRITE(2,552)
      WRITE(2,5007) SMAX
      XNCON=0.0
      XNDIF=0.0
      DO 8022 K=1,NCLAS
      DO 8022 L3=1,NCSIZE(NA(K))
      DO 8022 J=1,K-1
      DO 8022 L2=1,NCSIZE(NA(J))
      DO 8022 I=1,J-1
      DO 8022 L1=1,NCSIZE(NA(I))
      IF((NCOMP.NE.2).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).LT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2))
      IF((NCOMP.NE.2).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).GT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF +
     &Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))
      IF((NCOMP.NE.1).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).LT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)) -
     &Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3))
      IF((NCOMP.NE.1).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).GT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF +
     &Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))
8022  CONTINUE
      WRITE(2,552)
      SUM=XNCON+XNDIF
      DIF=XNCON-XNDIF
      WRITE(2,8003) XNCON,XNDIF,DIF,SUM,DIF/SUM
      GO TO 7777
5007  FORMAT(' INDEX OF GRADIENT COMPARISON IS', F9.3)
552   FORMAT(1H )
2000  CM=1.0
2005  SMAX=SMAX/CM
      WRITE(2,552)
      WRITE(2,2004) SMAX
      XNCON=0.0
      XNDIF=0.0
      DO 8002 K=1,NCLAS
      DO 8002 L3=1,NCSIZE(NA(K))
      DO 8002 J=1,K-1
      DO 8002 L2=1,NCSIZE(NA(J))
      DO 8002 I=1,J-1
      DO 8002 L1=1,NCSIZE(NA(I))
      IF((NCOMP.NE.2).AND.
     &(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).LT.  
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))))
     &XNCON=XNCON+1.0
      IF((NCOMP.NE.2).AND.
     &(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).GT.  
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))))
     &XNDIF=XNDIF+1.0
      IF((NCOMP.NE.1).AND.
     &(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).LT.  
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))))
     &XNCON=XNCON+1.0
      IF((NCOMP.NE.1).AND.
     &(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).GT.  
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))))
     &XNDIF=XNDIF+1.0
8002  CONTINUE
      WRITE(2,552)
      SUM=XNCON+XNDIF
      DIF=XNCON-XNDIF
      WRITE(2,8003) XNCON,XNDIF,DIF,SUM,DIF/SUM
8003  FORMAT('CONSISTENCIES ',F10.3,' INCONSISTENCIES ',F10.3,
     &' DIFFERENCE',F10.3,' SUM',F10.3,' RATIO',F10.3)
      GO TO 7777
8004  WRITE(2,552)
      WRITE(2,5007) SMAX
      XNCON=0.0
      XNDIF=0.0
      DO 8012 K=1,NCLAS
      DO 8012 L3=1,NCSIZE(NA(K))
      DO 8012 J=1,K-1
      DO 8012 L2=1,NCSIZE(NA(J))
      DO 8012 I=1,J-1
      DO 8012 L1=1,NCSIZE(NA(I))
      IF((NOPT.EQ.6).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).LT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+1
      IF((NOPT.EQ.6).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).GT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF+1
      IF((NOPT.EQ.6).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).GT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+1
      IF((NOPT.EQ.6).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).LT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF+1
      IF((NOPT.EQ.8).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).LT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+
     &ABS(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))
      IF((NOPT.EQ.8).AND.(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).GT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF+
     &ABS(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))
      IF((NOPT.EQ.8).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).GT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNCON=XNCON+
     &ABS(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))
      IF((NOPT.EQ.8).AND.(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)).LT.               
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))) XNDIF=XNDIF+
     &ABS(Q(MEMBER(NA(J),L2),MEMBER(NA(K),L3)) -
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3)))
8012  CONTINUE
      WRITE(2,552)
      SUM=XNCON+XNDIF
      DIF=XNCON-XNDIF
      WRITE(2,8003) XNCON,XNDIF,DIF,SUM,DIF/SUM
      GO TO 7777
8005  WRITE(2,552)
      WRITE(2,5007) SMAX
      XNCON=0.0
      XNDIF=0.0
      DO 8006 I=2,NCLAS-1
      DO 8006 L1=1,NCSIZE(NA(I))
      DO 8006 J=1,I-1
      DO 8006 L2=1,NCSIZE(NA(J))
      DO 8006 K=I+1,NCLAS
      DO 8006 L3=1,NCSIZE(NA(K))
      IF(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).LT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))) XNCON=XNCON+1.0
      IF(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2)).GT.
     &Q(MEMBER(NA(I),L1),MEMBER(NA(K),L3))) XNDIF=XNDIF+1.0
8006  CONTINUE
      WRITE(2,552)
      SUM=XNCON+XNDIF
      DIF=XNCON-XNDIF
      WRITE(2,8003) XNCON,XNDIF,SUM,DIF,DIF/SUM
2004  FORMAT(' THE NUMBER OF GRADIENT COMPARISONS SATISFIED MINUS THOSE
     &DISSATISFIED IS',F9.3)
      GO TO 7777
2003  SUM=0.0
      DO 2006 I=1,NCLAS
      DO 2006 L1=1,NCSIZE(NA(I))
      DO 2006 J=1,NCLAS
      DO 2006 L2=1,NCSIZE(NA(J))
      IF(I.EQ.J) GO TO 2006
      SUM=SUM+Q(MEMBER(I,L1),MEMBER(J,L2))
2006  CONTINUE
      TMAX=SUM-SMAX
      SMAX=SMAX
      WRITE(2,552)
      WRITE(2,2007) SMAX
      WRITE(2,2050) TMAX
      GO TO 7777
2007  FORMAT('  ABOVE DIAGONAL PROXIMITY SUM IS ',F13.3)
2050  FORMAT('  BELOW DIAGONAL PROXIMITY SUM IS ',F13.3)
2002  DO 2008 I=1,NCLAS
2008  IF(NOPT.EQ.3) C(NA(I))=0.0
      DO 2009 I=1,NCLAS
      DO 2009 J=1,NCLAS
      DO 2009 L1=1,NCSIZE(NA(I))
      DO 2009 L2=1,NCSIZE(NA(J))
      IF(I.EQ.J) GO TO 2009
      IF(I.GT.J) GO TO 2010
      C(NA(I))=C(NA(I))-Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2))
      GO TO 2009
2010  C(NA(I))=C(NA(I))+Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2))
2009  CONTINUE
      NSUM=0
      DO 4502 I=1,NCLAS
      NSUM=NSUM+NCSIZE(I)
4502  CONTINUE
      CN=NSUM
      DO 2011 I=1,NCLAS
2011  C(NA(I))=C(NA(I))/(CN*FLOAT(NCSIZE(NA(I))))
      WRITE(2,552)
      IF(NOPT.EQ.3) WRITE(2,*) ' COORDINATES BASED ON DEFAYS CRITERION'
      WRITE(2,2012) (C(NA(I)),I=1,NCLAS)
2012  FORMAT(20F10.3)
      WRITE(2,552)
      RESIDUAL=0.0
      DO 4500 I=1,NCLAS
      DO 4500 J=1,NCLAS
      DO 4500 L1=1,NCSIZE(NA(I))
      DO 4500 L2=1,NCSIZE(NA(J))
      IF(MEMBER(NA(I),L1).EQ.MEMBER(NA(J),L2)) GO TO 4500
      RESIDUAL=RESIDUAL +
     &(Q(MEMBER(NA(I),L1),MEMBER(NA(J),L2))-ABS(C(NA(I))-C(NA(J))))**2
4500  CONTINUE
      WRITE(2,4501) RESIDUAL
4501  FORMAT('RESIDUAL SUM OF SQUARES FOR THE SQUARE MATRIX',F15.4)
      GO TO 7777
7777  STOP
      END
