예제 #1
0
def operator_difference_and_compound_experiment():
    """Take the 6 permutation operators in pairs, and for each calculate
    the GINI/stddev/etc of the compound operator, and also the
    difference between the pair.

    """
    
    opss = ("two_opt", "twoh_opt", "three_opt", "three_opt_broad", "swap_two", "swap_adj")
    for n in range(6, 11):
        diff = np.zeros((len(opss), len(opss)))
        Gini = np.zeros((len(opss), len(opss)))
        SD = np.zeros((len(opss), len(opss)))
        CV = np.zeros((len(opss), len(opss)))
        for i, opi in enumerate(opss):
            for j, opj in enumerate(opss):
                basedir = sys.argv[1] + "/tsp_length_%d_" % n
                ops = [opi, opj]
                ps = [np.genfromtxt(basedir + op + "/TP_row0.dat") for op in ops]
                names = "+".join(ops)
                delta = random_walks.operator_difference_RMSE(*ps)
                print "delta", names, delta
                diff[i][j] = delta

                wts = [1.0 / len(ops) for _ in ops]
                compound_tp = random_walks.compound_operator(wts, ps)
                gini = random_walks.gini_coeff(compound_tp)
                sd = np.std(compound_tp)
                cv = np.std(compound_tp) / np.mean(compound_tp)
                print "compound gini", names, gini
                Gini[i][j] = gini
                print "compound stddev", names, sd
                SD[i][j] = sd
                print "compound coefvar", names, cv
                CV[i][j] = cv
                
        basedir = sys.argv[1] + "/permutation_size_%d_compound_diff/" % n
        
        filename = os.path.join(basedir + "diff")
        np.savetxt(filename+".dat", diff)
        plot_grid(filename, ("Operator difference %d" % n), opss, diff)
        
        filename = os.path.join(basedir + "diff_mds")
        op_diff_mds(filename, ("Operator difference %d" % n), opss, diff)

        filename = os.path.join(basedir + "compound_gini")
        np.savetxt(filename+".dat", Gini)
        plot_grid(filename, ("Compound operator Gini %d" % n), opss, Gini)

        filename = os.path.join(basedir + "compound_sd")
        np.savetxt(filename+".dat", SD)
        plot_grid(filename, ("Compound operator SD %d" % n), opss, SD)

        filename = os.path.join(basedir + "compound_cv")
        np.savetxt(filename+".dat", CV)
        plot_grid(filename, ("Compound operator CV %d" % n), opss, CV)
예제 #2
0
def operator_difference_and_compound_experiment():
    """Take the 6 operators in pairs, and for each calculate the
    GINI/stddev of the compound operator, and also the difference
    between the pair.

    """
    
    opss = ("two_opt", "twoh_opt", "three_opt", "three_opt_broad", "swap", "swap_adj")
    for n in range(6, 11):
        diff_result = np.zeros((len(opss), len(opss)))
        comp_result = np.zeros((len(opss), len(opss)))
        comp_result_stddev = np.zeros((len(opss), len(opss)))
        for i, opi in enumerate(opss):
            for j, opj in enumerate(opss):
                basedir = "/Users/jmmcd/Dropbox/GPDistance/results/tsp_length_%d_" % n
                ops = [opi, opj]
                ps = [np.genfromtxt(basedir + op + "/TP_row0.dat") for op in ops]
                names = "+".join(ops)
                delta = random_walks.operator_difference_RMSE(*ps)
                print "delta", names, delta
                diff_result[i][j] = delta

                wts = [1.0 / len(ops) for _ in ops]
                compound_tp = random_walks.compound_operator(wts, ps)
                gini = random_walks.gini_coeff(compound_tp)
                stddev = np.std(compound_tp)
                print "compound gini", names, gini
                comp_result[i][j] = gini
                print "compound stddev", names, stddev
                comp_result_stddev[i][j] = stddev
                
        op_diff_filename = "/Users/jmmcd/Dropbox/GPDistance/results/EURO2015/tsp_length_%d_op_diff" % n
        np.savetxt(op_diff_filename+".dat", diff_result)
        plot_grid(op_diff_filename, ("Operator difference %d" % n), opss, diff_result)
        
        comp_op_filename = "/Users/jmmcd/Dropbox/GPDistance/results/EURO2015/tsp_length_%d_comp_op" % n
        np.savetxt(comp_op_filename+".dat", comp_result)
        plot_grid(comp_op_filename, ("Compound operator Gini %d" % n), opss, comp_result)

        comp_op_stddev_filename = "/Users/jmmcd/Dropbox/GPDistance/results/EURO2015/tsp_length_%d_comp_op_stddev" % n
        np.savetxt(comp_op_stddev_filename+".dat", comp_result_stddev)
        plot_grid(comp_op_stddev_filename, ("Compound operator stddev %d" % n), opss, comp_result_stddev)

        mds_filename = "/Users/jmmcd/Dropbox/GPDistance/results/EURO2015/tsp_length_%d_mds" % n
        op_diff_mds(mds_filename, ("Operator difference %d" % n), opss, diff_result)
예제 #3
0
def basic_stats_and_plots():
    """For several TSP operators, read in their first rows and calculate
    the basic stats (Gini and stddev) and nneighbours, and make the basic
    plots of these."""
    
    basename = sys.argv[1]
    ops = ("two_opt", "twoh_opt", "three_opt", "three_opt_broad", "swap", "swap_adj")
    opfs = {
        "two_opt": tsp.two_opt,
        "twoh_opt": tsp.twoh_opt,
        "three_opt": tsp.three_opt,
        "three_opt_broad": tsp.three_opt_broad,
        "swap": tsp.swap_two,
        "swap_adj": tsp.swap_adj
        }
    
    lengths = range(6, 11)
    for length in lengths:
        stddev = []
        gini = []
        nneighbours = []
        prop_unique = []
        for op in ops:
            filename = os.path.join(basename,
                                    "tsp_length_%d_%s" % (length, op),
                                    "TP_row0.dat")
            print op, length
            x = np.genfromtxt(filename)
            # stats to get:
            stddev.append(np.std(x))
            gini.append(random_walks.gini_coeff(x))
            nneighbours.append(np.sum(x > 0))
            mu, sigma = rw_experiment_with_op(length, opfs[op])
            prop_unique.append((mu, sigma))

        gini_barchart(length, gini, ops)
        stddev_barchart(length, stddev, ops)
        plot_gini_v_nneighbours(length, gini, nneighbours, ops)
        plot_stddev_v_nneighbours(length, stddev, nneighbours, ops)
        plot_gini_v_prop_unique(length, gini, prop_unique, ops)
        plot_stddev_v_prop_unique(length, stddev, prop_unique, ops)
예제 #4
0
def basic_stats_and_plots(space, do_rw_experiment=False):
    """For several operators, read in their first rows and calculate the
    basic stats (SD, CV, logN SD, sqrtN SD, cuberootN SD, Gini) and nneighbours, and
    make the basic plots of these.

    """

    basename = sys.argv[1]

    if space == "permutation":
        ops = ("two_opt", "twoh_opt", "three_opt", "three_opt_broad", "swap_two", "swap_adj")
        opfs = {
            "two_opt": tsp.two_opt,
            "twoh_opt": tsp.twoh_opt,
            "three_opt": tsp.three_opt,
            "three_opt_broad": tsp.three_opt_broad,
            "swap_two": tsp.swap_two,
            "swap_adj": tsp.swap_adj
            }
        sizes = (9, 10, 11)
        
    elif space == "bitstring":
        ops = ("per_gene_0.0001", "per_gene_0.0003",
               "per_gene_0.0010", "per_gene_0.0033",
               "per_gene_0.0100", "per_gene_0.0333",
               "per_gene_0.1000", "per_gene_0.3333",
               "per_ind")
        opfs = {
            "per_ind": bitstring.bitstring_per_ind_mutation,
            "per_gene_0.0001": bitstring.make_bitstring_per_gene_mutation(0.0001),
            "per_gene_0.0003": bitstring.make_bitstring_per_gene_mutation(0.0003),
            "per_gene_0.0010": bitstring.make_bitstring_per_gene_mutation(0.0010),
            "per_gene_0.0033": bitstring.make_bitstring_per_gene_mutation(0.0033),
            "per_gene_0.0100": bitstring.make_bitstring_per_gene_mutation(0.0100),
            "per_gene_0.0333": bitstring.make_bitstring_per_gene_mutation(0.0333),
            "per_gene_0.1000": bitstring.make_bitstring_per_gene_mutation(0.1000),
            "per_gene_0.3333": bitstring.make_bitstring_per_gene_mutation(0.3333),
            }
        sizes = (10, 12, 14)
    elif space == "tree" and do_rw_experiment:
        basic_stats_and_plots_tree()
        return

    for size in sizes:
        prop_unique_v_expl = []
        stddev = []
        coefvar = []
        logN_sd_vals = []
        sqrtN_sd_vals = []
        cbrtN_sd_vals = []
        gini = []
        nneighbours = []
        prop_unique = []
        for op in ops:
            filename = os.path.join(basename,
                                    "space_%s/size_%d_op_%s" % (space, size, op),
                                    "TP_row0.dat")
            print op, size
            x = np.genfromtxt(filename)
            N = len(x)
            # stats to get:
            sd = np.std(x)
            stddev.append(sd)
            cv = np.std(x) * N
            coefvar.append(cv)
            logN_sd = np.std(x) * np.log(N)
            logN_sd_vals.append(logN_sd)
            sqrtN_sd = np.std(x) * (N**0.5)
            sqrtN_sd_vals.append(sqrtN_sd)
            cbrtN_sd = np.std(x) * (N**(1.0/3))
            cbrtN_sd_vals.append(cbrtN_sd)
            g = random_walks.gini_coeff(x)
            gini.append(g)
            nneighbours.append(np.sum(x > 0))
            if do_rw_experiment:
                try:
                    # load the results from a previous rw run
                    results = np.genfromtxt(basename + "/space_%s/rw_results_size_%d_op_%s.dat" % (space, size, op))
                except:
                    # but if there *was* no previous run, do it now and save
                    results = rw_experiment_with_op(basename, space, size, opfs[op])
                    np.savetxt(basename + "/space_%s/rw_results_size_%d_op_%s.dat" % (space, size, op), results)

                mu, sigma = np.mean(results), np.std(results)
    
                prop_unique.append((mu, sigma))
                prop_unique_v_expl.append("%s %d %s %f %f %f %f %f %f %f %f" % (
                    space, size, op, sd, cv, logN_sd, sqrtN_sd, cbrtN_sd, g, mu, sigma))

        basename = sys.argv[1]

        # barcharts
        barchart(basename, space, size, "Gini", gini, ops)
        barchart(basename, space, size, "SD", stddev, ops)
        barchart(basename, space, size, "CV", coefvar, ops)
        barchart(basename, space, size, "log(N) SD", logN_sd_vals, ops)
        barchart(basename, space, size, "sqrt(N) SD", sqrtN_sd_vals, ops)
        barchart(basename, space, size, "cbrt(N) SD", cbrtN_sd_vals, ops)

        # scatterplots v nneighbours
        scatterplot(basename, space, size, "Gini", "# Neighbours", gini, nneighbours, ops)
        scatterplot(basename, space, size, "SD", "# Neighbours", stddev, nneighbours, ops)
        scatterplot(basename, space, size, "CV", "# Neighbours", coefvar, nneighbours, ops)
        scatterplot(basename, space, size, "log(N) SD", "# Neighbours", logN_sd_vals, nneighbours, ops)
        scatterplot(basename, space, size, "sqrt(N) SD", "# Neighbours", sqrtN_sd_vals, nneighbours, ops)
        scatterplot(basename, space, size, "cbrt(N) SD", "# Neighbours", cbrtN_sd_vals, nneighbours, ops)
        
        if do_rw_experiment:
            # scatterplots v prop unique
            prop_unique = np.array(prop_unique)
            prop_unique[:,0] = 1.0 - prop_unique[:,0]
            scatterplot(basename, space, size, "Gini", "1 - prop. unique", gini, prop_unique, ops)
            scatterplot(basename, space, size, "SD", "1 - prop. unique", stddev, prop_unique, ops)
            scatterplot(basename, space, size, "CV", "1 - prop. unique", coefvar, prop_unique, ops)
            scatterplot(basename, space, size, "log(N) SD", "1 - prop. unique", logN_sd_vals, prop_unique, ops)
            scatterplot(basename, space, size, "sqrt(N) SD", "1 - prop. unique", sqrtN_sd_vals, prop_unique, ops)
            scatterplot(basename, space, size, "cbrt(N) SD", "1 - prop. unique", cbrtN_sd_vals, prop_unique, ops)

            
            filename = os.path.join(basename, "space_%s/size_%d_prop_unique_v_expl.dat" % (space, size))
            open(filename, "w").write("\n".join(prop_unique_v_expl))