示例#1
0
    def test_against_haystack(self):
        scenario = HaystackProblem(training_cycles=10000, input_size=500)

        algorithm = xcs.XCSAlgorithm()
        algorithm.ga_threshold = 1
        algorithm.crossover_probability = .5
        algorithm.exploration_probability = .1
        algorithm.discount_factor = 0
        algorithm.do_ga_subsumption = False
        algorithm.do_action_set_subsumption = True
        algorithm.wildcard_probability = 1 - 1 / 500
        algorithm.deletion_threshold = 1
        algorithm.mutation_probability = 1 / 500

        best = None
        expected = .6

        for _ in range(2):
            logging.disable(logging.CRITICAL)
            try:
                scenario.reset()
                steps, total_reward, time_passed, population = xcs.test(
                    algorithm,
                    scenario
                )
            finally:
                logging.disable(logging.NOTSET)

            average_reward = total_reward / steps
            self.assertGreater(average_reward, .48)
            self.assertLess(time_passed, 100)
            if average_reward >= expected:
                break
            elif best is None or best < average_reward:
                best = average_reward
        else:
            self.fail("Failed to achieve expected average reward level. "
                      "(Missed by %f.)" % (expected - best))
示例#2
0
文件: test_XCS.py 项目: cbtang01/SOC
    def test_against_haystack(self):
        scenario = HaystackProblem(training_cycles=10000, input_size=500)

        algorithm = xcs.XCSAlgorithm()
        algorithm.ga_threshold = 1
        algorithm.crossover_probability = .5
        algorithm.exploration_probability = .1
        algorithm.discount_factor = 0
        algorithm.do_ga_subsumption = False
        algorithm.do_action_set_subsumption = True
        algorithm.wildcard_probability = 1 - 1 / 500
        algorithm.deletion_threshold = 1
        algorithm.mutation_probability = 1 / 500

        best = None
        expected = .6

        for _ in range(2):
            logging.disable(logging.CRITICAL)
            try:
                scenario.reset()
                steps, total_reward, time_passed, population = xcs.test(
                    algorithm, scenario)
            finally:
                logging.disable(logging.NOTSET)

            average_reward = total_reward / steps
            self.assertGreater(average_reward, .48)
            self.assertLess(time_passed, 100)
            if average_reward >= expected:
                break
            elif best is None or best < average_reward:
                best = average_reward
        else:
            self.fail("Failed to achieve expected average reward level. "
                      "(Missed by %f.)" % (expected - best))
示例#3
0
 def setUp(self):
     self.scenario = HaystackProblem(10)
示例#4
0
class TestHaystackProblem(unittest.TestCase):
    def setUp(self):
        self.scenario = HaystackProblem(10)

    def test_get_possible_actions(self):
        actions = self.scenario.get_possible_actions()
        self.assertTrue(len(actions) == 2)
        self.assertTrue(True in actions)
        self.assertTrue(False in actions)

    def test_sense(self):
        previous = self.scenario.sense()
        self.assertIsInstance(previous, BitString)
        self.assertTrue(len(previous) == self.scenario.input_size)
        while self.scenario.more():
            current = self.scenario.sense()
            self.assertIsInstance(current, BitString)
            self.assertTrue(len(current) == self.scenario.input_size)
            if current != previous:
                break
        else:
            self.fail("All situations are the same.")

    def test_execute(self):
        situation = self.scenario.sense()
        value = situation[self.scenario.needle_index]
        self.assertEqual(1, self.scenario.execute(value))
        self.assertEqual(0, self.scenario.execute(not value))

    def test_more(self):
        self.scenario.reset()
        for _ in range(self.scenario.initial_training_cycles):
            self.scenario.sense()
            self.assertTrue(self.scenario.more())
            self.scenario.execute(False)
        self.assertFalse(self.scenario.more())
示例#5
0
class TestHaystackProblem(unittest.TestCase):

    def setUp(self):
        self.scenario = HaystackProblem(10)

    def test_get_possible_actions(self):
        actions = self.scenario.get_possible_actions()
        self.assertTrue(len(actions) == 2)
        self.assertTrue(True in actions)
        self.assertTrue(False in actions)

    def test_sense(self):
        previous = self.scenario.sense()
        self.assertIsInstance(previous, BitString)
        self.assertTrue(len(previous) == self.scenario.input_size)
        while self.scenario.more():
            current = self.scenario.sense()
            self.assertIsInstance(current, BitString)
            self.assertTrue(len(current) == self.scenario.input_size)
            if current != previous:
                break
        else:
            self.fail("All situations are the same.")

    def test_execute(self):
        situation = self.scenario.sense()
        value = situation[self.scenario.needle_index]
        self.assertEqual(1, self.scenario.execute(value))
        self.assertEqual(0, self.scenario.execute(not value))

    def test_more(self):
        self.scenario.reset()
        for _ in range(self.scenario.initial_training_cycles):
            self.scenario.sense()
            self.assertTrue(self.scenario.more())
            self.scenario.execute(False)
        self.assertFalse(self.scenario.more())
示例#6
0
 def setUp(self):
     self.scenario = HaystackProblem(10)