BerGeo/h2/tmptest.py

104 lines
3.5 KiB
Python
Raw Normal View History

2018-10-11 12:38:59 +00:00
import random
from time import time
2018-10-17 10:56:46 +00:00
from collections import namedtuple
2018-10-11 12:38:59 +00:00
import util
from gift_wrapper import rapper
from graham import graham_scan
from mbc import mbc
from quick_hull import quick_hull
2018-10-17 10:56:46 +00:00
import os.path
#random.seed(1337_420)
TimedResult = namedtuple("TimedResult", "algorithm points running_time")
2018-10-18 09:53:43 +00:00
def time_it(f: callable, args: tuple = (), iterations=1):
2018-10-17 10:56:46 +00:00
start = time()
for i in range(iterations):
f(*args)
return str(time() - start)
def initiate_file(file):
with open(file, "w+") as tmp:
tmp.write("algorithm\t\tpoints\t\ttime")
def write_to_log(file, data):
if not os.path.isfile(file):
initiate_file(file)
tmp = []
for res in data:
line = str.join("\t\t", res)
print(line)
tmp.append(line)
write_string = "\n" + str.join("\n", tmp)
with open(file, "a+") as open_file:
open_file.write(write_string)
def do_square_tests(amount_of_points):
points_square = {util.gen_point(0, 100) for _ in range(amount_of_points)}
amount_of_points = str(amount_of_points)
2018-10-18 10:07:08 +00:00
results = [TimedResult("graham", amount_of_points, time_it(graham_scan, args=(points_square,))),
TimedResult("gift", amount_of_points, time_it(rapper, args=(points_square,))),
TimedResult("quick", amount_of_points, time_it(quick_hull, args=(points_square,))),
TimedResult("mbch", amount_of_points, time_it(mbc, args=(points_square,)))]
2018-10-17 10:56:46 +00:00
write_to_log("square_tests.log", results)
2018-10-11 12:38:59 +00:00
2018-10-11 13:54:52 +00:00
2018-10-17 10:56:46 +00:00
def do_circular_tests(amount_of_points):
points_circular = {util.gen_point(0, 100) for _ in range(amount_of_points)}
2018-10-18 10:07:08 +00:00
results = [TimedResult("graham", amount_of_points, time_it(graham_scan, args=(points_circular,))),
TimedResult("gift", amount_of_points, time_it(rapper, args=(points_circular,))),
TimedResult("quick", amount_of_points, time_it(quick_hull, args=(points_circular,))),
TimedResult("mbc", amount_of_points, time_it(mbc, args=(points_circular,)))]
2018-10-17 10:56:46 +00:00
write_to_log("circular_tests.log", results)
def do_triangular_tests(amount_of_points):
left, right, top = util.Point(1,1), util.Point(51,1), util.Point(26,40)
points = {util.gen_triangular_point(left, right, top) for _ in range(amount_of_points)}
2018-10-18 10:07:08 +00:00
results = [TimedResult("graham", amount_of_points, time_it(graham_scan, args=(points,))),
TimedResult("gift", amount_of_points, time_it(rapper, args=(points,))),
TimedResult("quick", amount_of_points, time_it(quick_hull, args=(points,))),
TimedResult("mbc", amount_of_points, time_it(mbc, args=(points,)))]
2018-10-17 10:56:46 +00:00
write_to_log("triangular_tests.log", results)
def do_quadratic_tests(amount_of_points):
points = {util.gen_weird_point(-10, 10) for _ in range(amount_of_points)}
2018-10-18 10:07:08 +00:00
results = [TimedResult("graham", amount_of_points, time_it(graham_scan, args=(points,))),
TimedResult("gift", amount_of_points, time_it(rapper, args=(points,))),
TimedResult("quick", amount_of_points, time_it(quick_hull, args=(points,))),
TimedResult("mbc", amount_of_points, time_it(mbc, args=(points,)))]
2018-10-17 10:56:46 +00:00
write_to_log("quadratic_tests.log", results)
2018-10-18 10:07:08 +00:00
def sanity_check():
points = {util.gen_point(1, 50) for i in range(100)}
2018-10-11 13:54:52 +00:00
graham = set(graham_scan(points))
gift = set(rapper(points))
quick = quick_hull(points)
mbch = set.union(mbc(points))
2018-10-18 10:07:08 +00:00
assert gift == graham == quick == mbch
2018-10-11 13:54:52 +00:00
2018-10-17 10:56:46 +00:00
2018-10-18 09:53:43 +00:00
if __name__ == '__main__':
2018-10-18 10:07:08 +00:00
sanity_check()
2018-10-18 09:53:43 +00:00
for i in range(50, 1000, 50):
do_square_tests(i)