The objective nonlinear function (NLF) may belong to one of the following types:
NLF.NLF0 - no derivative information available,
NLF.NLF1 - analytic first order derivatives (gradient) available,
NLF.NLF2 - analytic first order and second order derivatives (Hessian) available,
NLF.FDNLF1 - no analytic first order derivatives available but gradient may be approximated by finite differences.
The objective function
should be defined as an object belonging to
a subclass of the AbstractNLF class.
In the constructor method of the function
the dimension of its
domain and its type should be specified .
If
is optimized with an algorithm which uses the gradient, then
the gradient should be calculated by the evaluate method of the function
.
If
is optimized with an algorithm which uses the Hessian, then the
Hessian should be calculated by the evaluate method of the function
.
It is also necessary to choose the initial (starting) point of the
algorithm with the setInitVector method (see Example) applied to the
objective function
.
It is possible to decrease the number of calculated
derivatives if such operation is very expensive. If so, please
invoke the setExpensive()method with parameter
TRUE on the NLF object.
Example 18.1. Objective Function (test function)
from biz.sc.math.opt.nlf import *
class TestFunction(AbstractNLF):
def __init__(self, dim, type):
AbstractNLF.__init__(self, dim, type)
print "Function dimension : ", dim
print "Function type : ", type
def evaluate(self, x, fx, gx, Hx):
"@sig void evaluate(double[ ] x, double[ ] fx, double[ ] gx, double[ ][ ] Hx)"
if (fx != None):
fx[0] = x[0]*x[0]+x[1]*x[1]-2*x[1]
if (gx != None):
gx[0] = 2*x[0]
gx[1] = 2*x[1]-2
if (Hx != None):
Hx[0][0] = Hx[1][1] = 2
Hx[0][1] = Hx[1][0] = 0
dim = 2
f = TestFunction(dim, NLF.NLF2)
f.setInitVector([1.3,0.6])
f.setExpensive(TRUE) # if there is a real necessity
Output:
Function dimension : 2 Function type : 2