Last active
March 9, 2018 13:01
-
-
Save Fasand/d5ea2fd175baefdeec47eb0f1d991dec to your computer and use it in GitHub Desktop.
A simple testing tool for Inf2D Coursework 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess, timeit | |
def construct_command(type, algorithm, start=None, ends=None, moves_set=None): | |
""" | |
type: "search" | "game" | |
algorithm: [1..6] | [1..2] | |
moves_set: [[(4,1), (4,2), ...],] | None | |
start: (1,1) | None | |
ends: [(1,2), (2,1), ...] | None | |
Available searches: | |
1. Breadth First Search | |
2. Depth First Search | |
3. Depth Limited Search (Test at a depth limit of 40) | |
4. Iterative Deepening Search | |
5. Best First Search | |
6. A* Search | |
Available game algorithms: | |
1. Minimax | |
2. Alphabeta | |
""" | |
if start is None: | |
start = "(1,1)" | |
if ends is None: | |
ends = ["(1,2)", "(2,1)", "(1,3)", "(3,1)", "(3,3)", "(4,4)", "(6,6)"] | |
if type == "search": | |
return "\n".join(sum([["main", "1", str(start), str(end), str(algorithm)] for end in ends], [":l Main.hs"])) | |
elif type == "game": | |
return "\n".join(sum([["main", "2", "1", str(algorithm), "\n".join(map(str, moves))] for moves in moves_set], [":l Main.hs"])) | |
else: | |
raise Exception("One or more of your arguments are wrong.") | |
def run_ghci(command, type, center_by=100): | |
""" | |
Runs the command in ghci and prints the output based on the type of the algorithm. | |
This function is necessary if we want to use timeit to get the running time. | |
""" | |
command = tuple(command) # because of timeit | |
print(command[0].center(center_by, " "), "\n") | |
output = subprocess.run(["ghci"], input=command[1], shell=True, stdout=subprocess.PIPE, universal_newlines=True).stdout.split("\n") | |
first_line = "6. A* Search" if type == "search" else " C1 C2 C3 C4 " | |
offset = (1, 12) if type == "search" else (0, 10) | |
output_starts = [(i, line) for i, line in enumerate(output) if line == first_line] | |
for start,_ in output_starts: | |
print("\n".join(map((lambda l: l.center(center_by, " ")), output[start+offset[0]:start+offset[1]])), "\n", "-"*center_by) | |
print("\n\n") | |
# Construct commands for the algorithms you want to test | |
commands = {} | |
commands["breadth"] = ("Breadth First Search", construct_command(type="search", algorithm=1), "search") | |
commands["depth"] = ("Depth First Search", construct_command(type="search", algorithm=2), "search") | |
commands["depth-limited"] = ("Depth Limited Search (d = 40)", construct_command(type="search", algorithm=3), "search") | |
commands["iterative"] = ("Iterative Deepening Search", construct_command(type="search", algorithm=4), "search") | |
commands["best-first"] = ("Best First Search", construct_command(type="search", algorithm=5), "search") | |
commands["a-star"] = ("A* Search", construct_command(type="search", algorithm=6), "search") | |
minimax_moves = [[(4,1),(4,2),(4,3),(3,3),(2,3),(3,4),(2,4),(2,2)]] | |
alphabeta_moves = [[(4,1),(4,2),(4,3),(3,3),(2,3),(3,4),(2,4),(2,2),(2,1)]] | |
commands["minimax"] = ("Minimax", construct_command(type="game", algorithm=1, moves_set=minimax_moves), "game") | |
commands["alphabeta"] = ("Alpha-beta", construct_command(type="game", algorithm=2, moves_set=alphabeta_moves), "game") | |
# Print the result of all predefined algorithms | |
center_by = 100 | |
print("\n", "="*center_by) | |
print("Testing Inf2D CW1".center(center_by, " ")) | |
print("="*center_by, "\n") | |
# Keep track of all running times | |
times = [] | |
for key, command in commands.items(): | |
time = timeit.timeit("run_ghci("+str(command)+", type='"+command[2]+"', center_by="+str(center_by)+")", setup="from __main__ import run_ghci", number=1) | |
times.append((command[0], time)) | |
print("Time: {:.5f}s".format(time)) | |
# Print the overall times | |
print("\n\n", "="*center_by, "\n") | |
print("The times for all algorithms:".center(center_by, " "), "\n") | |
for algorithm, time in times: | |
print("{:<35}{:>8.5f}s".format(algorithm, time).center(center_by, " ")) | |
print("") | |
print("{:<35}{:>8.5f}s".format("Overall time", sum(map(lambda t: t[1], times))).center(center_by, " ")) | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment