Skip to content

Commit 5e0682a

Browse files
committed
Let's get started gathering stats.
1 parent 16ad30a commit 5e0682a

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ src/*
1414
*.bak
1515
dist/
1616
build/
17+
stats/

python/stochastic/random_search.py

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#! usr/bin/env python3
22

3+
from ..switch import decide
4+
35
import random
6+
import sys
47

58
"""
69
2.2
@@ -12,52 +15,55 @@
1215
The example problem (solved by the code below) is an instance of a
1316
continuous function optimization that seeks min f(x) where
1417
f = \sum_{i=1}^{n} x_i^2, -5 <= x_i <= 5 and n = 2.
15-
16-
@author Chad Estioco
1718
"""
1819

1920
def objective_function(vector):
20-
sum = 0
21-
22-
for val in vector:
23-
sum += val ** 2
24-
25-
return sum
21+
sum = 0
22+
23+
for val in vector:
24+
sum += val ** 2
25+
26+
return sum
2627

2728
def random_vector(minmax):
28-
i = 0
29-
limit = len(minmax)
30-
random_vector = [0 for i in range(limit)]
31-
32-
for i in range(limit):
33-
spam = minmax[i][0]
34-
random_vector[i] = spam + ((minmax[i][1] - spam) * random.random())
35-
36-
return random_vector
37-
38-
def search(search_space, max_iter):
39-
best = None
40-
41-
for i in range(max_iter):
42-
candidate = {}
43-
candidate['vector'] = random_vector(search_space)
44-
candidate['cost'] = objective_function(candidate['vector'])
45-
46-
if best is None or candidate['cost'] < best['cost']:
47-
best = candidate
48-
49-
print("Iteration " + str(i) + ": best = " + str(best['cost']))
50-
51-
return best
29+
i = 0
30+
limit = len(minmax)
31+
random_vector = [0 for i in range(limit)]
32+
33+
for i in range(limit):
34+
spam = minmax[i][0]
35+
random_vector[i] = spam + ((minmax[i][1] - spam) * random.random())
36+
37+
return random_vector
38+
39+
def search(search_space, max_iter, show_log=True):
40+
best = None
41+
42+
for i in range(max_iter):
43+
candidate = {}
44+
candidate['vector'] = random_vector(search_space)
45+
candidate['cost'] = objective_function(candidate['vector'])
46+
47+
if best is None or candidate['cost'] < best['cost']:
48+
best = candidate
49+
50+
if show_log:
51+
print("Iteration " + str(i) + ": best = " + str(best['cost']))
52+
53+
return best
5254

5355
if __name__ == "__main__":
54-
# problem configuration
55-
problem_size = 2
56-
search_space = [[-5, 5] for i in range(problem_size)]
57-
58-
# algorithm configuration
59-
max_iter = 100
60-
61-
# execute the algorithm
62-
best = search(search_space ,max_iter)
63-
print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector']))
56+
# problem configuration
57+
problem_size = 2
58+
search_space = [[-5, 5] for i in range(problem_size)]
59+
60+
# algorithm configuration
61+
max_iter = 100
62+
63+
# execute the algorithm
64+
if decide():
65+
best = search(search_space ,max_iter)
66+
print("Done. Best Solution: cost = " + str(best['cost']) + ", v = " + str(best['vector']))
67+
else:
68+
best = search(search_space, max_iter, False)
69+
print(best["cost"])

python/switch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
3+
def decide():
4+
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

0 commit comments

Comments
 (0)