This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update kruskal algorithm to deal with duplicated points
- Loading branch information
Showing
11 changed files
with
339 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
@brief test log(time=7s) | ||
""" | ||
|
||
import sys | ||
import os | ||
import unittest | ||
import pandas | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
try: | ||
import src | ||
except ImportError: | ||
path = os.path.normpath( | ||
os.path.abspath( | ||
os.path.join( | ||
os.path.split(__file__)[0], | ||
"..", | ||
".."))) | ||
if path not in sys.path: | ||
sys.path.append(path) | ||
import src | ||
|
||
try: | ||
import pyquickhelper as skip_ | ||
except ImportError: | ||
path = os.path.normpath( | ||
os.path.abspath( | ||
os.path.join( | ||
os.path.split(__file__)[0], | ||
"..", | ||
"..", | ||
"..", | ||
"pyquickhelper", | ||
"src"))) | ||
if path not in sys.path: | ||
sys.path.append(path) | ||
import pyquickhelper as skip_ | ||
|
||
from pyquickhelper.loghelper import fLOG | ||
from pyquickhelper.pycode import get_temp_folder | ||
from src.ensae_teaching_cs.faq.faq_matplotlib import graph_cities | ||
from src.ensae_teaching_cs.special import tsp_kruskal_algorithm, distance_haversine | ||
|
||
|
||
class TestFaqMatplotlibVideo(unittest.TestCase): | ||
|
||
def test_american_cities(self): | ||
fLOG( | ||
__file__, | ||
self._testMethodName, | ||
OutputPrint=__name__ == "__main__") | ||
|
||
def haversine(p1, p2): | ||
return distance_haversine(p1[0], p1[1], p2[0], p2[1]) | ||
|
||
temp = get_temp_folder(__file__, "temp_matplotlib_video") | ||
data = os.path.join(temp, "..", "data", "american_cities.txt") | ||
df = pandas.read_csv(data) | ||
df["Longitude"] = -df["Longitude"] | ||
df = df[df.Latitude < 52] | ||
df = df[df.Longitude > -130].copy() | ||
fLOG(df.columns) | ||
df = df.dropna() | ||
|
||
if __name__ != "__main__": | ||
df = df[:40].copy() | ||
fLOG(df.shape) | ||
# df["City"] = df["City"].apply(lambda v: filter.get(v, "")) | ||
points = [(row[1], row[2], row[3]) | ||
for row in df.itertuples(index=False)] | ||
fLOG("number of cities:", len(points)) | ||
trip = tsp_kruskal_algorithm( | ||
points, distance=haversine, fLOG=fLOG, max_iter=10) | ||
|
||
# trip | ||
dftrip = pandas.DataFrame( | ||
trip, columns=["Latitude", "Longitude", "City"]) | ||
save = os.path.join(temp, "trip.txt") | ||
dftrip.to_csv(save, sep="\t", index=False) | ||
|
||
# graph | ||
for i in range(0, dftrip.shape[0]): | ||
if i % 10 != 0: | ||
dftrip.ix[i, "City"] = "" | ||
fig, ax = plt.subplots(figsize=(32, 32)) | ||
ax = graph_cities(dftrip, ax=ax, markersize=3, linked=True, fLOG=fLOG, | ||
fontcolor="red", fontsize='16', loop=True) | ||
assert ax is not None | ||
img = os.path.join(temp, "img.png") | ||
fig.savefig(img) | ||
assert os.path.exists(img) | ||
if __name__ == "__main__": | ||
fig.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
@file | ||
@brief Shortcuts to special | ||
.. _l-almost_reusable: | ||
List of almost reusable algorithms implemented in this module | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
* @see fn tsp_kruskal_algorithm: `TSP <https://en.wikipedia.org/wiki/Travelling_salesman_problem>`_ | ||
* @see fn draw_line: `Bresenham <https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm>`_ algorithm (line) | ||
* @see fn draw_ellipse: `Bresenham <https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm>`_ algorithm (ellipse) | ||
* @see fn distance_haversine: distance of `Haversine <https://en.wikipedia.org/wiki/Haversine_formula>`_ | ||
* @see fn bellman: shortest paths in a graph with `Bellman-Ford <http://fr.wikipedia.org/wiki/Algorithme_de_Bellman-Ford>`_ | ||
* @see fn connected_components: computes the `connected components <https://en.wikipedia.org/wiki/Connected_component_(graph_theory)>`_ | ||
* @see fn graph_degree: computes the degree of each node in a graph `degree <https://en.wikipedia.org/wiki/Degree_(graph_theory)>`_ | ||
* @see cl GraphDistance: computes a distance between two graphs (acyclic), see :ref:`l-graph_distance` | ||
""" | ||
|
||
from .tsp_kruskal import tsp_kruskal_algorithm | ||
from .tsp_bresenham import draw_line, draw_ellipse | ||
from .rues_paris import distance_haversine, bellman, connected_components, graph_degree | ||
from .graph_distance import GraphDistance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.