one script to rule them all
This commit is contained in:
parent
3dadef14a4
commit
c4dd110d23
1 changed files with 93 additions and 0 deletions
93
run_all.py
Executable file
93
run_all.py
Executable file
|
|
@ -0,0 +1,93 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import turtle
|
||||||
|
import lindenmayer
|
||||||
|
import graph
|
||||||
|
import ant_colony
|
||||||
|
import path
|
||||||
|
import uberplot
|
||||||
|
import matplotlib.pyplot as plot
|
||||||
|
|
||||||
|
depth = 1
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
depth = int( sys.argv[1] )
|
||||||
|
print "depth",depth
|
||||||
|
|
||||||
|
print "Draw the penrose tesselation"
|
||||||
|
|
||||||
|
segment_size = 10
|
||||||
|
float_rounding = 10
|
||||||
|
|
||||||
|
ttl = turtle.Turtle()
|
||||||
|
ttl.speed('fastest')
|
||||||
|
penrose = lindenmayer.DumpTurtleLSystem(ttl,
|
||||||
|
axiom="[X]++[X]++[X]++[X]++[X]",
|
||||||
|
rules={
|
||||||
|
'F': "",
|
||||||
|
'W': "YF++ZF----XF[-YF----WF]++",
|
||||||
|
'X': "+YF--ZF[---WF--XF]+",
|
||||||
|
'Y': "-WF++XF[+++YF++ZF]-",
|
||||||
|
'Z': "--YF++++WF[+ZF++++XF]--XF"
|
||||||
|
},
|
||||||
|
angle=36, heading=0, size=segment_size, rounding=float_rounding )
|
||||||
|
|
||||||
|
# actually do something
|
||||||
|
penrose.draw( depth )
|
||||||
|
print "segments",len(penrose.segments)
|
||||||
|
with open("penrose_%i.segments" % depth, "w") as fd:
|
||||||
|
fd.write( str(penrose) )
|
||||||
|
|
||||||
|
print "Convert the segment list into an adjacency list graph"
|
||||||
|
G = graph.graph( penrose.segments )
|
||||||
|
|
||||||
|
|
||||||
|
print "Solve the TSP with an Ant Colony Algorithm"
|
||||||
|
|
||||||
|
max_it = 10
|
||||||
|
num_ants = 10 #* depth
|
||||||
|
decay = 0.1
|
||||||
|
w_heur = 2.5
|
||||||
|
w_local_phero = 0.1
|
||||||
|
c_greed = 0.9
|
||||||
|
w_history = 1.0
|
||||||
|
|
||||||
|
best,phero = ant_colony.search( G, max_it, num_ants, decay, w_heur, w_local_phero, w_history, c_greed, cost_func = ant_colony.graph_distance )
|
||||||
|
|
||||||
|
print "Transform the resulting nodes permutation into a path on the graph"
|
||||||
|
# by finding the shortest path between two cities.
|
||||||
|
traj = []
|
||||||
|
for start,end in ant_colony.tour(best["permutation"]):
|
||||||
|
p,c = path.astar( G, start, end )
|
||||||
|
traj += p
|
||||||
|
print "traj",len(traj)
|
||||||
|
|
||||||
|
print "Plot the resulting tour"
|
||||||
|
fig = plot.figure()
|
||||||
|
ax = fig.add_subplot(111)
|
||||||
|
|
||||||
|
maxph=0
|
||||||
|
for i in phero:
|
||||||
|
maxph = max( maxph, max(phero[i].values()))
|
||||||
|
|
||||||
|
# ant colony
|
||||||
|
# pheromones
|
||||||
|
for i in phero:
|
||||||
|
for j in phero[i]:
|
||||||
|
if i == j:
|
||||||
|
continue
|
||||||
|
nph = phero[i][j]/maxph
|
||||||
|
seg = [(i,j)]
|
||||||
|
# print nph,seg
|
||||||
|
uberplot.plot_segments( ax, seg, edgecolor="red", alpha=0.03*nph, linewidth=7*nph )
|
||||||
|
# uberplot.scatter_segments( ax, seg, color="red", alpha=0.5, linewidth=nph )
|
||||||
|
|
||||||
|
# tour
|
||||||
|
uberplot.plot_segments( ax, ant_colony.tour(traj), color="green", alpha=0.9, linewidth=4 )
|
||||||
|
|
||||||
|
# tesselation
|
||||||
|
uberplot.plot_segments( ax, penrose.segments, edgecolor="black", alpha=0.9, linewidth=1 )
|
||||||
|
uberplot.scatter_segments( ax, penrose.segments, color="black", alpha=0.9, linewidth=1 )
|
||||||
|
|
||||||
|
plot.show()
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue