program main c*********************************************************************72 c cc tsp_dantzig42_test() tests tsp_dantzig42(). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 26 July 2026 c c Author: c c John Burkardt c implicit none double precision cost integer, allocatable :: dantzig42_order(:) double precision, allocatable :: dantzig42_xy(:,:) integer i integer i2 integer, parameter :: n = 42 double precision, allocatable :: x2(:,:) call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'tsp_dantzig42_test():' write ( *, '(a)' ) ' Fortran77 version' write ( *, '(a)' ) ' Test tsp_dantzig42(), which returns the' write ( *, '(a)' ) ' Dantzig 42 city traveling salesperson ' write ( *, '(a)' ) ' problem challenge data.' allocate ( dantzig42_order(1:n) ) call tsp_dantzig42_order ( dantzig42_order ) allocate ( dantzig42_xy(1:n,1:2) ) call tsp_dantzig42_xy ( dantzig42_xy ) write ( *, '(a)' ) '' write ( *, '(a)' ) ' City coordinates:' write ( *, '(a)' ) '' do i = 1, n write ( *, '(2x,i2,2x,f8.1,2x,f8.1)' ) & i, dantzig42_xy(i,1), dantzig42_xy(i,2) end do c c Compute and report the cost. c call tsp_tour_cost ( n, dantzig42_xy, dantzig42_order, cost ) write ( *, '(a)' ) '' write ( *, '(a,g14.6)' ) ' Length of round trip = ', cost c c Copy the city locations in TSP order. c allocate ( x2(n+1,2) ) do i2 = 1, n + 1 if ( i2 <= n ) then i = dantzig42_order(i2) else i = dantzig42_order(1) end if x2(i2,1) = dantzig42_xy(i,1) x2(i2,2) = dantzig42_xy(i,2) end do c c Display the locations. c call tsp_display ( "dantzig42", n + 1, x2 ) c c Free memory. c deallocate ( dantzig42_order ) deallocate ( dantzig42_xy ) deallocate ( x2 ) c c Terminate. c write ( *, '(a)' ) '' write ( *, '(a)' ) 'tsp_dantzig42_test():' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) stop end subroutine tsp_display ( prefix, n, x2 ) c*********************************************************************72 c cc tsp_display() plots cities and a tour connecting them. c c Discussion: c c If a "round trip" is desired, then the x2 array must repeat the c first city at the end. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 12 June 2026 c c Author: c c John Burkardt c c Input: c c character ( len = * ) prefix: a string that defines the name of c the problem. It is also used to create file names needed for c input to gnuplot. c c integer n: the number of cities. c c double precision x(n,2): the x and y coordinates of the cities. c implicit none integer n character ( len = 80 ) command_filename integer command_unit character ( len = 80 ) data_filename integer data_unit integer i character ( len = * ) prefix double precision x2(n,2) c c Create the data file. c call get_unit ( data_unit ) data_filename = prefix // '_data.txt' open ( unit = data_unit, file = data_filename, & status = 'replace' ) do i = 1, n write ( data_unit, '(2x,g14.6,2x,g14.6)' ) x2(i,1), x2(i,2) end do close ( unit = data_unit ) write ( *, '(a)' ) ' Created data file "' & // trim ( data_filename ) // '".' c c Create command file. c call get_unit ( command_unit ) command_filename = prefix // '_commands.txt' open ( unit = command_unit, file = command_filename, & status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' & // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set output "' // prefix // '.png"' write ( command_unit, '(a)' ) 'set xlabel "<-- X -->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y -->"' write ( command_unit, '(a)' ) 'set title "' // prefix // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'unset key' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'plot "' & // trim ( data_filename ) // & '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' "' & // trim ( data_filename ) // & '" using 1:2 with points pt 7 ps 3 linecolor rgb "green"' write ( command_unit, '(a)' ) 'quit' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' return end subroutine tsp_tour_cost ( n, x, order, cost ) c*********************************************************************72 c cc tsp_tour_cost() evaluates the cost of a round trip. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 26 July 2026 c c Author: c c John Burkardt c c Input: c c integer n: the number of cities. c c double precision x(n,2): the city locations. c c integer order(n): a permutation of 1:N, the route. c c Output: c c double precision cost: the cost of the route. c implicit none integer n double precision cost integer from integer order(n) integer to double precision x(n,2) cost = 0.0D+00 from = n do to = 1, n cost = cost + sqrt ( ( x(order(from),1) - x(order(to),1) )**2 & + ( x(order(from),2) - x(order(to),2) )**2 ) from = to end do return end subroutine get_unit ( iunit ) c*********************************************************************72 c cc get_unit() returns a free Fortran unit number. c c Discussion: c c A "free" Fortran unit number is a value between 1 and 99 which c is not currently associated with an I/O device. A free Fortran unit c number is needed in order to open a file with the OPEN command. c c If IUNIT = 0, then no free Fortran unit could be found, although c all 99 units were checked (except for units 5, 6 and 9, which c are commonly reserved for console I/O). c c Otherwise, IUNIT is a value between 1 and 99, representing a c free Fortran unit. Note that GET_UNIT assumes that units 5 and 6 c are special, and will never return those values. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 26 October 2008 c c Author: c c John Burkardt c c Output: c c integer IUNIT, the free unit number. c implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) 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 timestamp ( ) c*********************************************************************72 c cc timestamp() prints the YMDHMS date as a timestamp. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 12 June 2014 c c Author: c c John Burkardt c implicit none character * ( 8 ) ampm integer d character * ( 8 ) date integer h integer m integer mm character * ( 9 ) month(12) integer n integer s character * ( 10 ) time integer y save month data month / & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' / call date_and_time ( date, time ) read ( date, '(i4,i2,i2)' ) y, m, d read ( time, '(i2,i2,i2,1x,i3)' ) h, n, s, mm if ( h .lt. 12 ) then ampm = 'AM' else if ( h .eq. 12 ) then if ( n .eq. 0 .and. s .eq. 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h .lt. 12 ) then ampm = 'PM' else if ( h .eq. 12 ) then if ( n .eq. 0 .and. s .eq. 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, & '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, & trim ( ampm ) return end