<commit_before>import time

import numpy as np

import pymanopt
from pymanopt.solvers.solver import Solver
from pymanopt.solvers.steepest_descent import SteepestDescent


# TODO(nkoep): Check if a suitable autodiff backend is available, and solve the
#              problem using the TR solver if so.
def compute_centroid(manifold, points):
    """Compute the centroid of `points` on the `manifold` as Karcher mean."""
    num_points = len(points)

    @pymanopt.function.Callable
    def objective(y):
        accumulator = 0
        for i in range(num_points):
            accumulator += manifold.dist(y, points[i]) ** 2
        return accumulator / 2

    @pymanopt.function.Callable
    def gradient(y):
        g = manifold.zerovec(y)
        for i in range(num_points):
            g -= manifold.log(y, points[i])
        return g

    # XXX: Manopt runs a few TR iterations here. For us to do this, we either
    #      need to work out the Hessian of the Karcher mean by hand or
    #      implement approximations for the Hessian to use in the TR solver as
    #      Manopt. This is because we cannot implement the Karcher mean with
    #      Theano, say, and compute the Hessian automatically due to dependence
    #      on the manifold-dependent distance function, which is written in
    #      numpy.
    solver = SteepestDescent(maxiter=15)
    problem = pymanopt.Problem(manifold, objective, grad=gradient, verbosity=0)
    return solver.solve(problem)


class NelderMead(Solver):
    """Nelder-Mead alglorithm."""

    def __init__(self, maxcostevals=None, maxiter=None, reflection=1,
                 expansion=2, contraction=0.5, *args, **kwargs):
        """
        Instantiate Nelder-Mead method solver class.
        Variable attributes (defaults in brackets):
            - maxcostevals (max(5000, 2 * dim))
                Maximum number of allowed cost evaluations
            - maxiter (max(500, 4 * dim))
                Maximum number of allowed iterations
            - reflection (1)
                Determines how far to reflect away from the worst vertex;
                stretched (reflection > 1), compressed (0 < reflection < 1),
                or exact (reflection = 1)
            - expansion (2)
                Factor by which to expand the reflected simplex
            - contraction (0.5)
                Factor by which to contract the reflected simplex
        """
        super().__init__(*args, **kwargs)

        self._maxcostevals = maxcostevals
        self._maxiter = maxiter
        self._reflection = reflection
        self._expansion = expansion
        self._contraction = contraction

    def solve(self, problem, x=None):
        """
        Perform optimization using a Nelder-Mead minimization algorithm.
        Arguments:
            - problem
                Pymanopt problem setup using the Problem class, this must
                have a .manifold attribute specifying the manifold to optimize
                over, as well as a cost and enough information to compute
                the gradient of that cost.
            - x=None
                Optional parameter. Initial population of elements on the
                manifold. If None then an initial population will be randomly
                generated
        Returns:
            - x
                Local minimum of obj, or if algorithm terminated before
                convergence x will be the point at which it terminated
        """
        man = problem.manifold
        verbosity = problem.verbosity
        objective = problem.cost

        # Choose proper default algorithm parameters. We need to know about the
        # dimension of the manifold to limit the parameter range, so we have to
        # defer proper initialization until this point.
        dim = man.dim
        if self._maxcostevals is None:
            self._maxcostevals = max(1000, 2 * dim)
        if self._maxiter is None:
            self._maxiter = max(2000, 4 * dim)

        # If no initial simplex x is given by the user, generate one at random.
        if x is None:<commit_msg>Fix sphinx warnings for nelder mead<commit_after>import time

import numpy as np

import pymanopt
from pymanopt.solvers.solver import Solver
from pymanopt.solvers.steepest_descent import SteepestDescent


# TODO(nkoep): Check if a suitable autodiff backend is available, and solve the
#              problem using the TR solver if so.
def compute_centroid(manifold, points):
    """Compute the centroid of `points` on the `manifold` as Karcher mean."""
    num_points = len(points)

    @pymanopt.function.Callable
    def objective(y):
        accumulator = 0
        for i in range(num_points):
            accumulator += manifold.dist(y, points[i]) ** 2
        return accumulator / 2

    @pymanopt.function.Callable
    def gradient(y):
        g = manifold.zerovec(y)
        for i in range(num_points):
            g -= manifold.log(y, points[i])
        return g

    # XXX: Manopt runs a few TR iterations here. For us to do this, we either
    #      need to work out the Hessian of the Karcher mean by hand or
    #      implement approximations for the Hessian to use in the TR solver as
    #      Manopt. This is because we cannot implement the Karcher mean with
    #      Theano, say, and compute the Hessian automatically due to dependence
    #      on the manifold-dependent distance function, which is written in
    #      numpy.
    solver = SteepestDescent(maxiter=15)
    problem = pymanopt.Problem(manifold, objective, grad=gradient, verbosity=0)
    return solver.solve(problem)


class NelderMead(Solver):
    """Nelder-Mead alglorithm.

    Perform optimization using the derivative-free Nelder-Mead minimization
    algorithm.

    Args:
        maxcostevals: Maximum number of allowed cost function evaluations.
        maxiter: Maximum number of allowed iterations.
        reflection: Determines how far to reflect away from the worst vertex:
            stretched (reflection > 1), compressed (0 < reflection < 1),
            or exact (reflection = 1).
        expansion: Factor by which to expand the reflected simplex.
        contraction: Factor by which to contract the reflected simplex.
    """

    def __init__(self, maxcostevals=None, maxiter=None, reflection=1,
                 expansion=2, contraction=0.5, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._maxcostevals = maxcostevals
        self._maxiter = maxiter
        self._reflection = reflection
        self._expansion = expansion
        self._contraction = contraction

    def solve(self, problem, x=None):
        """Run Nelder-Mead algorithm.

        Args:
            problem: Pymanopt problem class instance exposing the cost function
                and the manifold to optimize over.
            x: Initial point on the manifold.
                If no value is provided then a starting point will be randomly
                generated.

        Returns:
            Local minimum of the cost function, or the most recent iterate if
            algorithm terminated before convergence.
        """
        man = problem.manifold
        verbosity = problem.verbosity
        objective = problem.cost

        # Choose proper default algorithm parameters. We need to know about the
        # dimension of the manifold to limit the parameter range, so we have to
        # defer proper initialization until this point.
        dim = man.dim
        if self._maxcostevals is None:
            self._maxcostevals = max(1000, 2 * dim)
        if self._maxiter is None:
            self._maxiter = max(2000, 4 * dim)

        # If no initial simplex x is given by the user, generate one at random.
        if x is None:
{'fraction_non_alphanumeric': 0.06494512588766946, 'fraction_numerical': 0.007876049063912201, 'mean_word_length': 2.215442092154421, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4111144', 'n_tokens_mistral': 2114, 'n_tokens_neox': 1967, 'n_words': 804}