-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkompasssuche.py
34 lines (32 loc) · 1.64 KB
/
kompasssuche.py
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
32
33
34
import numpy as np #line:1
def kompasssuche (O0OO0OO0OO0OOO0OO ,OOO0O00O0O0OOOOO0 ,s0 =0.5 ,theta =0.5 ,iters =100 ):#line:3
"""
Kompasssuche algorithm to find the minimum of a function.
Parameters:
f (callable): the function to minimize.
x0 (ndarray): the initial point.
s0 (float): the initial step size.
theta (float): reduction factor of the step size.
iters (float): maximal iterations
Returns:
dict:
'x' (np.ndarray): position of minimum
'fx' (float): value of minimum
"""
O0O00000O0OOOO0OO =np .concatenate ((np .identity (len (OOO0O00O0O0OOOOO0 )),-np .identity (len (OOO0O00O0O0OOOOO0 ))),axis =1 )#line:20
OO0OO0OO0OOO000OO =1 #line:23
O000O00O0O0O00OO0 =O0OO0OO0OO0OOO0OO (OOO0O00O0O0OOOOO0 )#line:25
while (OO0OO0OO0OOO000OO <=iters ):#line:28
O000OOO0OO0OO0O00 =False #line:30
OO0OO0O000OOO0OOO =0 #line:31
while OO0OO0O000OOO0OOO <O0O00000O0OOOO0OO .shape [1 ]and not O000OOO0OO0OO0O00 :#line:34
O0O0OO0OOOOOO00OO =O0OO0OO0OO0OOO0OO (OOO0O00O0O0OOOOO0 +s0 *O0O00000O0OOOO0OO [:,OO0OO0O000OOO0OOO ])#line:36
if O0O0OO0OOOOOO00OO <O000O00O0O0O00OO0 :#line:39
OOO0O00O0O0OOOOO0 =OOO0O00O0O0OOOOO0 +s0 *O0O00000O0OOOO0OO [:,OO0OO0O000OOO0OOO ]#line:41
O000O00O0O0O00OO0 =O0O0OO0OOOOOO00OO #line:42
O000OOO0OO0OO0O00 =True #line:45
OO0OO0O000OOO0OOO +=1 #line:46
if not O000OOO0OO0OO0O00 :#line:49
s0 =theta *s0 #line:50
OO0OO0OO0OOO000OO +=1 #line:53
return {'x':OOO0O00O0O0OOOOO0 ,'fx':O000O00O0O0O00OO0 }