PROGRAM simann_rmo

USE simulated_anneal_sp
USE simann_general
USE sep

IMPLICIT NONE

INTEGER, PARAMETER      :: neps = 4
REAL ,ALLOCATABLE       :: lb(:), ub(:), x(:), xopt(:), c(:), vm(:), m(:)
REAL                    :: fopt
INTEGER                 :: nfcnev, ier, i, nacc, nobds
integer                 :: ixs,ntotn1
real, allocatable       :: scan(:,:,:,:)
!
 call sep_init()
 call sep_begin_prog()
 open(unit=2,file="Rho_simann.out")
 call parameters()
 allocate(lb(ntot)); allocate(ub(ntot)); allocate(x(ntot)); allocate(xopt(ntot)); allocate(c(ntot)); allocate(vm(ntot))
 call sreed("rho0",x,4*ntot)
 if (cbound) then
    lb = lb1; ub = ub1
 else
    lb=x*.95
    ub=x*1.05
    where(lb <= os4) lb=os4
    where(ub >= (ns4-1)*ds4+os4) ub=(ns4-1)*ds4+os4
 end if

; vm = svm1; c = sla1;
WRITE(2, '(/, "  ****   Inputs to SA   ****   ")')
WRITE(2,1000) ntot, max, t, rt, eps, ns, nt, neps,maxevl, iprint, iseed1, iseed2

 call sa_init()
 call sa(ntot, x, max, rt, eps, ns, nt, neps, maxevl, lb, ub, c, iprint, iseed1,  &
        iseed2, t, vm, xopt, fopt, nacc, nfcnev, nobds, ier)
 call kernel(xopt)
 call sa_close()
 call to_history("n1",n1,"optrho");call to_history("d1",d1,"optrho");call to_history("o1",o1,"optrho");
 call to_history("n2",n2,"optrho");call to_history("d2",d2,"optrho");call to_history("o2",o2,"optrho");
 call srite("optrho",xopt,4*ntot)

WRITE(2, '(/, "  ****   RESULTS AFTER SA   ****   ")')
WRITE(2,1001) fopt, nfcnev, nacc, nobds, t, ier

1000 FORMAT(/,' SIMULATED ANNEALING EXAMPLE',/,/,  &
             ' NUMBER OF PARAMETERS: ',i3,'   MAXIMIZATION: ',l5, /, &
             ' INITIAL TEMP: ', g8.2, '   RT: ',g8.2, '   EPS: ',g8.2, /, &
             ' NS: ',i3, '   NT: ',i2, '   NEPS: ',i2, /,  &
             ' MAXEVL: ',i10, '   IPRINT: ',i1, '   ISEED1: ',i4,  &
             '   ISEED2: ',i4)
1001 FORMAT(/,' OPTIMAL FUNCTION VALUE: ',g20.13  &
            /,' NUMBER OF FUNCTION EVALUATIONS:     ',i10,  &
            /,' NUMBER OF ACCEPTED EVALUATIONS:     ',i10,  &
            /,' NUMBER OF OUT OF BOUND EVALUATIONS: ',i10,  &
            /,' FINAL TEMP: ', g20.13,'  IER: ', i3)

 close(2)
 call sep_end_prog()
STOP
END PROGRAM simann_rmo
