def test_init_success(self, mock_dm): mock_rl_app = MockRLApplication() mock_engine_config = MockEngineConfig() mock_engine_config.start_dt = datetime(2019, 11, 2, 0, 0, 0) mock_engine_config.training_interval = timedelta(days=1) MockEngineConfig._get_current_datetime = lambda _: datetime( 2019, 11, 2, 10, 0, 0) run_context = { TrainAgentOperation.TRAINING_GLOBAL_STEP: 0, "available_data": [] } mock_dm.get.return_value = run_context engine = BaseEngine(mock_rl_app, mock_engine_config) engine._dm = mock_dm engine.init() expected_run_context = { TrainAgentOperation.TRAINING_GLOBAL_STEP: 0, "available_data": [(DATANAME.MODEL, 0)] } mock_dm.get.assert_any_call(DATANAME.RUN_CONTEXT, 0) store_calls = [ call(mock_rl_app.test_agent, DATANAME.MODEL, 0), call(expected_run_context, DATANAME.RUN_CONTEXT, 0) ] mock_dm.store.assert_has_calls(store_calls, any_order=False)
def test_init_run_id_not_0(self): mock_rl_app = MockRLApplication() mock_engine_config = MockEngineConfig() mock_engine_config.start_dt = datetime(2019, 11, 2, 0, 0, 0) mock_engine_config.training_interval = timedelta(days=1) mock_engine_config = MockEngineConfig() engine = BaseEngine(mock_rl_app, mock_engine_config) with self.assertRaises(Exception): engine.init()
def run(self): engine = BaseEngine(self._application, self._dm) engine.init(force_run=True) logger.info("Training started") eval_avg_rwd = [] for run_id in range(1, self._num_runs): engine.train(run_id) if run_id % self._eval_interval == 0: avg_rwd = self._evaluate_agent(run_id, self._num_eval_episodes) eval_avg_rwd.append(avg_rwd) logger.info("Training is done") logger.info("Eval result: %s" % str(eval_avg_rwd)) return eval_avg_rwd
def test_init_success(self, mock_dm): run_context = { TrainAgentOperation.TRAINING_GLOBAL_STEP: 0, "available_data": [] } mock_dm.get.return_value = run_context engine = BaseEngine(self._application, mock_dm) engine._dm = mock_dm engine.init() expected_run_context = { TrainAgentOperation.TRAINING_GLOBAL_STEP: 0, "available_data": [(DATANAME.MODEL, 0)] } mock_dm.get.assert_any_call(DATANAME.RUN_CONTEXT, 0) store_calls = [ call(self._mock_agent_init, DATANAME.MODEL, 0), call(expected_run_context, DATANAME.RUN_CONTEXT, 0) ] mock_dm.store.assert_has_calls(store_calls, any_order=False)
def test_init_run_id_not_0(self, mock_dm): self._application.first_timestep_dt -= timedelta(days=1) engine = BaseEngine(self._application, mock_dm) with self.assertRaises(Exception): engine.init()