From 460a4a816681588a8a937263cbe92cc562b6084d Mon Sep 17 00:00:00 2001 From: Maximilian Schlosser Date: Wed, 12 Jul 2017 17:55:16 +0200 Subject: [PATCH] main method, command line options --- src/main.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index e1e7dda..68fe28e 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,65 @@ #! /usr/bin/env python -def main() -> None: - pass -if "__name__" == "__main__": +import sys +import getopt +from SchedulingAlgorithms import simanneal as sim +from Outpout import output as o + + +def usage(): + s= """ +Command line options: + -h show this help + -p activate pretty output (matplotlib) + -l assume that a file contains multiple problems, default is only 1 + -i index of the problem you want solved. has no effect without l + +Invocation: + python [-hlp] file +""" + return s + + +def main(): + js1 = False + plot = False + try: + opts, args = getopt.getopt(sys.argv[1:], 'hpli:') + except getoptGetoptError as err: + print(err) + sys.exit() + if ('-h', '') in opts: + print(usage()) + if ('-p', '') in opts: + print("Plotting enabled.") + plot = True + if('-l', '') in opts: + js1 = True + idx = [int(x[1]) for x in opts if x[0]=='-i'] + idx = idx[0] if idx else -1 + if not args: + print("No file given.") + sys.exit() + else: + infile = args[0] + if js1: + from Parser import js1_style as parser + else: + from Parser import js2_style as parser + print("Parsing file: " + infile) + problem = parser.parse_file(infile) + if js1 and idx == -1: + print("File contains " + str(len(problem)) + " problems.") + idx = int(input("Which problem do you want so solve? [0-" + str(len(problem)-1) + "] ")) + problem = problem[idx] + print(problem) + sim.init(problem) + solution = sim.anneal() + print(solution) + print(sim.rate(solution)) + if plot: + o.create_plot(problem, solution) + +if __name__ == "__main__": main() +