def test_wrapped_not_picklable_learner_with_reduce(self): sim1 = LambdaSimulation(5, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) learner = WrappedLearner(NotPicklableLearnerWithReduce()) experiment = Experiment([sim1], [learner]) experiment.evaluate()
def test_wrapped_not_picklable_learner_sans_reduce(self): sim1 = LambdaSimulation(5, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) learner = WrappedLearner(NotPicklableLearner()) experiment = Experiment([sim1], [learner]) CobaContext.logger = BasicLogger(ListSink()) experiment.evaluate() self.assertEqual(1, len(CobaContext.logger.sink.items)) self.assertIn("pickle", CobaContext.logger.sink.items[0])
def test_no_params(self): sim1 = NoParamsEnvironment() learner = NoParamsLearner() experiment = Experiment([sim1], [learner], evaluation_task=OnlineOnPolicyEvalTask(False)) result = experiment.evaluate() actual_learners = result.learners.to_tuples() actual_environments = result.environments.to_tuples() actual_interactions = result.interactions.to_tuples() expected_learners = [(0, 'NoParamsLearner', 'NoParamsLearner')] expected_environments = [(0, 'NoParamsEnvironment')] expected_interactions = [(0, 0, 1, 0), (0, 0, 2, 1)] self.assertCountEqual(actual_learners, expected_learners) self.assertCountEqual(actual_environments, expected_environments) self.assertCountEqual(actual_interactions, expected_interactions)
def test_sim(self): sim1 = LambdaSimulation(2, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) learner = ModuloLearner() experiment = Experiment([sim1], [learner], evaluation_task=OnlineOnPolicyEvalTask(False)) result = experiment.evaluate() actual_learners = result.learners.to_tuples() actual_environments = result.environments.to_tuples() actual_interactions = result.interactions.to_tuples() expected_learners = [(0, "Modulo", "Modulo(p=0)", '0')] expected_environments = [(0, 'LambdaSimulation')] expected_interactions = [(0, 0, 1, 0), (0, 0, 2, 1)] self.assertCountEqual(actual_learners, expected_learners) self.assertCountEqual(actual_environments, expected_environments) self.assertCountEqual(actual_interactions, expected_interactions)
def test_learners(self): sim = LambdaSimulation(2, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) learner1 = ModuloLearner("0") #type: ignore learner2 = ModuloLearner("1") #type: ignore experiment = Experiment([sim], [learner1, learner2], evaluation_task=OnlineOnPolicyEvalTask(False)) actual_result = experiment.evaluate() actual_learners = actual_result._learners.to_tuples() actual_environments = actual_result._environments.to_tuples() actual_interactions = actual_result.interactions.to_tuples() expected_learners = [(0, "Modulo", "Modulo(p=0)", '0'), (1, "Modulo", "Modulo(p=1)", '1')] expected_environments = [(0, 'LambdaSimulation')] expected_interactions = [(0, 0, 1, 0), (0, 0, 2, 1), (0, 1, 1, 0), (0, 1, 2, 1)] self.assertCountEqual(actual_learners, expected_learners) self.assertCountEqual(actual_environments, expected_environments) self.assertCountEqual(actual_interactions, expected_interactions)
def test_ignore_raise(self): CobaContext.logger = IndentLogger(ListSink()) sim1 = LambdaSimulation(2, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) sim2 = LambdaSimulation(3, lambda i: i, lambda i, c: [3, 4, 5], lambda i, c, a: cast(float, a)) experiment = Experiment( [sim1, sim2], [ModuloLearner(), BrokenLearner()], evaluation_task=OnlineOnPolicyEvalTask(False)) result = experiment.evaluate() actual_learners = result.learners.to_tuples() actual_environments = result.environments.to_tuples() actual_interactions = result.interactions.to_tuples() expected_learners = [(0, "Modulo", "Modulo(p=0)", '0'), (1, "Broken", "Broken", float('nan'))] expected_environments = [(0, 'LambdaSimulation'), (1, 'LambdaSimulation')] expected_interactions = [(0, 0, 1, 0), (0, 0, 2, 1), (1, 0, 1, 3), (1, 0, 2, 4), (1, 0, 3, 5)] self.assertIsInstance(CobaContext.logger, IndentLogger) self.assertEqual( 2, sum([ int("Unexpected exception:" in item) for item in CobaContext.logger.sink.items ])) self.assertCountEqual(actual_learners[0], expected_learners[0]) self.assertCountEqual(actual_learners[1][:3], expected_learners[1][:3]) self.assertTrue(math.isnan(expected_learners[1][3])) self.assertCountEqual(actual_environments, expected_environments) self.assertCountEqual(actual_interactions, expected_interactions)