From 7d6f8bd4e3fa6ebedb7a29fa4d3f630b913febc9 Mon Sep 17 00:00:00 2001 From: Chad Estioco Date: Sun, 14 Oct 2018 22:36:01 +0800 Subject: [PATCH] Modify switch module beyond just a boolean flag. For more granular data collection. --- python/stochastic/random_search.py | 17 +++++++++-------- python/switch.py | 10 ++-------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/python/stochastic/random_search.py b/python/stochastic/random_search.py index 24d6eaff..af497796 100644 --- a/python/stochastic/random_search.py +++ b/python/stochastic/random_search.py @@ -36,7 +36,7 @@ def random_vector(minmax): return random_vector -def search(search_space, max_iter, show_log=True): +def search(search_space, max_iter, output_format="human"): best = None for i in range(max_iter): @@ -47,7 +47,9 @@ def search(search_space, max_iter, show_log=True): if best is None or candidate['cost'] < best['cost']: best = candidate - if show_log: + if output_format == "csv": + print("%s,%s" % (i, best["cost"])) + else: print("Iteration " + str(i) + ": best = " + str(best['cost'])) return best @@ -59,11 +61,10 @@ def search(search_space, max_iter, show_log=True): # algorithm configuration max_iter = 100 - show_log = decide() - best = search(search_space, max_iter, show_log) + output_format = decide() + best = search(search_space, max_iter, output_format) - # execute the algorithm - if show_log: - print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector'])) + if output_format == "csv": + print("%s,%s" % (best["cost"], ",".join([str(i) for i in best["vector"]]))) else: - print(best["cost"]) + print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector'])) diff --git a/python/switch.py b/python/switch.py index 74bc4a07..7ccc6a9f 100644 --- a/python/switch.py +++ b/python/switch.py @@ -2,11 +2,5 @@ def decide(): if len(sys.argv) == 2: - flag = sys.argv[1].lower() - - if flag == "true": - return True - elif flag != "false": - print("Usage: python3 %s " % sys.argv[0]) - exit(1) - return False + return sys.argv[1] + return "human"