示例#1
0
 def __init__(self):
     samples = []
     classes = []
     for i in range(10):
         for j in range(10):
             samples.append([10 * i + j])
             classes.append(i)
     Environment.__init__(self, np.array(samples), np.array(classes))
示例#2
0
class TestEnvironment:
    """
    Functions tested in TestEnvironment:
    - get_random_sample
    - get_random_sample_index
    - get_sample
    - get_samples

    Functions not tested:
    - __init__
    - standard_distance
    - get_all
    - get_all_classes
    - get_all_samples
    - get_class
    - set_distance
    """

    def __init__(self):
        irises = datasets.load_iris()
        self.env = Environment(irises.data, irises.target)

    def get_random(self):
        _, sample, _ = self.env.get_random_sample()
        return sample

    def get_index(self):
        index, _, _ = self.env.get_random_sample()
        return index

    def get_sample(self, index):
        return self.env.get_sample(index)

    def test_get_random_sample_multiple_sampling_gives_more_than_one_sample(self):
        sample = self.get_random()

        different = False

        for _ in range(1000):
            new_sample = self.get_random()
            if (new_sample == sample).all():
                different = True
                break

        assert different

    def test_get_random_sample_index_multiple_sampling_gives_more_than_one_sample(self):
        sample = self.get_index()

        different = False

        for _ in range(1000):
            new_sample = self.get_index()
            if new_sample == sample:
                different = True
                break

        assert different

    def test_get_sample_the_same_results_like_normal_indexes_x_100(self):
        irises = datasets.load_iris()

        for _ in range(100):
            index = self.get_index()
            assert (irises.data[index] == self.get_sample(index)).all()

    def test_get_samples_full_returned_array_identical_with_original(self):
        original = self.env.get_all_samples()
        copy = self.env.get_samples(range(len(original)))
        assert (original == copy).all()
示例#3
0
 def __init__(self):
     irises = datasets.load_iris()
     self.env = Environment(irises.data, irises.target)
示例#4
0
    def __init__(self):
        self.agent = None

        irises = datasets.load_iris()
        self.environment = Environment(irises.data, irises.target)
示例#5
0
class TestAgent:
    """
    Functions tested in TestAgent:
    - classify

    Functions not tested:
    - __init__
    - add_sample
    - decrease_weight_word_category
    - forget
    - increase_weights_sample_category
    - increase_weight_word_category
    - learn
    - update_fitness_measure
    - decrease_weights_for_other_categories
    - decrease_weights_for_other_words
    - get_category_class
    - get_id
    - get_fitness_measure
    - get_words
    - set_fitness
    """

    def __init__(self):
        self.agent = None

        irises = datasets.load_iris()
        self.environment = Environment(irises.data, irises.target)

    def add(self, sample_index, category=None, environment=None):
        environment = environment or self.environment
        self.agent.add_sample(sample_index, environment, category)

    def classify(self, sample=None):
        if sample is None:
            sample = self.environment.get_random_sample()
        return self.agent.predict(sample)

    def setup(self):
        self.agent = SteelsAgent()

    def test_classify(self):
        sample_index, _, _ = self.environment.get_random_sample()
        sample = self.environment.get_sample(sample_index)

        # Classify with no samples returns None
        assert_equals(self.classify(sample), None)

        # Classify with one sample returns this sample category
        self.add(1, 2)
        assert_equals(self.classify(sample), 2)

        # Classify before fitting (learning) returns None
        self.add(51, 1)
        assert_equals(self.classify(sample), None)

        # Classify after fitting (learning) doesn't return None
        self.agent.learn()
        assert self.classify(sample) is not None

    def test_learning_cycle(self):
        self.add(1, 1)
        self.add(51, 2)
        self.add(101, 3)
        self.agent.learn()
        for _ in range(10):
            assert self.classify() is not None

    def test_single_classification_game_iterations(self):
        agent = self.agent
        env = self.environment

        agent.set_fitness("DF", DummyFitness())
        for _ in range(100):
            agent.learn()
            sample_index = env.get_random_sample_index()
            sample = env.get_sample(sample_index)
            category = agent.predict(sample)
            if category is None:
                self.add(sample_index)
                value = 0
            elif env.get_class(sample_index) == agent.get_category_class(category):
                agent.increase_weights_sample_category(sample_index, env, category)
                value = 1
            elif agent.get_fitness_measure("DF") > 0.95:
                agent.increase_weights_sample_category(sample_index, env, category)
                value = 0
            else:
                self.add(sample_index)
                value = 0

            self.agent.update_fitness_measure("DF", value)
            self.agent.forget()