from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
from builtins import *  # NOQA
standard_library.install_aliases()  # NOQA

import basetest_dqn_like as base
import chainerrl
from chainerrl.agents.dqn import DQN


class TestDQNOnDiscreteABC(base._TestDQNOnDiscreteABC):

    def make_dqn_agent(self, env, q_func, opt, explorer, rbuf, gpu):
        return DQN(q_func, opt, rbuf, gpu=gpu, gamma=0.9, explorer=explorer,
                   replay_start_size=100, target_update_interval=100)


class TestDQNOnDiscreteABCBoltzmann(base._TestDQNOnDiscreteABC):

    def make_dqn_agent(self, env, q_func, opt, explorer, rbuf, gpu):
        explorer = chainerrl.explorers.Boltzmann()
        return DQN(q_func, opt, rbuf, gpu=gpu, gamma=0.9, explorer=explorer,
                   replay_start_size=100, target_update_interval=100)


class TestDQNOnContinuousABC(base._TestDQNOnContinuousABC):

    def make_dqn_agent(self, env, q_func, opt, explorer, rbuf, gpu):
        return DQN(q_func, opt, rbuf, gpu=gpu, gamma=0.9, explorer=explorer,
                   replay_start_size=100, target_update_interval=100)


class TestDQNOnDiscretePOABC(base._TestDQNOnDiscretePOABC):

    def make_dqn_agent(self, env, q_func, opt, explorer, rbuf, gpu):
        return DQN(q_func, opt, rbuf, gpu=gpu, gamma=0.9, explorer=explorer,
                   replay_start_size=100, target_update_interval=100,
                   episodic_update=True)

@@ -251,16 +251,52 @@
   # NOQA
+%0Aimport unittest%0A%0Aimport numpy as np
 %0A%0Aimport
@@ -339,16 +339,129 @@
 ainerrl%0A
+from chainerrl.agents.dqn import compute_value_loss%0Afrom chainerrl.agents.dqn import compute_weighted_value_loss%0A
 from cha
@@ -1657,16 +1657,16 @@
 al=100,%0A
-
         
@@ -1698,8 +1698,1762 @@
 e=True)%0A
+%0A%0Adef _huber_loss_1(a):%0A    if abs(a) %3C 1:%0A        return 0.5 * a ** 2%0A    else:%0A        return abs(a) - 0.5%0A%0A%0Aclass TestComputeWeightedValueLoss(unittest.TestCase):%0A%0A    def setUp(self):%0A        pass%0A%0A    def test(self):%0A        y = np.asarray(%5B1.0, 2.0, 3.0%5D, dtype='f')%0A        t = np.asarray(%5B2.1, 2.2, 2.3%5D, dtype='f')%0A        gt_huber_losses = np.asarray(%5B_huber_loss_1(a) for a in y - t%5D)%0A%0A        mean_loss = compute_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='mean').data%0A        self.assertAlmostEqual(mean_loss, gt_huber_losses.mean(), places=5)%0A%0A        sum_loss = compute_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='sum').data%0A        self.assertAlmostEqual(sum_loss, gt_huber_losses.sum(), places=5)%0A%0A        w1 = np.asarray(%5B1, 1, 1%5D)%0A%0A        mean_loss_w1 = compute_weighted_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='mean',%0A            weights=w1).data%0A        self.assertAlmostEqual(mean_loss_w1, mean_loss, places=5)%0A%0A        sum_loss_w1 = compute_weighted_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='sum',%0A            weights=w1).data%0A        self.assertAlmostEqual(sum_loss_w1, sum_loss, places=5)%0A%0A        wu = np.random.uniform(low=0, high=2, size=3).astype('f')%0A        mean_loss_wu = compute_weighted_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='mean',%0A            weights=wu).data%0A        self.assertAlmostEqual(%0A            mean_loss_wu, (gt_huber_losses * wu).mean(), places=5)%0A%0A        sum_loss_wu = compute_weighted_value_loss(%0A            y, t, clip_delta=True, batch_accumulator='sum',%0A            weights=wu).data%0A        self.assertAlmostEqual(%0A            sum_loss_wu, (gt_huber_losses * wu).sum(), places=5)%0A

Add tests for DQN loss functions