<commit_before># Copyright 2012 <NAME>. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from numpy import array, zeros, ones, hstack, delete, insert

from randnums import FltRand, IntRand, FlipCoin

def SimpleChromo(x, nbases):
    """
    SimpleChromo splits x into 'nbases' unequal parts
    Input:
      x -- a single number of a list whose size equals the number of genes
    Output:
      c -- the chromosome (numpy.ndarray)
    Note:
      If x is a list, ngenes = len(x) and then:
      If ngenes > 1, each gene is split into 'nbases' and the following
      chromosome structure is assumed:

          c = [0, 1, 2, ... nbases-1,  0, 1, 2, ... nbases-1]
               \___________________/   \___________________/
                      gene # 0               gene # 1
    """
    if isinstance(x, float):
        vals = FltRand(nbases)
        sumv = sum(vals)
        return x * vals / sumv
    if isinstance(x, list): x = array(x)
    ngenes = len(x)
    c = zeros(nbases * ngenes)
    for i, v in enumerate(x):
        vals = FltRand(nbases)
        sumv = sum(vals)
        a = i * nbases
        b = a + nbases
        c[a:b] = v * vals / sumv
    return c


def Fitness(Y):
    """
    Fitness function: map objective function into [0, 1]
     Y -- objective values
    """
    ymin, ymax = min(Y), max(Y)
    if abs(ymax - ymin) < 1e-14: return ones(len(Y))
    return (ymax - Y) / (ymax - ymin)


def SortPop(C, Y, F):
    """
    SortPop sort individuals by fitness (decreasing order)
     C -- chromosomes/population
     Y -- objective values
     F -- fitness
    """
    I = F.argsort()[::-1] # the [::-1] is a trick to reverse the sorting order
    C = C[I]              # sorted chromosomes
    Y = Y[I]              # sorted objective values
    F = F[I]              # sorted fitness
    return C, Y, F


def RouletteSelect(M, n, sample=None):
    """
    RouletteSelect selects n individuals
    Input:
      M      -- cumulated probabilities (from sorted population)
      sample -- a list of random numbers
    Output:
      S -- selected individuals (indices)
    """
    if sample==None: sample = FltRand(n)
    S = zeros(n, dtype=int) # selected individuals
    for i, s in enumerate(sample):
        for j, m in enumerate(M):
            if m > s:
                S[i] = j
                break
    return S


def SUSselect(M, n, pb=None):
    """
    SUSselect performs the Stochastic-Universal-Sampling selection
    It selects n individuals
    Input:
      M  -- cumulated probabilities (from sorted population)
      pb -- one random number corresponding to the first probability (pointer/position)
    Output:
      S -- selected individuals (indices)
    """
    d = 1.0 / float(n)
    if pb == None: pb = FltRand(1, 0.0, d)
    S = zeros(n, dtype=int) # selected individuals
    for i in range(n):
        j = 0
        while pb > M[j]: j += 1
        pb += d<commit_msg>stochastic universal sampling selection added<commit_after># Copyright 2012 <NAME>. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from numpy import array, zeros, ones, hstack, delete, insert

from randnums import FltRand, IntRand, FlipCoin

def SimpleChromo(x, nbases):
    """
    SimpleChromo splits x into 'nbases' unequal parts
    Input:
      x -- a single number of a list whose size equals the number of genes
    Output:
      c -- the chromosome (numpy.ndarray)
    Note:
      If x is a list, ngenes = len(x) and then:
      If ngenes > 1, each gene is split into 'nbases' and the following
      chromosome structure is assumed:

          c = [0, 1, 2, ... nbases-1,  0, 1, 2, ... nbases-1]
               \___________________/   \___________________/
                      gene # 0               gene # 1
    """
    if isinstance(x, float):
        vals = FltRand(nbases)
        sumv = sum(vals)
        return x * vals / sumv
    if isinstance(x, list): x = array(x)
    ngenes = len(x)
    c = zeros(nbases * ngenes)
    for i, v in enumerate(x):
        vals = FltRand(nbases)
        sumv = sum(vals)
        a = i * nbases
        b = a + nbases
        c[a:b] = v * vals / sumv
    return c


def Fitness(Y):
    """
    Fitness function: map objective function into [0, 1]
     Y -- objective values
    """
    ymin, ymax = min(Y), max(Y)
    if abs(ymax - ymin) < 1e-14: return ones(len(Y))
    return (ymax - Y) / (ymax - ymin)


def SortPop(C, Y, F):
    """
    SortPop sort individuals by fitness (decreasing order)
     C -- chromosomes/population
     Y -- objective values
     F -- fitness
    """
    I = F.argsort()[::-1] # the [::-1] is a trick to reverse the sorting order
    C = C[I]              # sorted chromosomes
    Y = Y[I]              # sorted objective values
    F = F[I]              # sorted fitness
    return C, Y, F


def RouletteSelect(M, n, sample=None):
    """
    RouletteSelect selects n individuals
    Input:
      M      -- cumulated probabilities (from sorted population)
      sample -- a list of random numbers
    Output:
      S -- selected individuals (indices)
    """
    if sample==None: sample = FltRand(n)
    S = zeros(n, dtype=int) # selected individuals
    for i, s in enumerate(sample):
        for j, m in enumerate(M):
            if m > s:
                S[i] = j
                break
    return S


def SUSselect(M, n, pb=None):
    """
    SUSselect performs the Stochastic-Universal-Sampling selection
    It selects n individuals
    Input:
      M  -- cumulated probabilities (from sorted population)
      pb -- one random number corresponding to the first probability (pointer/position)
    Output:
      S -- selected individuals (indices)
    """
    dp = 1.0 / float(n)
    if pb == None: pb = FltRand(1, 0.0, dp)
    S = zeros(n, dtype=int) # selected individuals
    for i in range(n):
        j = 0
        while pb > M[j]: j += 1
        pb += dp
{'fraction_non_alphanumeric': 0.10613874563300615, 'fraction_numerical': 0.009648976875727832, 'mean_word_length': 1.9806643529995043, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '871222', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1851, 'n_words': 689}