!*************************************************************************
!*                                                                       *
!*     D-Bundle - Diagonal bundle method for nonsmooth nonconvex         *
!*                optimization                                           *
!*                                                                       *
!*                                                                       *
!*     by Napsu Karmitsa 2014 (last modified 2016)                       *
!*                                                                       *
!*                                                                       *
!*     The software is free for academic teaching and research           *
!*     purposes but I ask you to refer the reference given below,        *
!*     if you use it.                                                    *
!*                                                                       *
!*************************************************************************
!*     
!*
!*     Codes included in D-bundle:
!* 
!*     tdbundle.f95         - mainprogram for D-Bundle (this file).
!*     initialization.f95   - user-specified initial values for parameters 
!*                            and starting point x.
!*     objfun.f95           - user-specified computation of the objective.
!*                            function value (and the subgradient).
!*     parameters.f95       - parameters.
!*     dbundle.f95          - D-Bundle method.
!*     subpro.f95           - subprograms.
!*     Makefile             - makefile.
!*
!*     testps.f95           - large-scale nonsmooth test problems 
!*                            (includes also partially separable problems).
!*
!*     To use the software modify tdbundle.f95, initialization.f95 and 
!*     objfun.f95 as needed.
!*
!*
!*     References:
!*
!*     N. Karmitsa: "Diagonal Bundle Method for Nonsmooth Sparse Optimization".
!*     Journal of Optimization Theory and Applications, Vol. 166, No. 3,
!*     pp. 889-905, 2015. DOI 10.1007/s10957-014-0666-8.
!*
!*     N. Karmitsa: "Diagonal Bundle Method for Nonsmooth Sparse Optimization".
!*     TUCS Technical Report No 1116, Turku Centre for Computer Science, Turku, 
!*     2014.
!*
!*
!*************************************************************************
!*
!*     * PROGRAM tdbundle *
!*
!*     Main program for D-Bundle subroutine for unconstrained 
!*     nonsmooth optimization.
!*
!*************************************************************************

PROGRAM tdbundle

  USE r_precision, ONLY : prec      ! Precision for reals.
  USE initializat, ONLY : init_x,init_par,n  ! Initialization of x and parameters.
  USE exe_time, ONLY : getime       ! Execution time.
  USE dbundle_mod, ONLY : init_db,dbundle  ! D-bundle.
  USE problem_data, ONLY : next     ! Number of the problem 
                                    ! (remove this when you use your own problem).
  IMPLICIT NONE

! Scalar Arguments
  REAL(KIND=prec) :: f              ! Value of the objective function.

! Array Arguments
  INTEGER, DIMENSION(4) :: iout     ! Output integer parameters.
                                    !   iout(1)   Number of used iterations.
                                    !   iout(2)   Number of used function evaluations.
                                    !   iout(3)   Number of used subgradient evaluations.
                                    !   iout(4)   Cause of termination:
                                    !               1  - The problem has been solved
                                    !                    with desired accuracy.
                                    !               2  - Changes in function values < tolf in mtesf
                                    !                    subsequent iterations.
                                    !               3  - Changes in function value < 
                                    !                    tolf*small*MAX(|f_k|,|f_(k-1)|,1),
                                    !                    where small is the smallest positive number
                                    !                    such that 1.0 + small > 1.0.
                                    !               4  - Number of function calls > mfe.
                                    !               5  - Number of iterations > mit.
                                    !               6  - Time limit exceeded. 
                                    !               7  - f < tolb.
                                    !              -1  - Two consecutive restarts.
                                    !              -2  - Number of restarts > maximum number
                                    !                    of restarts.
                                    !              -3  - Failure in function or subgradient
                                    !                    calculations (assigned by the user).
                                    !              -4  - Failure in attaining the demanded
                                    !                    accuracy.
                                    !              -5  - Invalid input parameters.
                                    !              -6  - Unspecified error.

! Local Scalars
  INTEGER :: j
  REAL :: start,fini 

      
  DO j=1,10  ! Loop for large-scale nonsmooth test problems
             ! (remove this if you use your own problem).

     next=j  ! Number of the test problem
             ! (remove this if you use your own problem).

     PRINT*,'next = ',next

! Initiation

     CALL init_x() 
     CALL init_par()


! CPU-time
    
     CALL getime(start)
     
! Solution

     CALL init_db(iout(4))  ! 
     IF (iout(4) /= 0) STOP 'Something wrong with the initial parameters.'

     CALL dbundle(f,iout(1),iout(2),iout(3),iout(4),start)  

! CPU-time

     CALL getime(fini)


! Result (additional printout, remove ONLY -statement from USE initializat)
      
!     PRINT*
!     PRINT*,'next    = ',next
!     PRINT*,'iterm   = ',iout(4)
!     PRINT*
!     PRINT*,'f(x)    = ',f
!     PRINT*,'n       = ',n
!     PRINT*,'na      = ',na
!     PRINT*,'nit     = ',iout(1)
!     PRINT*,'nfe     = ',iout(2)
!     PRINT*,'nge     = ',iout(3)
!     PRINT*,'xmax    = ',xmax
!     PRINT*,'gam     = ',eta
!     PRINT*,'epsl    = ',epsl
!     PRINT*,'eps     = ',tolg
!     PRINT*,'scaling = ',iscale

!     PRINT*,x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10)
      
!     PRINT* 
!     PRINT*,'Used time = ',fini - start
!     PRINT* 

     WRITE (6,FMT='(''D-bundle'',1X,''n='',I4,2X,''Ni='',I7,1X,''Nf='',I7,1X, &
          ''Ng='',I7,1X,''Nfg='',I7,1X,''f='',G15.8,2X,''Term='',I3,2X,''cpu='',F9.4,2X)') &
          ,n,iout(1),iout(2),iout(3),iout(2)+iout(3),f,iout(4),fini - start
     PRINT* 

  100  CONTINUE
  END DO

  STOP

END PROGRAM tdbundle
      
