示例#1
0
def plot_it(length, ones_range_min, ones_range_max, reps, numStrings,
            attempts):
    ones, match_avg, match_std_dev, color_avg, color_std_dev = experiment(
        length, ones_range_min, ones_range_max, reps, numStrings, attempts)

    q = [compute_q(length, x) for x in ones]

    plt.errorbar(np.asarray(q),
                 np.asarray(match_avg),
                 np.asarray(match_std_dev),
                 markersize=3,
                 lw=1,
                 fmt='-o')
    plt.errorbar(np.asarray(q),
                 np.asarray(color_avg),
                 np.asarray(color_std_dev),
                 markersize=3,
                 lw=1,
                 fmt='-o')
    plt.ylim([0, 60])
    #    plt.xlim([0,.2])
    plt.ylabel('Percentage of pages freed')
    plt.xlabel('q (probability of 2 pages meshing)')
    blue_patch = mpatches.Patch(color='blue', label='random matching')
    green_patch = mpatches.Patch(color='green', label='greedy matching')
    plt.legend(handles=[blue_patch, green_patch], bbox_to_anchor=(1, .3))
    plt.title(
        'RANDOM MATCHING VS GREEDY MATCHING MESHING RESULTS \n{}-object pages, {} pages, n*log(n) attempts'
        .format(length, numStrings))
    plt.show()
    #    plt.savefig('randvgreedy{},{},n^1.5_attempts'.format(length, numStrings) + '.png', dpi = 1000)
    plt.close()
示例#2
0
def compute_isolated_edge_bound(length, numOnes, numStrings):
    q = compute_q(length, numOnes)
    p3 = compute_p3(length, numOnes)
    m = numStrings
    bound1 = (m - 1) * q * (1 - (2 * q) + p3)**(m - 2)
    bound2 = 2 - 2 * (1 - q)**(m - 1) - (m - 1) * q
    return (m / 2) * max(bound1, bound2)
示例#3
0
def compute_degree_bound(length, numOnes, numStrings):
    print length
    print numOnes
    q = compute_q(length, numOnes)
    exp_degree = (numStrings - 1) * q
    a = exp_degree
    b = exp_degree
    return numStrings / 2 * (a / (b + 1))
示例#4
0
def compute_improved_degreeplusone_bound(length, numOnes, numStrings):
    bound, bound_dict = lookup("impdeg+1", length, numOnes, numStrings)
    if bound:
        print 'value already exists, retrieving from database'
        return bound
    q = compute_q(length, numOnes)
    p3 = compute_p3(length, numOnes)
    p1 = q - p3
    p2 = p1

    p4 = 1 - p1 - p2 - p3

    sum = 0
    for a in range(numStrings - 2 + 1):
        for b in range(numStrings - 2 - a + 1):
            for c in range(numStrings - 2 - a - b + 1):
                if a + c + 1 == 1 and b + c + 1 == 1:
                    add = 1 * prob(a, b, c, numStrings, numOnes, p1, p2, p3)
                elif a + c + 1 == 1:
                    add = (1 / (b + c + 1)) * prob(a, b, c, numStrings,
                                                   numOnes, p1, p2, p3)
                elif b + c + 1 == 1:
                    add = (1 / (a + c + 1)) * prob(a, b, c, numStrings,
                                                   numOnes, p1, p2, p3)
#                elif a+c+1==2 and b+c+1==2:
##                    add = .5*prob(a,b,c,numStrings, numOnes,p1,p2,p3)
                elif (a + c + 1 == 2
                      and b + c + 1 == 3) or (a + c + 1 == 3
                                              and b + c + 1 == 2):
                    add = prob(a, b, c, numStrings, numOnes, p1, p2, p3) / 3.0
#                elif a+c+1 != b+c+1:
#                    add = min(1/(a+c+1), 1/(b+c+1))*prob(a,b,c,numStrings, numOnes,p1,p2,p3)
                else:
                    add = min(1 / (a + c + 2), 1 / (b + c + 2)) * prob(
                        a, b, c, numStrings, numOnes, p1, p2, p3)


#                add = .5*(1/(a+c+2) + 1/(b+c+2))*prob(a,b,c,numStrings, numOnes,p1,p2,p3)
                sum += add
    sum *= q
    result = sum * nCr(numStrings, 2)
    bound_dict[(length, numOnes, numStrings)] = result
    store("impdeg+1", bound_dict)
    print 'value did not already exist, writing to database'
    return result
示例#5
0
def compute_degreeplusone_bound(length, numOnes, numStrings):
    q = compute_q(length, numOnes)
    p3 = compute_p3(length, numOnes)
    p1 = q - p3
    p2 = p1

    p4 = 1 - p1 - p2 - p3

    sum = 0
    for a in range(numStrings - 2 + 1):
        for b in range(numStrings - 2 - a + 1):
            for c in range(numStrings - 2 - a - b + 1):
                add = min(1 / (a + c + 2), 1 / (b + c + 2)) * prob(
                    a, b, c, numStrings, numOnes, p1, p2, p3)
                #                add = .5*(1/(a+c+2) + 1/(b+c+2))*prob(a,b,c,numStrings, numOnes,p1,p2,p3)
                sum += add
    sum *= q
    return sum * nCr(numStrings, 2)
示例#6
0
def plot_it(length, ones_range_min, ones_range_max, reps, numStrings):

    ones, avg, stddev = experiment(length, ones_range_min, ones_range_max,
                                   reps, numStrings)

    q = [compute_q(length, x) for x in ones]

    plt.errorbar(np.asarray(q),
                 np.asarray(avg),
                 np.asarray(stddev),
                 markersize=3,
                 lw=1,
                 fmt='-o')
    plt.ylim([0, 60])
    plt.ylabel('Percentage of pages freed')
    plt.xlabel('q (probability of 2 pages meshing)')
    plt.title('GREEDY FIRST-MATCH MESHING RESULTS \n{}-object pages, {} pages'.
              format(length, numStrings))
    plt.show()
示例#7
0
def compute_exp_Y(length, numOnes, numStrings):
    q = compute_q(length, numOnes)
    p3 = compute_p3(length, numOnes)
    p1 = q - p3
    p2 = p1

    p4 = 1 - p1 - p2 - p3

    sum = 0
    for a in range(numStrings - 2 + 1):
        for b in range(numStrings - 2 - a + 1):
            for c in range(numStrings - 2 - a - b + 1):
                add = min(1 / (a + c + 1), 1 / (b + c + 1)) * prob(
                    a, b, c, numStrings, numOnes, p1, p2, p3)
                #                add = min(1/(a+c), 1/(b+c))*prob(a,b,c,numStrings, numOnes,p1,p2,p3)
                sum += add


#                sum += min(1/(a+c+1), 1/(b+c+1))*prob(a,b,c,numStrings,p1,p2,p3)
    sum *= q
    return sum * nCr(numStrings, 2)
示例#8
0
def experiment(length, ones_range_min, ones_range_max, reps, numStrings):
    constant_strings = []
    independent_strings = []
    ones = []
    constant_occupancy_avg = []
    constant_occupancy_std_dev = []
    indep_occupancy_avg = []
    indep_occupancy_std_dev = []
    qs = []
    constant_edges = []
    independent_edges = []
    
    for numOnes in range(ones_range_min, ones_range_max+1):
        ones.append(numOnes)
        q = compute_q(length, numOnes)
        qs.append(q)
        freed_pages_constant = []
        freed_pages_independent = []
        const_edges = []
        indep_edges = []
        for iterations in range (reps):
            for i in range(numStrings):
               constant_strings.append(createRandomString(length, numOnes))
               
            graph = makeGraph(constant_strings)
            const_edges.append(graph.number_of_edges())
            frdpgs_constant = len(nx.max_weight_matching(graph))/2
            perc = (frdpgs_constant/numStrings)*100
            freed_pages_constant.append(perc)
            
#include only q or numOnes by name to choose which version of indep random strings you want            
#            independent_strings = createIndependentRandomStrings(length, numStrings, q, numOnes)
            independent_strings = createIndependentRandomStrings(length, numStrings, numOnes = numOnes)
            graph = makeGraph(independent_strings)
            indep_edges.append(graph.number_of_edges())
            frdpgs_indep = len(nx.max_weight_matching(graph))/2
            perc = (frdpgs_indep/numStrings)*100
            freed_pages_independent.append(perc)
        
            
            constant_strings = []
            independent_strings = []
            
        
        m = np.asarray(freed_pages_constant)
#        raw.append(freed_pages_constant)
        m_a = np.mean(m)
        constant_occupancy_avg.append(m_a)
        m_s = np.std(m)
        constant_occupancy_std_dev.append(m_s)
        
        m = np.asarray(const_edges)
        constant_edges.append(np.mean(m))
        
        m = np.asarray(freed_pages_independent)
#        raw.append(freed_pages_constant)
        m_a = np.mean(m)
        indep_occupancy_avg.append(m_a)
        m_s = np.std(m)
        indep_occupancy_std_dev.append(m_s)
        
        m = np.asarray(indep_edges)
        independent_edges.append(np.mean(m))
        
    return ones, constant_occupancy_avg, constant_occupancy_std_dev, indep_occupancy_avg, indep_occupancy_std_dev, constant_edges, independent_edges, qs
示例#9
0
def experiment(length, ones_range_min, ones_range_max, reps, numStrings):
    strings = []
    ones = []
    maxmatch_avg = []
    maxmatch_std_dev = []
    #    greedymatch_avg = []
    #    greedymatch_std_dev = []
    y_estimate = []
    justy = []
    raw = []
    qs = []

    for numOnes in range(ones_range_min, ones_range_max + 1):
        ones.append(numOnes)
        q = compute_q(length, numOnes)
        qs.append(q * 100)
        #        qs.append(compute_q(length, numOnes)*100)
        freed_pages_maxmatching = []
        freed_pages_greedymatching = []
        for iterations in range(reps):
            for i in range(numStrings):
                strings.append(createRandomString(length, numOnes))
#            strings = createIndependentRandomStrings(length = length, numStrings = numStrings, q = q)

            graph = makeGraph(strings)
            frdpgs_maxmatching = len(nx.max_weight_matching(graph)) / 2
            perc = (frdpgs_maxmatching / numStrings) * 100
            freed_pages_maxmatching.append(perc)

            strings = []

        m = np.asarray(freed_pages_maxmatching)
        raw.append(freed_pages_maxmatching)
        m_a = np.mean(m)
        maxmatch_avg.append(m_a)
        m_s = np.std(m)
        maxmatch_std_dev.append(m_s)

        #        y = compute_exp_Y(length, numOnes, numStrings)
        #        y = compute_degreeplusone_bound(length, numOnes, numStrings)
        y = compute_improved_degreeplusone_bound(length, numOnes, numStrings)

        #        y_est_raw = max(y,compute_degree_bound(length, numOnes, numStrings),compute_isolated_edge_bound(length, numOnes, numStrings))
        #        y_est_raw = compute_isolated_edge_bound(length, numOnes, numStrings)
        #        y_est_raw = compute_degree_bound(length, numOnes, numStrings)
        #        y_est_raw = y
        yperc = (math.floor(y) / numStrings) * 100
        y_estimate.append(yperc)


#    mistakes = {}
#    for i in range(len(raw)):
#        oops = []
#        for entry in raw[i]:
#            if entry < y_estimate[i]:
#                oops.append(entry)
#        mistakes[i+1] = oops
#    print 'mistakes:'
#    print mistakes

#    use this version of mistakes
    mistakes = {}
    for i in range(len(raw)):
        oops = 0
        for entry in raw[i]:
            if entry < y_estimate[i]:
                oops += 1
        mistakes[i + 1] = oops
    print 'mistakes:'
    print mistakes

    print 'E[Y]:'
    ey = {}
    for i in range(len(y_estimate)):
        ey[i + 1] = y_estimate[i]
    print ey
    #

    #        yperc = (y/numStrings)*100
    #        justy.append(yperc)

    #        c = np.asarray(freed_pages_greedymatching)
    #        c_a = np.mean(c)
    #        greedymatch_avg.append(c_a)
    #        c_s = np.std(c)
    #        greedymatch_std_dev.append(c_s)

    return ones, maxmatch_avg, maxmatch_std_dev, y_estimate, justy, qs