Question
Hey, FORTRAN 95! I got some issues with my codes, and need some helps to fit in the description. please take a look at my
Hey, FORTRAN 95!
I got some issues with my codes, and need some helps to fit in the description.
please take a look at my red zones, thank you!
******Below, my codes, please make it fit into the output example***********
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! PROGRAM P4 ! Traveling Salesman Problem in FORTRAN 95 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PROGRAM P4 IMPLICIT NONE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Variable Declarations
INTEGER :: number, status, numCities, I, J, K, first, last, globalMin,permutation = 0 CHARACTER(len = 20) :: filename CHARACTER(len = 20), DIMENSION (:), ALLOCATABLE :: cityName INTEGER, DIMENSION (:,:), ALLOCATABLE :: cityDistance INTEGER, DIMENSION (:), ALLOCATABLE :: path, bestPath
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !Open the file and read number of cities
WRITE (*, '(1x,A)', ADVANCE="NO") "Enter filename: " READ *, filename OPEN(UNIT=9, FILE=filename, STATUS="OLD", ACTION="READ",& IOSTAT=status) IF(status /= 0) THEN PRINT *, "ERROR to open the file" stop END IF
! Read number of cities READ (UNIT = 9, FMT = 100) numCities
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !Allocate memory for all needed arrays
allocate(cityName(numCities)) allocate(cityDistance(numCities,numCities)) allocate(path(numCities)) allocate(bestPath(numCities))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !Fill in arrays from data file
DO I = 1, numCities READ (9,'(A)') cityName (I) path(I) = I DO J = 1, numCities READ(9, FMT = 100) cityDistance(i,j) END DO END DO
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Use recursion to find minimal distance
! Initializing values globalMin = 9001 bestPath = 0
CALL permute(2,numCities)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !Print formatted output
PRINT * PRINT *, "Number of cities:", numCities
DO I = 1, numCities IF (I .eq. numCities) THEN PRINT *, trim(cityName(bestPath(I))), " to ", trim(cityName(1)), " -- ", cityDistance(I, 1), " miles" ELSE PRINT *, trim(cityName(bestPath(I))), " to ", trim(cityName(bestPath(I+1))), " --", cityDistance(I, I + 1), " miles" END IF END DO
PRINT * PRINT *, "Best distance is: ", globalMin, " miles" PRINT * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Janitorial duties close (9) deallocate(cityName) deallocate(cityDistance) deallocate(path) deallocate(bestPath)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !Format labels 100 FORMAT (I6) 200 FORMAT (A)
CONTAINS
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! This subroutine permutes through the possible paths the ! salesman can take and finds the minimum distance for that ! permutation. Then compares against the globalMin, and if less ! than the globalMin, updates the value, and saves that path ! first The bottom value for permutation ! last The top value for permutation RECURSIVE SUBROUTINE permute(first, last) INTEGER :: first, last, I, J, K, currentMin IF (first .eq.last) THEN currentMin = cityDistance(1, path(2)) DO I = 2, (last - 1) currentMin = currentMin + cityDistance(path(I), path(I + 1)) END DO currentMin = currentMin + cityDistance(path(last), 1) IF (currentMin .lt. globalMin) THEN globalMin = currentMin DO I=1, numCities bestPath(I) = path(I) END DO END IF ELSE DO I = first, last CALL swap(first, I) CALL permute(first + 1, last) CALL swap(first, I) END DO END IF END SUBROUTINE permute !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! This subroutine swaps the values of the path for permutation ! first The bottom value for permutation ! I The iterated value for permutation SUBROUTINE swap (first, I) INTEGER :: holder, first, I holder = path(first) path(first) = path(I) path(I) = holder END SUBROUTINE swap
END PROGRAM P4
The Programs For this manigrument, you will write a program in PORTRAN 95 to solve the Minimum Tour problem aka the Traveling Salesma Problem). The problem may be described as follows: A salesman must leave his home city and visit N cities after which he then retans home. The arcer in which he visits the cilice is important, Iwwer le most visit each of the cities, and must stari odchudin los may Your task is to find the minimum tuur, txebatest possible out from home base through N cities and back home pain You project will sist of a single source od bilo, p. 195. You will find the shortest recte using the brune force method. You must saun the distance traveled for each possible route. retaining the shortest distance found as your answer. Tlus, your program will leave to gecerate all possible routes. This is a NP-complete problem with complexity On!). The algorithm ostale all puble rules i els aluyles. A legnage to nic thermo, vanilien in presence and lot the prema donc tot writenin FORTRAV OS. You may militair togetherculele mine your warm nic. Input You program will first port the ser ta enite a filene This file came is a stafile that comes to the following peeifications. The first line of the file is the somber of cities, including hemele. Following this count, dalanisiats of stwa dimensional cost going the diamee (in miles) hetween cach pair of cities. The bome base city is always the first city listed in the file. ----------- THAT IFTA Bon Vio G 110 0 601 DATA 90 what's inside input data 0 309 Dela B vf vitin SAS DA PESANAN 560 ... 220V BLANCA LA 2359 Pecenix das 389 dan Diego Distance is 23 E Vio 900 De: Phoenix 2.la ning 110 | Distance is $200 2264 TANCAT De-A | | DATA A1243 ITAA l: Arix & Viny 350 ITAA 110 - Bom Dic 560 350 DATARIA 1100 lla Phoenix | can tinyu . mi |henix Die is 3164 560 PAT NA TAANINA Poenix - - 304 zile 399 silu STAP te Stage 900 miles what the output should be looks like. Duat diatasce :: Grading: You program will be graded on correctness, efficiency and good programming practices. Your source code file will be compiled with the fos compiler on ecom is follows: Programme. This fills come will never let it. Your program will cell with a Glex al carballc logo Grading rubric will inchade Overall good practices and documentation Input ficname from keyboard Input data from ile Output intermediate data Output ties: path data The Programs For this manigrument, you will write a program in PORTRAN 95 to solve the Minimum Tour problem aka the Traveling Salesma Problem). The problem may be described as follows: A salesman must leave his home city and visit N cities after which he then retans home. The arcer in which he visits the cilice is important, Iwwer le most visit each of the cities, and must stari odchudin los may Your task is to find the minimum tuur, txebatest possible out from home base through N cities and back home pain You project will sist of a single source od bilo, p. 195. You will find the shortest recte using the brune force method. You must saun the distance traveled for each possible route. retaining the shortest distance found as your answer. Tlus, your program will leave to gecerate all possible routes. This is a NP-complete problem with complexity On!). The algorithm ostale all puble rules i els aluyles. A legnage to nic thermo, vanilien in presence and lot the prema donc tot writenin FORTRAV OS. You may militair togetherculele mine your warm nic. Input You program will first port the ser ta enite a filene This file came is a stafile that comes to the following peeifications. The first line of the file is the somber of cities, including hemele. Following this count, dalanisiats of stwa dimensional cost going the diamee (in miles) hetween cach pair of cities. The bome base city is always the first city listed in the file. ----------- THAT IFTA Bon Vio G 110 0 601 DATA 90 what's inside input data 0 309 Dela B vf vitin SAS DA PESANAN 560 ... 220V BLANCA LA 2359 Pecenix das 389 dan Diego Distance is 23 E Vio 900 De: Phoenix 2.la ning 110 | Distance is $200 2264 TANCAT De-A | | DATA A1243 ITAA l: Arix & Viny 350 ITAA 110 - Bom Dic 560 350 DATARIA 1100 lla Phoenix | can tinyu . mi |henix Die is 3164 560 PAT NA TAANINA Poenix - - 304 zile 399 silu STAP te Stage 900 miles what the output should be looks like. Duat diatasce :: Grading: You program will be graded on correctness, efficiency and good programming practices. Your source code file will be compiled with the fos compiler on ecom is follows: Programme. This fills come will never let it. Your program will cell with a Glex al carballc logo Grading rubric will inchade Overall good practices and documentation Input ficname from keyboard Input data from ile Output intermediate data Output ties: path data
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started