-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.py
More file actions
27 lines (22 loc) · 778 Bytes
/
optimization.py
File metadata and controls
27 lines (22 loc) · 778 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
# Ruiqi Chen
# March 10, 2020
import numpy as np
from typing import Callable, Tuple
# Tries every value in domain to find minimum. Returns (min, argmin)
def brute_force_1d(func: Callable[[np.ndarray], np.ndarray], domain: np.ndarray) -> Tuple[float, float]:
val = func(domain)
argind = np.argmin(val)
ind = np.unravel_index(argind, val.shape)
return val[ind], domain[ind]
def optimization_test():
def f(x):
return x**3
val, arg = brute_force_1d(f, np.linspace(-2, 2, 10))
tol = 1e-12
assert np.abs(val + 8) < tol, 'minimum is at -8, not {}'.format(val)
assert np.abs(arg + 2) < tol, 'argmin is at -2, not {}'.format(arg)
print('passed optimizer_test')
def main():
optimization_test()
if __name__ == '__main__':
main()