<commit_before>
import logging
import copy

import numpy as np

from .utils import bounded_sum, similarity

class Model(object):

    def __init__(self, num_primitives, num_actions):

        self.logger = logging.getLogger(self.__class__.__name__)
        
        self.SIMILARITY_THRESHOLD = 0.9       # real, 0 < x < 1
        self.MAX_ENTRIES = 10 ** 4               # integer, somewhat large
        self.CLEANING_PERIOD = 10 ** 5           # integer, somewhat large
        self.UPDATE_RATE = 0.1                # real, 0 < x < 1
        self.STEP_DISCOUNT = 0.5              # real, 0 < x < 1
        self.TRACE_LENGTH = 10                 # integer, small
        self.TRACE_DECAY_RATE = 0.2           # real, 0 < x < 1

        self.debug = False
        self.filename_postfix = '_model.mat'

        self.clean_count = 0

        self.cause = [[]]
        
        self.cause.append(np.zeros((num_primitives, 2*self.MAX_ENTRIES)))
        self.cause.append(np.zeros((num_actions, 2*self.MAX_ENTRIES)))

        self.effect = copy.deepcopy(self.cause)
        self.history = copy.deepcopy(self.cause)

        self.count = np.zeros(2*self.MAX_ENTRIES)
        self.reward_map = np.zeros(2*self.MAX_ENTRIES)
        self.goal_map = np.zeros(2*self.MAX_ENTRIES)

        self.trace_index = np.ones(self.TRACE_LENGTH)
        self.trace_reward = np.zeros(self.TRACE_LENGTH)

        # Initializes dummy transitions
        self.last_entry = 0
        self.cause[1][0,0] = 1
        self.effect[1][0,0] = 1
        #self.countp[0]= eps


    def add_group(self):
        size = (1, np.size(self.cause[-1], 2))
        self.history.append(np.zeros(size))
        self.cause.append(np.zeros(size))
        self.effect.append(np.zeros(size))


    def add_feature(self, nth_group, has_dummy):
        """
        add_feature to the model
        """
        
        self.history[nth_group] = np.vstack((self.history[nth_group],  np.zeros((1, np.size(self.history[nth_group], 2)))))
        self.cause[nth_group]   = np.vstack((self.cause[nth_group],  np.zeros((1, np.size(self.cause[nth_group],  2)))))
        self.effect[nth_group]  = np.vstack((self.effect[nth_group], np.zeros((1, np.size(self.effect[nth_group], 2)))))
        
        # if dummy feature is still in place, removes it
        if has_dummy:
            self.history[nth_group]   = self.history[nth_group][1:]
            self.cause[nth_group]  = self.cause[nth_group][1:]
            self.effect[nth_group] = self.effect[nth_group][1:]



    def train(self, effect, history, cause, reward):
        """
        Takes in 'cause' and 'effect' 
        to train the model of the environment.
        """

        # TODO: udpate comments to reflect new representation

        eps = np.finfo(np.double).eps            
        num_groups = len(cause)

        # Checks to see whether the new entry is already in the library 
        # TODO: make the similarity threshold a function of the count? This would
        # allow often-observed transitions to require a closer fit, and populate
        # the model space more densely in areas where it accumulates more
        # observations.
        transition_similarity = similarity(history, self.history, range(0,self.last_entry))<commit_msg>model: convert more 1xN 2D to 1D vectors<commit_after>
import logging
import copy

import numpy as np

from .utils import bounded_sum, similarity

class Model(object):

    def __init__(self, num_primitives, num_actions):

        self.logger = logging.getLogger(self.__class__.__name__)
        
        self.SIMILARITY_THRESHOLD = 0.9       # real, 0 < x < 1
        self.MAX_ENTRIES = 10 ** 4               # integer, somewhat large
        self.CLEANING_PERIOD = 10 ** 5           # integer, somewhat large
        self.UPDATE_RATE = 0.1                # real, 0 < x < 1
        self.STEP_DISCOUNT = 0.5              # real, 0 < x < 1
        self.TRACE_LENGTH = 10                 # integer, small
        self.TRACE_DECAY_RATE = 0.2           # real, 0 < x < 1

        self.debug = False
        self.filename_postfix = '_model.mat'

        self.clean_count = 0

        self.cause = [[]]
        
        self.cause.append(np.zeros((num_primitives, 2*self.MAX_ENTRIES)))
        self.cause.append(np.zeros((num_actions, 2*self.MAX_ENTRIES)))

        self.effect = copy.deepcopy(self.cause)
        self.history = copy.deepcopy(self.cause)

        self.count = np.zeros(2*self.MAX_ENTRIES)
        self.reward_map = np.zeros(2*self.MAX_ENTRIES)
        self.goal_map = np.zeros(2*self.MAX_ENTRIES)

        self.trace_index = np.ones(self.TRACE_LENGTH)
        self.trace_reward = np.zeros(self.TRACE_LENGTH)

        # Initializes dummy transitions
        self.last_entry = 0
        self.cause[1][0,0] = 1
        self.effect[1][0,0] = 1
        #self.countp[0]= eps


    def add_group(self):
        size = (1, np.size(self.cause[-1], 2))
        self.history.append(np.zeros(size))
        self.cause.append(np.zeros(size))
        self.effect.append(np.zeros(size))


    def add_feature(self, nth_group, has_dummy):
        """
        add_feature to the model
        """
        
        self.history[nth_group] = np.vstack((self.history[nth_group],  np.zeros(self.history[nth_group].shape[1])))
        self.cause[nth_group]   = np.vstack((self.cause[nth_group],  np.zeros(self.cause[nth_group].shape[1])))
        self.effect[nth_group]  = np.vstack((self.effect[nth_group], np.zeros(self.effect[nth_group].shape[1])))
        
        # if dummy feature is still in place, removes it
        if has_dummy:
            self.history[nth_group]   = self.history[nth_group][1:]
            self.cause[nth_group]  = self.cause[nth_group][1:]
            self.effect[nth_group] = self.effect[nth_group][1:]



    def train(self, effect, history, cause, reward):
        """
        Takes in 'cause' and 'effect' 
        to train the model of the environment.
        """

        # TODO: udpate comments to reflect new representation

        eps = np.finfo(np.double).eps            
        num_groups = len(cause)

        # Checks to see whether the new entry is already in the library 
        # TODO: make the similarity threshold a function of the count? This would
        # allow often-observed transitions to require a closer fit, and populate
        # the model space more densely in areas where it accumulates more
        # observations.
        transition_similarity = similarity(history, self.history, range(0,self.last_entry))
{'fraction_non_alphanumeric': 0.11392210978227538, 'fraction_numerical': 0.016252683226004292, 'mean_word_length': 2.227610094012865, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 3, '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': '6292270', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2071, 'n_words': 452}