Exemplo n.º 1
0
def make_grid_plots(dirname, plot_names=None):
    if "depth" in dirname:
        # Assume GP
        if "depth_6" not in dirname:
            ind_names = open(dirname + "/all_trees.dat").read().strip().split("\n")
            ind_names = map(lambda x: x.strip(), ind_names)
        else:
            ind_names = None
    elif "ga_length" in dirname:
        # Assume GA
        length = int(dirname.strip("/").split("_")[2])
        ind_names = [bin(i)[2:] for i in range(2**length)]
    elif "tsp_length" in dirname:
        # Assume TSP
        length = int(dirname.strip("/").split("_")[2])
        if length < 10:
            # 1-digit indices: just concatenate
            ind_names = map(lambda t: ''.join(str(ti) for ti in t),
                            tsp_tours(length))
        else:
            # use original tuple-style string
            ind_names = map(str, tsp_tours(length))

    if plot_names is None:
        syn_names = syntactic_distance_names(dirname)
        grph_names, grph_tex_names = graph_distance_names(dirname)
        plot_names = syn_names + grph_names

    for plot_name in plot_names:
        w = np.genfromtxt(dirname + "/" + plot_name + ".dat")
        if "depth_6" not in dirname:
            assert(len(w) == len(ind_names))
        print plot_name
        make_grid(w, False, dirname + "/" + plot_name)
    print ind_names # better to print them in a list somewhere than in the graph
Exemplo n.º 2
0
def make_grid_plots(dirname, plot_names=None):
    if "depth" in dirname:
        # Assume GP
        if "depth_6" not in dirname:
            ind_names = open(dirname +
                             "/all_trees.dat").read().strip().split("\n")
            ind_names = map(lambda x: x.strip(), ind_names)
        else:
            ind_names = None
    elif "ga_length" in dirname:
        # Assume GA
        length = int(dirname.strip("/").split("_")[2])
        ind_names = [bin(i)[2:] for i in range(2**length)]
    elif "tsp_length" in dirname:
        # Assume TSP
        length = int(dirname.strip("/").split("_")[2])
        if length < 10:
            # 1-digit indices: just concatenate
            ind_names = map(lambda t: ''.join(str(ti) for ti in t),
                            tsp_tours(length))
        else:
            # use original tuple-style string
            ind_names = map(str, tsp_tours(length))

    if plot_names is None:
        syn_names = syntactic_distance_names(dirname)
        grph_names, grph_tex_names = graph_distance_names(dirname)
        plot_names = syn_names + grph_names

    for plot_name in plot_names:
        w = np.genfromtxt(dirname + "/" + plot_name + ".dat")
        if "depth_6" not in dirname:
            assert (len(w) == len(ind_names))
        print plot_name
        make_grid(w, False, dirname + "/" + plot_name)
    print ind_names  # better to print them in a list somewhere than in the graph
Exemplo n.º 3
0
def make_SIGEvo_images():
    gp_dirname = "../../results/depth_2/"
    ga_dirname = "../../results/ga_length_10_per_ind/"
    tsp_dirname = "../../results/tsp_length_7_2_opt/"

    def clamp(x):
        return max(0.0, min(1.0, x))

    def colour_val(i):
        return clamp(np.log(fit_vals[i]) /
                     5.5)  # NB hardcoded for our fit vals

    def marker_size(tree_name):
        return 20 + 50 * tree_len(tree_name) / 7.0

    def tree_len(tree_name):
        return sum(tree_name.count(s) for s in "+-*/xy")

    def count_ones(i):
        # count ones in the binary rep
        return bin(i).count("1")

    def tsp_fit(t):
        def d(a, b):
            return sqrt((a[0] - b[0])**2.0 + (a[1] - b[1])**2.0)

        # place a few arbitrary cities
        c = [[0, 0], [10, 1], [5, 5], [2, 8], [8, 9], [5, 9], [7, 1]]
        s = t + (t[0], )  # replicate initial city to make it easy
        return sum(d(c[s[i]], c[s[i + 1]]) for i in range(len(t)))

    names = [
        "OVD", "FE", "SD_TP", "TED", "TAD1", "FVD", "CT", "SEMD", "Hamming",
        "SEMD_alternate_target", "KendallTau"
    ]
    colour_maps = [
        new_ocean(), cm.PuRd_r, cm.YlOrRd, cm.copper, cm.autumn, cm.Purples_r,
        cm.YlGnBu_r, cm.summer, cm.BuGn_r, cm.afmhot, cm.BuPu_r
    ]

    for name, colour_map in zip(names, colour_maps):

        # a hack for doing just a few quickly
        if name not in ["SEMD_alternate_target", "KendallTau"]: continue

        if name == "Hamming":
            # GA
            mds_data_filename = ga_dirname + "/" + name + "_MDS.dat"
            mds_output_filename = ga_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [count_ones(i) for i in range(2**10)]
            marker_sizes = [30 for i in range(2**10)]
            marker = 's'  # square

        elif name == "KendallTau":
            # TSP
            mds_data_filename = tsp_dirname + "/" + name + "_MDS.dat"
            mds_output_filename = tsp_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [tsp_fit(t) for t in tsp_tours(7)]  # will be scaled
            marker_sizes = [30 for i in range(len(colour_vals))]
            marker = 'o'  # circle

        else:
            # GP
            tree_names = open("../../results/depth_2/all_trees.dat").read(
            ).strip().split("\n")

            if name == "SEMD_alternate_target":
                fit_vals = np.genfromtxt(
                    "../../results/depth_2/all_trees_fitness_alternate_target.dat"
                )
            else:
                fit_vals = np.genfromtxt(
                    "../../results/depth_2/all_trees_fitness.dat")
            mds_data_filename = gp_dirname + "/SEMD_MDS.dat"  # not SEMD_alternate_target -- no need
            mds_output_filename = gp_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [colour_val(i) for i in range(len(tree_names))]
            marker_sizes = [marker_size(tree_name) for tree_name in tree_names]
            marker = '^'  # triangle

        # get existing MDS data
        p = np.genfromtxt(mds_data_filename)

        # Make an image
        fig, ax = plt.subplots(figsize=(5, 5), frameon=True)
        ax.axis('on')
        # x- and y-coordinates
        ax.set_aspect('equal')
        ax.scatter(p[:, 0],
                   p[:, 1],
                   s=marker_sizes,
                   marker=marker,
                   c=colour_vals,
                   cmap=colour_map,
                   alpha=0.5,
                   edgecolors='none')

        fig.savefig(mds_output_filename + ".png", bbox_inches='tight')
        fig.savefig(mds_output_filename + ".pdf", bbox_inches='tight')
Exemplo n.º 4
0
# elements relevant to a given tour t
def tour_to_idx(t):
    ij = zip(t, t[1:] + [t[0]])
    return zip(*ij)


# helper function: get the indices needed to set the elements of the
# 'a' matrix
def tour_to_1d_idx(tour, n):
    return (i * n + j for i, j in zip(*tour_to_idx(tour)))


C = random_tsp(n)

# calculate cost of each tour
b = np.array([cost(t, x) for t in tsp_tours(n)])

# calculate permuted cost values
b1 = b.copy()
amx, amn = b1.argmax(), b1.argmin()
b1[amx], b1[amn] = b1.min(), b1.max()

# have a look at
for item in tour_to_1d_idx(t, n):
    print(item)

# construct a
a = np.zeros((b.shape[0], n * n))

for ti, tour in enumerate(tsp_tours(n)):
    a[ti, list(tour_to_1d_idx(tour, n))] = 1
Exemplo n.º 5
0
def make_SIGEvo_images():
    gp_dirname = "../../results/depth_2/"
    ga_dirname = "../../results/ga_length_10_per_ind/"
    tsp_dirname = "../../results/tsp_length_7_2_opt/"

    def clamp(x):
        return max(0.0, min(1.0, x))
    def colour_val(i):
        return clamp(np.log(fit_vals[i]) / 5.5) # NB hardcoded for our fit vals
    def marker_size(tree_name):
        return 20 + 50 * tree_len(tree_name) / 7.0
    def tree_len(tree_name):
        return sum(tree_name.count(s) for s in "+-*/xy")
    def count_ones(i):
        # count ones in the binary rep
        return bin(i).count("1")
    def tsp_fit(t):
        def d(a, b): return sqrt((a[0]-b[0])**2.0 + (a[1]-b[1])**2.0)
        # place a few arbitrary cities
        c = [[0, 0],
             [10, 1],
             [5, 5],
             [2, 8],
             [8, 9],
             [5, 9],
             [7, 1]
        ]
        s = t + (t[0],) # replicate initial city to make it easy
        return sum(d(c[s[i]], c[s[i+1]]) for i in range(len(t)))
        
    
    names = ["OVD", "FE", "SD_TP", "TED", "TAD1", "FVD", "CT", "SEMD", "Hamming", "SEMD_alternate_target", "KendallTau"]
    colour_maps = [new_ocean(), cm.PuRd_r, cm.YlOrRd, cm.copper, cm.autumn, cm.Purples_r, cm.YlGnBu_r, cm.summer, cm.BuGn_r, cm.afmhot, cm.BuPu_r]

    for name, colour_map in zip(names, colour_maps):

        # a hack for doing just a few quickly
        if name not in ["SEMD_alternate_target", "KendallTau"]: continue
        
        if name == "Hamming":
            # GA
            mds_data_filename = ga_dirname + "/" + name + "_MDS.dat"
            mds_output_filename = ga_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [count_ones(i) for i in range(2**10)]
            marker_sizes = [30 for i in range(2**10)]
            marker = 's' # square

        elif name == "KendallTau":
            # TSP
            mds_data_filename = tsp_dirname + "/" + name + "_MDS.dat"
            mds_output_filename = tsp_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [tsp_fit(t) for t in tsp_tours(7)] # will be scaled
            marker_sizes = [30 for i in range(len(colour_vals))]
            marker = 'o' # circle

        else:
            # GP
            tree_names = open("../../results/depth_2/all_trees.dat").read().strip().split("\n")

            if name == "SEMD_alternate_target":
                fit_vals = np.genfromtxt("../../results/depth_2/all_trees_fitness_alternate_target.dat")
            else:
                fit_vals = np.genfromtxt("../../results/depth_2/all_trees_fitness.dat")
            mds_data_filename = gp_dirname + "/SEMD_MDS.dat" # not SEMD_alternate_target -- no need
            mds_output_filename = gp_dirname + "/SIGEvo_images/" + name + "_MDS"
            colour_vals = [colour_val(i) for i in range(len(tree_names))]
            marker_sizes = [marker_size(tree_name) for tree_name in tree_names]
            marker = '^' # triangle


        # get existing MDS data
        p = np.genfromtxt(mds_data_filename)
        
        # Make an image
        fig, ax = plt.subplots(figsize=(5, 5), frameon=True)
        ax.axis('on')
        # x- and y-coordinates
        ax.set_aspect('equal')
        ax.scatter(p[:,0], p[:,1], s=marker_sizes,
                   marker=marker, c=colour_vals, cmap=colour_map,
                   alpha=0.5, edgecolors='none')
        
        fig.savefig(mds_output_filename + ".png", bbox_inches='tight')
        fig.savefig(mds_output_filename + ".pdf", bbox_inches='tight')