<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal <EOL> from nose . tools import eq_ , ok_ , raises <EOL> from quantecon . game_theory import ( <EOL> Player , NormalFormGame , pure2mixed , best_response_2p <EOL> ) <EOL> class TestPlayer_1opponent : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> coordination_game_matrix = [ [ <NUM_LIT:4> , <NUM_LIT:0> ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] ] <EOL> self . player = Player ( coordination_game_matrix ) <EOL> def test_best_response_against_pure ( self ) : <EOL> eq_ ( self . player . best_response ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> def test_best_response_against_mixed ( self ) : <EOL> eq_ ( self . player . best_response ( [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] ) , <NUM_LIT:1> ) <EOL> def test_best_response_list_when_tie ( self ) : <EOL> """<STR_LIT>""" <EOL> assert_array_equal ( <EOL> sorted ( self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , tie_breaking = False ) ) , <EOL> sorted ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> ) <EOL> def test_best_response_with_random_tie_breaking ( self ) : <EOL> """<STR_LIT>""" <EOL> ok_ ( self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , tie_breaking = '<STR_LIT>' ) <EOL> in [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> seed = <NUM_LIT> <EOL> br0 = self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , tie_breaking = '<STR_LIT>' , <EOL> random_state = seed ) <EOL> br1 = self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , tie_breaking = '<STR_LIT>' , <EOL> random_state = seed ) <EOL> eq_ ( br0 , br1 ) <EOL> def test_best_response_with_smallest_tie_breaking ( self ) : <EOL> """<STR_LIT>""" <EOL> eq_ ( self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] ) , <NUM_LIT:0> ) <EOL> def test_best_response_with_payoff_perturbation ( self ) : <EOL> """<STR_LIT>""" <EOL> eq_ ( self . player . best_response ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , <EOL> payoff_perturbation = [ <NUM_LIT:0> , <NUM_LIT:0.1> ] ) , <EOL> <NUM_LIT:1> ) <EOL> eq_ ( self . player . best_response ( [ <NUM_LIT:2> , <NUM_LIT:1> ] , <EOL> payoff_perturbation = [ <NUM_LIT:0> , <NUM_LIT:0.1> ] ) , <EOL> <NUM_LIT:1> ) <EOL> def test_is_best_response_against_pure ( self ) : <EOL> ok_ ( self . player . is_best_response ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def test_is_best_response_against_mixed ( self ) : <EOL> ok_ ( self . player . is_best_response ( [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] , [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] ) ) <EOL> class TestPlayer_2opponents : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> payoffs_2opponents = [ [ [ <NUM_LIT:3> , <NUM_LIT:6> ] , <EOL> [ <NUM_LIT:4> , <NUM_LIT:2> ] ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:5> , <NUM_LIT:7> ] ] ] <EOL> self . player = Player ( payoffs_2opponents ) <EOL> def test_payoff_vector_against_pure ( self ) : <EOL> assert_array_equal ( self . player . payoff_vector ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) , [ <NUM_LIT:6> , <NUM_LIT:0> ] ) <EOL> def test_is_best_response_against_pure ( self ) : <EOL> ok_ ( not self . player . is_best_response ( <NUM_LIT:0> , ( <NUM_LIT:1> , <NUM_LIT:0> ) ) ) <EOL> def test_best_response_against_pure ( self ) : <EOL> eq_ ( self . player . best_response ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) , <NUM_LIT:1> ) <EOL> def test_best_response_list_when_tie ( self ) : <EOL> """<STR_LIT>""" <EOL> assert_array_equal ( <EOL> sorted ( self . player . best_response ( ( [ <NUM_LIT:3> / <NUM_LIT:7> , <NUM_LIT:4> / <NUM_LIT:7> ] , [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] ) , <EOL> tie_breaking = False ) ) , <EOL> sorted ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> ) <EOL> def test_random_choice ( ) : <EOL> n , m = <NUM_LIT:5> , <NUM_LIT:4> <EOL> payoff_matrix = np . zeros ( ( n , m ) ) <EOL> player = Player ( payoff_matrix ) <EOL> eq_ ( player . random_choice ( [ <NUM_LIT:0> ] ) , <NUM_LIT:0> ) <EOL> actions = list ( range ( player . num_actions ) ) <EOL> ok_ ( player . random_choice ( ) in actions ) <EOL> class TestNormalFormGame_Sym2p : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> coordination_game_matrix = [ [ <NUM_LIT:4> , <NUM_LIT:0> ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] ] <EOL> self . g = NormalFormGame ( coordination_game_matrix ) <EOL> def test_getitem ( self ) : <EOL> assert_array_equal ( self . g [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ) <EOL> def test_is_nash_pure ( self ) : <EOL> ok_ ( self . g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> def test_is_nash_mixed ( self ) : <EOL> ok_ ( self . g . is_nash ( ( [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] , [ <NUM_LIT:2> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] ) ) ) <EOL> class TestNormalFormGame_Asym2p : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> matching_pennies_bimatrix = [ [ ( <NUM_LIT:1> , - <NUM_LIT:1> ) , ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] , <EOL> [ ( - <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , - <NUM_LIT:1> ) ] ] <EOL> self . g = NormalFormGame ( matching_pennies_bimatrix ) <EOL> def test_getitem ( self ) : <EOL> assert_array_equal ( self . g [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ - <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> def test_is_nash_against_pure ( self ) : <EOL> ok_ ( not self . g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> def test_is_nash_against_mixed ( self ) : <EOL> ok_ ( self . g . is_nash ( ( [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] , [ <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] ) ) ) <EOL> class TestNormalFormGame_3p : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> payoffs_2opponents = [ [ [ <NUM_LIT:3> , <NUM_LIT:6> ] , <EOL> [ <NUM_LIT:4> , <NUM_LIT:2> ] ] , <EOL> [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:5> , <NUM_LIT:7> ] ] ] <EOL> player = Player ( payoffs_2opponents ) <EOL> self . g = NormalFormGame ( [ player for i in range ( <NUM_LIT:3> ) ] ) <EOL> def test_getitem ( self ) : <EOL> assert_array_equal ( self . g [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:6> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> def test_is_nash_pure ( self ) : <EOL> ok_ ( self . g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> ok_ ( not self . g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) ) <EOL> def test_is_nash_mixed ( self ) : <EOL> p = ( <NUM_LIT:1> + np . sqrt ( <NUM_LIT> ) ) / <NUM_LIT:16> <EOL> ok_ ( self . g . is_nash ( ( [ <NUM_LIT:1> - p , p ] , [ <NUM_LIT:1> - p , p ] , [ <NUM_LIT:1> - p , p ] ) ) ) <EOL> def test_normalformgame_input_action_sizes ( ) : <EOL> g = NormalFormGame ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> eq_ ( g . N , <NUM_LIT:3> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:0> ] . payoff_array , <EOL> np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:1> ] . payoff_array , <EOL> np . zeros ( ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:2> ) ) <EOL> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:2> ] . payoff_array , <EOL> np . zeros ( ( <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> ) <EOL> def test_normalformgame_setitem ( ) : <EOL> g = NormalFormGame ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> g [ <NUM_LIT:0> , <NUM_LIT:0> ] = ( <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> g [ <NUM_LIT:0> , <NUM_LIT:1> ] = ( <NUM_LIT:0> , <NUM_LIT:10> ) <EOL> g [ <NUM_LIT:1> , <NUM_LIT:0> ] = ( <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> g [ <NUM_LIT:1> , <NUM_LIT:1> ] = ( - <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:0> ] . payoff_array , <EOL> [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:3> , - <NUM_LIT:2> ] ] <EOL> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:1> ] . payoff_array , <EOL> [ [ <NUM_LIT:10> , <NUM_LIT:5> ] , [ <NUM_LIT:10> , <NUM_LIT:0> ] ] <EOL> ) <EOL> def test_normalformgame_constant_payoffs ( ) : <EOL> g = NormalFormGame ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> ok_ ( g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> ok_ ( g . is_nash ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) ) <EOL> ok_ ( g . is_nash ( ( <NUM_LIT:1> , <NUM_LIT:0> ) ) ) <EOL> ok_ ( g . is_nash ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> def test_normalformgame_payoff_profile_array ( ) : <EOL> nums_actions = ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> for N in range ( <NUM_LIT:1> , len ( nums_actions ) + <NUM_LIT:1> ) : <EOL> payoff_arrays = [ <EOL> np . arange ( np . prod ( nums_actions [ <NUM_LIT:0> : N ] ) ) . reshape ( nums_actions [ i : N ] + <EOL> nums_actions [ <NUM_LIT:0> : i ] ) <EOL> for i in range ( N ) <EOL> ] <EOL> players = [ Player ( payoff_array ) for payoff_array in payoff_arrays ] <EOL> g = NormalFormGame ( players ) <EOL> g_new = NormalFormGame ( g . payoff_profile_array ) <EOL> for player_new , payoff_array in zip ( g_new . players , payoff_arrays ) : <EOL> assert_array_equal ( player_new . payoff_array , payoff_array ) <EOL> class TestPlayer_0opponents : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> payoffs = [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> self . player = Player ( payoffs ) <EOL> def test_payoff_vector ( self ) : <EOL> """<STR_LIT>""" <EOL> assert_array_equal ( self . player . payoff_vector ( None ) , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> def test_is_best_response ( self ) : <EOL> """<STR_LIT>""" <EOL> ok_ ( self . player . is_best_response ( <NUM_LIT:1> , None ) ) <EOL> def test_best_response ( self ) : <EOL> """<STR_LIT>""" <EOL> eq_ ( self . player . best_response ( None ) , <NUM_LIT:1> ) <EOL> class TestNormalFormGame_1p : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> data = [ [ <NUM_LIT:0> ] , [ <NUM_LIT:1> ] , [ <NUM_LIT:1> ] ] <EOL> self . g = NormalFormGame ( data ) <EOL> def test_construction ( self ) : <EOL> """<STR_LIT>""" <EOL> ok_ ( self . g . N == <NUM_LIT:1> ) <EOL> assert_array_equal ( self . g . players [ <NUM_LIT:0> ] . payoff_array , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> def test_getitem ( self ) : <EOL> """<STR_LIT>""" <EOL> eq_ ( self . g [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> def test_is_nash_pure ( self ) : <EOL> """<STR_LIT>""" <EOL> ok_ ( self . g . is_nash ( ( <NUM_LIT:1> , ) ) ) <EOL> ok_ ( not self . g . is_nash ( ( <NUM_LIT:0> , ) ) ) <EOL> def test_is_nash_mixed ( self ) : <EOL> """<STR_LIT>""" <EOL> ok_ ( self . g . is_nash ( ( [ <NUM_LIT:0> , <NUM_LIT:1> / <NUM_LIT:2> , <NUM_LIT:1> / <NUM_LIT:2> ] , ) ) ) <EOL> def test_normalformgame_input_action_sizes_1p ( ) : <EOL> g = NormalFormGame ( <NUM_LIT:2> ) <EOL> eq_ ( g . N , <NUM_LIT:1> ) <EOL> assert_array_equal ( <EOL> g . players [ <NUM_LIT:0> ] . payoff_array , <EOL> np . zeros ( <NUM_LIT:2> ) <EOL> ) <EOL> def test_normalformgame_setitem_1p ( ) : <EOL> g = NormalFormGame ( <NUM_LIT:2> ) <EOL> eq_ ( g . N , <NUM_LIT:1> ) <EOL> g [ <NUM_LIT:0> ] = <NUM_LIT:10> <EOL> eq_ ( g . players [ <NUM_LIT:0> ] . payoff_array [ <NUM_LIT:0> ] , <NUM_LIT:10> ) <EOL> def test_player_repr ( ) : <EOL> nums_actions = ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> payoff_arrays = [ <EOL> np . arange ( np . prod ( nums_actions [ <NUM_LIT:0> : i ] ) ) . reshape ( nums_actions [ <NUM_LIT:0> : i ] ) <EOL> for i in range ( <NUM_LIT:1> , len ( nums_actions ) + <NUM_LIT:1> ) <EOL> ] <EOL> players = [ Player ( payoff_array ) for payoff_array in payoff_arrays ] <EOL> for player in players : <EOL> player_new = eval ( repr ( player ) ) <EOL> assert_array_equal ( player_new . payoff_array , player . payoff_array ) <EOL> @ raises ( ValueError ) <EOL> def test_normalformgame_invalid_input_players_shape_inconsistent ( ) : <EOL> p0 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> p1 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> g = NormalFormGame ( [ p0 , p1 ] ) <EOL> @ raises ( ValueError ) <EOL> def test_normalformgame_invalid_input_players_num_inconsistent ( ) : <EOL> p0 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> p1 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ) ) <EOL> g = NormalFormGame ( [ p0 , p1 ] ) <EOL> @ raises ( ValueError ) <EOL> def test_normalformgame_invalid_input_players_dtype_inconsistent ( ) : <EOL> p0 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = int ) ) <EOL> p1 = Player ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = float ) ) <EOL> g = NormalFormGame ( [ p0 , p1 ] ) <EOL> @ raises ( ValueError ) <EOL> def test_normalformgame_invalid_input_nosquare_matrix ( ) : <EOL> g = NormalFormGame ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> @ raises ( ValueError ) <EOL> def test_normalformgame_invalid_input_payoff_profiles ( ) : <EOL> g = NormalFormGame ( np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ) ) ) <EOL> def test_pure2mixed ( ) : <EOL> num_actions = <NUM_LIT:3> <EOL> pure_action = <NUM_LIT:0> <EOL> mixed_action = [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] <EOL> assert_array_equal ( pure2mixed ( num_actions , pure_action ) , mixed_action ) <EOL> def test_best_response_2p ( ) : <EOL> test_case0 = { <EOL> '<STR_LIT>' : np . array ( [ [ <NUM_LIT:4> , <NUM_LIT:0> ] , [ <NUM_LIT:3> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:3> ] ] ) , <EOL> '<STR_LIT>' : <EOL> [ np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> } <EOL> test_case1 = { <EOL> '<STR_LIT>' : np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) ) , <EOL> '<STR_LIT>' : [ np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] ) , np . array ( [ <NUM_LIT:1> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> , <NUM_LIT:1> / <NUM_LIT:3> ] ) ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> } <EOL> for test_case in [ test_case0 , test_case1 ] : <EOL> for mixed_action , br_expected in zip ( test_case [ '<STR_LIT>' ] , <EOL> test_case [ '<STR_LIT>' ] ) : <EOL> br_computed = best_response_2p ( test_case [ '<STR_LIT>' ] , mixed_action ) <EOL> eq_ ( br_computed , br_expected ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import nose <EOL> argv = sys . argv [ : ] <EOL> argv . append ( '<STR_LIT>' ) <EOL> argv . append ( '<STR_LIT>' ) <EOL> nose . main ( argv = argv , defaultTest = __file__ ) </s>
