Пример #1
0
    def __init__(self, f, x0, range=None, h=0.5, emax=1e-8, imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A one variable only function to be optimized. The function should
            have only one parameter and return the function value.
          x0
            First estimate of the minimum. Since this is a linear method, this
            should be a ``float`` or ``int``.
          range
            A range of values might be passed to the algorithm, but it is not
            necessary. If supplied, this parameter should be a tuples of two
            values, ``(x0, x1)``, where ``x0`` is the start of the interval, and
            ``x1`` its end. Obviously, ``x0`` should be smaller than ``x1``.
            When this parameter is present, the algorithm will not let the
            estimates fall outside the given interval.
          h
            The initial step of the search. Defaults to 0.5
          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.
          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = x0
        self.range = range
        '''Holds the range for the estimates. If this attribute is set, the
        algorithm will never let the estimates fall outside the given
        interval.'''
        self.__h = h
        self.__emax = float(emax)
        self.__imax = int(imax)
Пример #2
0
	def __init__(self, function, debug=0, fstop=None, maxiter=None, 
					reflect=1.0, expand=2.0, outerContract=0.5, innerContract=-0.5, shrink=0.5, 
					reltol=1e-15, ftol=1e-15, xtol=1e-9, simplex=None, looseContraction=False):
		Optimizer.__init__(self, function, debug, fstop, maxiter)
		
		# Coefficients
		self.reflect=reflect
		self.expand=expand
		self.outerContract=outerContract
		self.innerContract=innerContract
		self.shrink=shrink
		
		# Stopping condition
		self.reltol=reltol
		self.ftol=ftol
		self.xtol=xtol
		
		# Simplex
		self.simplex=simplex
		
		# Modifications
		self.looseContraction=looseContraction
Пример #3
0
    def __init__(self, f, x0, emax=1e-8, imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A one variable only function to be optimized. The function should
            have only one parameter and return the function value.
          x0
            First estimate of the minimum. The Fibonacci search needs two
            estimates to partition the interval. Thus, the first estimate must
            be a duple ``(xl, xh)``, with the property that ``xl < xh``. Be
            aware, however, that no checking is done -- if the estimate doesn't
            correspond to this condition, in some point an exception will be
            raised.

            Notice that, given the nature of the estimate of the Fibonacci
            method, it is not necessary to have a specific parameter to restrict
            the range of acceptable values -- it is already embedded in the
            estimate. If you need to restrict your estimate between an interval,
            just use its limits as ``xl`` and ``xh`` in the estimate.
          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.
          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = x0
        self.__k1 = 1.
        self.__k2 = 1.
        self.__emax = float(emax)
        self.__imax = int(imax)
Пример #4
0
    def __init__(self, f, x0, ranges=None, df=None, h=0.1, emax=1e-5, imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A multivariable function to be optimized. The function should have
            only one parameter, a multidimensional line-vector, and return the
            function value, a scalar.

          x0
            First estimate of the minimum. Estimates can be given in any format,
            but internally they are converted to a one-dimension vector, where
            each component corresponds to the estimate of that particular
            variable. The vector is computed by flattening the array.

          ranges
            A range of values might be passed to the algorithm, but it is not
            necessary. If supplied, this parameter should be a list of ranges
            for each variable of the objective function. It is specified as a
            list of tuples of two values, ``(x0, x1)``, where ``x0`` is the
            start of the interval, and ``x1`` its end. Obviously, ``x0`` should
            be smaller than ``x1``. It can also be given as a list with a simple
            tuple in the same format. In that case, the same range will be
            applied for every variable in the optimization.

          df
            A function to calculate the gradient vector of the cost function
            ``f``. Defaults to ``None``, if no gradient is supplied, then it is
            estimated from the cost function using Euler equations.

          h
            Convergence step. This method does not takes into consideration the
            possibility of varying the convergence step, to avoid Stiefel cages.

          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.

          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = array(x0).ravel()
        if df is None:
            self.__df = gradient(f)
        else:
            self.__df = df
        self.__B = inv(hessian(self.__f)(self.x))
        self.__h = h

        # Determine ranges of the variables
        if ranges is not None:
            ranges = list(ranges)
            if len(ranges) == 1:
                ranges = array(ranges * len(x0[0]))
            else:
                ranges = array(ranges)
        self.ranges = ranges
        '''Holds the ranges for every variable. Although it is a writable
        property, care should be taken in changing parameters before ending the
        convergence.'''

        self.__emax = float(emax)
        self.__imax = int(imax)
Пример #5
0
    def __init__(self,
                 f,
                 x0,
                 ranges=None,
                 df=None,
                 h=0.1,
                 emax=1e-5,
                 imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A multivariable function to be optimized. The function should have
            only one parameter, a multidimensional line-vector, and return the
            function value, a scalar.

          x0
            First estimate of the minimum. Estimates can be given in any format,
            but internally they are converted to a one-dimension vector, where
            each component corresponds to the estimate of that particular
            variable. The vector is computed by flattening the array.

          ranges
            A range of values might be passed to the algorithm, but it is not
            necessary. If supplied, this parameter should be a list of ranges
            for each variable of the objective function. It is specified as a
            list of tuples of two values, ``(x0, x1)``, where ``x0`` is the
            start of the interval, and ``x1`` its end. Obviously, ``x0`` should
            be smaller than ``x1``. It can also be given as a list with a simple
            tuple in the same format. In that case, the same range will be
            applied for every variable in the optimization.

          df
            A function to calculate the gradient vector of the cost function
            ``f``. Defaults to ``None``, if no gradient is supplied, then it is
            estimated from the cost function using Euler equations.

          h
            Convergence step. This method does not takes into consideration the
            possibility of varying the convergence step, to avoid Stiefel cages.

          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.

          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = array(x0).ravel()
        if df is None:
            self.__df = gradient(f)
        else:
            self.__df = df
        self.__B = inv(hessian(self.__f)(self.x))
        self.__h = h

        # Determine ranges of the variables
        if ranges is not None:
            ranges = list(ranges)
            if len(ranges) == 1:
                ranges = array(ranges * len(x0[0]))
            else:
                ranges = array(ranges)
        self.ranges = ranges
        '''Holds the ranges for every variable. Although it is a writable
        property, care should be taken in changing parameters before ending the
        convergence.'''

        self.__emax = float(emax)
        self.__imax = int(imax)
Пример #6
0
    def __init__(self, f, x0, ranges=None, h=0.5, emax=1e-8, imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A multivariable function to be optimized. The function should have
            only one parameter, a multidimensional line-vector, and return the
            function value, a scalar.

          x0
            First estimate of the minimum. Estimates can be given in any format,
            but internally they are converted to a one-dimension vector, where
            each component corresponds to the estimate of that particular
            variable. The vector is computed by flattening the array.

          ranges
            A range of values might be passed to the algorithm, but it is not
            necessary. If supplied, this parameter should be a list of ranges
            for each variable of the objective function. It is specified as a
            list of tuples of two values, ``(x0, x1)``, where ``x0`` is the
            start of the interval, and ``x1`` its end. Obviously, ``x0`` should
            be smaller than ``x1``. It can also be given as a list with a simple
            tuple in the same format. In that case, the same range will be
            applied for every variable in the optimization.

          h
            The initial step of the search. Defaults to 0.5

          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.

          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = array(x0).ravel()
        n = self.__x.size
        self.__h = ones((n, ))
        self.__h[0] = -0.5
        self.__dx = h * eye(n, 1).reshape(self.__x.shape)

        # Determine ranges of the variables
        if ranges is not None:
            ranges = list(ranges)
            if len(ranges) == 1:
                ranges = array(ranges * len(x0[0]))
            else:
                ranges = array(ranges)
        self.ranges = ranges
        '''Holds the ranges for every variable. Although it is a writable
        property, care should be taken in changing parameters before ending the
        convergence.'''

        self.__emax = float(emax)
        self.__imax = int(imax)
Пример #7
0
    def __init__(self, f, x0, ranges=None, h=0.5, emax=1e-8, imax=1000):
        '''
        Initializes the optimizer.

        To create an optimizer of this type, instantiate the class with the
        parameters given below:

        :Parameters:
          f
            A multivariable function to be optimized. The function should have
            only one parameter, a multidimensional line-vector, and return the
            function value, a scalar.

          x0
            First estimate of the minimum. Estimates can be given in any format,
            but internally they are converted to a one-dimension vector, where
            each component corresponds to the estimate of that particular
            variable. The vector is computed by flattening the array.

          ranges
            A range of values might be passed to the algorithm, but it is not
            necessary. If supplied, this parameter should be a list of ranges
            for each variable of the objective function. It is specified as a
            list of tuples of two values, ``(x0, x1)``, where ``x0`` is the
            start of the interval, and ``x1`` its end. Obviously, ``x0`` should
            be smaller than ``x1``. It can also be given as a list with a simple
            tuple in the same format. In that case, the same range will be
            applied for every variable in the optimization.

          h
            The initial step of the search. Defaults to 0.5

          emax
            Maximum allowed error. The algorithm stops as soon as the error is
            below this level. The error is absolute.

          imax
            Maximum number of iterations, the algorithm stops as soon this
            number of iterations are executed, no matter what the error is at
            the moment.
        '''
        Optimizer.__init__(self)
        self.__f = f
        self.__x = array(x0).ravel()
        n = self.__x.size
        self.__h = ones((n, ))
        self.__h[0] = -0.5
        self.__dx = h * eye(n, 1).reshape(self.__x.shape)

        # Determine ranges of the variables
        if ranges is not None:
            ranges = list(ranges)
            if len(ranges) == 1:
                ranges = array(ranges * len(x0[0]))
            else:
                ranges = array(ranges)
        self.ranges = ranges
        '''Holds the ranges for every variable. Although it is a writable
        property, care should be taken in changing parameters before ending the
        convergence.'''

        self.__emax = float(emax)
        self.__imax = int(imax)