<commit_before>"""SWM, A Simple Wildfire-inspired MDP model. Version 1.3"""

import random, math, numpy, MDP

def simulate(timesteps, policy=[0,0,0], random_seed=0, model_parameters={}, SILENT=False, PROBABILISTIC_CHOICES=True):
    
    random.seed(random_seed)
    
    #range of the randomly drawn, uniformally distributed "event" that corresponds to fire severity
    event_max = 1.0
    event_min = 0.0

    #state variable bounds
    vuln_max = 1.0
    vuln_min = 0.02

    timber_max = 10.0
    timber_min = 0.0
    



    timesteps = int(timesteps)


    #sanitize policy
    pol = sanitize_policy(policy)


    #REWARD STRUCTURE
    
    #cost of suppression in a mild event
    supp_cost_mild = 5
    if "Suppression Cost - Mild Event" in model_parameters.keys(): supp_cost_mild = model_parameters["Suppression Cost - Mild Event"]

    #cost of suppresion in a severe event
    supp_cost_severe = 10
    if "Suppression Cost - Severe Event" in model_parameters.keys(): supp_cost_severe = model_parameters["Suppression Cost - Severe Event"]

    #cost of a severe fire on the next timestep
    burn_cost = 40
    if "Severe Burn Cost" in model_parameters.keys(): burn_cost = model_parameters["Severe Burn Cost"]


    #TRANSITION VARIABLES

    vuln_change_after_suppression = 0.01
    vuln_change_after_mild = -0.01
    vuln_change_after_severe = -0.015
    if "Vulnerability Change After Suppression" in model_parameters.keys(): vuln_change_after_suppression = model_parameters["Vulnerability Change After Suppression"]
    if "Vulnerability Change After Mild" in model_parameters.keys(): vuln_change_after_mild = model_parameters["Vulnerability Change After Mild"]
    if "Vulnerability Change After Severe" in model_parameters.keys(): vuln_change_after_severe = model_parameters["Vulnerability Change After Severe"]

    timber_change_after_suppression = 0.1
    timber_change_after_mild = 0.1
    timber_change_after_severe = -3.0
    if "Timber Value Change After Suppression" in model_parameters.keys(): timber_change_after_suppression = model_parameters["Timber Value Change After Suppression"]
    if "Timber Value Change After Mild" in model_parameters.keys(): timber_change_after_mild = model_parameters["Timber Value Change After Mild"]
    if "Timber Value Change After Severe" in model_parameters.keys(): timber_change_after_severe = model_parameters["Timber Value Change After Severe"]


    if "Probabilistic Choices" in model_parameters.keys():
        if model_parameters["Probabilistic Choices"] == "True":
            PROBABILISTIC_CHOICES = True
        else:
            PROBABILISTIC_CHOICES = False


    #habitat transition variables
    habitat_mild_interval = 5
    habitat_severe_interval = 20
    habitat_loss_if_no_mild = 0.2
    habitat_loss_if_no_severe = 0.2
    habitat_gain = 0.1


    #starting_condition = 0.8
    starting_Vulnerability = random.uniform(0.2,0.8)
    if "Starting Vulnerability" in model_parameters.keys(): starting_condition = model_parameters["Starting Condition"]
    starting_timber = random.uniform(2,8)
    if "Starting Timber Value" in model_parameters.keys(): starting_timber = model_parameters["Starting Timber Value"]
    starting_habitat = random.uniform(2,8)
    if "Starting Habitat Value" in model_parameters.keys(): starting_habitat = model_parameters["Starting Habitat Value"]


    #setting 'enums'
    MILD=0
    SEVERE=1


    #starting simulations
    states = [None] * timesteps

    #start current condition randomly among the three states
    current_vulnerability = starting_Vulnerability
    current_timber = starting_timber
    current_habitat = starting_habitat
    time_since_severe = 0
    time_since_mild = 0


    for i in range(timesteps):

        #event value is the single "feature" of events in this MDP
        ev = random.uniform(event_min, event_max)

        #severity is meant to be a hidden, "black box" variable inside the MDP
        # and not available to the logistic function as a parameter
        severity = MILD
        if ev >= (1 - current_vulnerability): severity = SEVERE


        #logistic function for the policy choice
        #policy_crossproduct = policy[0] + policy[1]*ev
        #modified logistic policy function
        #                     CONSTANT       COEFFICIENT       SHIFT
        policy_crossproduct = pol[0] + ( pol[1] * (ev + pol[2]) )
        if policy_crossproduct > 100: policy_crossproduct = 100
        if policy_crossproduct < -100: policy_crossproduct = -100

        policy_value = 1.0 / (1.0 + math.exp(-1*(policy_crossproduct)))

        choice_roll = random.uniform(0,1)
        #assume let-burn
        choice = False
        choice_prob = 1.0 - policy_value
        #check for suppress, and update values if necessary
        if PROBABILISTIC_CHOICES:
            if choice_roll < policy_value:
                choice = True
                choice_prob = policy_value
        else:
            if policy_value >= 0.5:
                choice = True
                choice_prob = policy_value


        ### CALCULATE REWARD ###
        supp_cost = 0
        burn_penalty = 0
        if choice:
            #suppression was chosen
            if severity == MILD:
                supp_cost = supp_cost_mild
            elif severity == SEVERE: 
                supp_cost = supp_cost_severe
        else:
            #suppress was NOT chosen
            if severity == SEVERE:
                #set this timestep's burn penalty to the value given in the overall model parameter
                #this is modeling the timber values lost in a large fire.
                burn_penalty = burn_cost

        


        current_reward = 10 + current_timber - supp_cost - burn_penalty
                

        states[i] = [current_vulnerability, current_timber, ev, choice, choice_prob, policy_value, current_reward, current_habitat, i]



        ### TRANSITION ###
        if not choice:
            #no suppression
            if severity == SEVERE:
                current_vulnerability += vuln_change_after_severe
                current_timber += timber_change_after_severe

                #reset both timers
                time_since_severe = 0
                time_since_mild = 0

            elif severity == MILD:
                current_vulnerability += vuln_change_after_mild
                current_timber += timber_change_after_mild

                #reset mild, increment severe
                time_since_mild = 0
                time_since_severe += 1
        else:
            #suppression
            current_vulnerability += vuln_change_after_suppression
            current_timber += timber_change_after_suppression

            #increment both timers
            time_since_mild += 1
            time_since_severe += 1


        #check for habitat changes
        if (time_since_mild <= habitat_mild_interval) and (time_since_severe <= habitat_severe_interval):
            current_habitat += habitat_gain
        else:
            if time_since_mild > habitat_mild_interval:
                current_habitat -= habitat_loss_if_no_mild
            if time_since_severe > habitat_severe_interval:
                current_habitat -= habitat_loss_if_no_severe


        #Enforce state variable bounds
        if current_vulnerability > vuln_max: current_vulnerability = vuln_max
        if current_vulnerability < vuln_min: current_vulnerability = vuln_min
        if current_timber > timber_max: current_timber = timber_max
        if current_timber < timber_min: current_timber = timber_min
        if current_habitat > 10: current_habitat = 10
        if current_habitat < 0: current_habitat = 0


        


    #finished simulations, report some values
    vals = []
    hab = []
    suppressions = 0.0
    joint_prob = 1.0
    prob_sum = 0.0
    for i in range(timesteps):
        if states[i][3]: suppressions += 1
        joint_prob *= states[i][4]
        prob_sum += states[i][4]
        vals.append(states[i][6])
        hab.append(states[i][7])
    ave_prob = prob_sum / timesteps

    summary = {<commit_msg>tuning SWM v1.3 to have the proper MC value surface<commit_after>"""SWM, A Simple Wildfire-inspired MDP model. Version 1.3"""

import random, math, numpy, MDP

def simulate(timesteps, policy=[0,0,0], random_seed=0, model_parameters={}, SILENT=False, PROBABILISTIC_CHOICES=True):
    
    random.seed(random_seed)
    
    #range of the randomly drawn, uniformally distributed "event" that corresponds to fire severity
    event_max = 1.0
    event_min = 0.0

    #state variable bounds
    vuln_max = 1.0
    vuln_min = 0.02

    timber_max = 10.0
    timber_min = 0.0
    



    timesteps = int(timesteps)


    #sanitize policy
    pol = sanitize_policy(policy)


    #REWARD STRUCTURE
    
    #cost of suppression in a mild event
    supp_cost_mild = 9
    if "Suppression Cost - Mild Event" in model_parameters.keys(): supp_cost_mild = model_parameters["Suppression Cost - Mild Event"]

    #cost of suppresion in a severe event
    supp_cost_severe = 13
    if "Suppression Cost - Severe Event" in model_parameters.keys(): supp_cost_severe = model_parameters["Suppression Cost - Severe Event"]

    #cost of a severe fire on the next timestep
    burn_cost = 40
    if "Severe Burn Cost" in model_parameters.keys(): burn_cost = model_parameters["Severe Burn Cost"]


    #TRANSITION VARIABLES

    vuln_change_after_suppression = 0.01
    vuln_change_after_mild = -0.01
    vuln_change_after_severe = -0.015
    if "Vulnerability Change After Suppression" in model_parameters.keys(): vuln_change_after_suppression = model_parameters["Vulnerability Change After Suppression"]
    if "Vulnerability Change After Mild" in model_parameters.keys(): vuln_change_after_mild = model_parameters["Vulnerability Change After Mild"]
    if "Vulnerability Change After Severe" in model_parameters.keys(): vuln_change_after_severe = model_parameters["Vulnerability Change After Severe"]

    timber_change_after_suppression = 0.1
    timber_change_after_mild = 0.1
    timber_change_after_severe = -5.0
    if "Timber Value Change After Suppression" in model_parameters.keys(): timber_change_after_suppression = model_parameters["Timber Value Change After Suppression"]
    if "Timber Value Change After Mild" in model_parameters.keys(): timber_change_after_mild = model_parameters["Timber Value Change After Mild"]
    if "Timber Value Change After Severe" in model_parameters.keys(): timber_change_after_severe = model_parameters["Timber Value Change After Severe"]


    if "Probabilistic Choices" in model_parameters.keys():
        if model_parameters["Probabilistic Choices"] == "True":
            PROBABILISTIC_CHOICES = True
        else:
            PROBABILISTIC_CHOICES = False


    #habitat transition variables
    habitat_mild_maximum = 15
    habitat_mild_minimum = 0
    habitat_severe_maximum = 40
    habitat_severe_minimum = 10
    habitat_loss_if_no_mild = 0.2
    habitat_loss_if_no_severe = 0.2
    habitat_gain = 0.1


    #starting_condition = 0.8
    starting_Vulnerability = random.uniform(0.2,0.8)
    if "Starting Vulnerability" in model_parameters.keys(): starting_condition = model_parameters["Starting Condition"]
    starting_timber = random.uniform(2,8)
    if "Starting Timber Value" in model_parameters.keys(): starting_timber = model_parameters["Starting Timber Value"]
    starting_habitat = random.uniform(2,8)
    if "Starting Habitat Value" in model_parameters.keys(): starting_habitat = model_parameters["Starting Habitat Value"]


    #setting 'enums'
    MILD=0
    SEVERE=1


    #starting simulations
    states = [None] * timesteps

    #start current condition randomly among the three states
    current_vulnerability = starting_Vulnerability
    current_timber = starting_timber
    current_habitat = starting_habitat
    time_since_severe = 0
    time_since_mild = 0


    for i in range(timesteps):

        #event value is the single "feature" of events in this MDP
        ev = random.uniform(event_min, event_max)

        #severity is meant to be a hidden, "black box" variable inside the MDP
        # and not available to the logistic function as a parameter
        severity = MILD
        if ev >= (1 - current_vulnerability): severity = SEVERE


        #logistic function for the policy choice
        #policy_crossproduct = policy[0] + policy[1]*ev
        #modified logistic policy function
        #                     CONSTANT       COEFFICIENT       SHIFT
        policy_crossproduct = pol[0] + ( pol[1] * (ev + pol[2]) )
        if policy_crossproduct > 100: policy_crossproduct = 100
        if policy_crossproduct < -100: policy_crossproduct = -100

        policy_value = 1.0 / (1.0 + math.exp(-1*(policy_crossproduct)))

        choice_roll = random.uniform(0,1)
        #assume let-burn
        choice = False
        choice_prob = 1.0 - policy_value
        #check for suppress, and update values if necessary
        if PROBABILISTIC_CHOICES:
            if choice_roll < policy_value:
                choice = True
                choice_prob = policy_value
        else:
            if policy_value >= 0.5:
                choice = True
                choice_prob = policy_value


        ### CALCULATE REWARD ###
        supp_cost = 0
        burn_penalty = 0
        if choice:
            #suppression was chosen
            if severity == MILD:
                supp_cost = supp_cost_mild
            elif severity == SEVERE: 
                supp_cost = supp_cost_severe
        else:
            #suppress was NOT chosen
            if severity == SEVERE:
                #set this timestep's burn penalty to the value given in the overall model parameter
                #this is modeling the timber values lost in a large fire.
                burn_penalty = burn_cost

        


        current_reward = 10 + current_timber - supp_cost - burn_penalty
                

        states[i] = [current_vulnerability, current_timber, ev, choice, choice_prob, policy_value, current_reward, current_habitat, i]



        ### TRANSITION ###
        if not choice:
            #no suppression
            if severity == SEVERE:
                current_vulnerability += vuln_change_after_severe
                current_timber += timber_change_after_severe

                #reset both timers
                time_since_severe = 0
                time_since_mild += 1

            elif severity == MILD:
                current_vulnerability += vuln_change_after_mild
                current_timber += timber_change_after_mild

                #reset mild, increment severe
                time_since_mild = 0
                time_since_severe += 1
        else:
            #suppression
            current_vulnerability += vuln_change_after_suppression
            current_timber += timber_change_after_suppression

            #increment both timers
            time_since_mild += 1
            time_since_severe += 1


        #check for habitat changes. 
        #Note to self: suppression effects are already taken into account above
        if ( (time_since_mild <= habitat_mild_maximum) and 
             (time_since_mild >= habitat_mild_minimum) and
             (time_since_severe <= habitat_severe_maximum) and
             (time_since_severe >= habitat_severe_minimum)  ):

            #this fire is happy on all counts
            current_habitat += habitat_gain
        else:
            #this fire is unhappy in some way.
            if (time_since_mild > habitat_mild_maximum) or (time_since_mild < habitat_mild_minimum):
                current_habitat -= habitat_loss_if_no_mild
            if (time_since_severe > habitat_severe_maximum) or (time_since_severe < habitat_severe_minimum):
                current_habitat -= habitat_loss_if_no_severe



        #Enforce state variable bounds
        if current_vulnerability > vuln_max: current_vulnerability = vuln_max
        if current_vulnerability < vuln_min: current_vulnerability = vuln_min
        if current_timber > timber_max: current_timber = timber_max
        if current_timber < timber_min: current_timber = timber_min
        if current_habitat > 10: current_habitat = 10
        if current_habitat < 0: current_habitat = 0


        


    #finished simulations, report some values
    vals = []
    hab = []
    suppressions = 0.0
    joint_prob = 1.0
    prob_sum = 0.0
    for i in range(timesteps):
        if states[i][3]: suppressions += 1
        joint_prob *= states[i][4]
        prob_sum += states[i][4]
        vals.append(states[i][6])
        hab.append(states[i][7])
    ave_prob = prob_sum / timesteps

    summary = {
{'fraction_non_alphanumeric': 0.06899034135221069, 'fraction_numerical': 0.014877917091607174, 'mean_word_length': 2.600431965442765, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 21, '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': '6660827', 'n_tokens_mistral': 5198, 'n_tokens_neox': 4745, 'n_words': 1304}