subroutine ch_cap ( ch ) !*****************************************************************************80 ! !! ch_cap() capitalizes a single character. ! ! Discussion: ! ! Instead of CHAR and ICHAR, we now use the ACHAR and IACHAR functions, ! which guarantee the ASCII collating sequence. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 19 July 1998 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character CH, the character to capitalize. ! ! Output: ! ! character CH, the capitalized character. ! implicit none character ch integer itemp itemp = iachar ( ch ) if ( 97 <= itemp .and. itemp <= 122 ) then ch = achar ( itemp - 32 ) end if return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free Fortran unit number. ! ! Discussion: ! ! A "free" Fortran unit number is an integer between 1 and 99 which ! is not currently associated with an I/O device. A free Fortran unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free Fortran unit could be found, although ! all 99 units were checked (except for units 5 and 6). ! ! Otherwise, IUNIT is an integer between 1 and 99, representing a ! free Fortran unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 March 1999 ! ! Author: ! ! John Burkardt ! ! Output: ! ! integer IUNIT, the unit number. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine euclidean_distance ( n, x, d ) !*****************************************************************************80 ! !! euclidean_distance() computes the Euclidean distance. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 July 2026 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of cities. ! ! integer X(n,2): the coordinates of each city. ! ! Output: ! ! integer D(N,N), the distance table. ! implicit none integer n integer d(n,n) integer i integer j real r integer x(n,2) do i = 1, n do j = 1, n r = real ( ( x(i,1) - x(j,1) )**2 + ( x(i,2) - x(j,2) )**2 ) d(i,j) = int ( sqrt ( r ) ) end do end do return end subroutine i4mat_print ( m, n, A, title ) !*****************************************************************************80 ! !! i4mat_print() prints an I4MAT. ! ! Discussion: ! ! An I4MAT is an M by N array of I4's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 30 June 2003 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer M, the number of rows in A. ! ! integer N, the number of columns in A. ! ! integer A(M,N), the matrix to be printed. ! ! character ( len = * ) TITLE, a title. ! implicit none integer m integer n integer A(m,n) integer ihi integer ilo integer jhi integer jlo character ( len = * ) title ilo = 1 ihi = m jlo = 1 jhi = n call i4mat_print_some ( m, n, A, ilo, jlo, ihi, jhi, title ) return end subroutine i4mat_print_some ( m, n, A, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! i4mat_print_some() prints some of an I4MAT. ! ! Discussion: ! ! An I4MAT is an M by N array of I4's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 September 2009 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer M, N, the number of rows and columns. ! ! integer A(M,N), an M by N matrix to be printed. ! ! integer ILO, JLO, the first row and column to print. ! ! integer IHI, JHI, the last row and column to print. ! ! character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: incx = 10 integer m integer n integer A(m,n) character ( len = 8 ) ctemp(incx) integer i integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2 integer j2hi integer j2lo integer jhi integer jlo character ( len = * ) title if ( 0 < len_trim ( title ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if if ( m <= 0 .or. n <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' (None)' return end if do j2lo = max ( jlo, 1 ), min ( jhi, n ), incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i8)' ) j end do write ( *, '('' Col '',10a8)' ) ctemp(1:inc) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ' i2lo = max ( ilo, 1 ) i2hi = min ( ihi, m ) do i = i2lo, i2hi do j2 = 1, inc j = j2lo - 1 + j2 write ( ctemp(j2), '(i8)' ) A(i,j) end do write ( *, '(i5,a,10a8)' ) i, ':', ( ctemp(j), j = 1, inc ) end do end do return end subroutine i4mat_write ( output_filename, m, n, table ) !*****************************************************************************80 ! !! i4mat_write() writes an I4MAT file. ! ! Discussion: ! ! An I4MAT is an array of I4's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 August 2009 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) OUTPUT_FILENAME, the output file name. ! ! integer M, the spatial dimension. ! ! integer N, the number of points. ! ! integer TABLE(M,N), the data. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer m integer n integer j character ( len = * ) output_filename integer output_status integer output_unit character ( len = 30 ) string integer table(m,n) ! ! Open the file. ! call get_unit ( output_unit ) open ( unit = output_unit, file = output_filename, & status = 'replace', iostat = output_status ) if ( output_status /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'I4MAT_WRITE(): Fatal error!' write ( *, '(a,i8)' ) ' Could not open the output file "' // & trim ( output_filename ) // '" on unit ', output_unit output_unit = -1 stop 1 end if ! ! Create a format string. ! if ( 0 < m .and. 0 < n ) then write ( string, '(a1,i8,a4)' ) '(', m, 'i10)' ! ! Write the data. ! do j = 1, n write ( output_unit, string ) table(1:m,j) end do end if ! ! Close the file. ! close ( unit = output_unit ) return end subroutine pseudo_euclidean_distance ( n, x, d ) !*****************************************************************************80 ! !! pseudo_euclidean_distance() computes the distance used for the ATT48 example. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 July 2026 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of cities. ! ! integer x(n,2), the coordinates of each city. ! ! Output: ! ! integer D(N,N), the distance table. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n integer d(n,n) integer i integer j real ( kind = rk8 ) rij integer tij integer x(n,2) do i = 1, n do j = 1, n rij = sqrt ( real ( ( x(i,1) - x(j,1) )**2 & + ( x(i,2) - x(j,2) )**2 ) ) tij = nint ( rij ) if ( tij < rij ) then d(i,j) = tij + 1 else d(i,j) = tij end if end do end do return end function s_eqi ( s1, s2 ) !*****************************************************************************80 ! !! s_eqi() is a case insensitive comparison of two strings for equality. ! ! Discussion: ! ! s_eqi ( 'Anjana', 'ANJANA' ) is TRUE. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 September 2021 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) S1, S2, the strings to compare. ! ! Output: ! ! logical S_EQI, the result of the comparison. ! implicit none character c1 character c2 integer i integer lenc logical s_eqi character ( len = * ) s1 integer s1_length character ( len = * ) s2 integer s2_length s1_length = len ( s1 ) s2_length = len ( s2 ) lenc = min ( s1_length, s2_length ) s_eqi = .false. do i = 1, lenc c1 = s1(i:i) c2 = s2(i:i) call ch_cap ( c1 ) call ch_cap ( c2 ) if ( c1 /= c2 ) then return end if end do do i = lenc + 1, s1_length if ( s1(i:i) /= ' ' ) then return end if end do do i = lenc + 1, s2_length if ( s2(i:i) /= ' ' ) then return end if end do s_eqi = .true. return end subroutine s_to_i4 ( s, value, ierror, length ) !*****************************************************************************80 ! !! s_to_i4() reads an I4 from a string. ! ! Discussion: ! ! Instead of ICHAR, we now use the IACHAR function, which ! guarantees the ASCII collating sequence. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 September 2021 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) S, a string to be examined. ! ! Output: ! ! integer VALUE, the value read from the string. ! If the string is blank, then VALUE will be returned 0. ! ! integer IERROR, an error flag. ! 0, no error. ! 1, an error occurred. ! ! integer LENGTH, the number of characters ! of S used to make the integer. ! implicit none character c integer i integer ierror integer isgn integer length character ( len = * ) s integer state character :: TAB = achar ( 9 ) integer value value = 0 ierror = 0 length = 0 state = 0 isgn = 1 do i = 1, len_trim ( s ) c = s(i:i) ! ! STATE = 0, haven't read anything. ! if ( state == 0 ) then if ( c == ' ' .or. c == TAB ) then else if ( c == '-' ) then state = 1 isgn = -1 else if ( c == '+' ) then state = 1 isgn = +1 else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then state = 2 value = iachar ( c ) - iachar ( '0' ) else ierror = 1 return end if ! ! STATE = 1, have read the sign, expecting digits or spaces. ! else if ( state == 1 ) then if ( c == ' ' .or. c == TAB ) then else if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then state = 2 value = iachar ( c ) - iachar ( '0' ) else ierror = 1 return end if ! ! STATE = 2, have read at least one digit, expecting more. ! else if ( state == 2 ) then if ( lle ( '0', c ) .and. lle ( c, '9' ) ) then value = 10 * value + iachar ( c ) - iachar ( '0' ) else value = isgn * value ierror = 0 length = i - 1 return end if end if end do ! ! If we read all the characters in the string, see if we're OK. ! if ( state == 2 ) then value = isgn * value ierror = 0 length = len_trim ( s ) else value = 0 ierror = 1 length = 0 end if return end subroutine s_word_extract_first ( s, w ) !*****************************************************************************80 ! !! s_word_extract_first() extracts the first word from a string. ! ! Discussion: ! ! A "word" is a string of characters terminated by a blank or ! the end of the string. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 January 2006 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) S, the string. ! ! Output: ! ! character ( len = * ) S: the transformed string. The first word ! has been removed, and the remaining string shifted left. ! ! character ( len = * ) W, the leading word of the string. ! implicit none integer get1 integer get2 character ( len = * ) s integer s_length character ( len = * ) w w = ' ' s_length = len_trim ( s ) if ( s_length < 1 ) then return end if ! ! Find the first nonblank. ! get1 = 0 do get1 = get1 + 1 if ( s_length < get1 ) then return end if if ( s(get1:get1) /= ' ' ) then exit end if end do ! ! Look for the last contiguous nonblank. ! get2 = get1 do if ( s_length <= get2 ) then exit end if if ( s(get2+1:get2+1) == ' ' ) then exit end if get2 = get2 + 1 end do ! ! Copy the word. ! w = s(get1:get2) ! ! Shift the string. ! s(1:get2) = ' ' s = adjustl ( s ) return end subroutine text_split ( text, keyword, colon, field ) !*****************************************************************************80 ! !! text_split() uses a colon to locate the keyword and field in a line of text. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 13 June 2026 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) TEXT, a line of text. ! ! Output: ! ! character ( len = * ) KEYWORD, the portion of TEXT that ! precedes the first colon. ! ! character ( len = 1 ) COLON, the colon, if it occurred. ! Otherwise, a blank. ! ! character ( len = * ) FIELD, the portion of TEXT that follows ! the first colon. ! implicit none character ( len = 1 ) colon integer colon_loc character ( len = * ) field character ( len = * ) keyword character ( len = * ) text integer text_end colon_loc = index ( text, ':' ) if ( colon_loc == 0 ) then keyword = text colon = ' ' field = ' ' return end if keyword = text(1:colon_loc-1) keyword = adjustl ( keyword ) colon = ':' text_end = len_trim ( text ) field = text(colon_loc+1:text_end) field = adjustl ( field ) return end subroutine tsp_edge_weight_read ( tsp_filename, tsp_dimension, & tsp_edge_weight_type, tsp_edge_weight_format, tsp_edge_weight ) !*****************************************************************************80 ! !! tsp_edge_weight_read() reads the edge weights of a TSP file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 October 2010 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) TSP_FILENAME, the TSP filename. ! ! integer TSP_DIMENSION, the number of cities. ! ! character ( len = * ) TSP_EDGE_WEIGHT_TYPE: ! 'EXPLICIT': the edge weights are given as an explicit list. ! ! character ( len = * ) TSP_EDGE_WEIGHT_FORMAT: ! 'FULL_MATRIX': the full distance matrix is listed. ! 'LOWER_DIAG_ROW': the diagonal and lower triangle are listed. ! ! Output: ! ! integer TSP_EDGE_WEIGHT(TSP_DIMENSION,TSP_DIMENSION), ! the edge weights. ! implicit none integer tsp_dimension logical done logical found_keyword integer i integer ierror integer j integer length integer line_num logical matrix_full logical s_eqi character ( len = 255 ) text integer tsp_edge_weight(tsp_dimension,tsp_dimension) character ( len = * ) tsp_edge_weight_format character ( len = * ) tsp_edge_weight_type character ( len = * ) tsp_filename integer, allocatable :: tsp_node_coord(:,:) integer tsp_unit integer value character ( len = 255 ) word call get_unit ( tsp_unit ) open ( file = tsp_filename, unit = tsp_unit, status = 'old' ) found_keyword = .false. line_num = 0 matrix_full = .false. if ( s_eqi ( tsp_edge_weight_type, 'EXPLICIT' ) ) then do ! ! Read a line of text. ! read ( tsp_unit, '(a)' ) text line_num = line_num + 1 if ( .not. found_keyword ) then if ( s_eqi ( text, 'EDGE_WEIGHT_SECTION' ) ) then found_keyword = .true. i = 1 j = 1 end if else ! ! Reading a line of weights. Read as many numbers as you can from this line. ! done = .true. do call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) if ( length <= 0 ) then exit end if if ( s_eqi ( tsp_edge_weight_format, 'FULL_MATRIX' ) ) then tsp_edge_weight(i,j) = value i = i + 1 if ( tsp_dimension < i ) then i = 1 j = j + 1 if ( tsp_dimension < j ) then matrix_full = .true. exit end if end if else if ( s_eqi ( tsp_edge_weight_format, 'LOWER_DIAG_ROW' ) ) then tsp_edge_weight(i,j) = value tsp_edge_weight(j,i) = value j = j + 1 if ( i < j ) then j = 1 i = i + 1 if ( tsp_dimension < i ) then matrix_full = .true. exit end if end if else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TSP_EDGE_WEIGHT_READ(): Fatal error!' write ( *, '(a)' ) ' Do not know how to handle format:' write ( *, '(a)' ) ' "' // trim ( tsp_edge_weight_format ) // '".' stop end if end do if ( matrix_full ) then exit end if end if end do ! ! ATT Edge Weight Specification. ! else if ( s_eqi ( tsp_edge_weight_type, 'ATT' ) ) then allocate ( tsp_node_coord(tsp_dimension,2) ) do ! ! Read a line of text. ! read ( tsp_unit, '(a)' ) text line_num = line_num + 1 if ( .not. found_keyword ) then if ( s_eqi ( text, 'NODE_COORD_SECTION' ) ) then found_keyword = .true. i = 1 j = 1 end if else ! ! Expect to read a line of (index, X, Y). ! done = .true. call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) tsp_node_coord(j,1) = value call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) tsp_node_coord(j,2) = value if ( tsp_dimension <= j ) then exit end if j = j + 1 end if end do call pseudo_euclidean_distance ( tsp_dimension, tsp_node_coord, & tsp_edge_weight ) deallocate ( tsp_node_coord ) else if ( s_eqi ( tsp_edge_weight_type, 'EUC_2D' ) ) then allocate ( tsp_node_coord(tsp_dimension,2) ) do ! ! Read a line of text. ! read ( tsp_unit, '(a)' ) text line_num = line_num + 1 if ( .not. found_keyword ) then if ( s_eqi ( text, 'NODE_COORD_SECTION' ) ) then found_keyword = .true. i = 1 j = 1 end if else ! ! Expect to read a line of (index, X, Y). ! done = .true. call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) tsp_node_coord(j,1) = value call s_word_extract_first ( text, word ) call s_to_i4 ( word, value, ierror, length ) tsp_node_coord(j,2) = value if ( tsp_dimension <= j ) then exit end if j = j + 1 end if end do call euclidean_distance ( tsp_dimension, tsp_node_coord, & tsp_edge_weight ) deallocate ( tsp_node_coord ) else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TSP_EDGE_WEIGHT_READ(): Fatal error!' write ( *, '(a)' ) ' Do not know how to handle given type:' write ( *, '(a)' ) ' "' // trim ( tsp_edge_weight_type ) // '".' stop end if close ( unit = tsp_unit ) return end subroutine tsp_header_read ( tsp_filename, tsp_name, tsp_type, tsp_comment, & tsp_dimension, tsp_capacity, tsp_edge_weight_type, tsp_edge_weight_format, & tsp_edge_data_format, tsp_node_coord_type, tsp_display_data_type ) !*****************************************************************************80 ! !! tsp_header_read() reads the header of a TSP file. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 October 2010 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) TSP_FILENAME, the TSP filename. ! implicit none character ( len = 1 ) colon character ( len = 255 ) field integer ierror character ( len = 255 ) keyword integer leng logical s_eqi character ( len = 255 ) text integer tsp_capacity character ( len = * ) tsp_comment integer tsp_dimension character ( len = * ) tsp_display_data_type character ( len = * ) tsp_edge_data_format character ( len = * ) tsp_edge_weight_format character ( len = * ) tsp_edge_weight_type character ( len = * ) tsp_filename integer tsp_header_lines character ( len = * ) tsp_name character ( len = * ) tsp_node_coord_type character ( len = * ) tsp_type integer tsp_unit logical, parameter :: verbose = .false. ! ! Initialize. ! tsp_capacity = 0 tsp_comment = ' ' tsp_dimension = 0 tsp_display_data_type = 'DEFAULT' tsp_edge_data_format = ' ' tsp_edge_weight_format = ' ' tsp_edge_weight_type = ' ' tsp_name = ' ' tsp_node_coord_type = 'DEFAULT' tsp_type = ' ' call get_unit ( tsp_unit ) open ( file = tsp_filename, unit = tsp_unit, status = 'old' ) tsp_header_lines = 0 do ! ! Read a line of text. ! read ( tsp_unit, '(a)' ) text ! ! Split text into "KEYWORD : FIELD" ! call text_split ( text, keyword, colon, field ) if ( colon /= ':' ) then exit end if tsp_header_lines = tsp_header_lines + 1 ! ! Store the field in the variable indicated by the keyword. ! if ( s_eqi ( keyword, 'NAME' ) ) then tsp_name = field else if ( s_eqi ( keyword, 'TYPE' ) ) then tsp_type = field else if ( s_eqi ( keyword, 'COMMENT' ) ) then tsp_comment = field else if ( s_eqi ( keyword, 'DIMENSION' ) ) then call s_to_i4 ( field, tsp_dimension, ierror, leng ) else if ( s_eqi ( keyword, 'CAPACITY' ) ) then call s_to_i4 ( field, tsp_capacity, ierror, leng ) else if ( s_eqi ( keyword, 'EDGE_WEIGHT_TYPE' ) ) then tsp_edge_weight_type = field else if ( s_eqi ( keyword, 'EDGE_WEIGHT_FORMAT' ) ) then tsp_edge_weight_format = field else if ( s_eqi ( keyword, 'EDGE_DATA_FORMAT' ) ) then tsp_edge_data_format = field else if ( s_eqi ( keyword, 'NODE_COORD_TYPE' ) ) then tsp_node_coord_type = field else if ( s_eqi ( keyword, 'DISPLAY_DATA_TYPE' ) ) then tsp_display_data_type = field else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TSP_HEADER_READ(): Fatal error!' write ( *, '(a)' ) ' Unexpected keyword "' // trim ( keyword ) // '".' stop end if end do close ( unit = tsp_unit ) ! ! In some cases, if a variable was not defined, it has a default value. ! if ( tsp_node_coord_type == 'DEFAULT' ) then tsp_node_coord_type = 'NO_COORDS' end if if ( tsp_display_data_type == 'DEFAULT' ) then if ( s_eqi ( tsp_node_coord_type, 'TWOD_COORDS' ) .or. & s_eqi ( tsp_node_coord_type, 'THREED_COORDS' ) ) then tsp_display_data_type = 'COORD_DISPLAY' else tsp_display_data_type = 'NO_DISPLAY' end if end if if ( verbose ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TSP_HEADER_READ():' write ( *, '(a)' ) ' Closed "' // trim ( tsp_filename ) // '".' write ( *, '(a,i8)' ) ' Number of header lines is ', tsp_header_lines end if return end