Exemplo n.º 1
0
class BrainLinearTest(unittest.TestCase):
    def setUp(self):
        self.brain = BrainLinear()

    def test_zeroed_brain(self):
        ins = BrainLinear.G_INPUTNODES
        self.brain.import_genes([0] * BrainLinear.G_TOTAL_CONNECTIONS)
        #print "Zeroed tests:"
        self.assertEqual(self.brain.think([0] * ins), (0.005, 0),
                         'Output when detecting nothing is incorrect.')
        self.assertEqual(self.brain.think([1] * ins), (0, 0),
                         'Output with full input incorrect.')
        self.assertEqual(self.brain.think([1] * (ins / 2) + [0] * (ins / 2)),
                         (0, 0), 'Output with full input to left incorrect.')
        self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * (ins / 2)),
                         (0, 0), ' Output with full input to right incorrect.')

        self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0, 0),
                         'Output when not detecting 1 color incorrect.')
        self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)),
                         (0, 0),
                         'Output when not detecting 1 color incorrect.')
        self.assertEqual(
            self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0, 0),
            'Output when not detecting 1 color incorrect.')

        self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2),
                         (0, 0),
                         'Output when not detecting 1 color incorrect.')
        self.assertEqual(
            self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0, 0),
            'Output when not detecting 1 color incorrect.')
        self.assertEqual(
            self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0, 0),
            'Output when not detecting 1 color incorrect.')

    def test_left_antenna(self):
        ins = BrainLinear.G_INPUTNODES
        self.brain.import_genes([1] * (BrainLinear.G_TOTAL_CONNECTIONS / 2) +
                                [0] * (BrainLinear.G_TOTAL_CONNECTIONS / 2))
        #print "Left tests:"
        # self.assertEqual(self.brain.think([0] * ins), (0.005,0), 'Output when detecting nothing is incorrect.')
        # self.assertEqual(self.brain.think([1] * ins), (1.0,1.0), 'Output with full input incorrect.')
        # self.assertEqual(self.brain.think([1] * (ins/2) + [0] * (ins/2)), (0.5,0.5), 'Output with full input to left incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins/2) + [1] * (ins/2)), (0,0), 'Output with full input to right incorrect.')

        # self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')

        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2), (0,0), 'Output when not detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0,0), 'Output when not detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0,0), 'Output when not detecting 1 color incorrect.')

    def test_right_antenna(self):
        ins = BrainLinear.G_INPUTNODES
        self.brain.import_genes([0] * (BrainLinear.G_TOTAL_CONNECTIONS / 2) +
                                [1] * (BrainLinear.G_TOTAL_CONNECTIONS / 2))
        #print "Right tests:"
        # self.assertEqual(self.brain.think([0] * ins), (0.005,0), 'Output when detecting nothing is incorrect.')
        # self.assertEqual(self.brain.think([1] * ins), (0.5,0.5), 'Output with full input incorrect.')
        # self.assertEqual(self.brain.think([1] * (ins/2) + [0] * (ins/2)), (0,0), 'Output with full input to left incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins/2) + [1] * (ins/2)), (0.5,0.5), 'Output with full input to right incorrect.')

        # self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0,0), 'Output when not detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)), (0,0), 'Output when not detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0,0), 'Output when not detecting 1 color incorrect.')

        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
        # self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
    def test_fuzz(self):
        print "Linear:"
        num_tests = 10000
        ins = BrainLinear.G_INPUTNODES
        res = [0] * num_tests
        for i in xrange(num_tests):

            self.brain.import_genes(
                map(lambda x: random.random() * x * 2 - 1,
                    [1] * (BrainLinear.G_TOTAL_CONNECTIONS + 3)))
            res[i] = self.brain.think(
                map(lambda x: random.random() * x, [1] * ins))
            a = res[i]
            self.assertTrue(res[i][0] >= -1 and res[i][0] <= 1,
                            "Value range invalid: %f" % (res[i][0]))
            self.assertTrue(res[i][1] >= -1 and res[i][1] <= 1,
                            "Value range invalid: %f" % (res[i][1]))

        #print 'random weights, random input %d vs %d' % (len([ds for (ds,dr) in res if ds > 0]) , num_tests - len([ds for (ds,dr) in res if ds > 0]))
        self.assertTrue(
            len([ds for (ds, dr) in res if ds > 0]) / float(num_tests) > 0.45
            and len([ds
                     for (ds, dr) in res if ds > 0]) / float(num_tests) < 0.55,
            "fuzz testing gave abnormal distribution of results")
        res_a = np.array(res)
        print "10th percentile: %f" % (np.percentile(res_a, 10))
        print "25th percentile: %f" % (np.percentile(res_a, 25))
        print "50th percentile: %f" % (np.percentile(res_a, 50))
        print "75th percentile: %f" % (np.percentile(res_a, 75))
        print "90th percentile: %f" % (np.percentile(res_a, 90))

    def tearDown(self):
        del self.brain
Exemplo n.º 2
0
	def setUp(self):
		self.brain = BrainLinear()
Exemplo n.º 3
0
 def setUp(self):
     self.brain = BrainLinear()
Exemplo n.º 4
0
class BrainLinearTest(unittest.TestCase):
	def setUp(self):
		self.brain = BrainLinear()

	def test_zeroed_brain(self):
		ins = BrainLinear.G_INPUTNODES
		self.brain.import_genes([0] * BrainLinear.G_TOTAL_CONNECTIONS)
		#print "Zeroed tests:"
		self.assertEqual(self.brain.think([0] * ins), (0.005,0), 'Output when detecting nothing is incorrect.')
		self.assertEqual(self.brain.think([1] * ins), (0,0), 'Output with full input incorrect.')
		self.assertEqual(self.brain.think([1] * (ins/2) + [0] * (ins/2)), (0,0), 'Output with full input to left incorrect.')
		self.assertEqual(self.brain.think([0] * (ins/2) + [1] * (ins/2)), (0,0), ' Output with full input to right incorrect.')

		self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0,0), 'Output when not detecting 1 color incorrect.')
		self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)), (0,0), 'Output when not detecting 1 color incorrect.')
		self.assertEqual(self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0,0), 'Output when not detecting 1 color incorrect.')

		self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2), (0,0), 'Output when not detecting 1 color incorrect.')
		self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0,0), 'Output when not detecting 1 color incorrect.')
		self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0,0), 'Output when not detecting 1 color incorrect.')

	def test_left_antenna(self):
		ins = BrainLinear.G_INPUTNODES
		self.brain.import_genes([1] * (BrainLinear.G_TOTAL_CONNECTIONS / 2) + [0] * (BrainLinear.G_TOTAL_CONNECTIONS / 2))
		#print "Left tests:"
		# self.assertEqual(self.brain.think([0] * ins), (0.005,0), 'Output when detecting nothing is incorrect.')
		# self.assertEqual(self.brain.think([1] * ins), (1.0,1.0), 'Output with full input incorrect.')
		# self.assertEqual(self.brain.think([1] * (ins/2) + [0] * (ins/2)), (0.5,0.5), 'Output with full input to left incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins/2) + [1] * (ins/2)), (0,0), 'Output with full input to right incorrect.')		

		# self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')

		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2), (0,0), 'Output when not detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0,0), 'Output when not detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0,0), 'Output when not detecting 1 color incorrect.')

	def test_right_antenna(self):
		ins = BrainLinear.G_INPUTNODES
		self.brain.import_genes([0] * (BrainLinear.G_TOTAL_CONNECTIONS / 2) + [1] * (BrainLinear.G_TOTAL_CONNECTIONS / 2))
		#print "Right tests:"
		# self.assertEqual(self.brain.think([0] * ins), (0.005,0), 'Output when detecting nothing is incorrect.')
		# self.assertEqual(self.brain.think([1] * ins), (0.5,0.5), 'Output with full input incorrect.')
		# self.assertEqual(self.brain.think([1] * (ins/2) + [0] * (ins/2)), (0,0), 'Output with full input to left incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins/2) + [1] * (ins/2)), (0.5,0.5), 'Output with full input to right incorrect.')				

		# self.assertEqual(self.brain.think([1] * 2 + [0] * (ins - 2)), (0,0), 'Output when not detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([1] + [0] + [1] + [0] * (ins - 3)), (0,0), 'Output when not detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([1] + [0] * 2 + [1] + [0] * (ins - 4)), (0,0), 'Output when not detecting 1 color incorrect.')

		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] * 2 + [0] * 2), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] + [1] + [0]), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
		# self.assertEqual(self.brain.think([0] * (ins / 2) + [1] + [0] * 2 + [1]), (0.5/3,0.5/3), 'Output when only detecting 1 color incorrect.')
	def test_fuzz(self):
		print "Linear:"
		num_tests = 10000
		ins = BrainLinear.G_INPUTNODES
		res = [0]*num_tests 
		for i in xrange(num_tests) :
			
			self.brain.import_genes(map(lambda x: random.random()*x*2-1, [1]*(BrainLinear.G_TOTAL_CONNECTIONS+3)))
			res[i] = self.brain.think(map(lambda x: random.random()*x, [1]*ins))
			a = res[i]
			self.assertTrue(res[i][0] >= -1 and res[i][0] <= 1, "Value range invalid: %f" % (res[i][0]))
			self.assertTrue(res[i][1] >= -1 and res[i][1] <= 1, "Value range invalid: %f" % (res[i][1]))
			
		#print 'random weights, random input %d vs %d' % (len([ds for (ds,dr) in res if ds > 0]) , num_tests - len([ds for (ds,dr) in res if ds > 0]))
		self.assertTrue(len([ds for (ds,dr) in res if ds > 0]) / float(num_tests) > 0.45 and len([ds for (ds,dr) in res if ds > 0]) / float(num_tests) < 0.55, "fuzz testing gave abnormal distribution of results")
		res_a = np.array(res)
		print "10th percentile: %f" % (np.percentile(res_a, 10))
		print "25th percentile: %f" % (np.percentile(res_a, 25))
		print "50th percentile: %f" % (np.percentile(res_a, 50))
		print "75th percentile: %f" % (np.percentile(res_a, 75))
		print "90th percentile: %f" % (np.percentile(res_a, 90))


	def tearDown(self):
		del self.brain