\(\newcommand{\B}[1]{ {\bf #1} }\) \(\newcommand{\R}[1]{ {\rm #1} }\)
numeric_optimize_fun_class#
View page sourceA Helper Class That Defines Functions Needed for Optimization#
Syntax#
optimize_fun = optimize_fun_class ( objective_ad , constraint_ad )
Purpose#
This class is an aid solving optimization problems of the form
where \(x\) is a vector, \(f(x)\) is a scalar, and \(a, g(x), b\) are all vectors with the same length. We use \(n\), \(m\) for the length of the vectors \(x\) and \(g(x)\) respectively.
objective_ad#
This is a d_fun representation of the function \(f(x)\).
constraint_ad#
This is a d_fun representation of the function \(g(x)\).
optimize_fun#
This class object has the following functions defined:
objective_fun#
The syntax
.objective_fun ( x )sets \(y = f(x)\) where x is a numpy vector with length n and y is a scalar.
objective_grad#
The syntax
.objective_grad ( x )sets \(z = f^{(1)} (x)\) where x and z are numpy vectors with length n .
objective_hess#
The syntax
.objective_hess ( x )sets \(h = f^{(2)} (x)\) where x is a numpy vector with length n and h is a numpy n by n matrix.
constraint_fun#
The syntax
.constraint_fun ( x )sets \(y = g(x)\) where x ( y ) is a numpy vector with length n ( m ).
constraint_jac#
The syntax
.constraint_jac ( x )sets \(J = g^{(1)} (x)\) where x is a numpy vector with length n and J is a numpy m by n matrix.
constraint_hess#
The syntax
.constraint_hess ( x , v )sets
where x is a numpy vector with length n and H is a numpy n by n matrix.
Example#
Source Code#
import numpy
import cppad_py
class optimize_fun_class :
def __init__(self, objective_ad, constraint_ad=None) :
self.objective_ad = objective_ad
self.constraint_ad = constraint_ad
#
def objective_fun(self, x) :
# objective as a vector
y = self.objective_ad.forward(0, x)
return y[0]
#
def objective_grad(self, x) :
# Jacobian as a matrix
J = self.objective_ad.jacobian(x)
# change to a vector
return J.flatten()
#
def objective_hess(self, x) :
w = numpy.array( [ 1.0 ] )
H = self.objective_ad.hessian(x, w)
return H
#
def constraint_fun(self, x) :
return self.constraint_ad.forward(0, x)
#
def constraint_jac(self, x) :
# Jacobian as a matrix
J = self.constraint_ad.jacobian(x)
return J
#
def constraint_hess(self, x, v) :
H = self.constraint_ad.hessian(x, v)
return H