-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (103 loc) · 4.49 KB
/
Copy pathmain.py
File metadata and controls
116 lines (103 loc) · 4.49 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# coding=UTF-8
import numpy as np
import logging
import sys
import time
import argparse
from functions import BrownAndDennis, BrownAlmostLinear, Example, ExtendedPowellSingular
from methods import InExactLineSearch, NewtonMethod, QuasiNewton
# logger settings
log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout,
level=logging.INFO,
format=log_format,
datefmt='%m/%d %I:%M:%S %p')
logger = logging.getLogger()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--func_name",
default="example",
type=str,
help="Name of the objective function.",
choices=[
"example", "brown_and_dennis",
"brown_almost_linear", "extended_powell_singular"
])
parser.add_argument("--stepsize_method",
default="simple",
type=str,
help="Method of getting stepsize.",
choices=['simple', 'interpolate22', 'interpolate33'])
parser.add_argument(
"--criterion_method",
default="strong_wolfe",
type=str,
help="Criterion method.",
choices=['armijo', 'goldstein', 'wolfe', 'strong_wolfe'])
parser.add_argument("--opt_method",
default="newton",
type=str,
help="Optimization method.",
choices=[
'inexact', 'newton', 'damped', 'hybrid', 'lm',
'sr1', 'bfgs', 'dfp'
])
parser.add_argument("--max_iters",
default=1e3,
type=float,
help="Maximum iteration numbers.")
parser.add_argument("--rho", type=float, default=1e-4)
parser.add_argument("--sigma", type=float, default=0.9)
parser.add_argument("--eps",
default=1e-8,
type=float,
help="Stopping criterion.")
parser.add_argument("--m", default=20, type=int)
args = parser.parse_args()
if args.func_name == "brown_and_dennis":
question = BrownAndDennis(m=args.m)
elif args.func_name == "extended_powell_singular":
question = ExtendedPowellSingular(m=args.m)
elif args.func_name == "brown_almost_linear":
question = BrownAlmostLinear(m=args.m)
else:
question = Example()
start_time = time.process_time()
if args.stepsize_method != "simple":
logger.warning(
"Unstable behavior due to unknown bug, please dont use it.")
methods={
'inexact': InExactLineSearch,
'newton' : NewtonMethod,
'damped' : NewtonMethod,
'hybrid' : NewtonMethod,
'lm' : NewtonMethod,
'sr1': QuasiNewton,
'bfgs': QuasiNewton,
'dfp': QuasiNewton,
}
total_iter, x_k = methods[args.opt_method](start_point=question.x_0,
func=question.func,
grad=question.grad,
hessian=question.hessian,
x_star=question.x_star,
f_minimun=question.f_minimun,
max_iters=args.max_iters,
epsilon=args.eps,
rho=args.rho,
sigma=args.sigma,
method=args.stepsize_method +
args.criterion_method + args.opt_method,
logger=logger)
else:
raise NotImplementedError("Optimization method is not implemented.")
end_time = time.process_time()
logger.info("***** Final Results *****")
logger.info(" 迭代次数(ite): " + str(total_iter))
logger.info(" 函数调用次数(feva): " + str(question.call_f))
logger.info(" 迭代点的 x 值: " + str(x_k.reshape(1, -1)) + ", 函数值:" +
str(question.func(x_k)))
logger.info(" 最优函数值: " + str(question.f_minimun))
logger.info(" CPU时间(ms): " + str((end_time - start_time)))
if __name__ == "__main__":
main()