simulated_annealing_module_c Module

C interface wrapper for the simulated annealing optimization algorithm. This can be used to call the Fortran implementation from C or other languages that can interface with C (such as Python).


Uses

  • module~~simulated_annealing_module_c~~UsesGraph module~simulated_annealing_module_c simulated_annealing_module_c iso_c_binding iso_c_binding module~simulated_annealing_module_c->iso_c_binding module~simulated_annealing_module simulated_annealing_module module~simulated_annealing_module_c->module~simulated_annealing_module ieee_exceptions ieee_exceptions module~simulated_annealing_module->ieee_exceptions iso_fortran_env iso_fortran_env module~simulated_annealing_module->iso_fortran_env

Abstract Interfaces

abstract interface

  • public subroutine c_sa_func(iproblem, x, n, f, istat) bind(c)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=c_intptr_t), value :: iproblem
    real(kind=c_double), intent(in), dimension(n) :: x
    integer(kind=c_int), intent(in), value :: n
    real(kind=c_double), intent(out) :: f
    integer(kind=c_int), intent(out) :: istat

abstract interface

  • public subroutine c_sa_func_parallel_inputs(iproblem, n_inputs) bind(c)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=c_intptr_t), value :: iproblem
    integer(kind=c_int), intent(out) :: n_inputs

abstract interface

  • public subroutine c_sa_func_parallel_inputs_func(iproblem, x, n, n_inputs) bind(c)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=c_intptr_t), value :: iproblem
    real(kind=c_double), intent(in), dimension(n, n_inputs) :: x

    C's x[n_inputs][n] maps to Fortran's x(n,n_inputs) due to row/column-major difference

    integer(kind=c_int), intent(in), value :: n
    integer(kind=c_int), intent(in), value :: n_inputs

abstract interface

  • public subroutine c_sa_func_parallel_output_func(iproblem, x, n, f, istat) bind(c)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=c_intptr_t), value :: iproblem
    real(kind=c_double), intent(out), dimension(n) :: x
    integer(kind=c_int), intent(in), value :: n
    real(kind=c_double), intent(out) :: f
    integer(kind=c_int), intent(out) :: istat

abstract interface

  • public subroutine c_sa_report_func(iproblem, x, n, f, istat) bind(c)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=c_intptr_t), value :: iproblem
    real(kind=c_double), intent(in), dimension(n) :: x
    integer(kind=c_int), intent(in), value :: n
    real(kind=c_double), intent(in), value :: f
    integer(kind=c_int), intent(in), value :: istat

Derived Types

type, public, extends(simulated_annealing_type) ::  c_sa_wrapper_type

Wrapper type that encapsulates the C interface. extend the fortran type to hold the C function pointers.

Components

Type Visibility Attributes Name Initial
procedure(c_sa_func), public, pointer, nopass :: c_fcn_ptr => null()
procedure(c_sa_func_parallel_inputs), public, pointer, nopass :: c_n_inputs_ptr => null()
procedure(c_sa_func_parallel_inputs_func), public, pointer, nopass :: c_fcn_parallel_input_ptr => null()
procedure(c_sa_func_parallel_output_func), public, pointer, nopass :: c_fcn_parallel_output_ptr => null()
procedure(c_sa_report_func), public, pointer, nopass :: c_report_ptr => null()
integer(kind=c_intptr_t), public :: iproblem = 0

pointer to this wrapper (for callbacks)

Type-Bound Procedures

procedure, public :: initialize => initialize_sa
procedure, public :: optimize => sa
procedure, public :: destroy => destroy_sa
procedure, public :: perturb_variable

this is public so we can use it in the tests


Subroutines

public subroutine int_pointer_to_f_pointer(iproblem, p)

Convert an integer pointer to a c_sa_wrapper_type pointer.

Arguments

Type IntentOptional Attributes Name
integer(kind=c_intptr_t), intent(in) :: iproblem

integer pointer from C

type(c_sa_wrapper_type), pointer :: p

fortran pointer

public subroutine fcn_wrapper(me, x, f, istat)

Wrapper procedures for C callbacks (module-level, work with wrapper type)

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
real(kind=wp), intent(in), dimension(:) :: x
real(kind=wp), intent(out) :: f
integer, intent(out) :: istat

public subroutine n_inputs_wrapper(me, n_inputs)

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
integer, intent(out) :: n_inputs

public subroutine fcn_parallel_input_wrapper(me, x)

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
real(kind=wp), intent(in), dimension(:, :) :: x

public subroutine fcn_parallel_output_wrapper(me, x, f, istat)

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
real(kind=wp), intent(out), dimension(:) :: x
real(kind=wp), intent(out) :: f
integer, intent(out) :: istat

public subroutine report_wrapper(me, x, f, istat)

Arguments

Type IntentOptional Attributes Name
class(simulated_annealing_type), intent(inout) :: me
real(kind=wp), intent(in), dimension(:) :: x
real(kind=wp), intent(in) :: f
integer, intent(in) :: istat

public subroutine initialize_simulated_annealing(iproblem, n, lb, ub, c, maximize, eps, ns, nt, neps, maxevl, iprint, iseed1, iseed2, step_mode, vms, iunit, use_initial_guess, n_resets, cooling_schedule, cooling_param, optimal_f_specified, optimal_f, optimal_f_tol, distribution_mode, dist_std_dev, dist_scale, dist_shape, fcn, n_inputs_to_send, fcn_parallel_input, fcn_parallel_output, ireport, report) bind(C, name="initialize_simulated_annealing")

create a simulated_annealing_type from C

Arguments

Type IntentOptional Attributes Name
integer(kind=c_intptr_t), intent(out) :: iproblem
integer(kind=c_int), intent(in), value :: n
real(kind=c_double), intent(in), dimension(n) :: lb
real(kind=c_double), intent(in), dimension(n) :: ub
real(kind=c_double), intent(in), dimension(n) :: c
logical(kind=c_bool), intent(in), value :: maximize
real(kind=c_double), intent(in), value :: eps
integer(kind=c_int), intent(in), value :: ns
integer(kind=c_int), intent(in), value :: nt
integer(kind=c_int), intent(in), value :: neps
integer(kind=c_int), intent(in), value :: maxevl
integer(kind=c_int), intent(in), value :: iprint
integer(kind=c_int), intent(in), value :: iseed1
integer(kind=c_int), intent(in), value :: iseed2
integer(kind=c_int), intent(in), value :: step_mode
real(kind=c_double), intent(in), value :: vms
integer(kind=c_int), intent(in), value :: iunit
logical(kind=c_bool), intent(in), value :: use_initial_guess
integer(kind=c_int), intent(in), value :: n_resets
integer(kind=c_int), intent(in), value :: cooling_schedule
real(kind=c_double), intent(in), value :: cooling_param
logical(kind=c_bool), intent(in), value :: optimal_f_specified
real(kind=c_double), intent(in), value :: optimal_f
real(kind=c_double), intent(in), value :: optimal_f_tol
integer(kind=c_int), intent(in), dimension(n) :: distribution_mode
real(kind=c_double), intent(in), dimension(n) :: dist_std_dev
real(kind=c_double), intent(in), dimension(n) :: dist_scale
real(kind=c_double), intent(in), dimension(n) :: dist_shape
type(c_funptr), intent(in), value :: fcn

C function pointer (can be C_NULL_FUNPTR)

type(c_funptr), intent(in), value :: n_inputs_to_send

C function pointer (can be C_NULL_FUNPTR)

type(c_funptr), intent(in), value :: fcn_parallel_input

C function pointer (can be C_NULL_FUNPTR)

type(c_funptr), intent(in), value :: fcn_parallel_output

C function pointer (can be C_NULL_FUNPTR)

integer(kind=c_int), intent(in), value :: ireport

how often to report

type(c_funptr), intent(in), value :: report

C function pointer for reporting (can be C_NULL_FUNPTR)

public subroutine destroy_simulated_annealing(iproblem) bind(C, name="destroy_simulated_annealing")

destroy a simulated_annealing_type from C

Arguments

Type IntentOptional Attributes Name
integer(kind=c_intptr_t), intent(in), value :: iproblem

public subroutine solve_simulated_annealing(iproblem, n, x, rt, t, vm, xopt, fopt, nacc, nfcnev, ier) bind(C, name="solve_simulated_annealing")

solve optimization problem using simulated annealing from C

Arguments

Type IntentOptional Attributes Name
integer(kind=c_intptr_t), intent(in), value :: iproblem
integer(kind=c_int), intent(in), value :: n
real(kind=c_double), intent(inout), dimension(n) :: x
real(kind=c_double), intent(in), value :: rt
real(kind=c_double), intent(inout) :: t
real(kind=c_double), intent(inout), dimension(n) :: vm
real(kind=c_double), intent(out), dimension(n) :: xopt
real(kind=c_double), intent(out) :: fopt
integer(kind=c_int), intent(out) :: nacc
integer(kind=c_int), intent(out) :: nfcnev
integer(kind=c_int), intent(out) :: ier