New plot feature: cache and noplot-* options
This commit is contained in:
parent
a1838de026
commit
3c49dc9f6e
1 changed files with 82 additions and 55 deletions
129
run_all.py
129
run_all.py
|
|
@ -39,12 +39,29 @@ parser.add_argument('-g', "--triangulation", help="Do not compute the Delaunay t
|
||||||
parser.add_argument('-v', "--voronoi", help="Do not compute the Voronoï diagram but load it from a file",
|
parser.add_argument('-v', "--voronoi", help="Do not compute the Voronoï diagram but load it from a file",
|
||||||
default=None, action='store', type=str, metavar="POINTS")
|
default=None, action='store', type=str, metavar="POINTS")
|
||||||
|
|
||||||
args = parser.parse_args()
|
parser.add_argument('-C', "--cache", help="Try to load available precomputed files instead of computing from scratch",
|
||||||
|
default=False, action='store_true')
|
||||||
|
|
||||||
|
parser.add_argument('-P', "--noplot-penrose", help="Do not plot the Penrose tiling", default=False, action='store_true')
|
||||||
|
parser.add_argument('-T', "--noplot-tour", help="Do not plot the TSP tours", default=False, action='store_true')
|
||||||
|
parser.add_argument('-M', "--noplot-pheromones", help="Do not plot the pheromones matrix", default=False, action='store_true')
|
||||||
|
parser.add_argument('-G', "--noplot-triangulation", help="Do not plot the triangulation", default=False, action='store_true')
|
||||||
|
parser.add_argument('-V', "--noplot-voronoi", help="Do not plot the Voronoï diagram", default=False, action='store_true')
|
||||||
|
|
||||||
|
ask_for = parser.parse_args()
|
||||||
|
|
||||||
|
if ask_for.cache:
|
||||||
|
ask_for.penrose = "d%i_penrose.segments" % ask_for.depth
|
||||||
|
ask_for.triangulation = "d%i_triangulation.triangles" % ask_for.depth
|
||||||
|
ask_for.notsp = True
|
||||||
|
ask_for.tour = ["d%i_tour.points" % ask_for.depth]
|
||||||
|
ask_for.pheromones = "d%i_pheromones.mat" % ask_for.depth
|
||||||
|
ask_for.voronoi = "d%i_voronoi.graph" % ask_for.depth
|
||||||
|
|
||||||
|
|
||||||
error_codes = {"NOTSP":100}
|
error_codes = {"NOTSP":100}
|
||||||
|
|
||||||
depth = args.depth
|
depth = ask_for.depth
|
||||||
LOGN( "depth",depth )
|
LOGN( "depth",depth )
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
@ -53,9 +70,9 @@ LOGN( "depth",depth )
|
||||||
|
|
||||||
penrose_segments = set()
|
penrose_segments = set()
|
||||||
|
|
||||||
if args.penrose:
|
if ask_for.penrose:
|
||||||
LOGN( "Load the penrose tiling" )
|
LOGN( "Load the penrose tiling" )
|
||||||
with open(args.penrose) as fd:
|
with open(ask_for.penrose) as fd:
|
||||||
penrose_segments = utils.load_segments(fd)
|
penrose_segments = utils.load_segments(fd)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -95,18 +112,18 @@ else:
|
||||||
|
|
||||||
trajs = []
|
trajs = []
|
||||||
|
|
||||||
if args.tour != [None]:
|
if ask_for.tour != [None]:
|
||||||
for tour in args.tour:
|
for tour in ask_for.tour:
|
||||||
with open(tour) as fd:
|
with open(tour) as fd:
|
||||||
trajs.append( utils.load_points(fd) )
|
trajs.append( utils.load_points(fd) )
|
||||||
|
|
||||||
if args.notsp:
|
if ask_for.notsp:
|
||||||
if args.tour == [None] or not args.pheromones:
|
if ask_for.tour == [None] or not ask_for.pheromones:
|
||||||
LOGN( "If you do not want to solve the TSP, you must provide a solution tour (--tour) and a pheromones matrix (--pheromones)" )
|
LOGN( "If you do not want to solve the TSP, you must provide a solution tour (--tour) and a pheromones matrix (--pheromones)" )
|
||||||
sys.exit(error_codes["NO-TSP"])
|
sys.exit(error_codes["NO-TSP"])
|
||||||
|
|
||||||
if args.pheromones:
|
if ask_for.pheromones:
|
||||||
with open(args.pheromones) as fd:
|
with open(ask_for.pheromones) as fd:
|
||||||
phero = utils.load_matrix(fd)
|
phero = utils.load_matrix(fd)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -116,8 +133,10 @@ else:
|
||||||
G = graph.graph_of( penrose_segments )
|
G = graph.graph_of( penrose_segments )
|
||||||
|
|
||||||
LOGN( "\tCompute a tour" )
|
LOGN( "\tCompute a tour" )
|
||||||
max_it = 10
|
# max_it = 10
|
||||||
num_ants = 10 #* depth
|
max_it = 2
|
||||||
|
# num_ants = 10 #* depth
|
||||||
|
num_ants = 2 #* depth
|
||||||
decay = 0.1
|
decay = 0.1
|
||||||
w_heur = 2.5
|
w_heur = 2.5
|
||||||
w_local_phero = 0.1
|
w_local_phero = 0.1
|
||||||
|
|
@ -147,8 +166,8 @@ else:
|
||||||
|
|
||||||
triangulated = []
|
triangulated = []
|
||||||
|
|
||||||
if args.triangulation:
|
if ask_for.triangulation:
|
||||||
with open(args.triangulation) as fd:
|
with open(ask_for.triangulation) as fd:
|
||||||
triangulated = triangulation.load(fd)
|
triangulated = triangulation.load(fd)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -159,14 +178,19 @@ else:
|
||||||
LOGN( "\tRemove triangles that are not sub-parts of the Penrose tiling" )
|
LOGN( "\tRemove triangles that are not sub-parts of the Penrose tiling" )
|
||||||
|
|
||||||
|
|
||||||
def strictly_acute(triangle):
|
|
||||||
return triangulation.is_acute( triangle, exclude_edges = True )
|
|
||||||
# Filter (i.e. keep) triangles that are strictly acute,
|
# Filter (i.e. keep) triangles that are strictly acute,
|
||||||
# By excluding edges, we also ensure that no triangle can be collinear nor rectangle,
|
def strictly_acute(triangle):
|
||||||
triangulated = list(filter( strictly_acute, triangles ))
|
# By excluding edges, we also ensure that no triangle can be collinear nor rectangle,
|
||||||
|
return triangulation.is_acute( triangle, exclude_edges = True )
|
||||||
|
triangulated_acute = list(filter( strictly_acute, triangles ))
|
||||||
# A more consise but less readable one-liner would be:
|
# A more consise but less readable one-liner would be:
|
||||||
# triangulated = list(filter( lambda t: triangulation.is_acute( t, exclude_edges = True ), triangles ))
|
# triangulated = list(filter( lambda t: triangulation.is_acute( t, exclude_edges = True ), triangles ))
|
||||||
|
|
||||||
|
# def not_collinear(triangle):
|
||||||
|
# return not geometry.collinear(*triangle)
|
||||||
|
# triangulated = list(filter( not_collinear, triangulated_acute ))
|
||||||
|
triangulated = triangulated_acute
|
||||||
|
|
||||||
LOGN( "\t\tRemoved", len(triangles)-len(triangulated), "triangles from", len(triangles))
|
LOGN( "\t\tRemoved", len(triangles)-len(triangulated), "triangles from", len(triangles))
|
||||||
|
|
||||||
with open("d%i_triangulation.triangles" % depth, "w") as fd:
|
with open("d%i_triangulation.triangles" % depth, "w") as fd:
|
||||||
|
|
@ -181,8 +205,8 @@ triangulation_edges = triangulation.edges_of( triangulated )
|
||||||
|
|
||||||
voronoi_graph = {}
|
voronoi_graph = {}
|
||||||
|
|
||||||
if args.voronoi:
|
if ask_for.voronoi:
|
||||||
with open(args.voronoi) as fd:
|
with open(ask_for.voronoi) as fd:
|
||||||
voronoi_graph = graph.load( fd )
|
voronoi_graph = graph.load( fd )
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -238,45 +262,48 @@ for i in range(len(chosen_theme)):
|
||||||
theme[k]["zorder"] = i
|
theme[k]["zorder"] = i
|
||||||
|
|
||||||
|
|
||||||
LOGN( "Plot the resulting tour" )
|
LOGN( "Plot the image" )
|
||||||
fig = plot.figure()
|
fig = plot.figure()
|
||||||
ax = fig.add_subplot(111)
|
ax = fig.add_subplot(111)
|
||||||
|
|
||||||
LOGN( "\tpheromones",len(phero),"nodes" )#,"x",len(phero[traj[0]]) )
|
if not ask_for.noplot_pheromones:
|
||||||
maxph=0
|
LOGN( "\tpheromones",len(phero),"nodes" )#,"x",len(phero[traj[0]]) )
|
||||||
for i in phero:
|
maxph=0
|
||||||
maxph = max( maxph, max(phero[i].values()))
|
for i in phero:
|
||||||
|
maxph = max( maxph, max(phero[i].values()))
|
||||||
|
|
||||||
# ant colony
|
# ant colony
|
||||||
# pheromones
|
# pheromones
|
||||||
for i in phero:
|
for i in phero:
|
||||||
for j in phero[i]:
|
for j in phero[i]:
|
||||||
if i == j:
|
if i == j:
|
||||||
continue
|
continue
|
||||||
nph = phero[i][j]/maxph
|
nph = phero[i][j]/maxph
|
||||||
seg = [(i,j)]
|
seg = [(i,j)]
|
||||||
# LOGN( nph,seg )
|
# LOGN( nph,seg )
|
||||||
uberplot.plot_segments( ax, seg, alpha=0.01*nph, linewidth=1*nph, **theme["pheromones"] )
|
uberplot.plot_segments( ax, seg, alpha=0.01*nph, linewidth=1*nph, **theme["pheromones"] )
|
||||||
# uberplot.scatter_segments( ax, seg, color="red", alpha=0.5, linewidth=nph )
|
# uberplot.scatter_segments( ax, seg, color="red", alpha=0.5, linewidth=nph )
|
||||||
|
|
||||||
for traj in trajs:
|
if not ask_for.noplot_tour:
|
||||||
LOGN( "\ttraj",len(traj),"points" )
|
for traj in trajs:
|
||||||
# best tour
|
LOGN( "\ttraj",len(traj),"points" )
|
||||||
uberplot.plot_segments( ax, utils.tour(traj), **theme["tour"])
|
# best tour
|
||||||
|
uberplot.plot_segments( ax, utils.tour(traj), **theme["tour"])
|
||||||
|
|
||||||
LOGN( "\ttiling",len(penrose_segments),"segments" )
|
if not ask_for.noplot_penrose:
|
||||||
uberplot.plot_segments( ax, penrose_segments, **theme["penrose"])
|
LOGN( "\ttiling",len(penrose_segments),"segments" )
|
||||||
|
uberplot.plot_segments( ax, penrose_segments, **theme["penrose"])
|
||||||
|
|
||||||
# triangulation
|
if not ask_for.noplot_triangulation:
|
||||||
LOGN( "\ttriangulation",len(triangulation_edges),"edges" )
|
LOGN( "\ttriangulation",len(triangulation_edges),"edges" )
|
||||||
uberplot.plot_segments( ax, triangulation_edges, **theme["triangulation"])
|
uberplot.plot_segments( ax, triangulation_edges, **theme["triangulation"])
|
||||||
|
|
||||||
# Voronoï
|
if not ask_for.noplot_voronoi:
|
||||||
LOGN( "\tVoronoï",len(voronoi_edges),"edges")
|
LOGN( "\tVoronoï",len(voronoi_edges),"edges")
|
||||||
# uberplot.plot_segments( ax, voronoi_tri_edges, edgecolor="red", alpha=1, linewidth=1 )
|
# uberplot.plot_segments( ax, voronoi_tri_edges, edgecolor="red", alpha=1, linewidth=1 )
|
||||||
# uberplot.scatter_points( ax, voronoi_tri_centers, edgecolor="red", facecolor="white", s=200, alpha=1, zorder=10 )
|
# uberplot.scatter_points( ax, voronoi_tri_centers, edgecolor="red", facecolor="white", s=200, alpha=1, zorder=10 )
|
||||||
uberplot.plot_segments( ax, voronoi_edges, **theme["voronoi_edges"] )
|
uberplot.plot_segments( ax, voronoi_edges, **theme["voronoi_edges"] )
|
||||||
uberplot.scatter_points( ax, voronoi_centers, **theme["voronoi_nodes"] )
|
uberplot.scatter_points( ax, voronoi_centers, **theme["voronoi_nodes"] )
|
||||||
|
|
||||||
ax.set_aspect('equal')
|
ax.set_aspect('equal')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue