'\begin{verbatim} 'declarations DECLARE SUB display (n) DECLARE SUB AssignDistances (n!) DECLARE SUB GetPermutation (P!()) DECLARE FUNCTION PathLen! (P!()) '**** CLS 'RANDOMIZE TIMER 'uncomment this to run different simulation every time 'get number of cities (no choice which) LOCATE 2, 1 INPUT ; "Shortest distance between how many cities?", n CLS nMax = 19 ' current data size. Make sure not exceeded IF n > nMax THEN n = nMax 'declare arrays DIM SHARED dist(nMax, nMax) ' matrix of distances DIM SHARED city(nMax) AS STRING DIM P(n), BestP(n) 'read distances CALL AssignDistances(nMax) 'initial permutation FOR j = 1 TO n P(j) = j BestP(j) = j NEXT j MinLen = PathLen(P()) DO 'infinite loop no = no + 1 'check if key pressed k$ = INKEY$ IF k$ > "" THEN EXIT DO 'exit infinite loop CALL GetPermutation(P()) 'uncoment next line to see a different (better!) randomization 'CALL ImproveBest(P(), BestP()) display (no) x = PathLen(P()) IF x < MinLen THEN 'memorize Dlen = MinLen - x MinLen = x FOR j = 1 TO n BestP(j) = P(j) PRINT city(P(j)); "->"; NEXT j 'RANDOMIZE TIMER 'improve randomization somewhat 'display current outcome PRINT city(P(1)) PRINT "Best so far: "; MinLen PRINT "Progress rate "; Dlen / (no - Slen); " miles per trial" Slen = no END IF LOOP 'Print final message CLS PRINT "ALPHABETIC TOUR OF FIRST "; n; " CITIES in the USA" PRINT "Blind Search Recommended Route found in "; Slen; "-th search" FOR j = 1 TO n - 1 PRINT city(BestP(j)); "-->"; NEXT j PRINT city(BestP(n)); "-->"; city(BestP(1)) PRINT "Total distance: "; MinLen PRINT "Can you find a better one?" LOCATE 22, 40 PRINT "(Distances subject to change)" END cities: 'FIRST 19 cities in alphabetic order (Source: US map) DATA " Albany", "Albuquerque", "Atlanta", "Atlantic City", "Augusta" DATA "Austin", "Baltimore","Baton Rouge", "Billings", "Birmingham", "Bismarck", "Boise", "Boston" DATA "Buffalo", "Calgary", "Charlotte", "Cheyenne", "Chicago", "Cincinnati" distances: 'from city number RowNumber to RowNumber+k, k=0 to nMax DATA 0,2046,1067,315,336,1985,358,1585,2071,1167,1654,2615,174,285,2485,808,1789,823,724 DATA 0,1430,1986,2384,849,1949,1110,1006,1298,1423,1205,2222,1761,1712,1663,541,1289,1347 DATA 0,872,1287,1011,671,580,2019,158,1571,2451,1125,926,2425,258,1625,740,487 DATA 0,535,1790,163,1390,2085,968,1668,2629,373,455,2522,614,1803,837,664 DATA 0,2205,578,1805,2407,1383,1990,2951,162,621,2677,1029,2125,1159,1060 DATA 0,1627,436,1650,853,1558,2011,2043,1591,2356,1269,1204,1155,1152 DATA 0,1227,1948,805,1531,2492,416,459,2385,451,1666,700,527 DATA 0,1878,422,1589,2239,1643,1309,2443,838,1432,976,870 DATA 0,1861,417,732,2245,1786,706,2019,465,1248,1542 DATA 0, 1521,2293,1221,922,2375,416,1467,690,483 DATA 0,1149,1828,1369,927,1602,882,831,1125 DATA 0,2789,2330,875,2495,826,1792,2040 DATA 0,459,2515,867,1963,997,898 DATA 0,2200,693,1504,538,439 DATA 0,2456,1171,1685,1979 DATA 0,1669,771,490 DATA 0,966,1214 DATA 0,294 DATA 0 SUB AssignDistances (n) 'This assigns distances to d RESTORE cities FOR j = 1 TO n READ city(j) NEXT j RESTORE distances REDIM dist(n, n) FOR j = 1 TO n FOR i = j TO n READ x dist(i, j) = x dist(j, i) = x IF x = 0 AND i <> j THEN PRINT "Error in DATA before row "; j; "col"; i: STOP NEXT i NEXT j END SUB SUB display (tst) 'show information about selection y = CSRLIN x = POS(1) LOCATE 1, 45 PRINT " Hit any key to stop" LOCATE 2, 60 PRINT " " LOCATE 2, 60 PRINT "Trying path #"; tst LOCATE y, x END SUB SUB GetPermutation (P()) 'put random entries into P n = UBOUND(P) REDIM P(n) FOR j = 1 TO n x = INT(RND(1) * n) + 1 WHILE P(x) > 0 x = INT(RND(1) * n) + 1 WEND P(x) = j NEXT j END SUB SUB ImproveBest (P(), BP()) 'Modify bestPath BP() at some number of random locations n = UBOUND(P) 'choose random number of changes k = INT((RND(1) * n) + 1) 'copy BP() over P() FOR j = 1 TO n P(j) = BP(j) NEXT j 'make random changes FOR tr = 1 TO k i = INT(RND(1) * n + 1) j = INT(RND(1) * n + 1) SWAP P(i), P(j) NEXT tr END SUB FUNCTION PathLen (P()) 'Compute length of path P according to distances in Dist(i,j) n = UBOUND(P) S = dist(P(1), P(n)) FOR j = 1 TO n - 1 S = S + dist(P(j), P(j + 1)) NEXT j PathLen = S '\end{verbatim}% \ep END FUNCTION