import numpy as np
distance=np.array([[ 0. , 2538.94348106, 2873.80462106, 2575.27338355,
2318.09943704, 2158.7079469 , 2216.58408367, 3174.03969729,
3371.13452713, 3540.23841005, 1737.00115141, 1375.39085354,
1695.96491709, 1250.76976299, 1617.24240607, 2492.96550317,
2617.38132491, 2757.61001594, 2478.01856329, 2386.86824102,
2775.30196555, 3023.05176932, 2163.06287472, 2203.68645683,
2116.0484399 , 2312.69237038, 1876.5247134 , 2214.39382225,
1241.81560628, 1561.04836568, 1255.35851453],
[2538.94348106, 0. , 1073.53854146, 111.28791489,
266.83515511, 395.0316443 , 410.11827562, 637.94200363,
853.55374758, 1055.00426539, 910.19173804, 1163.79121839,
869.00920594, 1308.7723255 , 2388.85935124, 370.87059738,
907.93006339, 1136.26977431, 908.01431707, 1263.54184735,
1572.14153307, 1732.29096863, 629.0858447 , 1060.24902735,
1350.41067828, 1896.57480738, 2049.69290383, 2290.02751075,
1510.82758778, 1739.09229197, 2089.48821485],
[2873.80462106, 1073.53854146, 0. , 964.49468635,
988.63643469, 1094.32399224, 1382.73135496, 1240.14555597,
1460.25374507, 1686.99644339, 1201.65552468, 1687.11854948,
1579.97784795, 1883.67725473, 3239.35580016, 730.6161783 ,
267.03183331, 171.26587518, 398.28758454, 602.127063 ,
612.16010978, 692.36189959, 820. , 681.19674104,
878.80031862, 1208.50858499, 1592.03643174, 1667.63455229,
1635.90739347, 1515.23100549, 1949.25883351],
[2575.27338355, 111.28791489, 964.49468635, 0. ,
262.05342967, 416.70733135, 503.56330287, 624.72473939,
854.91578533, 1068.41798937, 907.2298496 , 1204.1382811 ,
928.56879121, 1359.51829704, 2481.93795249, 279.01612857,
806.74407342, 1031.81490588, 815.83883212, 1179.54949027,
1473.50262979, 1628.07401552, 582.38303547, 989.46904954,
1284.00155763, 1822.56220744, 1998.25173589, 2225.75492811,
1509.8519795 , 1705.48673404, 2069.9613523 ],
[2318.09943704, 266.83515511, 988.63643469, 262.05342967,
0. , 163.35544068, 395.14048135, 885. ,
1110.85597626, 1318.19156423, 648.52602107, 952.00682771,
701.38149391, 1115.94354696, 2313.85738541, 268.28715959,
774.36167261, 1012.6964007 , 737.28759653, 1059.80800148,
1410.84726317, 1596.69690298, 377.63739222, 832.21691884,
1111.98021565, 1666.72043247, 1792.43800451, 2044.82982177,
1250.97282145, 1473.35705109, 1823.05348248],
[2158.7079469 , 395.0316443 , 1094.32399224, 416.70733135,
163.35544068, 0. , 338.63402074, 1030.3416909 ,
1248.57999343, 1447.68643014, 522.64423846, 789.7442624 ,
541.91235454, 952.58857856, 2171.94774339, 407.68247448,
859.41433546, 1096.73196361, 797.77941814, 1080.27033654,
1462.09883387, 1663.90925233, 366.7601396 , 830.95246555,
1089.12487806, 1648.87658726, 1728.79640212, 2002.6562361 ,
1118.7323183 , 1383.18617691, 1710.99298654],
[2216.58408367, 410.11827562, 1382.73135496, 503.56330287,
395.14048135, 338.63402074, 0. , 984.06757898,
1160.25729905, 1323.70125028, 776.17137283, 857.14934521,
520.7148932 , 966.57022507, 1979.39182579, 655.08014777,
1168.28934772, 1406.84398566, 1122.52082386, 1418.32471599,
1792.91996475, 1986.81378091, 705.3523942 , 1169.36093658,
1422.57934752, 1982.21719294, 2033.69860107, 2323.0680145 ,
1323.87046194, 1661.92930054, 1949.85640497],
[3174.03969729, 637.94200363, 1240.14555597, 624.72473939,
885. , 1030.3416909 , 984.06757898, 0. ,
243.4173371 , 473.76787565, 1531.95202275, 1798.7384468 ,
1489.84328035, 1935.40925905, 2880.62094001, 828.03200421,
1207.43902538, 1372.65472716, 1277.62279253, 1657.67186138,
1841.58763028, 1928.16441208, 1185.53996137, 1527.24916107,
1824.69860525, 2323.75945399, 2567.06388701, 2756.31130317,
2134.57489913, 2308.76763664, 2686.8414542 ],
[3371.13452713, 853.55374758, 1460.25374507, 854.91578533,
1110.85597626, 1248.57999343, 1160.25729905, 243.4173371 ,
0. , 232.11204191, 1759.38199377, 1998.91370499,
1677.49128165, 2124.56042512, 2981.51387721, 1070.02476607,
1443.79950132, 1599.81280155, 1518.25821256, 1897.74603148,
2067.46051957, 2141.56064588, 1424.64486803, 1770.58013092,
2067.92964097, 2564.18603069, 2810.44996397, 2998.53030667,
2361.73199157, 2549.20615094, 2923.28394105],
[3540.23841005, 1055.00426539, 1686.99644339, 1068.41798937,
1318.19156423, 1447.68643014, 1323.70125028, 473.76787565,
232.11204191, 0. , 1965.10584957, 2175.67736579,
1844.40911947, 2289.8168049 , 3056.55705002, 1295.33972378,
1675.6804588 , 1829.10497238, 1750.25712397, 2129.82722304,
2295.92530366, 2364.20176804, 1645.02066856, 2000.46244654,
2298.12815134, 2796.24212113, 3038.79926945, 3230.00247678,
2565.66755446, 2770.41513135, 3138.19709387],
[1737.00115141, 910.19173804, 1201.65552468, 907.2298496 ,
648.52602107, 522.64423846, 776.17137283, 1531.95202275,
1759.38199377, 1965.10584957, 0. , 493.78234071,
526.68966195, 691.60104106, 2105.0534435 , 765.85116047,
934.66678554, 1127.34910298, 809.995679 , 904.00497786,
1340.86091747, 1581.47304751, 426.53018651, 638.44263642,
776.33626735, 1304.60147171, 1272.03930757, 1585.58790359,
602.67155234, 886.10213858, 1189.87142163],
[1375.39085354, 1163.79121839, 1687.11854948, 1204.1382811 ,
952.00682771, 789.7442624 , 857.14934521, 1798.7384468 ,
1998.91370499, 2175.67736579, 493.78234071, 0. ,
348.28293096, 197.89138435, 1624.38326758, 1155.6353231 ,
1420.4453527 , 1619.87561251, 1300.56141723, 1384.44212591,
1822.85846955, 2067.37176144, 880.22326713, 1125.32039882,
1216.13856118, 1690.31180556, 1530.15848852, 1884.81298807,
601.24038454, 1091.58416991, 1234.02795754],
[1695.96491709, 869.00920594, 1579.97784795, 928.56879121,
701.38149391, 541.91235454, 520.7148932 , 1489.84328035,
1677.49128165, 1844.40911947, 526.68966195, 348.28293096,
0. , 447.07270102, 1659.38301787, 945.67330511,
1322.96787565, 1546.98739491, 1226.33804475, 1403.60713877,
1831.52668558, 2061.36483913, 764.70255655, 1133.28593038,
1301.66816048, 1829.72702882, 1755.16665875, 2088.87170501,
899.44705236, 1335.03745266, 1541.74576374],
[1250.76976299, 1308.7723255 , 1883.67725473, 1359.51829704,
1115.94354696, 952.58857856, 966.57022507, 1935.40925905,
2124.56042512, 2289.8168049 , 691.60104106, 197.89138435,
447.07270102, 0. , 1436.23187543, 1334.00149925,
1617.21303482, 1817.70074545, 1498.1645437 , 1578.1726775 ,
2016.46919143, 2262.06741721, 1073.37225602, 1321.11960094,
1400.44921365, 1856.06815608, 1659.22029882, 2021.86967928,
700.46912851, 1216.59730396, 1299.04657345],
[1617.24240607, 2388.85935124, 3239.35580016, 2481.93795249,
2313.85738541, 2171.94774339, 1979.39182579, 2880.62094001,
2981.51387721, 3056.55705002, 2105.0534435 , 1624.38326758,
1659.38301787, 1436.23187543, 0. , 2577.7854837 ,
2981.55194488, 3202.04091167, 2879.92933941, 3006.66343311,
3444.74353182, 3686.52370127, 2422.5560881 , 2743.39370124,
2836.64379153, 3274.06246122, 3007.76229114, 3379.33262642,
2057.574543 , 2575.28192631, 2505.16346772],
[2492.96550317, 370.87059738, 730.6161783 , 279.01612857,
268.28715959, 407.68247448, 655.08014777, 828.03200421,
1070.02476607, 1295.33972378, 765.85116047, 1155.6353231 ,
945.67330511, 1334.00149925, 2577.7854837 , 0. ,
540.56451974, 773.67952021, 537.94144663, 900.84460369,
1201.74706157, 1367.59387246, 367.00953666, 719.71174785,
1016.98869217, 1547.80651246, 1745.92210594, 1958.31790065,
1352.75311864, 1481.84783294, 1868.48441257],
[2617.38132491, 907.93006339, 267.03183331, 806.74407342,
774.36167261, 859.41433546, 1168.28934772, 1207.43902538,
1443.79950132, 1675.6804588 , 934.66678554, 1420.4453527 ,
1322.96787565, 1617.21303482, 2981.55194488, 540.56451974,
0. , 238.60008382, 141.89080308, 466.65297599,
668.28287424, 827.36267743, 559.07244611, 451.95685635,
699.90856546, 1128.68286068, 1446.37823546, 1576.36448831,
1384.49015887, 1310.80471467, 1740.666539 ],
[2757.61001594, 1136.26977431, 171.26587518, 1031.81490588,
1012.6964007 , 1096.73196361, 1406.84398566, 1372.65472716,
1599.81280155, 1829.10497238, 1127.34910298, 1619.87561251,
1546.98739491, 1817.70074545, 3202.04091167, 773.67952021,
238.60008382, 0. , 322.3740064 , 437.59456121,
469.09274136, 596.25917184, 782.8588634 , 554.00812268,
720.70659772, 1038.0004817 , 1422.94623932, 1496.87708246,
1516.05573776, 1361.62586638, 1795.96937613],
[2478.01856329, 908.01431707, 398.28758454, 815.83883212,
737.28759653, 797.77941814, 1122.52082386, 1277.62279253,
1518.25821256, 1750.25712397, 809.995679 , 1300.56141723,
1226.33804475, 1498.1645437 , 2879.92933941, 537.94144663,
141.89080308, 322.3740064 , 0. , 380.48915885,
673.70394091, 866.16972933, 464.34577633, 313.9331139 ,
578.58188703, 1046.13670235, 1330.71747565, 1483.1871089 ,
1243.48301154, 1175.15956363, 1603.20585079],
[2386.86824102, 1263.54184735, 602.127063 , 1179.54949027,
1059.80800148, 1080.27033654, 1418.32471599, 1657.67186138,
1897.74603148, 2129.82722304, 904.00497786, 1384.44212591,
1403.60713877, 1578.1726775 , 3006.66343311, 900.84460369,
466.65297599, 437.59456121, 380.48915885, 0. ,
438.41646867, 684.96569257, 714.07912727, 270.33682694,
289.39419483, 666.55682428, 993.64228976, 1109.99099095,
1152.38969103, 931.61580064, 1365.00732599],
[2775.30196555, 1572.14153307, 612.16010978, 1473.50262979,
1410.84726317, 1462.09883387, 1792.91996475, 1841.58763028,
2067.46051957, 2295.92530366, 1340.86091747, 1822.85846955,
1831.52668558, 2016.46919143, 3444.74353182, 1201.74706157,
668.28287424, 469.09274136, 673.70394091, 438.41646867,
0. , 251.80349481, 1106.75200474, 703.08249872,
664.26651278, 692.72577547, 1165.52134258, 1138.97541677,
1560.00544871, 1251.05755263, 1664.64710975],
[3023.05176932, 1732.29096863, 692.36189959, 1628.07401552,
1596.69690298, 1663.90925233, 1986.81378091, 1928.16441208,
2141.56064588, 2364.20176804, 1581.47304751, 2067.37176144,
2061.36483913, 2262.06741721, 3686.52370127, 1367.59387246,
827.36267743, 596.25917184, 866.16972933, 684.96569257,
251.80349481, 0. , 1319.88067642, 943.20305343,
915.48074802, 867.10783643, 1363.47533898, 1282.29871715,
1811.3530854 , 1488.7074931 , 1893.51128859],
[2163.06287472, 629.0858447 , 820. , 582.38303547,
377.63739222, 366.7601396 , 705.3523942 , 1185.53996137,
1424.64486803, 1645.02066856, 426.53018651, 880.22326713,
764.70255655, 1073.37225602, 2422.5560881 , 367.00953666,
559.07244611, 782.8588634 , 464.34577633, 714.07912727,
1106.75200474, 1319.88067642, 0. , 465.58028309,
735.83286146, 1293.03866918, 1420.65477861, 1667.23873515,
991.4923096 , 1125.39992891, 1503.31965995],
[2203.68645683, 1060.24902735, 681.19674104, 989.46904954,
832.21691884, 830.95246555, 1169.36093658, 1527.24916107,
1770.58013092, 2000.46244654, 638.44263642, 1125.32039882,
1133.28593038, 1321.11960094, 2743.39370124, 719.71174785,
451.95685635, 554.00812268, 313.9331139 , 270.33682694,
703.08249872, 943.20305343, 465.58028309, 0. ,
298.23648335, 836.76759019, 1043.7015857 , 1238.61939271,
962.05197365, 861.46503121, 1289.35371408],
[2116.0484399 , 1350.41067828, 878.80031862, 1284.00155763,
1111.98021565, 1089.12487806, 1422.57934752, 1824.69860525,
2067.92964097, 2298.12815134, 776.33626735, 1216.13856118,
1301.66816048, 1400.44921365, 2836.64379153, 1016.98869217,
699.90856546, 720.70659772, 578.58188703, 289.39419483,
664.26651278, 915.48074802, 735.83286146, 298.23648335,
0. , 559.81157544, 753.05378294, 941.89436775,
895.87778184, 642.60796758, 1076.47573126],
[2312.69237038, 1896.57480738, 1208.50858499, 1822.56220744,
1666.72043247, 1648.87658726, 1982.21719294, 2323.75945399,
2564.18603069, 2796.24212113, 1304.60147171, 1690.31180556,
1829.72702882, 1856.06815608, 3274.06246122, 1547.80651246,
1128.68286068, 1038.0004817 , 1046.13670235, 666.55682428,
692.72577547, 867.10783643, 1293.03866918, 836.76759019,
559.81157544, 0. , 505.50667651, 459.56718769,
1229.46004408, 759.96447285, 1092.62848215],
[1876.5247134 , 2049.69290383, 1592.03643174, 1998.25173589,
1792.43800451, 1728.79640212, 2033.69860107, 2567.06388701,
2810.44996397, 3038.79926945, 1272.03930757, 1530.15848852,
1755.16665875, 1659.22029882, 3007.76229114, 1745.92210594,
1446.37823546, 1422.94623932, 1330.71747565, 993.64228976,
1165.52134258, 1363.47533898, 1420.65477861, 1043.7015857 ,
753.05378294, 505.50667651, 0. , 371.65171868,
965.29218375, 442.76969183, 624.05929205],
[2214.39382225, 2290.02751075, 1667.63455229, 2225.75492811,
2044.82982177, 2002.6562361 , 2323.0680145 , 2756.31130317,
2998.53030667, 3230.00247678, 1585.58790359, 1884.81298807,
2088.87170501, 2021.86967928, 3379.33262642, 1958.31790065,
1576.36448831, 1496.87708246, 1483.1871089 , 1109.99099095,
1138.97541677, 1282.29871715, 1667.23873515, 1238.61939271,
941.89436775, 459.56718769, 371.65171868, 0. ,
1333.14440328, 809.45660785, 961.00208116],
[1241.81560628, 1510.82758778, 1635.90739347, 1509.8519795 ,
1250.97282145, 1118.7323183 , 1323.87046194, 2134.57489913,
2361.73199157, 2565.66755446, 602.67155234, 601.24038454,
899.44705236, 700.46912851, 2057.574543 , 1352.75311864,
1384.49015887, 1516.05573776, 1243.48301154, 1152.38969103,
1560.00544871, 1811.3530854 , 991.4923096 , 962.05197365,
895.87778184, 1229.46004408, 965.29218375, 1333.14440328,
0. , 523.68883891, 642.29977425],
[1561.04836568, 1739.09229197, 1515.23100549, 1705.48673404,
1473.35705109, 1383.18617691, 1661.92930054, 2308.76763664,
2549.20615094, 2770.41513135, 886.10213858, 1091.58416991,
1335.03745266, 1216.59730396, 2575.28192631, 1481.84783294,
1310.80471467, 1361.62586638, 1175.15956363, 931.61580064,
1251.05755263, 1488.7074931 , 1125.39992891, 861.46503121,
642.60796758, 759.96447285, 442.76969183, 809.45660785,
523.68883891, 0. , 434.3558449 ],
[1255.35851453, 2089.48821485, 1949.25883351, 2069.9613523 ,
1823.05348248, 1710.99298654, 1949.85640497, 2686.8414542 ,
2923.28394105, 3138.19709387, 1189.87142163, 1234.02795754,
1541.74576374, 1299.04657345, 2505.16346772, 1868.48441257,
1740.666539 , 1795.96937613, 1603.20585079, 1365.00732599,
1664.64710975, 1893.51128859, 1503.31965995, 1289.35371408,
1076.47573126, 1092.62848215, 624.05929205, 961.00208116,
642.29977425, 434.3558449 , 0. ]])
print(distance.shape)
oldPopulation = [22, 5, 14, 19, 13, 1, 4, 18, 17, 25, 28, 16, 30, 24, 29, 26, 9, 2, 10, 6, 11, 7, 21, 12, 3, 23, 20, 8, 15, 27, 31]
def evaluate(chromosome):
len = 0
i = 1
while i<cityNum:
print("i=",i)
len += distance[chromosome[i - 1]][chromosome[i]]
i += 1
len += distance[chromosome[cityNum - 1]][chromosome[0]] # 回到起点
return len
evaluate(oldPopulation)