      subroutine easyminos(n,l,u,ml,meq,alin2,acol2,aval2,annz2,b,
     +bdstype,ivar,obj,icase)

      implicit none

C     SCALAR ARGUMENTS
      character * 5 bdstype
      integer annz2,icase,ivar,meq,ml,n
      double precision obj

C     ARRAY ARGUMENTS
      integer alin2(annz2),acol2(annz2)
      double precision aval2(annz2),b(ml),l(n),u(n)

C     PARAMETERS
      integer mmax,nmax,annzmax,nwcore
      parameter ( mmax    =         1000 )
      parameter ( nmax    =         1000 )
      parameter ( annzmax =  nmax * mmax )
      parameter ( nwcore  =       500000 )

C     There is another nmax declaration within subroutine coo2csc below.

C     LOCAL SCALARS
      logical nullc
      integer annz,i,inform,ispecs,iobj,iprint,isumm,m,mincor,nb,ninf,
     +        nname,nncon,nnobj,nnjac,ns
      double precision objadd,sinf

C     LOCAL ARRAYS
      character * 8 names(5)
      integer alin(annzmax),acol(annzmax),asta(nmax),hs(nmax+mmax),
     +        name1(1),name2(1)
      double precision aval(annzmax),blow(mmax),bupp(mmax),c(nmax),
     +        bl(nmax+mmax),bu(nmax+mmax),pi(mmax),xn(nmax+mmax),
     +        rc(nmax+mmax),z(nwcore)

C     ===================================================
C     P R O B L E M   D E F I N I T I O N
C     ===================================================

      m = ml

C     Set objective function

      do i = 1,n
          c(i) = 0.0d0
      end do

      if ( bdstype .eq. 'LOWER' ) then
          c(ivar) =   1.0d0
      else
          c(ivar) = - 1.0d0
      end if

C     Define blow and bupp to translate from:
C
C     (ai)^t x  = bi, i = 1,meq
C     (ai)^t x >= bi, i =meq+1,m
C
C     to blow <= Ax <= bupp.
 
      do i = 1,meq
          blow(i) = b(i)
          bupp(i) = b(i)
      end do

      do i = meq + 1,ml
          blow(i) = b(i)
          bupp(i) = 1.0d+20
      end do

C     Copy sparse matrix

      annz = annz2
 
      do i = 1,annz2
          alin(i) = alin2(i)
          acol(i) = acol2(i)
          aval(i) = aval2(i)
      end do

C     ===================================================
C     E N D   O F   P R O B L E M   D E F I N I T I O N
C     ===================================================

C     Set problem related data

      names(1) = 'MyTest  '
      names(2) = 'funobj  '
      names(3) = 'rhs     '
      names(4) = 'ranges  '
      names(5) = 'bounds  '

      ispecs = 0
C     iprint = 9
C     isumm  = 6
      iprint = 0
      isumm  = 0

      call mispec(ispecs,iprint,isumm,nwcore,inform)
      call miopt ('Jacobian    Sparse',iprint,isumm,inform)

      nname  = 0
      nncon  = 0
      nnjac  = 0
      nnobj  = 0

      objadd = 0.0d0

C     Set linear objective as a free row

      nullc = .true.
      do i = 1,n
          if ( c(i) .ne. 0.0d0 ) then
              nullc = .false.
          end if
      end do

      if ( .not. nullc ) then
          m = m + 1

          iobj = m

          blow(m) = - 1.0d+20
          bupp(m) =   1.0d+20

          do i = 1,n
              if ( c(i) .ne. 0.0d0 ) then
                  annz = annz + 1

                  alin(annz) = m
                  acol(annz) = i
                  aval(annz) = c(i)
              end if
          end do

      else
          iobj = 0
      end if

      nb = n + m

C     Add slacks coefficients

      do i = 1,m
          annz = annz + 1

          alin(annz) = i
          acol(annz) = n + i
          aval(annz) = 1.0d0
      end do

C     Covert to compressed sparse column format

      call coo2csc(nb,annz,alin,acol,aval,asta)

C     Set bounds on x

      do i = 1,n
          bl(i) = l(i)
          bu(i) = u(i)
      end do

C     Set slacks bounds

      do i = 1,m
          bl(n+i) = - bupp(i)
          bu(n+i) = - blow(i)
      end do

C     Call MINOS

      call minoss('Cold',m,n,nb,annz,nname,nncon,nnobj,nnjac,iobj,
     +            objadd,names,aval,alin,asta,bl,bu,name1,name2,
     +            hs,xn,pi,rc,icase,mincor,ns,ninf,sinf,obj,z,nwcore)

C     Write final values

C     write(*,*) ' icase  = ', icase
C     write(*,*) ' ninf   = ', ninf
C     write(*,*) ' sinf   = ', sinf
C     write(*,*) ' obj    = ', obj

C     write(*,*) ' Solution: ',(xn(i),i=1,n)

      if ( bdstype .eq. 'UPPER' ) then
          obj = - obj
      end if

      end

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

      subroutine coo2csc(n,nnz,alin,acol,aval,asta)

      implicit none

C     SCALAR ARGUMENTS
      integer n,nnz

C     ARRAY ARGUMENTS
      integer acol(nnz),alin(nnz),asta(n)
      double precision aval(nnz)

C     This subroutines converts a matrix from coordinate format to
C     compressed sparse column format.

C     PARAMETERS
      integer nmax
      parameter ( nmax = 1000 )

C     LOCAL SCALARS
      integer i,j,col,coltmp,lin,lintmp
      double precision val,valtmp

C     LOCAL ARRAY
      integer alen(nmax)

      do i = 1,n
          alen(i) = 0
      end do

      do i = 1,nnz
          col = acol(i)
          alen(col) = alen(col) + 1
      end do

      asta(1) = 1
      do i = 2,n
          asta(i) = asta(i-1) + alen(i-1)
      end do

      do i = 1,nnz

          val = aval(i)
          col = acol(i)
          lin = alin(i)

          acol(i) = - 1

 10       if ( col .ge. 0 ) then

              j = asta(col)
              asta(col) = j + 1

              valtmp = aval(j)
              coltmp = acol(j)
              lintmp = alin(j)

              aval(j) = val
              alin(j) = lin
              acol(j) = - 1

              val = valtmp
              col = coltmp
              lin = lintmp

              go to 10

          end if

      end do

      do i = 1,n
          asta(i) = asta(i) - alen(i)
      end do

      asta(n+1) = nnz + 1

      end
