<commit_before>#!/usr/bin/env python2
from __future__ import print_function
import os
import multiprocessing
filesRemaining = []
botScores = {}
def make_variants(variable, robot_file, possibilities):
    """Makes variants of the file robot_file  with the constant variable
    changed for each possibility.

    e.g. if the variable is "ELEPHANTS" and the possibilities are [1, 2, 3],
    this will find the line
    ELEPHANTS = 3
    in robobt_file and make a copy of the file for each value in possibilities.
    1.py will have ELEPHANTS = 1, 2.py will have ELEPHANTS = 2, etc.

    Raises IndexError if the variable name is not found in the file robot_file.
    The line assigning the constant variable must be the first line in that
    file has the variable name in it.
    """
    filenames = []
    with open(robot_file, 'r') as f:
        lines = f.readlines()
        #i = 0
        #while not variable in lines[i]:
        #    i += 1
        for i, line in enumerate(lines):
          if variable in line:
            break
        assert '=' in lines[i]
        for p in possibilities:
            varandp = variable + str(p)
            #lines[i] = variable + " = " + str(p) + '\n'
            lines[i] = "%s = %s\n" % (variable, p)
            filenames.append(varandp)
            with open(varandp, 'w') as pfile:
                for line in lines:
                    pfile.write(line)

    return filenames


def get_current_value(variable, robot_file):
    """
    Returns the value of the constant variable in the robot file.

    This function finds the first line in the file robot_file that has the
    variable name in it, and parses the value after the '=' in that line for a
    float, returning it.

    Raises IndexError if the variable name is not found in the file robot_file.
    The line assigning the constant variable must be the first line in that
    file has the variable name in it.
    """
    with open(robot_file, 'r') as f:
        #i = 0
        #while not variable in lines[i]:
        #    i += 1
        for i, line in enumerate(f):
          if variable in line:
            break
        assert '=' in line
    return float(line[line.index('=') + 1:])


def optimize_variable(enemies, variable, robot_file, processes):
    import shutil
    pool = multiprocessing.Pool(processes)
    """
    Creates a bunch of variants of the file robot_file, each with variable
    changed, then runs a tournament between the variants to find the best one.
    The file robot_fily is modified to contain the best value, and it is
    returned.
    """
    base_value = get_current_value(variable, robot_file)

    precision = 8.0

    while precision >= 0.1:
        print('RUNNING WITH BASE VALUE', base_value, \
                'PRECISION', precision)

        values_to_test = [
            base_value - precision,
            base_value + precision,
            base_value
        ]

        files = make_variants(variable, robot_file, values_to_test)
        best_file = run_tourney(enemies, files, pool)
        best_value = values_to_test[files.index(best_file)]
        if best_value == base_value:
            precision /= 2.0
            print('best value remains', best_value)
            print('decreasing precision to', precision)
        else:
            base_value = best_value
            print('new \'best\' value is', best_value)

    shutil.copy(make_variants(variable, robot_file, [base_value])[0],
                robot_file)

    return base_value

def run_match(bot1, bot2):
    import re
    import subprocess
    """Runs a match between two robot files."""
    p = subprocess.Popen(
        'rgrun -H ' + bot1 + ' ' + bot2,
        shell=True, stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    pall = re.compile('\d+')

    try:
        for line in p.stdout.readlines():
            if line[0] == '[':
                scores = pall.findall(line)
                if scores[0] > scores[1]:
                    return int(scores[0]), int(scores[1]), int(scores[0]) - int(scores[1]),bot1
                elif scores[1] > scores[0]:
                    return int(scores[0]), int(scores[1]), int(scores[1]) - int(scores[0]),bot2
                else:
                    return int(scores[0]), int(scores[1]), 0,'tie'


<commit_msg>forgot something also score is returned in tuples now becuase it houldn;t be changable<commit_after>#!/usr/bin/env python2
from __future__ import print_function
import os
import multiprocessing
filesRemaining = []
botScores = {}
def make_variants(variable, robot_file, possibilities):
    """Makes variants of the file robot_file  with the constant variable
    changed for each possibility.

    e.g. if the variable is "ELEPHANTS" and the possibilities are [1, 2, 3],
    this will find the line
    ELEPHANTS = 3
    in robobt_file and make a copy of the file for each value in possibilities.
    1.py will have ELEPHANTS = 1, 2.py will have ELEPHANTS = 2, etc.

    Raises IndexError if the variable name is not found in the file robot_file.
    The line assigning the constant variable must be the first line in that
    file has the variable name in it.
    """
    filenames = []
    with open(robot_file, 'r') as f:
        #i = 0
        #while not variable in lines[i]:
        #    i += 1
        for i, line in enumerate(f):
          if variable in line:
            break
        assert '=' in lines[i]
        for p in possibilities:
            varandp = variable + str(p)
            #lines[i] = variable + " = " + str(p) + '\n'
            lines[i] = "%s = %s\n" % (variable, p)
            filenames.append(varandp)
            with open(varandp, 'w') as pfile:
                for line in lines:
                    pfile.write(line)

    return filenames


def get_current_value(variable, robot_file):
    """
    Returns the value of the constant variable in the robot file.

    This function finds the first line in the file robot_file that has the
    variable name in it, and parses the value after the '=' in that line for a
    float, returning it.

    Raises IndexError if the variable name is not found in the file robot_file.
    The line assigning the constant variable must be the first line in that
    file has the variable name in it.
    """
    with open(robot_file, 'r') as f:
        #i = 0
        #while not variable in lines[i]:
        #    i += 1
        for i, line in enumerate(f):
          if variable in line:
            break
        assert '=' in line
    return float(line[line.index('=') + 1:])


def optimize_variable(enemies, variable, robot_file, processes):
    import shutil
    pool = multiprocessing.Pool(processes)
    """
    Creates a bunch of variants of the file robot_file, each with variable
    changed, then runs a tournament between the variants to find the best one.
    The file robot_fily is modified to contain the best value, and it is
    returned.
    """
    base_value = get_current_value(variable, robot_file)

    precision = 8.0

    while precision >= 0.1:
        print('RUNNING WITH BASE VALUE', base_value, \
                'PRECISION', precision)

        values_to_test = [
            base_value - precision,
            base_value + precision,
            base_value
        ]

        files = make_variants(variable, robot_file, values_to_test)
        best_file = run_tourney(enemies, files, pool)
        best_value = values_to_test[files.index(best_file)]
        if best_value == base_value:
            precision /= 2.0
            print('best value remains', best_value)
            print('decreasing precision to', precision)
        else:
            base_value = best_value
            print('new \'best\' value is', best_value)

    shutil.copy(make_variants(variable, robot_file, [base_value])[0],
                robot_file)

    return base_value

def run_match(bot1, bot2):
    import re
    import subprocess
    """Runs a match between two robot files."""
    p = subprocess.Popen(
        'rgrun -H ' + bot1 + ' ' + bot2,
        shell=True, stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    pall = re.compile('\d+')

    try:
        for line in p.stdout.readlines():
            if line[0] == '[':
                scores = pall.findall(line)
                if scores[0] > scores[1]:
                    return int(scores[0]), int(scores[1]), int(scores[0]) - int(scores[1]),bot1
                elif scores[1] > scores[0]:
                    return int(scores[0]), int(scores[1]), int(scores[1]) - int(scores[0]),bot2
                else:
                    return int(scores[0]),  int(scores[1]), 0,'tie'



{'fraction_non_alphanumeric': 0.09321445113478462, 'fraction_numerical': 0.009958314034275127, 'mean_word_length': 2.1977045538689373, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2695802', 'n_tokens_mistral': 2647, 'n_tokens_neox': 2505, 'n_words': 883}