<commit_before>############################################################################
# Copyright 2016 <NAME>                                          #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#     http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
############################################################################

""" Run simulations for the parameters returned by get_parameters()

Writes the results to disk as .csv files. Will also write the best
assignment matrix found to disk in the same directory.
"""

import math
import os
import warnings
from multiprocessing import Pool
import pandas as pd
import evaluation
import model

class Simulator(object):
    """ Create a simulator.

    Writes the results to disk as .csv files. Will also write the best
    assignment matrix found to disk in the same directory.
    """

    def __init__(self, solver=None, par_eval=None, directory=None,
                 num_assignments=1, num_samples=1000, rerun=False,
                 verbose=False):
        """Creates a assignment performance simulator.

        Writes the results to disk as .csv files. Will also write the best
        assignment matrix found to disk in the same directory.

        Args:

        solver: The solver to use when creating assignments. A
        par_eval method must be provided if set to None.

        par_eval: A method that takes a parameter object as its single
        argument and returns a dict with entries 'load' and 'delay'
        (and possibly others). This argument must be None if a solver
        is provided.

        directory: The directory to store the results in.

        num_assignments: The result is calculated over this number of
        generated assignments. Set this to a large number if
        assignments are generated randomly, and set it to 1 if
        assignments are deterministic.

        num_samples: If par_eval is set to None, assignment
        performance is evaluated by sampling its performance
        num_samples times and computing the average.

        rerun: If True, any result stored on disk are overwritten.

        verbose: Run the solver in verbose mode if True.

        """

        if solver is None:
            assert par_eval is not None
        elif solver is not None:
            assert par_eval is None

        if par_eval is not None:
            assert solver is None

        assert isinstance(directory, str)
        assert isinstance(num_assignments, int)
        assert isinstance(num_samples, int)
        assert isinstance(rerun, bool)
        assert isinstance(verbose, bool)

        # Make sure the directory name ends with a /
        if directory[-1] != '/':
            directory += '/'

        # Create the directory if it doesn't exist
        if not os.path.exists(directory):
            os.makedirs(directory)

        self.solver = solver
        self.par_eval = par_eval
        self.directory = directory
        self.num_assignments = num_assignments
        self.num_samples = num_samples
        self.rerun = rerun
        self.verbose = verbose
        return

    def simulate_parameter_list(self, parameter_list=None, processes=4):
        """ Run simulations for a list of parameters.

        Args:
        parameter_list: List of SystemParameters for which to run simulations.
        processes: The number of parellel processes to run.
        """

        assert isinstance(parameter_list, list)
        print('Running simulations for', len(parameter_list), 'parameters.')

        # Run the simulations
        with Pool(processes=processes) as pool:
            _ = pool.map(self.simulate, parameter_list)

        return

    def evaluate_assignment(self, par, assignment):
        """ Evaluate an assignment.

        Args:
        par: System parameters
        assignment: Assignment object to evaluate

        Returns:
        A dict with the entries ['load'] and ['delay'] storing the average load
        and delay for the given assignment.
        """

        result = evaluation.objective_function_sampled(par,
                                                       assignment.assignment_matrix,
                                                       assignment.labels,
                                                       num_samples=self.num_samples)
        return result

    def simulate(self, parameters):
        """Run simulations for a single set of parameters.

        Writes the results to disk as .csv files. Will also write the best
        assignment matrix found to disk in the same directory.

        Args:
        parameters: SystemParameters for which to run simulations.

        """

        assert isinstance(parameters, model.SystemParameters)
        assert os.path.exists(self.directory)
        par = parameters

        # Try to load the results from disk
        filename = self.directory + par.identifier() + '.csv'
        if os.path.isfile(filename) and not self.rerun:
            print('Found results for', filename,
                  'on disk. Skipping.',)
            return

        print('Running simulations for', filename)

        best_assignment = None
        best_avg_load = math.inf

        results = list()
        for _ in range(self.num_assignments):

            # If solver is None we should run the analysis provided by
            # the par_eval function.
            if self.solver is None:
                result = self.par_eval(par, num_samples=self.num_samples)

            # If a solver is provided we should find an assignment<commit_msg>Added periodic prints for long simulations<commit_after>############################################################################
# Copyright 2016 <NAME>                                          #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#     http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
############################################################################

""" Run simulations for the parameters returned by get_parameters()

Writes the results to disk as .csv files. Will also write the best
assignment matrix found to disk in the same directory.
"""

import math
import os
import warnings
from multiprocessing import Pool
import pandas as pd
import evaluation
import model

class Simulator(object):
    """ Create a simulator.

    Writes the results to disk as .csv files. Will also write the best
    assignment matrix found to disk in the same directory.
    """

    def __init__(self, solver=None, par_eval=None, directory=None,
                 num_assignments=1, num_samples=1000, rerun=False,
                 verbose=False):
        """Creates a assignment performance simulator.

        Writes the results to disk as .csv files. Will also write the best
        assignment matrix found to disk in the same directory.

        Args:

        solver: The solver to use when creating assignments. A
        par_eval method must be provided if set to None.

        par_eval: A method that takes a parameter object as its single
        argument and returns a dict with entries 'load' and 'delay'
        (and possibly others). This argument must be None if a solver
        is provided.

        directory: The directory to store the results in.

        num_assignments: The result is calculated over this number of
        generated assignments. Set this to a large number if
        assignments are generated randomly, and set it to 1 if
        assignments are deterministic.

        num_samples: If par_eval is set to None, assignment
        performance is evaluated by sampling its performance
        num_samples times and computing the average.

        rerun: If True, any result stored on disk are overwritten.

        verbose: Run the solver in verbose mode if True.

        """

        if solver is None:
            assert par_eval is not None
        elif solver is not None:
            assert par_eval is None

        if par_eval is not None:
            assert solver is None

        assert isinstance(directory, str)
        assert isinstance(num_assignments, int)
        assert isinstance(num_samples, int)
        assert isinstance(rerun, bool)
        assert isinstance(verbose, bool)

        # Make sure the directory name ends with a /
        if directory[-1] != '/':
            directory += '/'

        # Create the directory if it doesn't exist
        if not os.path.exists(directory):
            os.makedirs(directory)

        self.solver = solver
        self.par_eval = par_eval
        self.directory = directory
        self.num_assignments = num_assignments
        self.num_samples = num_samples
        self.rerun = rerun
        self.verbose = verbose
        return

    def simulate_parameter_list(self, parameter_list=None, processes=4):
        """ Run simulations for a list of parameters.

        Args:
        parameter_list: List of SystemParameters for which to run simulations.
        processes: The number of parellel processes to run.
        """

        assert isinstance(parameter_list, list)
        print('Running simulations for', len(parameter_list), 'parameters.')

        # Run the simulations
        with Pool(processes=processes) as pool:
            _ = pool.map(self.simulate, parameter_list)

        return

    def evaluate_assignment(self, par, assignment):
        """ Evaluate an assignment.

        Args:
        par: System parameters
        assignment: Assignment object to evaluate

        Returns:
        A dict with the entries ['load'] and ['delay'] storing the average load
        and delay for the given assignment.
        """

        result = evaluation.objective_function_sampled(par,
                                                       assignment.assignment_matrix,
                                                       assignment.labels,
                                                       num_samples=self.num_samples)
        return result

    def simulate(self, parameters):
        """Run simulations for a single set of parameters.

        Writes the results to disk as .csv files. Will also write the best
        assignment matrix found to disk in the same directory.

        Args:
        parameters: SystemParameters for which to run simulations.

        """

        assert isinstance(parameters, model.SystemParameters)
        assert os.path.exists(self.directory)
        par = parameters

        # Try to load the results from disk
        filename = self.directory + par.identifier() + '.csv'
        if os.path.isfile(filename) and not self.rerun:
            print('Found results for', filename,
                  'on disk. Skipping.',)
            return

        print('Running simulations for', filename)

        best_assignment = None
        best_avg_load = math.inf

        results = list()
        for i in range(self.num_assignments):
            if (i + 1) % 10 == 0:
                print(filename, (i + 1) / self.num_assignments * 100, '% finished.')

            # If solver is None we should run the analysis provided by
            # the par_eval function.
            if self.solver is None:
                result = self.par_eval(par, num_samples=self.num_samples)

            # If a solver is provided we should find an assignment
{'fraction_non_alphanumeric': 0.07990408415841584, 'fraction_numerical': 0.003094059405940594, 'mean_word_length': 1.8610312015932728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3983753', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2797, 'n_words': 1234}