-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_program.py
More file actions
31 lines (26 loc) · 996 Bytes
/
Copy pathtest_program.py
File metadata and controls
31 lines (26 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# initial_program.py
import random
import math
def search_algorithm():
# naive random search
# Improved search: Combines random search with a refined interval search.
best_x = 1.0
best_val = best_x ** best_x
# Initial random search (as before, but with more iterations)
for _ in range(100):
x = random.uniform(0.01, 1.5) # Broader initial search range
val = x ** x
if val < best_val:
best_x, best_val = x, val
# Refine the search near the best found value using a smaller interval and a more focused search
# This uses a shrinking interval and a higher number of iterations.
for _ in range(100):
interval = 0.1 * (0.95 ** _) # Shrinking interval
x = random.uniform(max(1e-5, best_x - interval), best_x + interval)
val = x ** x
if val < best_val:
best_x, best_val = x, val
return best_x
if __name__ == "__main__":
best_x = search_algorithm()
print(best_x*math.e)