-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathrosenbrock_minimization.py
73 lines (61 loc) · 2.56 KB
/
rosenbrock_minimization.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2014-2016 The ODL development group
#
# This file is part of ODL.
#
# ODL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ODL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ODL. If not, see <http://www.gnu.org/licenses/>.
"""Minimize the Rosenbrock functional.
This example shows how this can be done using a variety of solution methods.
"""
import odl
from matplotlib import pyplot as plt
# Create the solution space
space = odl.rn(2)
# Create objective functional
f = odl.solvers.RosenbrockFunctional(space)
# Define a line search method
line_search = odl.solvers.BacktrackingLineSearch(f)
# Solve problem using steepest descent
callback = odl.solvers.CallbackShowConvergence(f, logx=True, logy=True,
color='b')
x = space.zero()
odl.solvers.steepest_descent(f, x, line_search=line_search,
callback=callback)
legend_artists = [callback.ax.collections[-1], ]
legend_labels = ['SD', ]
# Solve problem using nonlinear conjugate gradient
callback = odl.solvers.CallbackShowConvergence(f, logx=True, logy=True,
color='g')
x = space.zero()
odl.solvers.conjugate_gradient_nonlinear(f, x, line_search=line_search,
callback=callback)
legend_artists.append(callback.ax.collections[-1])
legend_labels.append('CG')
# Solve problem using bfgs
callback = odl.solvers.CallbackShowConvergence(f, logx=True, logy=True,
color='r')
x = space.zero()
odl.solvers.bfgs_method(f, x, line_search=line_search,
callback=callback)
legend_artists.append(callback.ax.collections[-1])
legend_labels.append('BFGS')
# Solve problem using newtons method
callback = odl.solvers.CallbackShowConvergence(f, logx=True, logy=True,
color='k')
x = space.zero()
odl.solvers.newtons_method(f, x, line_search=line_search,
callback=callback)
legend_artists.append(callback.ax.collections[-1])
legend_labels.append('Newton')
plt.legend(legend_artists, legend_labels)
plt.show()