MODULE initializat  ! Initialization of parameters and x for LDGBM, LMBM, and D-Bundle

  USE r_precision, ONLY : prec  ! Precision for reals.
  IMPLICIT NONE

! Parameters 
  INTEGER, PARAMETER :: &
    n      =    1000, &     ! Number of the variables.
    na     =    2, &        ! Size of the bundle na >= 2.
    mcu    =    3, &        ! Upper limit for maximum number of stored corrections, 
                            ! mcu >= 3 for LMBM and LDGB, mcu >=1 for D-bundle. 
    mcinit =    7           ! Initial maximum number of stored corrections, mcu >= mcinit >= 3.
                            ! If mcinit <= 0, the default value mcinit = 3 will be used. 
                            ! However, the value mcinit = 7 is recommented (no need for D-bundle).


  REAL, PARAMETER :: &
    time = 7200.0           ! Maximum CPU-time in seconds. If time <= 0.0 the maximum time 
                            ! is ignored. REAL argument.

! Real parameters (if parameter value <= 0.0 the default value of the parameter will be used).
  REAL(KIND=prec), SAVE :: &
    tolf  = 1.0E-8_prec, &  ! Tolerance for change of function values (default = 1.0E-8).
    tolf2 =    0.0_prec, &  ! Second tolerance for change of function values.
                            !   - If tolf2 < 0 the the parameter and the corresponding termination 
                            !   criterion will be ignored. 
                            !   - If tolf2 = 0 the default value 1.0E+4 will be used. 
    tolb  =    0.0_prec, &  ! Tolerance for the function value (default = -large).
    tolg  = 1.0E-5_prec, &  ! Tolerance for the first termination criterion (default = 1.0E-6).
    tolg2 = 1.0E-5_prec, &  ! Tolerance for the second termination criterion (default = tolg,
                            ! not needed with D-bundle).
    told  =    0.50_prec, &  ! Tolerance for the diagonal elements (default = 1.0,
                            ! needed only with D-bundle).
    eta   =    0.0_prec, &  ! Distance measure parameter, eta >= 0. 
                            !   - If eta < 0  the default value 0.5 will be used. 
    epsl  =    0.0_prec, &  ! Line search parameter, 0 < epsl < 0.25 (default = 1.0E-4).
    xmax  =    1.50_prec    ! Maximum stepsize, 1 < XMAX (default = 1.5, however, with D-bundle 
                            ! a larger value, e.g. xmax = 1000, is recommend for convex problems).


! Integer parameters (if value <= 0 the default value of the parameter will be used).
  INTEGER, SAVE :: &
    mit    = 500000, &      ! Maximun number of iterations (default = 10000).
    mfe    = 50000000, &    ! Maximun number of function evaluations (default = n*mit).
    mtesf  =      0, &      ! Maximum number of iterations with changes of
                            ! function values smaller than tolf (default = 10).
    iprint =      1, &      ! Printout specification:
                            !    -1  - No printout.
                            !     0  - Only the error messages.
                            !     1  - The final values of the objective function 
                            !          (default used if iprint < -1).
                            !     2  - The final values of the objective function and the 
                            !          most serious warning messages.
                            !     3  - The whole final solution. 
                            !     4  - At each iteration values of the objective function.
                            !     5  - At each iteration the whole solution
    method =      3, &      ! Selection of the method:
                            !     0  - Limited memory bundle method (default).
                            !     1  - L-BFGS bundle method.
                            !     2  - Limited memory discrete gradient bundle method.
                            !     3  - Diagonal bundle method (no need really).
    iscale =      4         ! Selection of the scaling / diagonal matrix updating:
                            !     0  - Scaling at every iteration with STU/UTU 
                            !          (default with LMBM and LDGB).
                            !     1  - Scaling at every iteration with STS/STU.
                            !     2  - Interval scaling with STU/UTU.
                            !     3  - Interval scaling with STS/STU.
                            !     4  - Preliminary scaling with STU/UTU with LMBM and LDGM,
                            !          Herskovits's diagonal update with D-bundle (default 
                            !          with D-bundle).
                            !     5  - Preliminary scaling with STS/STU with LMBM and LDGM,
                            !          Complement of Herskovits's diagonal update with D-bundle.
                            !     6  - No scaling with STU/UTU with LMBM and LDGM, diagonal 
                            !          matrix is I with D-bundle.      


  REAL(KIND=prec), DIMENSION(n), SAVE :: x  ! Vector of variables (initialize x in subroutine init_x)


CONTAINS

  SUBROUTINE init_x()  ! User supplied subroutine for initialization of vector x(n).

    USE problem_data, ONLY : next      ! Number of the problem (needed with large-scale test  
                                       ! problems in testps).
    USE large_problems, ONLY : startx  ! Initialization of x for large-scale nonsmooth test problems.
    IMPLICIT NONE

    CALL startx(n,x)
    IF (next == -1) STOP 'Error in the problem number next.'
  
  END SUBROUTINE init_x


  SUBROUTINE init_par()  ! User supplied subroutine for further initialization of parameters (when needed).
                         ! This subroutine may be usefull if you want to run some test problems in loop.
                         ! May be left empty.
 
    USE problem_data, ONLY : next  ! Number of the problem (needed with large scale test problems in tnsunc).
    IMPLICIT NONE

    IF (next <= 5) THEN
       eta = 0.10_prec
       xmax = 1000.0_prec
    ELSE
       eta = 1.0_prec
       xmax = 1.50_prec
    END IF

  END SUBROUTINE init_par

END MODULE initializat
