<commit_before>from simulation_event import AbstractSimulationEvent
import math

class AbstractPheromoneVaporization(AbstractSimulationEvent):
    PERIOD = 100
    def __init__(self, trigger_level, rate=None, end_time=PERIOD):
        self.end_time = end_time
        self.rate = rate
        self.trigger_level = trigger_level
    def set_rate(self, rate):
        self.rate = rate
    def reset(self):
        self.end_time = self.PERIOD
        self.rate = None
    def process_end(self, reality, stats):
        changed = []
        avg_pheromone = reality.world.get_average_pheromone_level()
        max_pheromone = reality.world.get_max_pheromone_level()
        if max_pheromone/(avg_pheromone or 1) >= self.trigger_level:
            for edge in reality.world.edges:
                for edge_end in (edge.a_end, edge.b_end):
                    new = self.compute_new_value(edge_end.pheromone_level)
                    if edge_end.pheromone_level!=new:
                        changed.append(edge_end.edge)
                        edge_end.pheromone_level = new
        return self.__class__(self.trigger_level, self.rate, self.end_time+self.PERIOD), frozenset(changed)
    def compute_new_value(self, pheromone_level):
        raise NotImplementedError()

class MultiplierPheromoneVaporization(AbstractPheromoneVaporization):
    MULTIPLIER = 0.8
    def compute_new_value(self, pheromone_level):
        return max(0.0, pheromone_level*self.MULTIPLIER)

class ExponentPheromoneVaporization(AbstractPheromoneVaporization):
    # suspect this works well for values greater than 1
    EXPONENT = 0.95
    def compute_new_value(self, pheromone_level):
        return max(0.0, pheromone_level**self.EXPONENT)

class LogarithmPheromoneVaporization(AbstractPheromoneVaporization):
    def compute_new_value(self, pheromone_level):
        return max(0.0, math.log(pheromone_level+1))
<commit_msg>Extend vaporization interface to also support edge cost modification<commit_after>from simulation_event import AbstractSimulationEvent
import math

class AbstractPeriodicRealityProcessor(AbstractSimulationEvent):
    PERIOD = 100
    def __init__(self, trigger_level, rate=None, end_time=PERIOD):
        self.end_time = end_time
        self.rate = rate
        self.trigger_level = trigger_level
    def set_rate(self, rate):
        self.rate = rate
    def reset(self):
        self.end_time = self.PERIOD
        self.rate = None
    def process_end(self, reality, stats):
        raise NotImplementedError()

class AbstractEdgeProcessor(AbstractPeriodicRealityProcessor):
    def process_end(self, reality, stats):
        changed_pheromone_edges = []
        avg_pheromone = reality.world.get_average_pheromone_level()
        max_pheromone = reality.world.get_max_pheromone_level()
        if max_pheromone/(avg_pheromone or 1) >= self.trigger_level:
            for edge in reality.world.edges:
                for edge_end in (edge.a_end, edge.b_end):
                    new = self.compute_new_pheromone(edge_end.pheromone_level)
                    if edge_end.pheromone_level!=new:
                        changed_pheromone_edges.append(edge_end.edge)
                        edge_end.pheromone_level = new
                edge.cost = self.compute_new_cost(edge.cost)
        return self.__class__(self.trigger_level, self.rate, self.end_time+self.PERIOD), frozenset(changed_pheromone_edges)
    def compute_new_cost(self, old_cost):
        raise NotImplementedError()
    def compute_new_pheromone(self, old_pheromone_level):
        raise NotImplementedError()

class AbstractPeriodicEdgeCostModifier(AbstractPeriodicRealityProcessor):
    def compute_new_pheromone(self, old_pheromone_level):
        return old_pheromone_level

class AbstractPheromoneVaporization(AbstractPeriodicRealityProcessor):
    def compute_new_cost(self, old_cost):
        return old_cost

class MultiplierPheromoneVaporization(AbstractPheromoneVaporization):
    MULTIPLIER = 0.8
    def compute_new_pheromone(self, old_pheromone_level):
        return max(0.0, old_pheromone_level*self.MULTIPLIER)

class ExponentPheromoneVaporization(AbstractPheromoneVaporization):
    # suspect this works well for values greater than 1
    EXPONENT = 0.95
    def compute_new_pheromone(self, old_pheromone_level):
        return max(0.0, old_pheromone_level**self.EXPONENT)

class LogarithmPheromoneVaporization(AbstractPheromoneVaporization):
    def compute_new_pheromone(self, old_pheromone_level):
        return max(0.0, math.log(old_pheromone_level+1))

{'fraction_non_alphanumeric': 0.07397622192866579, 'fraction_numerical': 0.007485689123734038, 'mean_word_length': 3.502477700693756, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7287257', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1407, 'n_words': 255}