From ca19b8a5a4f26c9c9d68b19fe381e48ba945632d Mon Sep 17 00:00:00 2001 From: Maria Hartmann <lm.hartmann@gmx.de> Date: Fri, 12 Feb 2021 20:40:02 +0100 Subject: [PATCH] code cleanup --- scripts/paper-dijkstra-test.py | 61 +++++++++----------------------- scripts/paper-dijkstra-test2.py | 60 ++++++++++--------------------- scripts/paper-sorting-loc.py | 16 ++------- scripts/paper-sorting-sep.py | 5 +-- scripts/paper-sorting-subseq.py | 7 +--- scripts/paper-sorting-uniform.py | 11 ++---- 6 files changed, 43 insertions(+), 117 deletions(-) diff --git a/scripts/paper-dijkstra-test.py b/scripts/paper-dijkstra-test.py index a467324..aeaeada 100755 --- a/scripts/paper-dijkstra-test.py +++ b/scripts/paper-dijkstra-test.py @@ -25,47 +25,25 @@ COUNT_TYPE_COMPS = -2 TYPES = {21: "Pairing", 22: "Smooth"} MAX_TYPE_KEY = max(TYPES.keys()) -#COLOURS = ['xkcd:fire engine red', 'xkcd:dusty orange', 'xkcd:clear blue', 'xkcd:cool green', -# 'xkcd:macaroni and cheese', 'xkcd:fire engine red', 'xkcd:dusty orange', 'xkcd:clear blue', -# 'xkcd:cool green', 'xkcd:macaroni and cheese', 'xkcd:bright sky blue', 'xkcd:bright sky blue', 'xkcd:green', -# 'xkcd:ochre', 'xkcd:sea blue', 'xkcd:sea green', 'xkcd:sea blue', 'xkcd:warm grey', -# 'xkcd:bright sky blue', 'xkcd:bright sky blue'] -LINETYPES = 5 * ['-'] + 5 * ['--'] + ['--', '-'] + ['--', '--', '--', '--', '--', '--', '-', '-'] FIG_LABELS = ["comparisons", "links"] -MAX_TYPE_KEY = max(TYPES.keys()) COLOURS = {21:'xkcd:fire engine red', 22:'xkcd:sea green'} -SHADE_COLOURS = {21:'#fe4d4e', 22:'#58ab8e'} +SHADE_COLOURS = {21: '#fe4d4e', 22: '#58ab8e'} NUMBER_TESTS = 10 # number of tests to run -TEST_SIZE = 500 # ,6000,7000,8000,9000,10000,20000,30000,40000,50000,60000,70000,80000,90000,100000 +TEST_SIZE = 500 EDGE_PROBABILITY = 0.05 WEIGHT_RANGE = 10000 -def plot_avg_counts_old(avgCounts): - # colours from https://xkcd.com/color/rgb/ - linetypes = 5 * ["-"] + 5 * ["--"] + ["--", "-"] + ["-, -"] - plt.figure('Dijkstra with variable connectivity') - for k in TYPES.keys(): - avgComps = [acounts[k] for acounts in avgCounts[0]] - plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgComps, color=COLOURS[k], linestyle='-', - label=TYPES[k] + " comparisons") - avgLinks = [acounts[k] for acounts in avgCounts[1]] - plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgLinks, color=COLOURS[k], linestyle='--', - label=TYPES[k] + " links") - plt.grid(True) - plt.legend(loc='best') - plt.show() - def plot_avg_counts(avgCounts): + """generates and saves plot of results""" # colours from https://xkcd.com/color/rgb/ MARKERS_COMP = {21:"o", 12:"d", 22:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html MARKERS_LINK = {21:"o", 12:"D", 22:"D"} plt.figure('avg number of operations in Dijkstra\'s algorithm') deviations = [factor * EDGE_PROBABILITY for factor in range(1, 21, 1)] for k in TYPES.keys(): - #print(k) avgComps = [acounts[k] for acounts in avgCounts[0]] maxComps = [acounts[k] for acounts in avgCounts[2]] minComps = [acounts[k] for acounts in avgCounts[4]] @@ -77,15 +55,13 @@ def plot_avg_counts(avgCounts): plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) - plt.xlabel('Edge probability', fontsize=26) plt.ylabel('Avg. number of operations / size', fontsize=26) plt.xticks(fontsize=20) plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points + plt.rc('legend',fontsize=26) # using a size in points plt.legend() plt.grid(True) - #plt.gca().invert_xaxis() figure = plt.gcf() # get current figure figure.set_size_inches(16, 18) # set figure's size manually to full screen plt.savefig('../plots/paper-dijkstra-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces @@ -143,26 +119,25 @@ if __name__ == "__main__": for _ in range(NUMBER_TESTS): # some nice graph generators here: https://networkx.github.io/documentation/stable/reference/generators.html + # initialize input graph graph = nx.fast_gnp_random_graph(TEST_SIZE, x) - # graph = nx.random_regular_graph(10, 1000) - for (u, v) in graph.edges(): + for (u, v) in graph.edges(): # assign weights graph.edges[u, v]['w'] = random.randint(1, WEIGHT_RANGE) for heapType in TYPES.keys(): for v in graph.nodes(): - graph.nodes[v]['v'] = False # "visited" marker + graph.nodes[v]['v'] = False # "visited" marker linkCount = 0 compCount = 0 - vertex2qnode = {} + vertex2qnode = {} # mapping graph nodes to heap nodes dist = [888888888 for _ in range(len(graph.nodes()))] prev = [None for _ in range(len(graph.nodes()))] heap = PairingHeap(heapType, COUNT_TYPE_BOTH) heap.make_heap() - source = graph.nodes()[0] - dist[0] = 0 + # Dijkstra's algorithm + dist[0] = 0 # start node for idx, v in enumerate(graph.nodes()): - #qnode = Node(dist[idx]) qnode = Node(dist[v]) qnode.vertex = v vertex2qnode[v] = qnode @@ -180,24 +155,22 @@ if __name__ == "__main__": s)) u = minNode.vertex uk = minNode.key - #print(uk) - #print('extracted {}'.format(minNode.key)) - graph.nodes[u]['v'] = True # minNode has been visited + graph.nodes[u]['v'] = True # minNode has been visited for idx, v in enumerate(graph.neighbors(u)): alt = uk + graph.edges[u, v]['w'] if alt < dist[v] and not graph.nodes[v]['v']: (cc, lc) = heap.decrease_key(vertex2qnode[v], dist[v] - alt) - #print('decreased from {} to {}'.format(dist[v],alt)) linkCount += lc compCount += cc dist[v] = alt prev[v] = u + # track avg. results avgCountsLinks[heapType] += (linkCount / NUMBER_TESTS)/TEST_SIZE avgCountsComps[heapType] += (compCount / NUMBER_TESTS)/TEST_SIZE - maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/TEST_SIZE) - maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/TEST_SIZE) - minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/TEST_SIZE) - minCountsComps[heapType] = min(minCountsComps[heapType],compCount/TEST_SIZE) + maxCountsLinks[heapType] = max(maxCountsLinks[heapType], linkCount/TEST_SIZE) + maxCountsComps[heapType] = max(maxCountsComps[heapType], compCount/TEST_SIZE) + minCountsLinks[heapType] = min(minCountsLinks[heapType], linkCount/TEST_SIZE) + minCountsComps[heapType] = min(minCountsComps[heapType], compCount/TEST_SIZE) for heapType in TYPES.keys(): pid = os.getpid() @@ -212,6 +185,6 @@ if __name__ == "__main__": maxCompsPerSize += [maxCountsComps] minLinksPerSize += [minCountsLinks] minCompsPerSize += [minCountsComps] - # plot_avg_counts([avgCompsPerSize, avgLinksPerSize]) + plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) export_results(xs, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "dijkstra") diff --git a/scripts/paper-dijkstra-test2.py b/scripts/paper-dijkstra-test2.py index 532e1b7..20793e1 100755 --- a/scripts/paper-dijkstra-test2.py +++ b/scripts/paper-dijkstra-test2.py @@ -4,7 +4,11 @@ as priority queue in Dijkstra's algorithm. Algorithm is run on randomly generate 10-regular graphs of variable size. Results are stored as .csv files in ../data folder and plots of results in ../plots""" - +import networkx as nx +import random +import matplotlib.pyplot as plt +import psutil +import csv import os, sys, inspect # ensuring imports from parent directory work current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) @@ -12,11 +16,6 @@ parent_dir = os.path.dirname(current_dir) sys.path.insert(0, parent_dir) from node import Node from pairing_heap import PairingHeap -import networkx as nx -import random -import matplotlib.pyplot as plt -import psutil -import csv COUNT_TYPE_BOTH = 0 COUNT_TYPE_LINKS = -1 @@ -24,13 +23,11 @@ COUNT_TYPE_COMPS = -2 TYPES = {21: "Pairing", 22: "Smooth"} MAX_TYPE_KEY = max(TYPES.keys()) -LINETYPES = 5 * ['-'] + 5 * ['--'] + ['--', '-'] + ['--', '--', '--', '--', '--', '--', '-', '-'] FIG_LABELS = ["comparisons", "links"] -MAX_TYPE_KEY = max(TYPES.keys()) # colours from https://xkcd.com/color/rgb/ COLOURS = {21:'xkcd:fire engine red', 22:'xkcd:sea green'} -SHADE_COLOURS = {21:'#fe4d4e', 22:'#58ab8e'} +SHADE_COLOURS = {21: '#fe4d4e', 22: '#58ab8e'} NUMBER_TESTS = 10 # number of tests to run TEST_SIZE = 500 @@ -38,26 +35,12 @@ EDGE_PROBABILITY = 0.05 WEIGHT_RANGE = 10000 -def plot_avg_counts_old(avgCounts): - plt.figure('Dijkstra with variable connectivity') - for k in TYPES.keys(): - avgComps = [acounts[k] for acounts in avgCounts[0]] - plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgComps, color=COLOURS[k], linestyle='-', - label=TYPES[k] + " comparisons") - avgLinks = [acounts[k] for acounts in avgCounts[1]] - plt.plot([factor * EDGE_PROBABILITY for factor in range(1, 101, 1)], avgLinks, color=COLOURS[k], linestyle='--', - label=TYPES[k] + " links") - plt.grid(True) - plt.legend(loc='best') - plt.show() - def plot_avg_counts(avgCounts): MARKERS_COMP = {21:"o", 12:"d", 22:"^"}#https://matplotlib.org/3.1.1/api/markers_api.html MARKERS_LINK = {21:"o", 12:"D", 22:"D"} plt.figure('avg number of operations in Dijkstra\'s algorithm') deviations = [ 10+round(TEST_SIZE*20*factor * EDGE_PROBABILITY) for factor in range(1, 21, 1)] for k in TYPES.keys(): - #print(k) avgComps = [acounts[k] for acounts in avgCounts[0]] maxComps = [acounts[k] for acounts in avgCounts[2]] minComps = [acounts[k] for acounts in avgCounts[4]] @@ -73,11 +56,9 @@ def plot_avg_counts(avgCounts): plt.ylabel('Avg. number of operations / size', fontsize=26) plt.xticks(fontsize=20) plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points - #plt.gca().invert_xaxis() + plt.rc('legend', fontsize=26) # using a size in points plt.legend() plt.grid(True) - #plt.gca().invert_xaxis() figure = plt.gcf() # get current figure figure.set_size_inches(16, 18) # set figure's size manually to full screen plt.savefig('../plots/paper-dijkstra2-new.svg', bbox_inches='tight') # bbox_inches removes extra white spaces @@ -135,15 +116,13 @@ if __name__ == "__main__": for _ in range(NUMBER_TESTS): # some nice graph generators here: https://networkx.github.io/documentation/stable/reference/generators.html - ##graph = nx.fast_gnp_random_graph(100, 0.1) + # initializing input graph = nx.random_regular_graph(10,10+round(TEST_SIZE*x*20)) - ##graph = nx.random_regular_graph(10,100) for (u, v) in graph.edges(): graph.edges[u, v]['w'] = random.randint(1, WEIGHT_RANGE) for heapType in TYPES.keys(): - #print('---') for v in graph.nodes(): - graph.nodes[v]['v'] = False # "visited" marker + graph.nodes[v]['v'] = False # "visited" marker linkCount = 0 compCount = 0 vertex2qnode = {} @@ -153,15 +132,13 @@ if __name__ == "__main__": heap = PairingHeap(heapType, COUNT_TYPE_BOTH) heap.make_heap() - source = graph.nodes()[0] + # Dijkstra's algorithm dist[0] = 0 for idx, v in enumerate(graph.nodes()): -# qnode = Node(dist[idx]) qnode = Node(dist[v]) qnode.vertex = v vertex2qnode[v] = qnode (cc, lc) = heap.insert(qnode) - #print('inserted key {}',qnode.key) linkCount += lc compCount += cc @@ -175,25 +152,24 @@ if __name__ == "__main__": s)) u = minNode.vertex uk = minNode.key - #print(uk) - #print('extracted {}'.format(minNode.key)) - graph.nodes[u]['v'] = True # minNode has been visited + graph.nodes[u]['v'] = True # minNode has been visited for idx, v in enumerate(graph.neighbors(u)): alt = uk + graph.edges[u, v]['w'] if alt < dist[v] and not graph.nodes[v]['v']: (cc, lc) = heap.decrease_key(vertex2qnode[v], dist[v] - alt) - #print('decreased from {} to {}'.format(dist[v],alt)) linkCount += lc compCount += cc dist[v] = alt prev[v] = u + + # tracking avg. results TSIZE = 10+round(TEST_SIZE*x*20) avgCountsLinks[heapType] += (linkCount / NUMBER_TESTS)/TSIZE avgCountsComps[heapType] += (compCount / NUMBER_TESTS)/TSIZE - maxCountsLinks[heapType] = max(maxCountsLinks[heapType],linkCount/TSIZE) - maxCountsComps[heapType] = max(maxCountsComps[heapType],compCount/TSIZE) - minCountsLinks[heapType] = min(minCountsLinks[heapType],linkCount/TSIZE) - minCountsComps[heapType] = min(minCountsComps[heapType],compCount/TSIZE) + maxCountsLinks[heapType] = max(maxCountsLinks[heapType], linkCount/TSIZE) + maxCountsComps[heapType] = max(maxCountsComps[heapType], compCount/TSIZE) + minCountsLinks[heapType] = min(minCountsLinks[heapType], linkCount/TSIZE) + minCountsComps[heapType] = min(minCountsComps[heapType], compCount/TSIZE) for heapType in TYPES.keys(): pid = os.getpid() @@ -208,6 +184,6 @@ if __name__ == "__main__": maxCompsPerSize += [maxCountsComps] minLinksPerSize += [minCountsLinks] minCompsPerSize += [minCountsComps] - # plot_avg_counts([avgCompsPerSize, avgLinksPerSize]) + plot_avg_counts([avgCompsPerSize, avgLinksPerSize, maxCompsPerSize, maxLinksPerSize, minCompsPerSize, minLinksPerSize]) export_results(xs, [avgCompsPerSize, avgLinksPerSize], COUNT_TYPE_BOTH, TYPES, "dijkstra") diff --git a/scripts/paper-sorting-loc.py b/scripts/paper-sorting-loc.py index 403323e..d7c5b34 100644 --- a/scripts/paper-sorting-loc.py +++ b/scripts/paper-sorting-loc.py @@ -21,18 +21,12 @@ from pairing_heap import PairingHeap COUNT_TYPE_BOTH = 0 COUNT_TYPE_LINKS = -1 COUNT_TYPE_COMPS = -2 -MAXSIZE = 17 NUMBER_TESTS = 10 # number of tests to run -TEST_SIZES = [j for j in range(MAXSIZE)] -LIST_LEN = 10000 # number of elements in test list -TEST_SIZE = 10000 # number of elements in test list -STEP_SIZE = 100 INCREMENT_LOC = 0.01 -INCREMENT_SUBSEQS = 100 TYPES = {0: "Pairing", 12: "Smooth"} MAX_TYPE_KEY = max(TYPES.keys()) -COLOURS = {0:'xkcd:fire engine red', 12:'xkcd:green'} -SHADE_COLOURS = {0:'#fe4d4e', 12:'#58ab8e'} +COLOURS = {0: 'xkcd:fire engine red', 12: 'xkcd:green'} +SHADE_COLOURS = {0: '#fe4d4e', 12: '#58ab8e'} def isSorted(list0): @@ -47,7 +41,6 @@ def localizedShuffleByIndex(llist, sdev): tuples += [(key, element)] # store element with key sortedTuples = sorted(tuples, key=lambda x: x[0]) # sort key-element tuples by keys sortedList = [tup[1] for tup in sortedTuples] # discard keys - # print(sortedList) return sortedList @@ -59,7 +52,6 @@ def plot_avg_counts(avgCounts): plt.figure('avg number of operations by heap type') deviations = [fac * INCREMENT_LOC for fac in range(0, math.ceil(0.3 / INCREMENT_LOC), 1)] for k in TYPES.keys(): - #print(k) avgComps = [acounts[k] for acounts in avgCounts[0]] maxComps = [acounts[k] for acounts in avgCounts[2]] minComps = [acounts[k] for acounts in avgCounts[4]] @@ -71,12 +63,11 @@ def plot_avg_counts(avgCounts): plt.plot(deviations, avgLinks, color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") plt.fill_between(deviations, minLinks, maxLinks, color=SHADE_COLOURS[k], alpha=.3) - #plt.title('Sorting random separable permutations', fontsize=25) plt.xlabel('Locality parameter', fontsize=26) plt.ylabel('Avg. number of operations / size', fontsize=26) plt.xticks(fontsize=20) plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points + plt.rc('legend', fontsize=26) # using a size in points plt.legend() plt.grid(True) figure = plt.gcf() # get current figure @@ -126,7 +117,6 @@ if __name__ == "__main__": minCompsPerSize = [] sortedInput = [] - #testInput = [] # ----------localised permutation inputs-------------- # randomness parameter: standard deviation diff --git a/scripts/paper-sorting-sep.py b/scripts/paper-sorting-sep.py index 48cab31..533b79c 100644 --- a/scripts/paper-sorting-sep.py +++ b/scripts/paper-sorting-sep.py @@ -57,12 +57,11 @@ def plot_avg_counts(avgCounts): plt.plot([2**p for p in range(4, MAXSIZE)], avgLinks[3:MAXSIZE-1], color=COLOURS[k], linestyle="--", marker=MARKERS_LINK[k], markerfacecolor=COLOURS[k], markersize=9, markeredgewidth=1, markeredgecolor='black', label=TYPES[k] + " links") plt.fill_between([2**p for p in range(4, MAXSIZE)], minLinks[3:MAXSIZE-1], maxLinks[3:MAXSIZE-1], color=SHADE_COLOURS[k], alpha=.3) - #plt.title('Sorting random separable permutations', fontsize=25) plt.xlabel('Input size', fontsize=26) plt.ylabel('Avg. number of operations / size', fontsize=26) plt.xticks(fontsize=20) plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points + plt.rc('legend', fontsize=26) # using a size in points plt.legend() plt.grid(True) figure = plt.gcf() # get current figure @@ -131,7 +130,6 @@ if __name__ == "__main__": minCompsPerSize = [] sortedInput = [] - # testInput = [] # ----------separable permutation--------------------- # parameter: length (must be power of two) @@ -147,7 +145,6 @@ if __name__ == "__main__": minCountsComps = [1000000000000 for _ in range(MAX_TYPE_KEY + 1)] for zz in range(NUMBER_TESTS): - testInput = copy.copy(sortedInput) testInput = separablePermutation(x) testInput[0] = -1 for heapType in TYPES.keys(): diff --git a/scripts/paper-sorting-subseq.py b/scripts/paper-sorting-subseq.py index 2db58de..6c8e24d 100644 --- a/scripts/paper-sorting-subseq.py +++ b/scripts/paper-sorting-subseq.py @@ -23,12 +23,9 @@ from pairing_heap import PairingHeap COUNT_TYPE_BOTH = 0 COUNT_TYPE_LINKS = -1 COUNT_TYPE_COMPS = -2 -MAXSIZE = 17 NUMBER_TESTS = 20 # number of tests to run -TEST_SIZES = [j for j in range(MAXSIZE)] LIST_LEN = 10000 # number of elements in test list TEST_SIZE = 10000 # number of elements in test list -STEP_SIZE = 100 INCREMENT_SUBSEQS = 100 TYPES = {0: "Pairing", 12: "Smooth"} MAX_TYPE_KEY = max(TYPES.keys()) @@ -79,7 +76,7 @@ def plot_avg_counts(avgCounts): plt.ylabel('Avg. number of operations / size', fontsize=26) plt.xticks(fontsize=20) plt.yticks(fontsize=20) - plt.rc('legend',fontsize=26) # using a size in points + plt.rc('legend', fontsize=26) # using a size in points plt.legend() plt.grid(True) plt.gca().invert_xaxis() @@ -129,7 +126,6 @@ if __name__ == "__main__": minCompsPerSize = [] sortedInput = [] - # testInput = [] # ----------continuous sorted subsequences inputs----- # randomness parameter: subsequence lengths params = [fac*INCREMENT_SUBSEQS for fac in range(1, math.ceil((TEST_SIZE/5)/INCREMENT_SUBSEQS), 1)] @@ -146,7 +142,6 @@ if __name__ == "__main__": for zz in range(NUMBER_TESTS): testInput = generateContSortedSubseq(sortedInput, x) - #print(len(testInput)) for heapType in TYPES.keys(): linkCount = 0 compCount = 0 diff --git a/scripts/paper-sorting-uniform.py b/scripts/paper-sorting-uniform.py index 776b869..5e445d0 100644 --- a/scripts/paper-sorting-uniform.py +++ b/scripts/paper-sorting-uniform.py @@ -8,8 +8,6 @@ from random import shuffle import matplotlib.pyplot as plt import copy import csv -from node import Node -from pairing_heap import PairingHeap import os, sys, inspect @@ -17,17 +15,14 @@ import os, sys, inspect current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) parent_dir = os.path.dirname(current_dir) sys.path.insert(0, parent_dir) +from node import Node +from pairing_heap import PairingHeap + COUNT_TYPE_BOTH = 0 COUNT_TYPE_LINKS = -1 COUNT_TYPE_COMPS = -2 MAXSIZE = 18 NUMBER_TESTS = 5 # number of tests to run -TEST_SIZES = [j for j in range(MAXSIZE)] # -LIST_LEN = 10000 # number of elements in test list -TEST_SIZE = 10000 # number of elements in test list -STEP_SIZE = 100 -INCREMENT_LOC = 0.005 -INCREMENT_SUBSEQS = 100 TYPES = {0: "Pairing", 12: "Smooth"} MAX_TYPE_KEY = max(TYPES.keys()) COLOURS = {0: 'xkcd:fire engine red', 12: 'xkcd:green'} -- GitLab