# Copyright 2019 DeepMind Technologies Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for mirror descent."""

from absl.testing import absltest

from open_spiel.python.mfg.algorithms import mirror_descent
from open_spiel.python.mfg.algorithms import nash_conv
from open_spiel.python.mfg.games import crowd_modelling
import pyspiel


class MirrorDescentTest(absltest.TestCase):

  def test_fp_python_game(self):
    """Checks if mirror descent works."""
    game = crowd_modelling.MFGCrowdModellingGame()
    md = mirror_descent.MirrorDescent(game)
    for _ in range(10):
      md.iteration()
    md_policy = md.get_policy()
    nash_conv_md = nash_conv.NashConv(game, md_policy)

    self.assertAlmostEqual(nash_conv_md.nash_conv(), 2.2730324915546056)

  def test_fp_cpp_game(self):
    """Checks if mirror descent works."""
    game = pyspiel.load_game("mfg_crowd_modelling")
    md = mirror_descent.MirrorDescent(game)
    for _ in range(10):
      md.iteration()
    md_policy = md.get_policy()
    nash_conv_md = nash_conv.NashConv(game, md_policy)

    self.assertAlmostEqual(nash_conv_md.nash_conv(), 2.2730324915546056)


if __name__ == "__main__":
  absltest.main()
