Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F91563666
optimizer.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Nov 12, 06:14
Size
1 KB
Mime Type
text/x-python
Expires
Thu, Nov 14, 06:14 (2 d)
Engine
blob
Format
Raw Data
Handle
22258293
Attached To
R9490 Homework_sp4e_Peruzzo_SáezUribe
optimizer.py
View Options
import
scipy.optimize
import
numpy
as
np
def
optimizer
(
f
,
method
,
x0
,
tol
):
"""
Find the minimizer of a scalar function of 2 variables.
Parameters
----------
f : scalar function of a vector of 2 variables.
method : strig
e.g. "CG" for Conjigate gradient, "BFGS" for quasi-Newton method of Broyden,
Fletcher, Goldfarb, and Shanno
x0 : ndarray
array of 2 elements with the initial guess of the minimizer.
tol : float
exit tollerance for the method e.g. 10^-8
Returns
-------
minimizer : ndarray
The solution of the optimization
resultINFO: OptimizeResult object
Important attributes are:
x: ndarray - The solution of the optimization.
success: bool - Whether or not the optimizer exited successfully.
message: str - Description of the cause of the termination.
nit: int - Number of iterations performed by the optimizer.
points : numpy ndarray
2-D array of shape (number of iterations + 1, 2) containing the successive approximations
for the solution obtained with the iterations
"""
points
=
[[
x0
[
0
],
x0
[
1
]]]
# Define function to store iteration points when calling scipy.optimize.minimize
def
store_iterations
(
x
):
points
.
append
([
x
[
0
],
x
[
1
]])
# Compute optimizer and store iterations by using callback
resultINFO
=
scipy
.
optimize
.
minimize
(
f
,
x0
,
method
=
method
,
callback
=
store_iterations
,
options
=
{
'gtol'
:
tol
})
minimizer
=
resultINFO
.
x
return
minimizer
,
resultINFO
,
np
.
asarray
(
points
)
Event Timeline
Log In to Comment