<commit_before>import random
import numpy as np


class Agent(object):

    """Agent class.
    Create an agent who has costs and rewards.

    Attributes:
    CostDimensions [int]   Dimension of cost simplex
    RewardDimensions [int] Dimensions of reward simplex
    costs [list]           list with sampled costs
    rewards [list]         list with sampled rewards
    """

    def __init__(self, Map, CostParam=0.1, RewardParam=10):
        # ARGUMENTS:
        #
        # Map          MAP object
        # CostParam    Parameter used for sampling costs.
        # RewardParam  Parameter used for sampling rewards.
        # Both parameters change meaning depending on the distribution you use.

        self.CostDimensions = len(np.unique(Map.StateTypes))
        # Get dimensions over which you'll build your simplex
        self.RewardDimensions = sum([i > 0 for i in map(len, Map.Locations)])
        if self.RewardDimensions == 0:
            print "WARNING: No rewards on map."
        self.CostParam = CostParam
        self.RewardParam = RewardParam
        self.ResampleCosts()  # Generate random cost of map
        self.ResampleRewards()  # Generate random rewards for objects

    def ResampleAgent(self, Apathy=0, Restrict=False):
        # Generate a random agent by changing it's competence and motivation
        # If Restrict is set to true the agent assumes that the first terrain
        # is always the least costly
        self.ResampleCosts(Apathy)
        self.ResampleRewards(Apathy)
        if Restrict:
            temp = self.costs[0]
            new = self.costs.argmin()
            minval = self.costs[new]
            self.costs[new] = temp
            self.costs[0] = minval

    def ResampleCosts(self, Apathy=0):
        # Resample the agent's competence
        self.costs = self.Sample(
            self.CostDimensions, self.CostParam, Kind="Exponential")
        self.costs = [0 if random.random() < Apathy else i for i in self.costs]

    def ResampleRewards(self, Apathy=0):
        # Resample the agent's preferences
        self.rewards = self.Sample(
            self.RewardDimensions, self.RewardParam, Kind="Exponential")
        self.rewards = [
            0 if random.random() < Apathy else i for i in self.rewards]

    def Sample(self, dimensions, SamplingParam, Kind):
        # Generate a random sample from different distributions.
        # ARGUMENTS: Dimensions and sample kind
        if(Kind == "Simplex"):
            # Output: Simplex sample of length 'dimensions' (Adds to 1)
            sample = -np.log(np.random.rand(dimensions))
            return sample / sum(sample)
        if(Kind == "Exponential"):
            sample = [
                np.random.exponential(SamplingParam) for j in range(dimensions)]
            return sample

    def Display(self, Full=True):
        # Print class properties
        if Full:
            for (property, value) in vars(self).iteritems():
                print property, ': ', value
        else:
            for (property, value) in vars(self).iteritems():
                print property
<commit_msg>New agent class<commit_after># -*- coding: utf-8 -*-

"""
Markov Decision Process solver.
"""

__license__ = "MIT"

import random
import numpy as np


class Agent(object):

    def __init__(self, Map, Prior="Exponential", CostParam=0.1, RewardParam=10):
        """
        Agent class.

        Create an agent who has costs and rewards.

        Args:
            Map (Map): A map object.
            Prior (str): String indicating prior's name (Only Simplex and Exponential supported)
            CostParam (float): Parameter for sampling costs
            RewardParam (float): Parameter for sampling rewards
        """
        self.CostDimensions = len(np.unique(Map.StateTypes))
        # Get dimensions over which you'll build your simplex
        self.RewardDimensions = sum([i > 0 for i in map(len, Map.Locations)])
        if self.RewardDimensions == 0:
            print "WARNING: No rewards on map."
        self.CostParam = CostParam
        self.RewardParam = RewardParam
        self.ResampleCosts()  # Generate random cost of map
        self.ResampleRewards()  # Generate random rewards for objects

    def ResampleAgent(self, Apathy=0, Restrict=False):
        """
        Reset the agent with random parameters.

        Args:
            Apathy (float): Probability that agent doesn't like objects.
            Restrict (bool): If true, first terrain is always the least costly

        Returns:
            None
        """
        self.ResampleCosts(Apathy)
        self.ResampleRewards(Apathy)
        if Restrict:
            temp = self.costs[0]
            new = self.costs.argmin()
            minval = self.costs[new]
            self.costs[new] = temp
            self.costs[0] = minval

    def ResampleCosts(self, Apathy=0):
        """
        Reset agent's costs.

        Args:
            Apathy (float): Probability that agent doesn't like objects.

        Returns:
            None
        """
        # Resample the agent's competence
        self.costs = self.Sample(
            self.CostDimensions, self.CostParam, Kind=self.Prior)
        self.costs = [0 if random.random() < Apathy else i for i in self.costs]

    def ResampleRewards(self, Apathy=0):
        """
        Reset agent's rewards.

        Args:
            Apathy (float): Probability that agent doesn't like objects.

        Returns:
            None
        """
        # Resample the agent's preferences
        self.rewards = self.Sample(
            self.RewardDimensions, self.RewardParam, Kind=self.Prior)
        self.rewards = [
            0 if random.random() < Apathy else i for i in self.rewards]

    def Sample(self, dimensions, SamplingParam, Kind):
        """
        Generate a sample from some distribution

        Args:
            dimensions (int): Number of dimensions
            SamplingParam: Parameter to use on distribution
            Kind (str): Name of distribution

        Returns:
            None
        """
        if(Kind == "Simplex"):
            # Output: Simplex sample of length 'dimensions' (Adds to 1)
            sample = -np.log(np.random.rand(dimensions))
            return sample / sum(sample)
        if(Kind == "Exponential"):
            sample = [
                np.random.exponential(SamplingParam) for j in range(dimensions)]
            return sample

    def Display(self, Full=True):
        # Print class properties
        if Full:
            for (property, value) in vars(self).iteritems():
                print property, ': ', value
        else:
            for (property, value) in vars(self).iteritems():
                print property

{'fraction_non_alphanumeric': 0.08828775267538644, 'fraction_numerical': 0.004310344827586207, 'mean_word_length': 2.010738255033557, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3657689', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1777, 'n_words': 583}