def test_GroupedAggroByColor_WithWimpyBraveAndKNN(self):
        brave = BraveHypothesis()
        wimpy = WimpyHypothesis()
        knn = KNearestNeighbors(3)

        self.setUpDecisioner(brave, wimpy, knn)

        def create_monster():
            color = randint(1, 100)

            # Every monster below 50 is aggressive
            if color < 50:
                return Monster(1, [color], 'aggressive')
            # Otherwise if they are above 50 they are
            # passive
            else:
                return Monster(0, [color], 'passive')

        # Next we load up the training data
        for i in range(100):
            # Create the monster and generate all the data, by default
            # we expect everything in the training period to be true
            # meaning attack for data
            monster = create_monster()
            self.assertTrue(self.decisioner.get_guess(monster.color))
            self.decisioner.update(monster.color, 1, monster.action(True))

        # Finally we need to know that normal KNN was matched as the best
        # fitness for the data set
        self.assertGreater(knn.fitness(), brave.fitness())
        self.assertGreater(knn.fitness(), wimpy.fitness())

        # Then we are going to run over all of the data sets
        # We want to also track the actual value and the maximum
        # value for comparison
        maximum_value = 5000.0
        actual_value = 0.0

        for i in range(5000):
            # Create the monster, guess on it and grab the outcome
            # which is all required information for the loop
            monster = create_monster()
            guess = self.decisioner.get_guess(monster.color)
            outcome = monster.action(guess)

            # update values for comparison after loop
            maximum_value -= monster._aggressive
            actual_value += outcome

            # 
            self.decisioner.update(monster.color, guess, outcome)

            # We need to know that KNN is still our best fit for this
            # data set
            self.assertGreater(knn.fitness(), brave.fitness())
            self.assertGreater(knn.fitness(), wimpy.fitness())

        # Finally we expect that the standard KNN process will obtain
        # within 10 percent margin of the best possible solution
        self.assertGreater(actual_value / maximum_value, 0.9)
    def test_with_all_hypothesis(self):
        brave = BraveHypothesis()
        wimpy = WimpyHypothesis()

        knn3 = KNearestNeighbors(3)
        knn5 = KNearestNeighbors(5)
        knn7 = KNearestNeighbors(7)
        knn11 = KNearestNeighbors(11)

        rando = RandoHypothesis()
        prob = SimpleProbabilityHypothesis()

#        drP3 = OptimusPerceptron(11)
#        drP5 = DrPerceptron(5)
#        drP7 = DrPerceptron(7)
#        drP11 = DrPerceptron(11)
#
        self.setUpDecisioner(brave,
                             wimpy,
                             knn3,
                             knn5,
                             knn7,
                             knn11,
                             rando,
                             prob,
#                             drP3,
#                             drP5,
#                             drP7,
#                             drP11,
                            )

        def create_monster():
            color = randint(1, 100)

            if color < 70:
                if random() >= 0.3:
                    return Monster(0, [color], 'passive')
                return Monster(1, [color], 'aggressive')
            else:
                if random() >= 0.7:
                    return Monster(0, [color], 'passive')
                return Monster(1, [color], 'aggressive')

        for i in range(100):
            monster = create_monster()
            self.decisioner.update(monster.color, 1, monster.action(True))

        maximum_value = 1000.0
        actual_value = 0.0

        for i in range(1000):
            monster = create_monster()

            guess = self.decisioner.get_guess(monster.color)
            outcome = monster.action(guess)
            self.decisioner.update(monster.color, guess, outcome)

            maximum_value -= monster._aggressive
            actual_value += outcome
    def test_update_ratio_matches_expected(self):
        """Test the KNN algorithm with our built in rolling window"""
        knn = KNearestNeighbors(1)

        # First we define the method for generating our two
        # categories in a single dimension
        def generate_monster():
            color = randint(1, 100)

            # All colors less than 50 are passive
            if color < 50:
                return Monster(0, [color], 'passive')

            # Otherwise the monster is aggresive
            return Monster(1, [color], 'aggressive')

        # Run the inital training phase
        for i in range(100):
            monster = generate_monster()
            knn.update(monster.color, 1, monster.action(True))

        # Then we need to run over a set of values, we should see
        # a relatively high ratio of actual value vs maximum value
        actual_value = 0.0
        maximum_value = 5000.0

        for i in range(5000):
            monster = generate_monster()
            guess = knn.get_guess(monster.color)
            outcome = monster.action(guess)

            maximum_value -= monster._aggressive
            actual_value += outcome

            knn.update(monster.color, guess, outcome)

        self.assertGreaterEqual(actual_value / maximum_value, 0.9)
    def test_update_ratio_matches_expected(self):
        """Test the KNN algorithm with our built in rolling window"""
        knn = KNearestNeighbors(1)

        # First we define the method for generating our two
        # categories in a single dimension
        def generate_monster():
            color = randint(1, 100)

            # All colors less than 50 are passive
            if color < 50:
                return Monster(0, [color], 'passive')

            # Otherwise the monster is aggresive
            return Monster(1, [color], 'aggressive')

        # Run the inital training phase
        for i in range(100):
            monster = generate_monster()
            knn.update(monster.color, 1, monster.action(True))

        # Then we need to run over a set of values, we should see
        # a relatively high ratio of actual value vs maximum value
        actual_value = 0.0
        maximum_value = 5000.0

        for i in range(5000):
            monster = generate_monster()
            guess = knn.get_guess(monster.color)
            outcome = monster.action(guess)

            maximum_value -= monster._aggressive
            actual_value += outcome

            knn.update(monster.color, guess, outcome)
        
        self.assertGreaterEqual(actual_value/ maximum_value, 0.9)
Example #5
0
    def create_monster():
        color = randint(1, 100)

        if color < 70:
            if random() < 0.3:
                return Monster(1, [color], 'aggressive')
            return Monster(0, [color], 'passive')
        else:
            if random() < 0.7:
                return Monster(1, [color], 'aggressive')
            return Monster(0, [color], 'passive')

    hyps = [
        BraveHypothesis(),
        WimpyHypothesis(),
        KNearestNeighbors(3),
        KNearestNeighbors(5),
        KNearestNeighbors(7),
        KNearestNeighbors(11),
        KNearestNeighbors(17),
        KNearestNeighbors(23),
        KNearestNeighbors(29),
        KNearestNeighbors(31),
        KNearestNeighbors(37),
        KNearestNeighbors(41),
        KNearestNeighbors(43),
        KNearestNeighbors(47),
        SimpleProbabilityHypothesis(),
        RandoHypothesis(),
        OptimusPerceptron(47),  # mod 47 universe
        OptimusPerceptron(43),  # mod 43 universe
 def setUp(self):
     # We set the window to 99 to allow the first set to key
     # on 100 values updated
     self._hypothesis = KNearestNeighbors(3, window=99)
class KNearestNeighborsTest(unittest.TestCase):

    def setUp(self):
        # We set the window to 99 to allow the first set to key
        # on 100 values updated
        self._hypothesis = KNearestNeighbors(3, window=99)

    def tearDown(self):
        self._hypothesis = None

    def test_update_1dimension(self):
        """Test the KNN algorithm in one dimension at multiple points"""
        # Set up cluster of two distant groups in one dimension
        for i in range(50):
            self._hypothesis.update([1], 1, -1)

        for i in range(50):
            self._hypothesis.update([50], 1, 1)

        self.assertFalse(self._hypothesis.get_guess([-1]))
        self.assertFalse(self._hypothesis.get_guess([1]))

        self.assertFalse(self._hypothesis.get_guess([-15]))
        self.assertFalse(self._hypothesis.get_guess([15]))

        self.assertFalse(self._hypothesis.get_guess([-24]))
        self.assertFalse(self._hypothesis.get_guess([24]))

        self.assertTrue(self._hypothesis.get_guess([50]))

        self.assertTrue(self._hypothesis.get_guess([49]))
        self.assertTrue(self._hypothesis.get_guess([51]))

        self.assertTrue(self._hypothesis.get_guess([26]))
        self.assertTrue(self._hypothesis.get_guess([74]))

    def test_update_2dimension(self):
        """Test the KNN algorithm in two dimensions at multiple points"""
        # Set up cluster of two distance groups in two dimensions
        for i in range(50):
            self._hypothesis.update([1, 1], 1, -1)

        for i in range(50):
            self._hypothesis.update([50, 50], 1, 1)

        self.assertFalse(self._hypothesis.get_guess([1, 1]))
        self.assertFalse(self._hypothesis.get_guess([-1, -1]))

        self.assertFalse(self._hypothesis.get_guess([15, 15]))
        self.assertFalse(self._hypothesis.get_guess([-15, -15]))

        self.assertFalse(self._hypothesis.get_guess([24, 24]))
        self.assertFalse(self._hypothesis.get_guess([-24, -24]))

        self.assertTrue(self._hypothesis.get_guess([50, 50]))

        self.assertTrue(self._hypothesis.get_guess([49, 49]))
        self.assertTrue(self._hypothesis.get_guess([51, 51]))

        self.assertTrue(self._hypothesis.get_guess([26, 26]))
        self.assertTrue(self._hypothesis.get_guess([74, 74]))

    def test_update_over_Ndimensions(self):
        """Test the KNN algorithm in between 2 and 10 dimensions"""
        for dimension in range(2, 10):

            # first set the aggressive monsters at [10, ..., 10]
            for i in range(50):
                self._hypothesis.update([10] * dimension, 1, -1)

            # Next set the non-aggresive monsters at [90, ..., 90]
            for i in range(50):
                self._hypothesis.update([90] * dimension, 1, 1)

            self.assertFalse(self._hypothesis.get_guess([0] * dimension))
            self.assertFalse(self._hypothesis.get_guess([40] * dimension))

            self.assertTrue(self._hypothesis.get_guess([100] * dimension))
            self.assertTrue(self._hypothesis.get_guess([55] * dimension))

    def test_update_ratio_matches_expected(self):
        """Test the KNN algorithm with our built in rolling window"""
        knn = KNearestNeighbors(1)

        # First we define the method for generating our two
        # categories in a single dimension
        def generate_monster():
            color = randint(1, 100)

            # All colors less than 50 are passive
            if color < 50:
                return Monster(0, [color], 'passive')

            # Otherwise the monster is aggresive
            return Monster(1, [color], 'aggressive')

        # Run the inital training phase
        for i in range(100):
            monster = generate_monster()
            knn.update(monster.color, 1, monster.action(True))

        # Then we need to run over a set of values, we should see
        # a relatively high ratio of actual value vs maximum value
        actual_value = 0.0
        maximum_value = 5000.0

        for i in range(5000):
            monster = generate_monster()
            guess = knn.get_guess(monster.color)
            outcome = monster.action(guess)

            maximum_value -= monster._aggressive
            actual_value += outcome

            knn.update(monster.color, guess, outcome)
        
        self.assertGreaterEqual(actual_value/ maximum_value, 0.9)
 def setUp(self):
     # We set the window to 99 to allow the first set to key
     # on 100 values updated
     self._hypothesis = KNearestNeighbors(3, window=99)
class KNearestNeighborsTest(unittest.TestCase):
    def setUp(self):
        # We set the window to 99 to allow the first set to key
        # on 100 values updated
        self._hypothesis = KNearestNeighbors(3, window=99)

    def tearDown(self):
        self._hypothesis = None

    def test_update_1dimension(self):
        """Test the KNN algorithm in one dimension at multiple points"""
        # Set up cluster of two distant groups in one dimension
        for i in range(50):
            self._hypothesis.update([1], 1, -1)

        for i in range(50):
            self._hypothesis.update([50], 1, 1)

        self.assertFalse(self._hypothesis.get_guess([-1]))
        self.assertFalse(self._hypothesis.get_guess([1]))

        self.assertFalse(self._hypothesis.get_guess([-15]))
        self.assertFalse(self._hypothesis.get_guess([15]))

        self.assertFalse(self._hypothesis.get_guess([-24]))
        self.assertFalse(self._hypothesis.get_guess([24]))

        self.assertTrue(self._hypothesis.get_guess([50]))

        self.assertTrue(self._hypothesis.get_guess([49]))
        self.assertTrue(self._hypothesis.get_guess([51]))

        self.assertTrue(self._hypothesis.get_guess([26]))
        self.assertTrue(self._hypothesis.get_guess([74]))

    def test_update_2dimension(self):
        """Test the KNN algorithm in two dimensions at multiple points"""
        # Set up cluster of two distance groups in two dimensions
        for i in range(50):
            self._hypothesis.update([1, 1], 1, -1)

        for i in range(50):
            self._hypothesis.update([50, 50], 1, 1)

        self.assertFalse(self._hypothesis.get_guess([1, 1]))
        self.assertFalse(self._hypothesis.get_guess([-1, -1]))

        self.assertFalse(self._hypothesis.get_guess([15, 15]))
        self.assertFalse(self._hypothesis.get_guess([-15, -15]))

        self.assertFalse(self._hypothesis.get_guess([24, 24]))
        self.assertFalse(self._hypothesis.get_guess([-24, -24]))

        self.assertTrue(self._hypothesis.get_guess([50, 50]))

        self.assertTrue(self._hypothesis.get_guess([49, 49]))
        self.assertTrue(self._hypothesis.get_guess([51, 51]))

        self.assertTrue(self._hypothesis.get_guess([26, 26]))
        self.assertTrue(self._hypothesis.get_guess([74, 74]))

    def test_update_over_Ndimensions(self):
        """Test the KNN algorithm in between 2 and 10 dimensions"""
        for dimension in range(2, 10):

            # first set the aggressive monsters at [10, ..., 10]
            for i in range(50):
                self._hypothesis.update([10] * dimension, 1, -1)

            # Next set the non-aggresive monsters at [90, ..., 90]
            for i in range(50):
                self._hypothesis.update([90] * dimension, 1, 1)

            self.assertFalse(self._hypothesis.get_guess([0] * dimension))
            self.assertFalse(self._hypothesis.get_guess([40] * dimension))

            self.assertTrue(self._hypothesis.get_guess([100] * dimension))
            self.assertTrue(self._hypothesis.get_guess([55] * dimension))

    def test_update_ratio_matches_expected(self):
        """Test the KNN algorithm with our built in rolling window"""
        knn = KNearestNeighbors(1)

        # First we define the method for generating our two
        # categories in a single dimension
        def generate_monster():
            color = randint(1, 100)

            # All colors less than 50 are passive
            if color < 50:
                return Monster(0, [color], 'passive')

            # Otherwise the monster is aggresive
            return Monster(1, [color], 'aggressive')

        # Run the inital training phase
        for i in range(100):
            monster = generate_monster()
            knn.update(monster.color, 1, monster.action(True))

        # Then we need to run over a set of values, we should see
        # a relatively high ratio of actual value vs maximum value
        actual_value = 0.0
        maximum_value = 5000.0

        for i in range(5000):
            monster = generate_monster()
            guess = knn.get_guess(monster.color)
            outcome = monster.action(guess)

            maximum_value -= monster._aggressive
            actual_value += outcome

            knn.update(monster.color, guess, outcome)

        self.assertGreaterEqual(actual_value / maximum_value, 0.9)