<commit_before>import logging

import numpy as np

logging.basicConfig(level=logging.DEBUG)

class NelderMead:
    def __init__(self, objective, simplex):
        """
        Arguments:
        objective -- objective function to be minimised. It is
        assumed the function takes a numpy array (vector) as input.
        simplex -- initial simplex. It is assumed the simplex is
        a 2D numpy array.
        """
        self.objective = objective
        self.simplex = simplex
        self.alpha = 1
        self.beta = 0.5
        self.gamma = 2
        self.epsilon = 1e-1

    def solve(self):
        """
        Returns numpy array that minimises the objective function.
        """
        # Initialise numpy arrays
        n = self.simplex.shape[0] - 1
        func_simplex = np.empty(n+1, dtype=np.float)
        func_simplex_without_1 = np.empty(n, dtype=np.float)

        for i in range(14):
            logging.debug("Current simplex:\n%s", self.simplex)

            # Compute function values for the current simplex
            for i in np.arange(n+1):
                func_simplex[i] = self.objective(self.simplex[i])
            logging.debug("Function values for the simplex:\n%s", func_simplex)

            # Compute x for which the objective assumes the highest value
            high_index = np.argmax(func_simplex)
            high = self.simplex[high_index]
            logging.debug("Current maximum: %s", high)

            # Compute function values for the simplex without the highest
            # value
            j = 0
            for i in np.arange(n):
                value = self.simplex[i]

                if (value == high).all():
                    continue

                func_simplex_without_1[j] = self.objective(value)
                j += 1

            # Compute the centroid value
            centroid = self.__centroid(high)

            # Check for convergence to minimum
            centroids = np.ones(n+1, dtype=np.float) * self.objective(centroid)
            print(np.sqrt(1 / n+1 * np.sum((func_simplex - centroids)**2)))
            if np.sqrt(1 / n+1 * np.sum((func_simplex - centroids)**2)) < self.epsilon:
                break

            # Compute x for which the objective assumes the lowest value
            low = self.simplex[np.argmin(func_simplex)]

            # Reflect
            reflection = self.__reflect(centroid, high)

            if self.objective(low) > self.objective(reflection):
                # Reflection is the new lowest value. Expand
                expansion = self.__expand(centroid, reflection)

                if self.objective(reflection) > self.objective(expansion):
                    # Expansion is the new highest value. Create new simplex
                    self.simplex[high_index] = expansion

                else:
                    # Expansion failed. Reflection is the new highest value.
                    # Create new simplex
                    self.simplex[high_index] = reflection

            elif np.amax(func_simplex_without_1, axis=0) >= self.objective(reflection):
                # Reflection is the new highest value. Create new simplex
                self.simplex[high_index] = reflection

            else:
                # Define augmented highest value
                if self.objective(high) > self.objective(reflection):
                    high = reflection
                
                # Contract
                contraction = self.__contract(centroid, high)

                if self.objective(contraction) <= self.objective(high):
                    # Contraction is the new highest value.
                    # Create new simplex
                    self.simplex[high_index] = contraction

                else:<commit_msg>Fix improper convergence of the algorithm.<commit_after>import logging

import numpy as np

logging.basicConfig(level=logging.DEBUG)

class NelderMead:
    def __init__(self, objective, simplex):
        """
        Arguments:
        objective -- objective function to be minimised. It is
        assumed the function takes a numpy array (vector) as input.
        simplex -- initial simplex. It is assumed the simplex is
        a 2D numpy array.
        """
        self.objective = objective
        self.simplex = simplex
        self.alpha = 1
        self.beta = 0.5
        self.gamma = 2
        self.epsilon = 1e-6

    def solve(self):
        """
        Returns numpy array that minimises the objective function.
        """
        # Initialise numpy arrays
        n = self.simplex.shape[0] - 1
        func_simplex = np.empty(n+1, dtype=np.float)
        func_simplex_without_1 = np.empty(n, dtype=np.float)

        while True:
            logging.debug("Current simplex:\n%s", self.simplex)

            # Compute function values for the current simplex
            for i in np.arange(n+1):
                func_simplex[i] = self.objective(self.simplex[i])

            # Compute x for which the objective assumes the highest value
            high_index = np.argmax(func_simplex)
            high = self.simplex[high_index]
            f_high = self.objective(high)

            # Compute function values for the simplex without the highest
            # value
            j = 0
            for i in np.arange(n+1):
                value = self.simplex[i]

                if (value == high).all():
                    continue

                func_simplex_without_1[j] = self.objective(value)
                j += 1

            # Compute the centroid value
            centroid = self.__centroid(high)

            # Check for convergence to minimum
            centroids = np.ones(n+1, dtype=np.float) * self.objective(centroid)
            stopping_condition = np.sqrt(np.sum((func_simplex - centroids)**2) / (n+1))
            logging.debug("Stopping condition: %f", stopping_condition)
            if stopping_condition < self.epsilon:
                break

            # Compute x for which the objective assumes the lowest value
            low = self.simplex[np.argmin(func_simplex)]
            f_low = self.objective(low)

            # Reflect
            reflection = self.__reflect(centroid, high)
            f_reflection = self.objective(reflection)

            if f_low > f_reflection:
                # Reflection is the new lowest value. Expand
                expansion = self.__expand(centroid, reflection)
                f_expansion = self.objective(expansion)

                if f_reflection > f_expansion:
                    # Expansion is the new highest value. Create new simplex
                    self.simplex[high_index] = expansion

                else:
                    # Expansion failed. Reflection is the new highest value.
                    # Create new simplex
                    self.simplex[high_index] = reflection

            elif np.amax(func_simplex_without_1, axis=0) >= f_reflection and f_reflection >= f_low:
                # Reflection is the new highest value. Create new simplex
                self.simplex[high_index] = reflection

            else:
                # Define augmented highest value
                if f_high > f_reflection:
                    high = reflection
                    f_high = self.objective(high)
                
                # Contract
                contraction = self.__contract(centroid, high)
                f_contraction = self.objective(contraction)

                if f_contraction <= f_high:
                    # Contraction is the new highest value.
                    # Create new simplex
                    self.simplex[high_index] = contraction

                else:
{'fraction_non_alphanumeric': 0.07814535035165408, 'fraction_numerical': 0.006121385777546236, 'mean_word_length': 1.624401913875598, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '5119311', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1936, 'n_words': 560}