<commit_before>import numpy

from ._helpers import delete_shuffle_sync, delete_shuffle_async, strategies
from .._common import messages, parallelize, selection_sync, selection_async
from .._helpers import register, OptimizeResult

__all__ = [
    "minimize",
]


def minimize(
    fun,
    bounds,
    x0=None,
    args=(),
    options={},
):
    # Cost function
    if not hasattr(fun, "__call__"):
        raise TypeError()

    # Dimensionality and search space
    if numpy.ndim(bounds) != 2:
        raise ValueError()

    ndim = len(bounds)
    lower, upper = numpy.transpose(bounds)

    # Initial guess x0
    if x0 is not None:
        if numpy.ndim(x0) != 2 or numpy.shape(x0)[1] != ndim:
            raise ValueError()

    # Options
    _options = {
        "maxiter": 100,
        "popsize": 10,
        "CR": 0.1,
        "F": 0.5,
        "strategy": "rand1",
        "xtol": 1.0e-8,
        "ftol": 1.0e-8,
        "constraints": False,
        "sync": True,
        "parallel": False,
    }
    _options.update(options)

    maxiter = _options["maxiter"]
    popsize = _options["popsize"]
    F = _options["F"]
    CR = _options["CR"]
    strategy = _options["strategy"]
    xtol = _options["xtol"]
    ftol = _options["ftol"]
    constraints = _options["constraints"]
    sync = _options["sync"]
    parallel = _options["parallel"]

    # Population size
    if popsize < 2:
        raise ValueError()

    if x0 is not None and len(x0) != popsize:
        raise ValueError()

    # DE parameters
    if not 0.0 <= F <= 2.0:
        raise ValueError()

    if not 0.0 <= CR <= 1.0:
        raise ValueError()

    mut = strategies[strategy]

    # Constraints
    cons = constrain(constraints, lower, upper)

    # Synchronize
    de_iter = de_sync if sync else de_async

    # Parallel
    fun = parallelize(fun, args, sync, parallel)

    # Initialize arrays
    xall = numpy.empty((popsize, ndim, maxiter))
    funall = numpy.empty((popsize, maxiter))
    U = numpy.empty((popsize, ndim))

    # Initial population
    X = (
        x0
        if x0 is not None
        else numpy.random.uniform(lower, upper, (popsize, ndim)) 
    )

    # Evaluate initial population
    pfit = fun(X) if sync else numpy.array([fun(xx) for xx in X])
    pbestfit = numpy.array(pfit)

    # Initial best solution
    gbidx = numpy.argmin(pbestfit)
    gfit = pbestfit[gbidx]
    gbest = numpy.copy(X[gbidx])

    # Iterate until one of the termination criterion is satisfied
    it = 1
    converged = False
    while not converged:
        it += 1

        r1 = numpy.random.rand(popsize, ndim)
        X, gbest, pbestfit, gfit, pfit, status = de_iter(it, X, U, gbest, pbestfit, gfit, pfit, F, CR, r1, maxiter, xtol, ftol, fun, mut, cons)

        xall[:, :, it - 1] = numpy.copy(X)
        funall[:, it - 1] = numpy.copy(pbestfit)

        converged = status is not None

    return OptimizeResult(
        x=gbest,
        success=status >= 0,
        status=status,
        message=messages[status],
        fun=gfit,
        nfev=it * popsize,
        nit=it,<commit_msg>fix initial arrays<commit_after>import numpy

from ._helpers import delete_shuffle_sync, delete_shuffle_async, strategies
from .._common import messages, parallelize, selection_sync, selection_async
from .._helpers import register, OptimizeResult

__all__ = [
    "minimize",
]


def minimize(
    fun,
    bounds,
    x0=None,
    args=(),
    options={},
):
    # Cost function
    if not hasattr(fun, "__call__"):
        raise TypeError()

    # Dimensionality and search space
    if numpy.ndim(bounds) != 2:
        raise ValueError()

    ndim = len(bounds)
    lower, upper = numpy.transpose(bounds)

    # Initial guess x0
    if x0 is not None:
        if numpy.ndim(x0) != 2 or numpy.shape(x0)[1] != ndim:
            raise ValueError()

    # Options
    _options = {
        "maxiter": 100,
        "popsize": 10,
        "CR": 0.1,
        "F": 0.5,
        "strategy": "rand1",
        "xtol": 1.0e-8,
        "ftol": 1.0e-8,
        "constraints": False,
        "sync": True,
        "parallel": False,
    }
    _options.update(options)

    maxiter = _options["maxiter"]
    popsize = _options["popsize"]
    F = _options["F"]
    CR = _options["CR"]
    strategy = _options["strategy"]
    xtol = _options["xtol"]
    ftol = _options["ftol"]
    constraints = _options["constraints"]
    sync = _options["sync"]
    parallel = _options["parallel"]

    # Population size
    if popsize < 2:
        raise ValueError()

    if x0 is not None and len(x0) != popsize:
        raise ValueError()

    # DE parameters
    if not 0.0 <= F <= 2.0:
        raise ValueError()

    if not 0.0 <= CR <= 1.0:
        raise ValueError()

    mut = strategies[strategy]

    # Constraints
    cons = constrain(constraints, lower, upper)

    # Synchronize
    de_iter = de_sync if sync else de_async

    # Parallel
    fun = parallelize(fun, args, sync, parallel)

    # Initial population
    X = (
        x0
        if x0 is not None
        else numpy.random.uniform(lower, upper, (popsize, ndim)) 
    )
    U = numpy.empty((popsize, ndim))

    # Evaluate initial population
    pfit = fun(X) if sync else numpy.array([fun(xx) for xx in X])
    pbestfit = numpy.array(pfit)

    # Initial best solution
    gbidx = numpy.argmin(pbestfit)
    gfit = pbestfit[gbidx]
    gbest = numpy.copy(X[gbidx])

    # Initialize arrays
    xall = numpy.empty((popsize, ndim, maxiter))
    funall = numpy.empty((popsize, maxiter))
    xall[:, :, 0] = numpy.copy(X)
    funall[:, 0] = numpy.copy(pfit)

    # Iterate until one of the termination criterion is satisfied
    it = 1
    converged = False
    while not converged:
        it += 1

        r1 = numpy.random.rand(popsize, ndim)
        X, gbest, pbestfit, gfit, pfit, status = de_iter(it, X, U, gbest, pbestfit, gfit, pfit, F, CR, r1, maxiter, xtol, ftol, fun, mut, cons)

        xall[:, :, it - 1] = numpy.copy(X)
        funall[:, it - 1] = numpy.copy(pbestfit)

        converged = status is not None

    return OptimizeResult(
        x=gbest,
        success=status >= 0,
        status=status,
        message=messages[status],
        fun=gfit,
        nfev=it * popsize,
        nit=it,
{'fraction_non_alphanumeric': 0.12245552171822408, 'fraction_numerical': 0.014425388684083988, 'mean_word_length': 2.326226012793177, 'pattern_counts': {'":': 20, '<': 13, '<?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': '6409593', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2101, 'n_words': 567}