MODULE initializat  ! Initialization of parameters and x for LDGBM and LMBM

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

! Parameters 
  INTEGER, PARAMETER :: &
    n      =   50, &        ! Number of the variables.
    na     =    2, &        ! Size of the bundle na >= 2.
    mcu    =    15, &       ! Upper limit for maximum number of stored corrections, mcu >= 3.
    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.

  REAL, PARAMETER :: &
    time =  1800.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-5_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).
    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  =    0.0_prec     ! Maximum stepsize, 1 < XMAX (default = 1.5).


! 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 =      2, &      ! Selection of the method:
                            !     0  - Limited memory bundle method (default).
                            !     1  - L-BFGS bundle method.
                            !     2  - Limited memory discrete gradient bundle method.
    iscale =      0         ! Selection of the scaling:
                            !     0  - Scaling at every iteration with STU/UTU (default).
                            !     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.
                            !     5  - Preliminary scaling with STS/STU.
                            !     6  - No scaling.      


  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 tnsunc).
    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.0_prec
    ELSE
       eta = 0.50_prec
    END IF

  END SUBROUTINE init_par

END MODULE initializat
