示例#1
0
def to_NFG(game):
	if isinstance(game, list):
		return map(to_NFG, game)
	if is_symmetric(game):
		return to_NFG_sym(game)
	if is_asymmetric(game):
		return to_NFG_asym(game)
	return to_NFG_rsym(game)
示例#2
0
def deviation_preserving_reduction(game, players={}):
	if not players:
		return twins_reduction(game)

	#it's often faster to go through all of the full-game profiles
	DPR_game = type(game)(game.roles, players, game.strategies)
	if len(game) <= DPR_game.size and is_symmetric(game):
		reduced_profile_data = {}
		divisible = True
		for full_prof in game.knownProfiles():
			try:
				rp, role, strat = dpr_profile(full_prof, players, game.players)
			except NotImplementedError:
				divisible = False
				break
			if rp not in reduced_profile_data:
				reduced_profile_data[rp] = {r:[] for r in game.roles}
			count = rp[role][strat]
			value = game.getPayoffData(full_prof, role, strat)
			reduced_profile_data[rp][role].append(PayoffData(strat,count,value))
		if divisible:
			for prof_data in reduced_profile_data.values():
				valid_profile = True
				for r,l in prof_data.items():
					if sum([d.count for d in l]) != players[r]:
						valid_profile = False
						break
				if valid_profile:
					DPR_game.addProfile(prof_data)
			return DPR_game
	
	#it's conceptually simpler to go through all of the reduced-game profiles
	DPR_game = type(game)(game.roles, players, game.strategies)
	for reduced_profile in DPR_game.allProfiles():
		try:
			role_payoffs = {}
			for role in game.roles:
				role_payoffs[role] = []
				for s in reduced_profile[role]:
					full_profile = {}
					for r in game.roles:
						if r == role:
							opp_prof = reduced_profile.asDict()[r]
							opp_prof[s] -= 1
							full_profile[r] = full_game_profile(opp_prof, \
									game.players[r] - 1)
							full_profile[r][s] += 1
						else:
							full_profile[r] = full_game_profile( \
									reduced_profile[r], game.players[r])
					role_payoffs[r].append(PayoffData(s,reduced_profile[r][s],\
							game.getPayoffData(Profile(full_profile), r, s)))
			DPR_game.addProfile(role_payoffs)
		except KeyError:
			continue
	return DPR_game
示例#3
0
def deviation_preserving_reduction(game, players={}):
	if not players:
		return twins_reduction(game)
	elif len(game.roles) == 1 and isinstance(players, int):
		players = {game.roles[0]:players}
	elif isinstance(players, list):
		players = dict(zip(game.roles, players))

	#it's often faster to go through all of the full-game profiles
	DPR_game = type(game)(game.roles, players, game.strategies)
	if len(game) <= DPR_game.size and is_symmetric(game):
		DPR_prof_data = {}
		divisible = True
		for full_prof in game.knownProfiles():
			try:
				rp, role, strat = dpr_profile(full_prof, players, game.players)
			except NotImplementedError:
				divisible = False
				break
			if rp not in DPR_prof_data:
				DPR_prof_data[rp] = {r:[] for r in game.roles}
			count = rp[role][strat]
			value = game.getPayoffData(full_prof, role, strat)
			DPR_prof_data[rp][role].append(PayoffData(strat,count,value))
		if divisible:
			for prof_data in DPR_prof_data.values():
				valid_profile = True
				for r,l in prof_data.items():
					if sum([d.count for d in l]) != players[r]:
						valid_profile = False
						break
				if valid_profile:
					DPR_game.addProfile(prof_data)
			return DPR_game

	#it's conceptually simpler to go through all of the reduced-game profiles
	for DPR_prof in DPR_game.allProfiles():
		try:
			role_payoffs = {}
			for r in game.roles:
				role_payoffs[r] = []
				for s in DPR_prof[r]:
					full_prof = full_prof_DPR(DPR_prof, r, s, game.players)
					role_payoffs[r].append(PayoffData(s,DPR_prof[r][s],\
							game.getPayoffData(full_prof, r, s)))
			DPR_game.addProfile(role_payoffs)
		except KeyError:
			continue
	return DPR_game