Skip to content

Commit b4b4f01

Browse files
committed
Add stats work for Ruby.
1 parent 25a5bdc commit b4b4f01

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

python/stat_runner.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import subprocess
44
import sys
55

6+
def determine_runner(script_path):
7+
dot_split = script_path.split(".")
8+
return ["ruby", script_path] if dot_split[-1] == "rb" else ["python3", "-m", script_path]
9+
610
if __name__ == "__main__":
711
if len(sys.argv) != 3:
8-
print("Usage: python3 %s <script.package.path> <iters>" % __file__)
12+
print("Usage: python3 %s <script.path> <iters>" % __file__)
913
exit(1)
1014
else:
1115
running_sum = 0
1216
limit = int(sys.argv[2])
1317

1418
for _ in range(limit):
15-
out = subprocess.run(["python3", "-m", sys.argv[1]], stdout=subprocess.PIPE)
19+
out = subprocess.run(determine_runner(sys.argv[1]), stdout=subprocess.PIPE)
1620
running_sum += float(out.stdout)
1721

1822
print("Mean: %s" % (running_sum / limit))

python/stochastic/random_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ def search(search_space, max_iter, show_log=True):
5959

6060
# algorithm configuration
6161
max_iter = 100
62+
show_log = decide()
63+
best = search(search_space, max_iter, show_log)
6264

6365
# execute the algorithm
64-
if decide():
65-
best = search(search_space ,max_iter)
66+
if show_log:
6667
print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector']))
6768
else:
68-
best = search(search_space, max_iter, False)
6969
print(best["cost"])

src/algorithms/stochastic/random_search.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# The Clever Algorithms Project: http://www.CleverAlgorithms.com
44
# (c) Copyright 2010 Jason Brownlee. Some Rights Reserved.
55
# This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Australia License.
6+
require_relative "../switch.rb"
67

78
def objective_function(vector)
89
return vector.inject(0) {|sum, x| sum + (x ** 2.0)}
@@ -14,14 +15,16 @@ def random_vector(minmax)
1415
end
1516
end
1617

17-
def search(search_space, max_iter)
18+
def search(search_space, max_iter, show_log=true)
1819
best = nil
1920
max_iter.times do |iter|
2021
candidate = {}
2122
candidate[:vector] = random_vector(search_space)
2223
candidate[:cost] = objective_function(candidate[:vector])
2324
best = candidate if best.nil? or candidate[:cost] < best[:cost]
24-
puts " > iteration=#{(iter+1)}, best=#{best[:cost]}"
25+
if show_log
26+
puts " > iteration=#{(iter+1)}, best=#{best[:cost]}"
27+
end
2528
end
2629
return best
2730
end
@@ -32,7 +35,14 @@ def search(search_space, max_iter)
3235
search_space = Array.new(problem_size) {|i| [-5, +5]}
3336
# algorithm configuration
3437
max_iter = 100
38+
show_log = decide()
3539
# execute the algorithm
36-
best = search(search_space, max_iter)
37-
puts "Done. Best Solution: c=#{best[:cost]}, v=#{best[:vector].inspect}"
40+
best = search(search_space, max_iter, show_log)
41+
42+
if show_log
43+
puts "Done. Best Solution: c=#{best[:cost]}, v=#{best[:vector].inspect}"
44+
else
45+
puts best[:cost]
46+
end
47+
3848
end

0 commit comments

Comments
 (0)