def benchmark(func, test): filename = "Data/{}.dat".format(test) listPts = readDataPts(filename) times = [] time1 = time() result = func(listPts) time2 = time() result = func(listPts) time3 = time() result = func(listPts) time4 = time() result = func(listPts) time5 = time() result = func(listPts) time6 = time() times.append(time2 - time1) times.append(time3 - time2) times.append(time4 - time3) times.append(time5 - time4) times.append(time6 - time5) total_time = 0 for i in times: total_time += i average = total_time / len(times) return average
def giftwrap(): '''Testing (Gift-Wrapping Algorithm)''' setA = 'Set_A.dat' setB = 'Set_B.dat' print("---GIFT-WRAPPING TESTING---") print('Set A:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setA, i) start = time.time() convexhull.giftwrap(listPts) end = time.time() elapsed = end - start print(elapsed) print() print('Set B:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setB, i) start = time.time() convexhull.giftwrap(listPts) end = time.time() elapsed = end - start print(elapsed)
def test_all_same_answer(tests=tests_A): for test_case in tests: answer = read_answer(test_case) filename = "Data/{}.dat".format(test_case) listPts = readDataPts(filename) gift = giftwrap(listPts) graham = grahamscan(listPts) mono_tone = amethod(listPts) print("Giftwrap = answer: {}".format(gift == answer)) print("Grahamscan = answer: {}".format(graham == answer)) print("Monotone Chain = answer: {}".format(mono_tone == answer))
def monotone_chaining(): setA = 'Set_A.dat' setB = 'Set_B.dat' '''Testing (Monotone Chain Testing)''' print() print("---MONOTONE CHAIN TESTING----") print('Set A:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setA, i) start = time.time() convexhull.amethod(listPts) end = time.time() elapsed = end - start print(elapsed) print() print('Set B:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setA, i) start = time.time() convexhull.amethod(listPts) end = time.time() elapsed = end - start print(elapsed)
def grahamscan(): setA = 'Set_A.dat' setB = 'Set_B.dat' '''Testing Graham Scan''' print() print('------- GRAHAM SCAN -------') print('Set A:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setA, i) start = time.time() convexhull.grahamscan(listPts) end = time.time() elapsed = end - start print(elapsed) print() print('Set B:') for i in range(2000, 32000, 2000): listPts = convexhull.readDataPts(setB, i) start = time.time() convexhull.grahamscan(listPts) end = time.time() elapsed = end - start print(elapsed)
def validity_test(datasets, run_print=True): """ Method to check outputs of algorithms is correct """ # Output array for graph plotting output = [] # Loading the verification file verify_raw = open('verify.txt') verify_lines = verify_raw.readlines() verify_raw.close() verify_lines = [x.strip() for x in verify_lines] # Formatting the output title_string = "=" * 50 + "\nDataset\t\tExpected output?\tTime taken\n" + "=" * 50 if run_print: print(title_string) # Running the output and validating for algorithm in [cvh.grahamscan, cvh.amethod, cvh.giftwrap]: alg_out = [algorithm.__name__, []] if run_print: if algorithm.__name__ == "amethod": algorithm.__name__ = "monotone chain" print('Algorithm: {0}'.format(algorithm.__name__)) for index, dataset in enumerate(datasets): exp_out = eval(verify_lines[index]) start = time.time() act_out = algorithm(cvh.readDataPts('Sets/' + dataset + '.dat', int(dataset[2:]))) # Only print to stout if the textual option is selected if run_print: # If the 3rd algorithm is running ignore the order of the convexhull if algorithm.__name__ == "monotone chain": print('{0}\t\t{1}'.format(dataset, set(exp_out) == set(act_out)), end='') else: print('{0}\t\t{1}'.format(dataset, exp_out == act_out), end='') print('\t\t\t{0:.4f} ms'.format((time.time() - start) * 1e3)) alg_out[1].append((dataset, time.time() - start)) if run_print: print() output.append(alg_out) if not run_print: return output
def main(): """Add-on time tracker for Convex Hull Algorithms of Giftwrap, Graham Scan and Andrew's Monotone Chain Algorithm """ listPts = convexhull.readDataPts('B_30000.dat', 30000) #change to whichever needed #Gift Wrap Timer start_timer = time() giftwrap_list = convexhull.giftwrap(listPts) print("Gift Wrap algorithm used {:.15f} seconds.".format(time() - start_timer)) #Graham Scan Timer start_timer = time() graham_list = convexhull.grahamscan(listPts) print("Graham Scan algorithm used {:.15f} seconds.".format(time() - start_timer)) #Monotone Chain Timer start_timer = time() monotone_list = convexhull.amethod(listPts) print("Monotone Chain algorithm used {:.15f} seconds.".format(time() - start_timer))
def test_setA(): """test all the files in set A against their expected output, prints the results with the time it took each method to get the convex hull""" # test file A_10 filename = 'A_10.dat' output_file = 'A_10.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_50 filename = 'A_50.dat' output_file = 'A_50.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_500 filename = 'A_500.dat' output_file = 'A_500.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_3000 filename = 'A_3000.dat' output_file = 'A_3000.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_6000 filename = 'A_6000.dat' output_file = 'A_6000.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_9000 filename = 'A_9000.dat' output_file = 'A_9000.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_15000 filename = 'A_15000.dat' output_file = 'A_15000.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------') # test file A_30000 filename = 'A_30000.dat' output_file = 'A_30000.out' print("Convex hull for file", filename + ":") print(expected_output(output_file)) listPts = convexhull.readDataPts(filename) print('giftwrap method gives expected output:', expected_output(output_file) == convexhull.giftwrap(listPts), '\tTime taken:'.expandtabs(6), convexhull.time_giftwrap) print('grahamscan method gives expected output:', expected_output(output_file) == convexhull.grahamscan(listPts), '\tTime taken:'.expandtabs(4), convexhull.time_grahamscan) print('amethod method gives expected output:', expected_output(output_file) == convexhull.amethod(listPts), '\tTime taken:'.expandtabs(7), convexhull.time_amethod) print('------------------------------------------------')
def output_tests(): """ Runs tests that validate all algrithms """ sizes = [3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000, 27000, 30000] """ These filenames may need to be altered dependent on their location """ filenames_a = ["Set_A/A_3000.dat", "Set_A/A_6000.dat", "Set_A/A_9000.dat", "Set_A/A_12000.dat", "Set_A/A_15000.dat", "Set_A/A_18000.dat", "Set_A/A_21000.dat", "Set_A/A_24000.dat", "Set_A/A_27000.dat", "Set_A/A_30000.dat"] filenames_b = ["Set_B/B_3000.dat", "Set_B/B_6000.dat", "Set_B/B_9000.dat", "Set_B/B_12000.dat", "Set_B/B_15000.dat", "Set_B/B_18000.dat", "Set_B/B_21000.dat", "Set_B/B_24000.dat", "Set_B/B_27000.dat", "Set_B/B_30000.dat"] print("Set A") print("---------------") #Looping over each file in set A for index, filename in enumerate(filenames_a): #Testing data set A print(filename) #Getting points listPts = convex.readDataPts(filename, sizes[index]) #Calling algorithms gft_hull = convex.giftwrap(listPts[:]) grs_hull = convex.grahamscan(listPts[:]) mono_hull = convex.monotone_chain(listPts[:]) #Getting result of the algorithm expected = outputs_A[index] gft = "Fail" grs = "Fail" mono = "Fail" if gft_hull == expected: gft = "Pass" if grs_hull == expected: grs = "Pass" if sorted(mono_hull) == sorted(expected): mono = "Pass" #Outputting results of the test print("Gifftwrap: " + gft) print("Grahamscan: " + grs) print("Monotone chain: " + mono) print("---------------") print("\nSet B") print("---------------") #Looping over each file in set B for index, filename in enumerate(filenames_b): #Testing data set B print(filename) #Getting points listPts = convex.readDataPts(filename, sizes[index]) #Calling algorithms gft_hull = convex.giftwrap(listPts[:]) grs_hull = convex.grahamscan(listPts[:]) mono_hull = convex.monotone_chain(listPts[:]) #Getting result of the algorithm expected = outputs_B[index] gft = "Fail" grs = "Fail" mono = "Fail" if gft_hull == expected: gft = "Pass" if grs_hull == expected: grs = "Pass" if sorted(mono_hull) == sorted(expected): mono = "Pass" #Outputting results of the test print("Gifftwrap: " + gft) print("Grahamscan: " + grs) print("Monotone chain: " + mono) print("---------------")