import optimtool as oo
from optimtool.base import np, sp, plt
pip install optimtool>=2.4.2
加载barzilar_borwein算法
import optimtool.unconstrain as ou
barzilar_borwein = ou.gradient_descent.barzilar_borwein
初始化输入数据
f ( x ) = ∑ i = 1 n / 2 c ( x 2 i − x 2 i − 1 2 ) 2 + ( 1 − x 2 i − 1 ) 2 , x 0 = [ − 1.2 , 1 , . . . , − 1.2 , 1 ] . c = 100 f(x)=\sum_{i=1}^{n/2}c(x_{2i}-x_{2i-1}^2)^2+(1-x_{2i-1})^2, x_0=[-1.2, 1, ...,-1.2, 1]. c=100 f(x)=i=1∑n/2c(x2i−x2i−12)2+(1−x2i−1)2,x0=[−1.2,1,...,−1.2,1].c=100
# make data(4 dimension)
x = sp.symbols("x1:5")
f = 100 * (x[1] - x[0]**2)**2 + \
(1 - x[0])**2 + \
100 * (x[3] - x[2]**2)**2 + \
(1 - x[2])**2
x_0 = (-1.2, 1, -1.2, 1) # Random given
采用默认配置研究
print(f, x, x_0, verbose=True)
出现如下报错:
Traceback (most recent call last):
File "E:\workspace\test.py", line 10, in <module>
print(barzilar_borwein(f, x, x_0, verbose=True))
File "D:\anaconda\lib\site-packages\optimtool\unconstrain\gradient_descent.py", line 167, in barzilar_borwein
alpha = search(funcs, args, x_0, dk, k, point, c1, beta, alpha, constant)
File "D:\anaconda\lib\site-packages\optimtool\_search.py", line 190, in Grippo
assert alpha > 0
AssertionError
意思是罚因子项小了些,需要把基于Grippo的算法的约束因子微调大些,因为只有alpha>0才会梯度下降。
微调算法默认参数
print(barzilar_borwein(f, x, x_0, verbose=True, c1=0.9, beta=0.9, M=10))
(-1.2, 1, -1.2, 1) 48.4 0
[-1.17218609 1.01135262 -1.17218609 1.01135262] 35.74234399993523 1
[-1.07573959 1.05146734 -1.07573959 1.05146734] 10.853930877079154 2
[-1.03729194 1.06784358 -1.03729194 1.06784358] 8.314339559517752 3
[-1.03059542 1.06930565 -1.03059542 1.06930565] 8.256942356362453 4
[-1.02949345 1.06786974 -1.02949345 1.06786974] 8.250528895172161 5
[-1.01145948 1.02980508 -1.01145948 1.02980508] 8.101063953719645 6
[ 0.18155041 -0.21954138 0.18155041 -0.21954138] 14.091164315347264 7
[0.06257751 0.14023334 0.06257751 0.14023334] 5.474008089570103 8
[0.08739254 0.0122698 0.08739254 0.0122698 ] 1.6699964827703286 9
[0.09717293 0.00770988 0.09717293 0.00770988] 1.6307938771999826 10
[0.12174316 0.01260807 0.12174316 0.01260807] 1.5436499168969129 11
[0.71993385 0.17321525 0.71993385 0.17321525] 23.974225961732103 12
[0.69341458 0.19173749 0.69341458 0.19173749] 16.902165798059535 13
[0.59947343 0.25999758 0.59947343 0.25999758] 2.2957547438866586 14
[0.56017728 0.29391331 0.56017728 0.29391331] 0.4659728719309865 15
[0.55278525 0.30213422 0.55278525 0.30213422] 0.4023650853432392 16
[0.55308681 0.30367679 0.55308681 0.30367679] 0.4004558072779859 17
[0.55647063 0.30743861 0.55647063 0.30743861] 0.3944231285815026 18
[0.86021216 0.65100256 0.86021216 0.65100256] 1.6219430382036113 19
[0.70931846 0.73951826 0.70931846 0.73951826] 11.344620053720568 20
[0.8100935 0.66909219 0.8100935 0.66909219] 0.10510577406964994 21
[0.81731167 0.6650097 0.81731167 0.6650097 ] 0.06853647456044182 22
[0.8164723 0.66582992 0.8164723 0.66582992] 0.0674919060017248 23
[0.81662502 0.66605803 0.81662502 0.66605803] 0.06738671804221721 24
[0.86835509 0.75122239 0.86835509 0.75122239] 0.03624918137947759 25
[0.77235521 0.82683779 0.77235521 0.82683779] 10.711743471603352 26
[0.81192998 0.80138115 0.81192998 0.80138115] 4.1121138687376 27
[0.88346099 0.75768716 0.88346099 0.75768716] 0.1312782107449447 28
[0.87314179 0.76370122 0.87314179 0.76370122] 0.0325369376654432 29
[0.87401353 0.76337883 0.87401353 0.76337883] 0.03179943209268424 30
[0.87410199 0.76351067 0.87410199 0.76351067] 0.031759723095652316 31
[0.88694234 0.78612821 0.88694234 0.78612821] 0.025622065675204374 32
[0.95184857 0.98548447 0.95184857 0.98548447] 1.267694088111409 33
[1.03892769 0.93988749 1.03892769 0.93988749] 3.8941464214885255 34
[0.98107815 0.96769113 0.98107815 0.96769113] 0.006075928176673569 35
[0.9830691 0.96669501 0.9830691 0.96669501] 0.0005879072914185411 36
[0.98321319 0.96663944 0.98321319 0.96663944] 0.0005645390367161203 37
[0.98321997 0.96665369 0.98321997 0.96665369] 0.0005640588458026667 38
[0.98971109 0.97943603 0.98971109 0.97943603] 0.0002134165931058616 39
[0.95171388 1.02355346 0.95171388 1.02355346] 2.779755313116296 40
[0.98444683 1.00639357 0.98444683 1.00639357] 0.2781156378581681 41
[1.00030797 0.99835475 1.00030797 0.99835475] 0.001022880219129762 42
[0.99938771 0.99881442 0.99938771 0.99881442] 1.0480525859292297e-06 43
[0.99940435 0.99880671 0.99940435 0.99880671] 7.106978129659511e-07 44
[0.9994046 0.99880718 0.9994046 0.99880718] 7.101292197589673e-07 45
[0.99953764 0.99907365 0.99953764 0.99907365] 4.282354083211168e-07 46
[1.00000356 0.9999974 1.00000356 0.9999974 ] 1.8955821269642996e-08 47
[0.99066961 1.00465583 0.99066961 1.00465583] 0.10809652952210036 48
[0.9999825 0.99996504 0.9999825 0.99996504] 6.131958176710077e-10 49
[0.99998255 0.99996503 0.99998255 0.99996503] 6.100493838834033e-10 50
[0.99998256 0.99996504 0.99998256 0.99996504] 6.095180589185439e-10 51
[0.9999865 0.99997287 0.9999865 0.99997287] 3.676877863890199e-10 52
[0.99993462 1.00003166 0.99993462 1.00003166] 5.284526365615982e-06 53
[1.00001028 0.99999391 1.00001028 0.99999391] 1.4234808255557074e-07 54
[0.99999962 0.99999923 0.99999962 0.99999923] 2.964397028197504e-13 55
[0.99999962 0.99999923 0.99999962 0.99999923] 2.9534013511438936e-13 56
[0.99999962 0.99999923 0.99999962 0.99999923] 2.9507256358442176e-13 57
[1. 1. 1. 1.] 1.88063786036214e-20 58
[1.00000001 1. 1.00000001 1. ] 1.0948317471092592e-13 59
[1. 1. 1. 1.] 1.1024327807665554e-22 60
[1. 1. 1. 1.] 3.549874073494553e-30 61
另一种步长搜索器
barzilar_borwein(f, x, x_0, verbose=True, method="ZhangHanger", c1=0.8, beta=0.8, eta=0.6)
(-1.2, 1, -1.2, 1) 48.4 0
[-1.14402704 1.02284611 -1.14402704 1.02284611] 25.547385906612575 1
[-1.04917293 1.06298694 -1.04917293 1.06298694] 8.683638321782801 2
[-1.03278859 1.06919128 -1.03278859 1.06919128] 8.265748234572825 3
[-1.02996132 1.06871536 -1.02996132 1.06871536] 8.253952272254296 4
[-1.02880459 1.06645286 -1.02880459 1.06645286] 8.244940882294298 5
[-0.75881417 0.49681826 -0.75881417 0.49681826] 7.434444489232861 6
[-0.71206416 0.52368118 -0.71206416 0.52368118] 5.917743983089465 7
[-0.71412212 0.51847911 -0.71412212 0.51847911] 5.890908900222328 8
[-0.71176181 0.51445338 -0.71176181 0.51445338] 5.872576785186908 9
[-0.38846072 0.08764221 -0.38846072 0.08764221] 4.655999792942503 10
[-0.34569326 0.13056384 -0.34569326 0.13056384] 3.646245482183912 11
[-0.34190925 0.12336077 -0.34190925 0.12336077] 3.609784188103343 12
[-0.31361399 0.10306018 -0.31361399 0.10306018] 3.4555935448716233 13
[ 0.00397922 -0.04371105 0.00397922 -0.04371105] 2.3665227959068664 14
[0.02130878 0.03512281 0.02130878 0.03512281] 2.1560573397774148 15
[ 0.03323879 -0.00159448 0.03323879 -0.00159448] 1.8707117086887108 16
[0.04338918 0.00129321 0.04338918 0.00129321] 1.8302779870130716 17
[0.22543897 0.01257035 0.22543897 0.01257035] 1.4925384627561797 18
[0.21426361 0.05756136 0.21426361 0.05756136] 1.2619193148303254 19
[0.22533239 0.04752472 0.22533239 0.04752472] 1.2023322716998874 20
[0.23164194 0.05078892 0.23164194 0.05078892] 1.1823945212759244 21
[0.39858439 0.12616496 0.39858439 0.12616496] 0.9373190719661812 22
[0.38145931 0.15408892 0.38145931 0.15408892] 0.7799006036978148 23
[0.38942728 0.14871975 0.38942728 0.14871975] 0.7473195943947577 24
[0.39208339 0.15075934 0.39208339 0.15075934] 0.740889445363389 25
[0.54384873 0.27095384 0.54384873 0.27095384] 0.5393305593466469 26
[0.52980061 0.28649559 0.52980061 0.28649559] 0.4489189523781992 27
[0.53484966 0.28379459 0.53484966 0.28379459] 0.4337598682697777 28
[0.53596463 0.28493254 0.53596463 0.28493254] 0.43173927738659845 29
[0.66711966 0.42695983 0.66711966 0.42695983] 0.28705969635936956 30
[0.6555018 0.43706044 0.6555018 0.43706044] 0.24824451482332732 31
[0.66027465 0.43437596 0.66027465 0.43437596] 0.23133012100250183 32
[0.6607599 0.4349673 0.6607599 0.4349673] 0.2307032124435715 33
[0.76387974 0.57216118 0.76387974 0.57216118] 0.13727493959232465 34
[0.75426998 0.57944271 0.75426998 0.57944271] 0.14289849619262165 35
[0.75982798 0.57625237 0.75982798 0.57625237] 0.1156011583706373 36
[0.76005697 0.57658352 0.76005697 0.57658352] 0.11538866972480637 37
[0.86143683 0.73133553 0.86143683 0.73133553] 0.061459919065018784 38
[0.85446275 0.73571119 0.85446275 0.73571119] 0.04864447302661772 39
[0.85725529 0.73429265 0.85725529 0.73429265] 0.040822666441657225 40
[0.85735963 0.73444416 0.85735963 0.73444416] 0.04076977055273804 41
[0.87135784 0.75854423 0.87135784 0.75854423] 0.033201365652168045 42
[0.87174017 0.76736276 0.87174017 0.76736276] 0.04394760040221391 43
[0.87398087 0.76619333 0.87398087 0.76619333] 0.03286687198523424 44
[0.8753094 0.76561167 0.8753094 0.76561167] 0.031157068795362352 45
[0.87537792 0.76574967 0.87537792 0.76574967] 0.031118963457695742 46
[0.89466678 0.79955226 0.89466678 0.79955226] 0.022343784290254107 47
[0.89301787 0.80235929 0.89301787 0.80235929] 0.02765007340992803 48
[0.89462001 0.80156035 0.89462001 0.80156035] 0.02250531824469297 49
[0.89539064 0.80127024 0.89539064 0.80127024] 0.021927489016178737 50
[0.89544715 0.80138049 0.89544715 0.80138049] 0.021902219932405945 51
[0.92813781 0.85996455 0.92813781 0.85996455] 0.010763616666667527 52
[0.92614688 0.86141869 0.92614688 0.86141869] 0.013603307269649175 53
[0.92787507 0.8605771 0.92787507 0.8605771 ] 0.01043214239830986 54
[0.92788077 0.86066166 0.92788077 0.86066166] 0.010420494397709812 55
[0.928094 0.86105668 0.928094 0.86105668] 0.010359161042322382 56
[0.9705483 0.94169756 0.9705483 0.94169756] 0.001749003669665157 57
[0.96879935 0.94379027 0.96879935 0.94379027] 0.007392649706546043 58
[0.96964345 0.94336767 0.96964345 0.94336767] 0.003839211350773539 59
[0.97099486 0.9427037 0.97099486 0.9427037 ] 0.0016858380908765503 60
[0.97100383 0.9427304 0.97100383 0.9427304 ] 0.0016843424445882975 61
[0.97155834 0.94380875 0.97155834 0.94380875] 0.0016205873167718177 62
[0.99298678 0.98746597 0.99298678 0.98746597] 0.0005149481798177243 63
[0.99405866 0.98693913 0.99405866 0.98693913] 0.0003651100160972216 64
[0.99358383 0.987184 0.99358383 0.987184 ] 8.245778054657986e-05 65
[0.99358682 0.987189 0.99358682 0.987189 ] 8.239052921973421e-05 66
[0.99405188 0.98811496 0.99405188 0.98811496] 7.087718695175268e-05 67
[0.99968373 1.0000437 0.99968373 1.0000437 ] 9.163261199483933e-05 68
[1.00005465 0.99985861 1.00005465 0.99985861] 1.2575323104527597e-05 69
[0.99995439 0.99990868 0.99995439 0.99990868] 4.162393555661443e-09 70
[0.99995444 0.9999087 0.99995444 0.9999087 ] 4.1577523758631505e-09 71
[0.99995448 0.99990879 0.99995448 0.99990879] 4.150285949048248e-09 72
[0.99999998 1. 0.99999998 1. ] 1.6347843490821497e-13 73
[1.00002865 0.9999857 1.00002865 0.9999857 ] 1.027136418355962e-06 74
[1. 1. 1. 1.] 6.014856540114234e-20 75
[1. 1. 1. 1.] 5.359465147840669e-20 76
[1. 1. 1. 1.] 5.3551774107473235e-20 77
[1. 1. 1. 1.] 1.225496980276194e-21 78