Skip to content

Commit 7d6f8bd

Browse files
committed
Modify switch module beyond just a boolean flag.
For more granular data collection.
1 parent bb3ac39 commit 7d6f8bd

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

python/stochastic/random_search.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def random_vector(minmax):
3636

3737
return random_vector
3838

39-
def search(search_space, max_iter, show_log=True):
39+
def search(search_space, max_iter, output_format="human"):
4040
best = None
4141

4242
for i in range(max_iter):
@@ -47,7 +47,9 @@ def search(search_space, max_iter, show_log=True):
4747
if best is None or candidate['cost'] < best['cost']:
4848
best = candidate
4949

50-
if show_log:
50+
if output_format == "csv":
51+
print("%s,%s" % (i, best["cost"]))
52+
else:
5153
print("Iteration " + str(i) + ": best = " + str(best['cost']))
5254

5355
return best
@@ -59,11 +61,10 @@ def search(search_space, max_iter, show_log=True):
5961

6062
# algorithm configuration
6163
max_iter = 100
62-
show_log = decide()
63-
best = search(search_space, max_iter, show_log)
64+
output_format = decide()
65+
best = search(search_space, max_iter, output_format)
6466

65-
# execute the algorithm
66-
if show_log:
67-
print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector']))
67+
if output_format == "csv":
68+
print("%s,%s" % (best["cost"], ",".join([str(i) for i in best["vector"]])))
6869
else:
69-
print(best["cost"])
70+
print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector']))

python/switch.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,5 @@
22

33
def decide():
44
if len(sys.argv) == 2:
5-
flag = sys.argv[1].lower()
6-
7-
if flag == "true":
8-
return True
9-
elif flag != "false":
10-
print("Usage: python3 %s <true|false>" % sys.argv[0])
11-
exit(1)
12-
return False
5+
return sys.argv[1]
6+
return "human"

0 commit comments

Comments
 (0)