      subroutine evalf (f,ns,ips,xs,n,x,sfx,nfe,lower,upper)
      use subplex_global
      use opt_global, only:exit_early,first_val,exit_early_factor 
      use global, only: var_vect,xpass
      implicit none
      INTEGER, PARAMETER :: iknd=SELECTED_INT_KIND(8)  ! nobody does this
      !INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(14)  ! this is double precision
      INTEGER, PARAMETER :: SP=SELECTED_REAL_KIND(6)  ! this is single precision
c
      integer ns,n,nfe
      integer ips(*)
      real(DP) xs(*),x(n),sfx
      !real(DP),allocatable :: xkeep(:,:)
      !real(DP) :: xkeep(10,8)
c
c                                         Coded by Tom Rowan
c                            Department of Computer Sciences
c                              University of Texas at Austin
c
c evalf evaluates the function f at a point defined by x
c with ns of its components replaced by those in xs.
c
c input
c
c   f      - user supplied function f(n,x) to be optimized
c
c   ns     - subspace dimension
c
c   ips    - permutation vector
c
c   xs     - double precision ns-vector to be mapped to x
c
c   n      - problem dimension
c
c   x      - double precision n-vector
c
c   nfe    - number of function evaluations
c
c output
c
c   sfx    - signed value of f evaluated at x
c
c   nfe    - incremented number of function evaluations
c
c common
c
      integer nsmin,nsmax,irepl,ifxsw,nfstop,nfxe
      real(DP) alpha,beta,gamma,delta,psi,omega,
     *     bonus,fstop,fxstat,ftest
      logical minf,initx,newx
c
      common /usubc/ alpha,beta,gamma,delta,psi,omega,nsmin,
     *               nsmax,irepl,ifxsw,bonus,fstop,nfstop,
     *               nfxe,fxstat(4),ftest,minf,initx,newx
c
      real(DP) fbonus,sfstop,sfbest
      logical new
c
      common /isubc/ fbonus,sfstop,sfbest,new
c
c local variables
c
      integer i,ii
      real(DP) fx
      logical newbst
c
      save
      real(DP) :: lower(n),upper(n)
c subroutines and functions
c
      external f,fstats
c
c-----------------------------------------------------------
c
      do 10 i = 1,ns
        x(ips(i)) = xs(i)
   10 continue
      newx = new .or. irepl .ne. 2
      do i = 1,n
        x(i) = max(x(i),lower(i))*abs(x(i) < lower(i)) + 
     1         min(x(i),upper(i))*abs(x(i) > upper(i)) + 
     1         x(i)*abs(x(i) >= lower(i))*abs(x(i) <= upper(i))
      enddo
      do ii = 2,n+1
        xpass(var_vect(ii)) = x(ii - 1)
      enddo
      call f(xpass,fx)
      if((exit_early == 0))then
        exit_early_factor = 1*fx
        first_val = 1
      endif
      do i = 1,n
        xkeep(nfe+1,i) = x(i)
      enddo
      xkeep(nfe+1,n+1) = fx
      xkeep(nfe+1,n+2) = first_cost
      xkeep(nfe+1,n+3) = total_fuel_cost
      !fx = f(n,x)
!      xkeep(nfe+1,1:n) = x
!      xkeep(nfe+1,n+1) = fx
      if (irepl .eq. 0) then
        if (minf) then
          sfx = fx
        else
          sfx = -fx
        end if
      else if (new) then
        if (minf) then
          sfx = fx
          newbst = fx .lt. ftest
        else
          sfx = -fx
          newbst = fx .gt. ftest
        end if
        if (initx .or. newbst) then
          if (irepl .eq. 1) call fstats (fx,1,.true.)
          ftest = fx
          sfbest = sfx
        end if
      else
        if (irepl .eq. 1) then
          call fstats (fx,1,.false.)
          fx = fxstat(ifxsw)
        end if
        ftest = fx+fbonus*fxstat(4)
        if (minf) then
          sfx = ftest
          sfbest = fx
        else
          sfx = -ftest
          sfbest = -fx
        end if
      end if
      if(fx >= 1e9)then
            nfe = nfe
      else
            nfe = nfe+1
      endif
      
      return
      end
