module tspglobals implicit none save ! problem configuration integer :: n_cities integer, allocatable, dimension(:) :: city_order, city_order_temp real, allocatable, dimension(:) :: city_x, city_y ! running solution variables integer, allocatable, dimension(:) :: city_order_min real :: distance_min integer :: trials real :: time_begin, time_end ! run configuration integer :: n_trials integer :: out_samples, out_sample_trials, max_sampling, sampling_threshold integer :: trials_at_tem integer :: recalc_trials = 1000 integer :: sched_changes real :: tem_init, sched_quad_const integer :: n_parents, birth_rate logical :: show_chgs integer, parameter :: & & MONTE_CARLO = 2, & & ANNEALING = 3, & & EVOLUTIONARY = 4, & & PARALLEL_SA = 5 integer :: run_type integer, parameter :: & & INIT_NUMERICAL = 1, & & INIT_RANDOM = 2, & & INIT_GREEDY = 3 integer :: init_type integer, parameter :: & & PERTURB_SWAP = 1, & & PERTURB_REVSHIFT = 2 integer :: perturb_type integer, parameter :: & & SCHED_CONSTANT = 1, & & SCHED_LINEAR = 2, & & SCHED_QUADRATIC = 3, & & SCHED_STEP = 4 integer :: sched_type integer, parameter :: & & SELECT_RANK = 1, & & SELECT_BOLTZ_10 = 2 integer :: select_type = SELECT_BOLTZ_10 contains subroutine load_config CHARACTER(256) :: line CHARACTER(12) :: param ! name of parameter read from config file CHARACTER(32) :: data_file ! name of file to read TSP from INTEGER :: nheaderlines=7 ! number of header lines in TSP file INTEGER :: i, city_index ! read config file print *, 'Reading configuration from tsp.conf...' OPEN(unit=1, file='tsp.conf', action='read') ! open the config file do while (trim(param) /= 'end') read(1, '(a)') line param = trim(line(1:12)) select case(trim(param)) case('#') ! comment line case('n_cities'); read(line(13:),*) n_cities print *, 'Number of cities: ', n_cities case('data_file'); read(line(13:),*) data_file print *, 'TSP data file: ', data_file case('run_type'); read(line(13:),*) run_type print *, 'Run type: ', run_type case('n_trials'); read(line(13:),*) n_trials print *, 'Trials to run: ', n_trials case('out_samples'); read(line(13:),*) out_samples print *, 'Output sample points: ', out_samples case('max_sampling'); read(line(13:),*) max_sampling print *, 'Maximum sampling: ', max_sampling case('show_chgs'); read(line(13:),*) show_chgs print *, 'Output on improvement? ', show_chgs case('init_type'); read(line(13:),*) init_type print *, 'Initialisation type: ', init_type case('perturb_type'); read(line(13:),*) perturb_type print *, 'Perturbation type: ', perturb_type case('sched_type'); read(line(13:),*) sched_type print *, 'Schedule type: ', sched_type case('sched_chngs'); read(line(13:),*) sched_changes print *, 'Schedule changes: ', sched_changes case('tem_init'); read(line(13:),*) tem_init print *, 'Initial temperature: ', tem_init case('n_parents'); read(line(13:),*) n_parents print *, 'Parent population: ', n_parents case('birth_rate'); read(line(13:),*) birth_rate print *, 'Birth rate: ', birth_rate case('select_type'); read(line(13:),*) select_type print *, 'Selection type: ', select_type case('end') print *, '' case default print *, '*** Something unrecognised in config file' end select end do CLOSE(1) if (sched_type == SCHED_CONSTANT) then sched_changes = 1 end if ! precalculate some constants out_sample_trials = max(1, n_trials / out_samples) sampling_threshold = n_trials / max_sampling ! set constant number of trials between temperature changes trials_at_tem = max(1, n_trials / sched_changes) ! scaling factor for quadratic schedule sched_quad_const = tem_init / sched_changes**2 ! allocate arrays allocate(city_order_min(0:n_cities-1)) allocate(city_order(0:n_cities-1), city_order_temp(0:n_cities-1)) allocate(city_x(0:n_cities-1), city_y(0:n_cities-1)) ! read in city data OPEN(unit=1, file=data_file, action='read') ! open the data file DO i = 1, nheaderlines ! skip over header information READ(1, '(a)') line ! read a line of charcter data END DO DO i = 0, n_cities-1 ! read the city data READ(1, *) city_index, city_x(i), city_y(i) ! read 3 numbers END DO CLOSE(1) end subroutine load_config end module tspglobals