perturb_variable Function

private function perturb_variable(me, ivar, x, mode, lower, upper) result(r)

Perturb a single variable using its assigned distribution and parameters.

Type Bound

simulated_annealing_type

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
integer, intent(in) :: ivar

variable index

real(kind=wp), intent(in) :: x

variable value to perturb

integer, intent(in) :: mode

perturbation distribution mode (see distribution_mode for details)

real(kind=wp), intent(in) :: lower

lower bound

real(kind=wp), intent(in) :: upper

upper bound

Return Value real(kind=wp)

perturbed value


Calls

proc~~perturb_variable~~CallsGraph proc~perturb_variable simulated_annealing_type%perturb_variable proc~bipareto bipareto proc~perturb_variable->proc~bipareto proc~cauchy cauchy proc~perturb_variable->proc~cauchy proc~triangular_dist triangular_dist proc~perturb_variable->proc~triangular_dist proc~truncated_normal truncated_normal proc~perturb_variable->proc~truncated_normal proc~uniform uniform proc~perturb_variable->proc~uniform proc~bipareto->proc~uniform proc~uniform_random_number uniform_random_number proc~bipareto->proc~uniform_random_number proc~cauchy->proc~uniform_random_number proc~triangular_dist->proc~uniform_random_number proc~truncated_normal->proc~uniform proc~normal normal proc~truncated_normal->proc~normal proc~uniform->proc~uniform_random_number proc~normal->proc~uniform_random_number

Called by

proc~~perturb_variable~~CalledByGraph proc~perturb_variable simulated_annealing_type%perturb_variable proc~perturb_and_evaluate simulated_annealing_type%perturb_and_evaluate proc~perturb_and_evaluate->proc~perturb_variable proc~sa simulated_annealing_type%sa proc~sa->proc~perturb_and_evaluate proc~solve_simulated_annealing solve_simulated_annealing proc~solve_simulated_annealing->proc~sa

Source Code

   function perturb_variable(me, ivar, x, mode, lower, upper) result(r)

      class(simulated_annealing_type),intent(inout) :: me
      integer,intent(in)  :: ivar   !! variable index
      real(wp),intent(in) :: x      !! variable value to perturb
      integer,intent(in)  :: mode   !! perturbation distribution mode (see `distribution_mode` for details)
      real(wp),intent(in) :: lower  !! lower bound
      real(wp),intent(in) :: upper  !! upper bound
      real(wp)            :: r      !! perturbed value

      integer :: i !! counter
      integer,parameter :: max_tries = 1000 !! max tries for rejection sampling

      ! select distribution based on the variable's distribution_mode:
      select case (mode)

       case(sa_mode_uniform)  ! uniform
         r = uniform(lower, upper)

       case(sa_mode_normal)  ! normal (truncated)
         ! center the distribution on the current value of the variable
         r = truncated_normal(x, me%dist_std_dev(ivar), lower, upper)

       case(sa_mode_cauchy)  ! cauchy
         ! center the distribution on the current value of the variable
         ! rejection sampling to ensure within bounds
         do i = 1, max_tries
            r = cauchy(x, me%dist_scale(ivar))
            if (r >= lower .and. r <= upper) return
         end do
         ! fallback to uniform if rejection sampling fails
         r = uniform(lower, upper)

       case(sa_mode_triangular)  ! triangular
         ! center the peak at the current value of the variable
         ! handle degenerate interval to avoid division by zero
         if (upper == lower) then
            ! interval has collapsed to a point: always return that value
            r = lower
         else
            ! compute normalized position of x in [lower, upper]
            r = lower + (upper - lower) * triangular_dist((x - lower) / (upper - lower))
         end if

       case(sa_mode_bipareto)  ! bipareto
         ! center the distribution on the current value of the variable
         r = bipareto(x, me%dist_scale(ivar), &
                      me%dist_shape(ivar), lower, upper)

       case default
         error stop 'Error: invalid distribution_mode in perturb_variable'
      end select

   end function perturb_variable