def test_frequencyResponse_forHarmonic_WithOptimusPerceptron(self): brave = BraveHypothesis() wimpy = WimpyHypothesis() oPP = OptimusPerceptron() self.setUpDecisioner(brave, wimpy, oPP) # First we will start with the basic training case # which is the first 100 in the range for i in range(101): nonAggro = 0 # this means not aggro color = [randint(1, 100)] monster = Monster(nonAggro, color, 'passive') # Get the guess from the decisioner for the first 100, # we expect every guess to be 1 self.assertTrue(self.decisioner.get_guess(monster.color)) # Then we will update from this guess self.decisioner.update(monster.color, 1, monster.action(True)) # Dr. Perceptron should do better than brave and wimpy # when encountering monsters that repeat with a frequency # that is trainable given Dr.Perceptron's window size. # In otherwords, Dr. Perceptron trains on a frequency of # Monsters but training on a pattern is limited to the # input size of Dr. Perceptron (which as of this check-in # is 5) # # We test a staggered input with a repeating pattern for i in range(1000): aggroPattern = [0, 1, 1, 0, 1] aggroIdx = i%len(aggroPattern) color = [randint(1, 100)] monster = Monster(aggroPattern[aggroIdx], color, 'aggressiveish') # Then we will update from this guess self.decisioner.update(monster.color, aggroPattern[aggroIdx], aggroPattern[aggroIdx]) # drP should always be better than wimpy self.assertGreater(oPP.fitness(), wimpy.fitness()) # after a lot of training for a pattern that is harmonic within the input size Dr.P should beat out brave self.assertGreater(oPP.fitness(), brave.fitness()) # Since the pattern is highly regular within the input size, the fitness should be really really close to 1. # With great harmony results great trainability and therefore great fitness self.assertGreater(oPP.fitness(), 0.99)
def test_frequencyResponse_WithOptimusPerceptron(self): brave = BraveHypothesis() wimpy = WimpyHypothesis() oPP = OptimusPerceptron(4) self.setUpDecisioner(brave, wimpy, oPP) # First we will start with the basic training case # which is the first 100 in the range for i in range(101): nonAggro = 0 # this means not aggro color = [randint(1, 100)] monster = Monster(nonAggro, color, 'passive') # Get the guess from the decisioner for the first 100, # we expect every guess to be 1 self.assertTrue(self.decisioner.get_guess(monster.color)) # Then we will update from this guess self.decisioner.update(monster.color, 1, monster.action(True)) # Dr. Perceptron should do better than brave and wimpy # when encountering monsters that repeat with a frequency # that is trainable given Dr.Perceptron's window size. # In otherwords, Dr. Perceptron trains on a frequency of # Monsters but training on a pattern is limited to the # input size of Dr. Perceptron (which as of this check-in # is 5) # # We test a staggered input for i in range(100): aggro = 1 passive = 0 evenMonstersPassive = i%2 color = [randint(1, 100)] monster = Monster(evenMonstersPassive, color, 'aggressiveish') # Then we will update from this guess self.decisioner.update(monster.color, evenMonstersPassive, evenMonstersPassive) #print ('i:'+str(i)+' '+str(evenMonstersPassive)+' '+str(oPP.fitness())) self.assertGreater(oPP.fitness(), wimpy.fitness()) self.assertGreater(oPP.fitness(), brave.fitness())
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 OptimusPerceptron(41), # mod 41 universe OptimusPerceptron(37), # mod 37 universe OptimusPerceptron(31), # mod 31 universe OptimusPerceptron(29), # mod 29 universe OptimusPerceptron(23), # mod 23 universe OptimusPerceptron(17), # mod 17 universe OptimusPerceptron(11), # mod 11 universe OptimusPerceptron(7), # mod 7 universe OptimusPerceptron(5), # mod 5 universe OptimusPerceptron(3), # mod 3 universe OptimusPerceptron(2), # even universe OptimusPerceptron( 1), # lonliest number universe, interesting but not a good idea ]
def setUp(self): self._hypothesis = OptimusPerceptron()
class OptimusPerceptronHypothesisTest(unittest.TestCase): def setUp(self): self._hypothesis = OptimusPerceptron() def tearDown(self): self._hypothesis = None def test_attacked_bad(self): """Perceptron: Test the aggro == bad case""" for i in range(100): self._hypothesis.update([1], 1, -1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_bad TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess == False) def test_attacked_good(self): """Perceptron: Test the non aggro == good case""" for i in range(100): self._hypothesis.update([1], 1, 1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_good TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess == True) def test_attacked_bad_randoWeights(self): """Perceptron: Randomize the weights: Test the aggro == bad case""" self._hypothesis._randoWeights() for i in range(100): self._hypothesis.update([1], 1, -1) trainedGuess = self._hypothesis.get_guess([1]) print('OptimusPERCEPTRON: test_attacked_bad TrainedGuess: ' + str(trainedGuess)) print(str(self._hypothesis)) self.assertTrue(trainedGuess == False) def test_attacked_good_randoWeights(self): """Perceptron: Randomize the weights: Test the non aggro == good case""" self._hypothesis._randoWeights() for i in range(100): self._hypothesis.update([1], 1, 1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_good TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess == True)
class OptimusPerceptronHypothesisTest(unittest.TestCase): def setUp(self): self._hypothesis = OptimusPerceptron() def tearDown(self): self._hypothesis = None def test_attacked_bad(self): """Perceptron: Test the aggro == bad case""" for i in range(100): self._hypothesis.update([1],1,-1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_bad TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess==False) def test_attacked_good(self): """Perceptron: Test the non aggro == good case""" for i in range(100): self._hypothesis.update([1],1,1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_good TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess==True) def test_attacked_bad_randoWeights(self): """Perceptron: Randomize the weights: Test the aggro == bad case""" self._hypothesis._randoWeights() for i in range(100): self._hypothesis.update([1],1,-1) trainedGuess = self._hypothesis.get_guess([1]) print ('OptimusPERCEPTRON: test_attacked_bad TrainedGuess: '+str(trainedGuess)) print (str(self._hypothesis)) self.assertTrue(trainedGuess==False) def test_attacked_good_randoWeights(self): """Perceptron: Randomize the weights: Test the non aggro == good case""" self._hypothesis._randoWeights() for i in range(100): self._hypothesis.update([1],1,1) trainedGuess = self._hypothesis.get_guess([1]) #print ('DRPERCEPTRON: test_attacked_good TrainedGuess: '+str(trainedGuess)) #print (str(self._hypothesis)) self.assertTrue(trainedGuess==True)