-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.py
53 lines (47 loc) · 1.58 KB
/
example.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 16 12:42:01 2022
@author: dsy
"""
import numpy as np
from dsy_numberical_optimization import target_function,Optimizer
A=np.mat([[2,0,0],
[0,3,0],
[0,0,5]])
b=np.mat([[4],
[1],
[5]])
def f(x):
y=0.5*x.T*A*x-b.T*x
return y
def deri_f(x):
y=A*x-b
return y
def Hessian(x):
return A.T
x=np.mat([[9],
[9],
[9]])
tf=target_function([1,3],f,deri_f)
opt=Optimizer(tf,
Interpolate="quadratic",
#steps_logs=True,
)
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="steepest_descent")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="linear_conjugate_gradient")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="FR_conjugate_gradient")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="PR_conjugate_gradient")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="HR_conjugate_gradient")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="SR1")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="DFP")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="BFGS")
print("optmal x:\n{x}".format(x=x_min))
x_min=opt.GD_optmize(start=tf.start_point(),A=A,Method="LS_Newton_CG")
print("optmal x:\n{x}".format(x=x_min))