Hi You also can use network solver for TSP. This approach more efficient. data Cities;
input city $20. lat long;
datalines;
Albany, NY 42.652552778 -73.75732222
Annapolis, MD 38.978611111 -76.49111111
Atlanta, GA 33.749272222 -84.38826111
Augusta, ME 44.307236111 -69.78167778
Austin, TX 30.274722222 -97.74055556
Baton Rouge, LA 30.457072222 -91.18740556
Bismarck, ND 46.820813889 -100.7827417
Boise, ID 43.617697222 -116.1996139
Boston, MA 42.357708333 -71.06356389
Carson City, NV 39.164075 -119.7662917
Charleston, WV 38.336388889 -81.61222222
Cheyenne, WY 41.140277778 -104.8197222
Columbia, SC 34.000433333 -81.03314722
Columbus, OH 39.961388889 -82.99888889
Concord, NH 43.206747222 -71.53812778
Denver, CO 39.739094444 -104.9848972
Des Moines, IA 41.591177778 -93.60386944
Dover, DE 39.157305556 -75.51972222
Frankfort, KY 38.186777778 -84.87533333
Harrisburg, PA 40.264444444 -76.86666667
Hartford, CT 41.764136111 -72.68277778
Helena, MT 46.5857 -112.0184
Indianapolis, IN 39.768611111 -86.1625
Jackson, MS 32.303888889 -90.18222222
Jefferson City, MO 38.579119444 -92.17299167
Lansing, MI 42.733727778 -84.55558889
Lincoln, NE 40.808088889 -96.69958611
Little Rock, AR 34.746758333 -92.28876111
Madison, WI 43.074444444 -89.38472222
Montgomery, AL 32.377447222 -86.30094167
Montpelier, VT 44.262222222 -72.58083333
Nashville, TN 36.165833333 -86.78416667
Oklahoma City, OK 35.492280556 -97.50337222
Olympia, WA 47.035277778 -122.9063889
Phoenix, AZ 33.448097222 -112.0970944
Pierre, SD 44.367166667 -100.3463528
Providence, RI 41.830833333 -71.415
Raleigh, NC 35.780277778 -78.63916667
Richmond, VA 37.538758333 -77.43359444
Sacramento, CA 38.576572222 -121.4934111
Saint Paul, MN 44.955147222 -93.10223611
Salem, OR 44.938730556 -123.0300972
Salt Lake City, UT 40.777222222 -111.8880556
Santa Fe, NM 35.682280556 -105.9396583
Springfield, IL 39.798516667 -89.65488889
Tallahassee, FL 30.438111111 -84.2816
Topeka, KS 39.048008333 -95.67815556
Trenton, NJ 40.220436111 -74.76990278
Washington, DC 38.889802778 -77.00911389
; From this list, you can generate a links data set, CitiesDist , that contains the distances (in miles) between each pair of cities. The distances are calculated by using the SAS function GEODIST. /* create a list of all the possible pairs of cities */
proc sql;
create table CitiesDist as
select
a.city as city1, a.lat as lat1, a.long as long1,
b.city as city2, b.lat as lat2, b.long as long2,
geodist(lat1, long1, lat2, long2, 'DM') as distance
from Cities as a, Cities as b
where a.city < b.city;
quit; The following PROC OPTMODEL statements find the optimal tour of all the capital cities: /* find optimal tour by using the network solver */
proc optmodel;
set<str,str> CAPPAIRS;
set<str> CAPITALS = union {<i,j> in CAPPAIRS} {i,j};
num distance{i in CAPITALS, j in CAPITALS: i < j};
read data CitiesDist into CAPPAIRS=[city1 city2] distance;
set<str,str> TOUR;
num order{CAPITALS};
solve with NETWORK /
loglevel = moderate
links = (weight=distance)
tsp
out = (order=order tour=TOUR)
;
put (sum{<i,j> in TOUR} distance[i,j]);
/* create tour-ordered pairs (rather than input-ordered pairs) */
str CAPbyOrder{1..card(CAPITALS)};
for {i in CAPITALS} CAPbyOrder[order[i]] = i;
set TSPEDGES init
setof{i in 2..card(CAPITALS)} <CAPbyOrder[i-1],CAPbyOrder[i]>
union {<CAPbyOrder[card(CAPITALS)],CAPbyOrder[1]>};
num distance2{<i,j> in TSPEDGES} =
if i < j then distance[i,j] else distance[j,i];
create data TSPTourNodes from [node] tsp_order=order;
create data TSPTourLinks from [city1 city2]=TSPEDGES distance=distance2;
quit;
... View more