Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60894117
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
Fri, May 3, 05:26
Size
1 KB
Mime Type
text/x-python
Expires
Sun, May 5, 05:26 (2 d)
Engine
blob
Format
Raw Data
Handle
17406672
Attached To
R9484 sp4e-homework-lars-bertil
optimizer.py
View Options
# Import libraries
import
numpy
as
np
import
scipy.optimize
### Global variables (only necessary for plotting of steps taken by optimizer)
# Dimension of problem, default value = 2 will be overwritten by main_minimize
DIM
=
2
# Initial guess to minimizer, default value = (4,4) will be overwritten by main_minimize
STEPS
=
np
.
array
([
4
,
4
])
# This array will contain the steps of the minimizer on its way to the minimum
STEPS
=
STEPS
.
reshape
(
-
1
,
DIM
)
### This function fills the STEPS array, and is used as input to scipy.optimize.minimize
def
my_callback
(
xk
):
global
STEPS
STEPS
=
np
.
vstack
((
STEPS
,
np
.
array
(
xk
)))
#### This function returns the minimum value of the function func
# x: initial guess of the minimum
# tolerance: termination criteria for algortihm
# max_iterations: maximum number of iterations as termination criteria for algortihm
# extra_args: tuple containing matrix A and b to be used if not default A and b used
def
find_minimum
(
func
,
x
,
tolerance
,
max_iterations
,
extra_args
=
()
):
result
=
scipy
.
optimize
.
minimize
(
func
,
x0
=
x
,
args
=
extra_args
,
method
=
'CG'
,
tol
=
tolerance
,
options
=
{
"maxiter"
:
max_iterations
},
callback
=
my_callback
)
return
result
.
x
Event Timeline
Log In to Comment