Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F62192673
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
Sat, May 11, 13:01
Size
1 KB
Mime Type
text/x-python
Expires
Mon, May 13, 13:01 (2 d)
Engine
blob
Format
Raw Data
Handle
17614337
Attached To
R9482 SP4E_Homework_Ashtari_Sieber
optimizer.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 10 10:19:11 2019
Authors: Omid Ashtari and Armand Sieber
Description: Scripts intended to minimize a quadratic function using scipy.optimize built-in function.
The function to minimize is defined as S(X)= X'AX - X'B, with X = [x, y], X' the transform of X
A is a 2x2 matrix and B 2X1 vector
"""
import
numpy
as
np
import
sys
from
scipy.optimize
import
minimize
import
post_processing
def
objective
(
X
,
*
args
):
#objective function to be minimized
A
=
args
[
0
]
B
=
args
[
1
]
.
T
S
=
np
.
dot
(
X
.
T
,
np
.
dot
(
A
,
X
))
-
np
.
dot
(
X
.
T
,
B
)
return
S
def
reporter
(
X_r
):
global
X_int
X_int
=
np
.
vstack
([
X_int
,
X_r
])
return
0
# Import minimization method
if
len
(
sys
.
argv
)
<
2
:
print
(
'Solver method missing. Default method implemented --> CG'
)
method
=
'CG'
else
:
method
=
sys
.
argv
[
1
]
available_methods
=
(
'Nelder-Mead'
,
'Powell'
,
'CG'
,
'BFGS'
,
'L-BFGS-B'
,
'TNC'
,
'SLSQP'
)
if
method
in
available_methods
:
# test availibility of user inputs
print
(
'Minimzation performed with the
%s
solver'
%
method
)
else
:
sys
.
exit
(
'Minimization method missing or not available!'
)
# Parameters of the function to minimize
A
=
np
.
array
([[
4
,
0
],
[
1
,
3
]])
B
=
np
.
array
([
0
,
1
])
# Minimization process
X0
=
np
.
array
([
3
,
2
])
#initial guess
X_int
=
X0
#stores solution at each step
solution
=
minimize
(
objective
,
X0
,
args
=
(
A
,
B
),
method
=
method
,
callback
=
reporter
,
options
=
{
'maxiter'
:
100
})
print
(
solution
)
print
(
X_int
)
# Post-processing
post_processing
.
plot_results
(
X_int
,
A
,
B
,
method
)
Event Timeline
Log In to Comment