In which order should you visit cities so as to travel the shortest distance?
Run the code to get a list of the capitals of Africa. Try other regions, like “northern europe” or “the caribbean”:
\!\(\*
NamespaceBox["LinguisticAssistant",
DynamicModuleBox[{Typeset`query$ = "capitals of africa",
Typeset`boxes$ = RowBox[{"EntityValue", "[",
RowBox[{
TemplateBox[{"\"Africa\"",
RowBox[{"EntityClass", "[",
RowBox[{"\"Country\"", ",", "\"Africa\""}], "]"}],
"\"EntityClass[\\\"Country\\\", \\\"Africa\\\"]\"",
"\"countries\""}, "EntityClass"], ",",
TemplateBox[{"\"capital city\"",
RowBox[{"EntityProperty", "[",
RowBox[{"\"Country\"", ",", "\"CapitalCity\""}], "]"}],
"\"EntityProperty[\\\"Country\\\", \\\"CapitalCity\\\"]\""},
"EntityProperty"]}], "]"}], Typeset`allassumptions$ = {},
Typeset`assumptions$ = {}, Typeset`open$ = {1},
Typeset`querystate$ = {
"Online" -> True, "Allowed" -> True,
"mparse.jsp" -> 0.287733`5.910534667329372, "Messages" -> {}}},
DynamicBox[ToBoxes[
AlphaIntegration`LinguisticAssistantBoxes["", 4, Automatic,
Dynamic[Typeset`query$],
Dynamic[Typeset`boxes$],
Dynamic[Typeset`allassumptions$],
Dynamic[Typeset`assumptions$],
Dynamic[Typeset`open$],
Dynamic[Typeset`querystate$]], StandardForm],
ImageSizeCache->{116., {7., 16.}},
TrackedSymbols:>{
Typeset`query$, Typeset`boxes$, Typeset`allassumptions$,
Typeset`assumptions$, Typeset`open$, Typeset`querystate$}],
DynamicModuleValues:>{},
UndoTrackedVariables:>{Typeset`open$}],
BaseStyle->{"Deploy"},
DeleteWithContents->True,
Editable->False,
SelectWithContents->True]\)
Get the geographic positions of the capitals. Try getting other properties, like "Population" or "Elevation":
positions = EntityValue[\!\(\*
NamespaceBox["LinguisticAssistant",
DynamicModuleBox[{Typeset`query$ = "capitals of africa",
Typeset`boxes$ = RowBox[{"EntityValue", "[",
RowBox[{
TemplateBox[{"\"Africa\"",
RowBox[{"EntityClass", "[",
RowBox[{"\"Country\"", ",", "\"Africa\""}], "]"}],
"\"EntityClass[\\\"Country\\\", \\\"Africa\\\"]\"",
"\"countries\""}, "EntityClass"], ",",
TemplateBox[{"\"capital city\"",
RowBox[{"EntityProperty", "[",
RowBox[{"\"Country\"", ",", "\"CapitalCity\""}], "]"}],
"\"EntityProperty[\\\"Country\\\", \
\\\"CapitalCity\\\"]\""}, "EntityProperty"]}], "]"}],
Typeset`allassumptions$ = {}, Typeset`assumptions$ = {},
Typeset`open$ = {1}, Typeset`querystate$ = {
"Online" -> True, "Allowed" -> True,
"mparse.jsp" -> 0.287733`5.910534667329372, "Messages" -> {}}},
DynamicBox[ToBoxes[
AlphaIntegration`LinguisticAssistantBoxes["", 4, Automatic,
Dynamic[Typeset`query$],
Dynamic[Typeset`boxes$],
Dynamic[Typeset`allassumptions$],
Dynamic[Typeset`assumptions$],
Dynamic[Typeset`open$],
Dynamic[Typeset`querystate$]], StandardForm],
ImageSizeCache->{116., {7., 16.}},
TrackedSymbols:>{
Typeset`query$, Typeset`boxes$, Typeset`allassumptions$,
Typeset`assumptions$, Typeset`open$, Typeset`querystate$}],
DynamicModuleValues:>{},
UndoTrackedVariables:>{Typeset`open$}],
BaseStyle->{"Deploy"},
DeleteWithContents->True,
Editable->False,
SelectWithContents->True]\), "Position"]
Find the order of capitals that gives the shortest route:
Note: run the code in the previous step first.
tour = Last[FindShortestTour[positions]]
Show the order of the capitals along the shortest route:
Note: run the code in the previous step first.
\!\(\*
NamespaceBox["LinguisticAssistant",
DynamicModuleBox[{Typeset`query$ = "capitals of africa",
Typeset`boxes$ = RowBox[{"EntityValue", "[",
RowBox[{
TemplateBox[{"\"Africa\"",
RowBox[{"EntityClass", "[",
RowBox[{"\"Country\"", ",", "\"Africa\""}], "]"}],
"\"EntityClass[\\\"Country\\\", \\\"Africa\\\"]\"",
"\"countries\""}, "EntityClass"], ",",
TemplateBox[{"\"capital city\"",
RowBox[{"EntityProperty", "[",
RowBox[{"\"Country\"", ",", "\"CapitalCity\""}], "]"}],
"\"EntityProperty[\\\"Country\\\", \
\\\"CapitalCity\\\"]\""}, "EntityProperty"]}], "]"}],
Typeset`allassumptions$ = {}, Typeset`assumptions$ = {},
Typeset`open$ = {1}, Typeset`querystate$ = {
"Online" -> True, "Allowed" -> True,
"mparse.jsp" -> 0.287733`5.910534667329372, "Messages" -> {}}},
DynamicBox[ToBoxes[
AlphaIntegration`LinguisticAssistantBoxes["", 4, Automatic,
Dynamic[Typeset`query$],
Dynamic[Typeset`boxes$],
Dynamic[Typeset`allassumptions$],
Dynamic[Typeset`assumptions$],
Dynamic[Typeset`open$],
Dynamic[Typeset`querystate$]], StandardForm],
ImageSizeCache->{116., {7., 16.}},
TrackedSymbols:>{
Typeset`query$, Typeset`boxes$, Typeset`allassumptions$,
Typeset`assumptions$, Typeset`open$, Typeset`querystate$}],
DynamicModuleValues:>{},
UndoTrackedVariables:>{Typeset`open$}],
BaseStyle->{"Deploy"},
DeleteWithContents->True,
Editable->False,
SelectWithContents->True]\)[[tour]]
Make a map of the shortest tour:
GeoGraphics[{Thick, Red, Line[positions[[tour]]]}]
Share It—Make a website that shows the shortest route through cities that you specify:
CloudDeploy[
FormFunction[{{"cities", "cities (city1; city2; ...)"} ->
DelimitedSequence["City"]},
Block[{positions, tour},
positions = EntityValue[#cities, "Position"];
tour = Last[FindShortestTour[positions]];
GeoGraphics[{Thick, Red, Line[positions[[tour]]]},
GeoRangePadding -> Scaled[.5]]
] &,
"PNG"
],
Permissions -> "Public"
]