Skip to content

Commit

Permalink
Modify switch module beyond just a boolean flag.
Browse files Browse the repository at this point in the history
For more granular data collection.
  • Loading branch information
skytreader committed Oct 14, 2018
1 parent bb3ac39 commit 7d6f8bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
17 changes: 9 additions & 8 deletions python/stochastic/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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']))
10 changes: 2 additions & 8 deletions python/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <true|false>" % sys.argv[0])
exit(1)
return False
return sys.argv[1]
return "human"

0 comments on commit 7d6f8bd

Please sign in to comment.