def test_allclose_perm(_): """Test allclose_perm accurately detected permutation""" one, two = np.random.random((2, 10, 4)) three = one.copy() np.random.shuffle(three) assert utils.allclose_perm(one, three) assert not utils.allclose_perm(one, two)
def test_canon(): """Test basic canon game""" profs = [[2, 0, 0, 3], [1, 1, 0, 3]] pays = [[1, 0, 0, 1], [2, 3, 0, np.nan]] game = paygame.game([2, 3], [3, 1], profs, pays) cgame = canongame.canon(game) assert cgame.num_profiles == 2 assert cgame.num_complete_profiles == 1 pay = cgame.get_payoffs([2, 0, 0]) assert np.allclose(pay, [1, 0, 0]) expected = [[2, 0, 0], [1, 1, 0]] assert np.all(cgame.profiles() == expected) expected = [[1, 0, 0], [2, 3, 0]] assert np.allclose(cgame.payoffs(), expected) assert np.allclose(cgame.deviation_payoffs([1, 0, 0]), [1, 3, np.nan], equal_nan=True) dev, jac = cgame.deviation_payoffs([0.5, 0.5, 0], jacobian=True) assert dev.shape == (3,) assert jac.shape == (3, 3) assert np.allclose(cgame.min_strat_payoffs(), [1, 3, np.nan], equal_nan=True) assert np.allclose(cgame.max_strat_payoffs(), [2, 3, np.nan], equal_nan=True) ngame = cgame.normalize() expected = [[0, 0, 0], [0.5, 1, 0]] assert utils.allclose_perm(ngame.payoffs(), expected) rgame = cgame.restrict([True, True, False]) expected = [[1, 0], [2, 3]] assert utils.allclose_perm(rgame.payoffs(), expected) copy_str = json.dumps(cgame.to_json()) copy = canongame.canon_json(json.loads(copy_str)) assert hash(cgame) == hash(copy) assert cgame == copy assert [2, 0, 0] in cgame assert [0, 2, 0] not in cgame assert repr(cgame) == 'CanonGame([2], [3], 2 / 6)' other = canongame.canon(gamegen.normal_aggfn([2, 2, 3], [3, 1, 1], 2)) assert other + cgame == cgame + other
def test_canon(): """Test basic canon game""" profs = [[2, 0, 0, 3], [1, 1, 0, 3]] pays = [[1, 0, 0, 1], [2, 3, 0, np.nan]] game = paygame.game([2, 3], [3, 1], profs, pays) cgame = canongame.canon(game) assert cgame.num_profiles == 2 assert cgame.num_complete_profiles == 1 pay = cgame.get_payoffs([2, 0, 0]) assert np.allclose(pay, [1, 0, 0]) expected = [[2, 0, 0], [1, 1, 0]] assert np.all(cgame.profiles() == expected) expected = [[1, 0, 0], [2, 3, 0]] assert np.allclose(cgame.payoffs(), expected) assert np.allclose(cgame.deviation_payoffs([1, 0, 0]), [1, 3, np.nan], equal_nan=True) dev, jac = cgame.deviation_payoffs([0.5, 0.5, 0], jacobian=True) assert dev.shape == (3, ) assert jac.shape == (3, 3) assert np.allclose(cgame.min_strat_payoffs(), [1, 3, np.nan], equal_nan=True) assert np.allclose(cgame.max_strat_payoffs(), [2, 3, np.nan], equal_nan=True) ngame = cgame.normalize() expected = [[0, 0, 0], [0.5, 1, 0]] assert utils.allclose_perm(ngame.payoffs(), expected) rgame = cgame.restrict([True, True, False]) expected = [[1, 0], [2, 3]] assert utils.allclose_perm(rgame.payoffs(), expected) copy_str = json.dumps(cgame.to_json()) copy = canongame.canon_json(json.loads(copy_str)) assert hash(cgame) == hash(copy) assert cgame == copy assert [2, 0, 0] in cgame assert [0, 2, 0] not in cgame assert repr(cgame) == 'CanonGame([2], [3], 2 / 6)' other = canongame.canon(gamegen.normal_aggfn([2, 2, 3], [3, 1, 1], 2)) assert other + cgame == cgame + other
async def test_innerloop_game(base): """Test that inner loop works for a game""" game = gamegen.samplegame_replace(base) sched = gamesched.gamesched(game) eqas = await innerloop.inner_loop(schedgame.schedgame(sched)) verify_dist_thresh(eqas) eqag = await innerloop.inner_loop(asyncgame.wrap(game)) assert utils.allclose_perm(eqas, eqag, atol=1e-3, rtol=1e3)
def __eq__(self, othr): return (super().__eq__(othr) and self.num_functions == othr.num_functions and np.allclose(self.offsets, othr.offsets) and utils.allclose_perm( np.concatenate([ self.action_weights, self.function_inputs.T, self.function_table.reshape(self.num_functions, -1) ], 1), np.concatenate([ othr.action_weights, othr.function_inputs.T, othr.function_table.reshape(othr.num_functions, -1) ], 1)))
def __eq__(self, othr): # pylint: disable-msg=protected-access return ( super().__eq__(othr) and np.allclose(self._offset, othr._offset) and np.allclose(self._coefs, othr._coefs) and np.allclose(self._lengths, othr._lengths) and np.all(self._sizes == othr._sizes) and utils.allclose_perm( np.concatenate([ np.arange(self.num_strats).repeat(self._sizes)[:, None], self._profiles, self._alpha[:, None] ], 1), np.concatenate([ np.arange(othr.num_strats).repeat(othr._sizes)[:, None], othr._profiles, othr._alpha[:, None] ], 1)))
def __eq__(self, othr): # pylint: disable-msg=protected-access return (super().__eq__(othr) and np.allclose(self._offset, othr._offset) and np.allclose(self._coefs, othr._coefs) and np.allclose(self._lengths, othr._lengths) and np.all(self._sizes == othr._sizes) and utils.allclose_perm( np.concatenate([ np.arange(self.num_strats).repeat( self._sizes)[:, None], self._profiles, self._alpha[:, None]], 1), np.concatenate([ np.arange(othr.num_strats).repeat( othr._sizes)[:, None], othr._profiles, othr._alpha[:, None]], 1)))
def test_replicator_dynamics_failure(): """Test that it fails on divergent games""" game = gamegen.rock_paper_scissors() eqm = nash.replicator_dynamics(game, [0.6, 0.3, 0.1]) assert utils.allclose_perm(eqm, [1, 0, 0])