<commit_before>from random import shuffle, randint

from knapsack.problem import *


def add_random(size, weights, costs, current):
    indexes = list(range(len(weights)))
    shuffle(indexes)
    while len(indexes) > 0:
        result = list(current)
        candidate_index = indexes.pop()

        if result[candidate_index] != 0:
            continue

        result[candidate_index] = 1
        if validate(result, costs, weights, size):
            return result
    return current


def add_best(size, weights, costs, current):
    priorities = list(map(lambda x: x[0] / float(x[1]), zip(costs, weights)))
    index_priorities = list(zip(range(len(weights)), priorities))
    index_priorities.sort(key=lambda x: x[1])

    while len(index_priorities) > 0:
        result = list(current)
        candidate_index, _ = index_priorities.pop()

        if result[candidate_index] != 0:
            continue

        result[candidate_index] = 1
        if validate(result, costs, weights, size):
            return result
    return current


def remove_random(size, weights, costs, current):
    indexes = [i for i in range(len(current)) if current[i] == 1]

    if len(indexes) == 0:
        return current

    delete_index = indexes[randint(0, len(indexes) - 1)]
    current[delete_index] = 0
    return current


def remove_worst(size, weights, costs, current):
    indexes = [i for i in range(len(current)) if current[i] == 1]<commit_msg>optionalize validation for simple ksp heuristics<commit_after>from random import shuffle, randint

from knapsack.problem import *


def add_random(size, weights, costs, current, validation=True):
    indexes = list(range(len(weights)))
    shuffle(indexes)
    while len(indexes) > 0:
        result = list(current)
        candidate_index = indexes.pop()

        if result[candidate_index] != 0:
            continue

        result[candidate_index] = 1
        if not validation or validate(result, costs, weights, size):
            return result
    return current


def add_best(size, weights, costs, current, validation=True):
    priorities = list(map(lambda x: x[0] / float(x[1]), zip(costs, weights)))
    index_priorities = list(zip(range(len(weights)), priorities))
    index_priorities.sort(key=lambda x: x[1])

    while len(index_priorities) > 0:
        result = list(current)
        candidate_index, _ = index_priorities.pop()

        if result[candidate_index] != 0:
            continue

        result[candidate_index] = 1
        if not validation or validate(result, costs, weights, size):
            return result
    return current


def remove_random(size, weights, costs, current):
    indexes = [i for i in range(len(current)) if current[i] == 1]

    if len(indexes) == 0:
        return current

    delete_index = indexes[randint(0, len(indexes) - 1)]
    current[delete_index] = 0
    return current


def remove_worst(size, weights, costs, current):
    indexes = [i for i in range(len(current)) if current[i] == 1]
{'fraction_non_alphanumeric': 0.11018363939899833, 'fraction_numerical': 0.01001669449081803, 'mean_word_length': 3.0817438692098094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2861227', 'n_tokens_mistral': 947, 'n_tokens_neox': 892, 'n_words': 250}