@Test public void when_runIsCalledMultipleTimes_expect_rewardSentToLearningBehaviorToBeCorrect() { AgentLearner.Configuration configuration = AgentLearner.Configuration.builder() .maxEpisodeSteps(4) .build(); AgentLearner<Integer> sut = new AgentLearner(environmentMock, transformProcessMock, policyMock, configuration, null, learningBehaviorMock); Schema schema = new Schema(new IntegerActionSchema(0, -1)); when(environmentMock.getSchema()).thenReturn(schema); when(environmentMock.reset()).thenReturn(new HashMap<>()); double[] reward = new double[] { 0.0 }; when(environmentMock.step(any(Integer.class))) .thenAnswer(a -> new StepResult(new HashMap<>(), ++reward[0], reward[0] == 4.0)); when(environmentMock.isEpisodeFinished()).thenAnswer(x -> reward[0] == 4.0); when(transformProcessMock.transform(any(Map.class), anyInt(), anyBoolean())) .thenAnswer(new Answer<Observation>() { public Observation answer(InvocationOnMock invocation) throws Throwable { int step = (int)invocation.getArgument(1); boolean isTerminal = (boolean)invocation.getArgument(2); return (step % 2 == 0 || isTerminal) ? new Observation(Nd4j.create(new double[] { step * 1.1 })) : Observation.SkippedObservation; } }); when(policyMock.nextAction(any(Observation.class))).thenAnswer(x -> (int)reward[0]); sut.run(); reward[0] = 0.0; sut.run(); ArgumentCaptor<Double> rewardCaptor = ArgumentCaptor.forClass(Double.class); verify(learningBehaviorMock, times(4)).handleNewExperience(any(Observation.class), any(Integer.class), rewardCaptor.capture(), any(Boolean.class)); List<Double> rewards = rewardCaptor.getAllValues(); assertEquals(0.0 + 1.0, rewards.get(2), 0.00001); assertEquals(2.0 + 3.0, rewards.get(3), 0.00001); }
@Override protected void reset() { super.reset(); rewardAtLastExperience = 0; }
AgentLearner extends Agent<ACTION> implements IAgentLearner<ACTION> { @Override protected void reset() { super.reset(); rewardAtLastExperience = 0; } }
AgentLearner extends Agent<ACTION> implements IAgentLearner<ACTION> { @Override protected void reset() { super.reset(); rewardAtLastExperience = 0; } AgentLearner(Environment<ACTION> environment,
                        TransformProcess transformProcess,
                        IPolicy<ACTION> policy,
                        Configuration configuration,
                        String id,
                        @NonNull ILearningBehavior<ACTION> learningBehavior); }
AgentLearner extends Agent<ACTION> implements IAgentLearner<ACTION> { @Override protected void reset() { super.reset(); rewardAtLastExperience = 0; } AgentLearner(Environment<ACTION> environment,
                        TransformProcess transformProcess,
                        IPolicy<ACTION> policy,
                        Configuration configuration,
                        String id,
                        @NonNull ILearningBehavior<ACTION> learningBehavior);  }
AgentLearner extends Agent<ACTION> implements IAgentLearner<ACTION> { @Override protected void reset() { super.reset(); rewardAtLastExperience = 0; } AgentLearner(Environment<ACTION> environment,
                        TransformProcess transformProcess,
                        IPolicy<ACTION> policy,
                        Configuration configuration,
                        String id,
                        @NonNull ILearningBehavior<ACTION> learningBehavior);   }