
      SUBROUTINE simplex (constary, objary, rhsary, numrow, numcol,
     +                    optsol, basisid, objfval)
      INCLUDE 'STDHDR.FOR'
      REAL constary(0:maxr, 0:maxc), objary(0:maxv), rhsary(0:maxv)
      REAL optsol(0:maxv),objfval(0:maxv),workary(0: maxr, 0:maxc)
      INTEGER basisid(0:maxc),numrow, numcol,maxcoefc,minratr
      INTEGER strow, stcol, i, j
      LOGICAL simpcrit1

      strow = numrow
      stcol = numcol + numrow + 2

      DO i = 0, strow
        DO j = 0, stcol - 1
          workary(i, j) = 0.0
        END DO
      END DO

      DO i = 1, numrow
        DO j = 1, numcol
          workary(i, j) = constary(i - 1, j - 1)
        END DO
      END DO

      DO j = 1, numcol
        workary(0, j) = -objary(j - 1)
      END DO

      workary(0, 0) = 1.0

      DO i = 1, numrow
        workary(i, i + numcol) = 1.0
        workary(i, stcol - 1) = rhsary(i - 1)
      END DO

      basisid(0) = 0
      DO i = 1, numrow
        basisid(i) = i + numcol
      END DO

      simpcrit1 = .FALSE.
      DO WHILE (simpcrit1 .EQV. .FALSE.)
        maxcoef = workary(0, 0)
        maxcoefc = 0.0
        minrat = 99999999.0
        minratr = 0
        DO j = 0, stcol - 2
          IF (workary(0,j) .LT. maxcoef .AND.
     +        workary(0,j) .LT. 0) THEN
            maxcoef = workary(0, j)
            maxcoefc = j
          END IF
        END DO
        DO i = 1, strow
          IF (workary(i,stcol-1).GT.0 .AND.
     +        workary(i,maxcoefc).GT.0) THEN
              IF (workary(i,stcol-1)/workary(i,maxcoefc)
     +           .LT. minrat) THEN
                 minrat = workary(i, stcol - 1) / workary(i, maxcoefc)
                 minratr = i
              END IF
           END IF
        END DO
        basisid(minratr) = maxcoefc

        coldiv = workary(minratr, maxcoefc)
        DO j = 0, stcol - 1
          workary(minratr, j) = workary(minratr, j) / coldiv
        END DO

        DO i = 0, strow
          IF (i .NE. minratr) THEN
            pivot = -workary(i, maxcoefc)
            DO j = 0, stcol - 1
              workary(i, j) = workary(minratr, j)
     +                         * pivot + workary(i, j)
            END DO
          END IF
        END DO
        simpcrit1 = .TRUE.
        DO j = 0, stcol - 2
          simpcrit1 = (workary(0, j) .GE. 0) .AND. simpcrit1
        END DO
      END DO
      DO j = 0, numrow
        optsol(j) = workary(j, stcol - 1)
      END DO
      DO j = 0, stcol - 2
        objfval(j) = workary(0, j)
      END DO


      END !SUB simplex

