示例#1
0
	def test_seed_different(self):
		s = State.generate()
		s1 = State.generate()
		if s.get_deck().get_card_states() == s1.get_deck().get_card_states():
			raise RuntimeError("The decks are shuffled in the same way.")
			print s.get_deck().get_card_states()
			print s1.get_deck().get_card_states()
示例#2
0
 def heuristic_1a(self, player: int, depth: int,
                  curr_state: State) -> float:
     if (curr_state.get_points(util.other(player)) +
             curr_state.get_points(player)) == 0:
         return 0
     return curr_state.get_points(
         util.other(player)) / (curr_state.get_points(util.other(player)) +
                                curr_state.get_points(player))
示例#3
0
	def test_seed_same(self):
		for i in range(1,1000):
			id = i
			s = State.generate(id)
			s1 = State.generate(id)
			if s.get_deck().get_card_states() != s1.get_deck().get_card_states() or s.whose_turn() != s.whose_turn():
				raise RuntimeError("The decks are not shuffled in the same way.")
				print s.get_deck().get_card_states()
				print s1.get_deck().get_card_states()
示例#4
0
 def heuristic_3c(self, player: int, depth: int,
                  curr_state: State) -> float:
     trumprange = range(15, 20)
     trumpamount = 0
     if curr_state.get_trump_suit == "C":
         trumprange = range(0, 5)
     elif curr_state.get_trump_suit == "D":
         trumprange = range(5, 10)
     elif curr_state.get_trump_suit == 'H':
         trumprange = range(10, 15)
     for move in curr_state.moves():
         if move[0] in trumprange:
             trumpamount += 1
     return trumpamount / len(curr_state.moves())
示例#5
0
    def test_trump_jack_non_leading(self):
        state = State.generate(6)
        me = state.whose_turn()
        s1 = state.clone(signature=me)
        trump_suit = state.get_trump_suit()

        jacks = [
            move for move in s1.moves()
            if (move[0] == 4 or move[0] == 9 or move[0] == 14 or move[0] == 19)
        ]
        trump_jacks = [
            move for move in jacks if util.get_suit(move[0]) == trump_suit
        ]

        self.assertEqual(len(s1.moves()), 5 + len(trump_jacks))

        state = state.next(random.choice(state.moves()))
        s1 = state.clone(me)

        jacks = [
            move for move in state.moves()
            if (move[0] == 4 or move[0] == 9 or move[0] == 14 or move[0] == 19)
        ]
        trump_jacks = [
            move for move in jacks if util.get_suit(move[0]) == trump_suit
        ]

        self.assertGreater(len(trump_jacks), 0)
        self.assertEqual(len(state.moves()), 5)
示例#6
0
def create_dataset(path, player=ml_ml_rdeep.Bot(), games=2000, phase=1):

    data = []
    target = []

    # For progress bar
    bar_length = 30
    start = time.time()

    for g in range(games - 1):

        # For progress bar
        if g % 10 == 0:
            percent = 100.0 * g / games
            sys.stdout.write('\r')
            sys.stdout.write("Generating dataset: [{:{}}] {:>3}%".format(
                '=' * int(percent / (100.0 / bar_length)), bar_length,
                int(percent)))
            sys.stdout.flush()

        # Randomly generate a state object starting in specified phase.
        state = State.generate(phase=phase)

        state_vectors = []

        while not state.finished():

            # Give the state a signature if in phase 1, obscuring information that a player shouldn't see.
            given_state = state.clone(signature=state.whose_turn()
                                      ) if state.get_phase() == 1 else state

            # Add the features representation of a state to the state_vectors array
            state_vectors.append(features(given_state))

            # Advance to the next state
            move = player.get_move(given_state)
            state = state.next(move)

        winner, score = state.winner()

        for state_vector in state_vectors:
            data.append(state_vector)

            if winner == 1:
                result = 'won'

            elif winner == 2:
                result = 'lost'

            target.append(result)

    with open(path, 'wb') as output:
        pickle.dump((data, target), output, pickle.HIGHEST_PROTOCOL)

    # For printing newline after progress bar
    print(
        "\nDone. Time to generate dataset: {:.2f} seconds".format(time.time() -
                                                                  start))

    return data, target
示例#7
0
	def test_game10(self):
		state = State.generate(0)

		for i in range(10):
			if not state.finished():
				moves = state.moves()
				state = state.next(moves[0])
示例#8
0
	def test_game15(self):
		state = State.generate(0)

		for i in range(15):
			if not state.finished():
				# print state
				moves = state.moves()
				state = state.next(moves[0])
示例#9
0
def run_tournament(options):

    botnames = options.players.split(",")

    bots = []
    for botname in botnames:
        bots.append(util.load_player(botname))

    n = len(bots)
    wins = [0] * len(bots)
    matches = [(p1, p2) for p1 in range(n) for p2 in range(n) if p1 < p2]

    totalgames = (n * n - n) / 2 * options.repeats
    playedgames = 0
    wins_player1 = 0
    wins_player2 = 0
    seed = 7453876

    print('Playing {} games:'.format(int(totalgames)))
    for a, b in matches:
        for r in range(options.repeats):

            if random.choice([True, False]):
                p = [a, b]
            else:
                p = [b, a]

            # Generate a state with a random seed
            state = State.generate(id=seed, phase=int(options.phase))

            winner, score = engine.play(bots[p[0]],
                                        bots[p[1]],
                                        state,
                                        options.max_time * 1000,
                                        verbose=options.verbose,
                                        fast=options.fast)

            if winner is not None:
                winner = p[winner - 1]
                wins[winner] += score

            if winner == 0:
                wins_player1 += 1
            elif winner == 1:
                wins_player2 += 1

            seed += 1
            playedgames += 1
            print('Played {} out of {:.0f} games ({:.0f}%): {} \r'.format(
                playedgames, totalgames, playedgames / float(totalgames) * 100,
                wins))

    print('Results:')
    for i in range(len(bots)):
        print('    bot {}: {} points'.format(bots[i], wins[i]))

    print('Player 1 won %d games. Player 2 won %d games.' %
          (wins_player1, wins_player2))
def execute(params):
    ids, (player1, player2), (map_size, seed) = params
    start, _ = State.generate(map_size, seed, symmetric=not args.asym)

    winner = engine.play(player1[1], player2[1], start, verbose=(args.verbose > 2), outfile=None,
                         max_time=args.max_time * 1000, max_turns=args.max_turns)

    if winner is not None:
        winner = (player1[0], player2[0])[winner-1]
    return ids, winner, (player1[0], player2[0]), (map_size, seed)
示例#11
0
	def test_mariage_visible(self):
		# for i in range(1,200):
		# 	s = State.generate(i)
		# 	moves = s.moves()
		# 	# if
		# 	print moves

		s = State.generate(2)
		moves = s.moves()
		# if
		print moves
示例#12
0
    def kb_consistent_trumpmarriage(self, state, move):
        # type: (State, move) -> bool

        # each time we check for consistency we initialise a new knowledge-base
        kb = KB()

        # Add general information about the game

        suit = State.get_trump_suit(state)

        if suit == "C":
            card1 = 2
            card2 = 3
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "D":
            card1 = 7
            card2 = 8
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "H":
            card1 = 12
            card2 = 13
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)
        elif suit == "S":
            card1 = 17
            card2 = 18
            variable_string = "m" + str(card1) + str(card2)
            strategy_variable = Boolean(variable_string)
            kb.add_clause(strategy_variable)

        load.general_information(kb)

        # Add the necessary knowledge about the strategy
        load.strategy_knowledge(kb)

        # This line stores the index of the card in the deck.
        # If this doesn't make sense, refer to _deck.py for the card index mapping
        index = move[0]

        variable_string = "pm" + str(index)
        strategy_variable = Boolean(variable_string)

        # Add the relevant clause to the loaded knowledge base
        kb.add_clause(~strategy_variable)

        # If the knowledge base is not satisfiable, the strategy variable is
        # entailed (proof by refutation)

        return kb.satisfiable()
示例#13
0
 def test_few_moves(self):
     state = State.generate(50)
     self.assertEqual(state.get_perspective(), [
         'P1H', 'P1H', 'S', 'S', 'P2H', 'P2H', 'S', 'P2H', 'P1H', 'P2H',
         'S', 'P2H', 'S', 'S', 'P1H', 'S', 'P1H', 'S', 'S', 'S'
     ])
     s1 = state.clone(1)
     self.assertEqual(s1.get_perspective(), [
         'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'P1H', 'U', 'S', 'U',
         'U', 'U', 'P1H', 'U', 'P1H', 'U', 'U', 'U'
     ])
示例#14
0
def run_tournament(options):

    botnames = options.players.split(",")

    bots = []
    for botname in botnames:
        bots.append(util.load_player(botname))

    n = len(bots)
    wins = [0] * len(bots)
    ranks = [0] * len(bots)

    matches = [(p1, p2) for p1 in range(n) for p2 in range(n) if p1 < p2]

    totalgames = (n * n - n) / 2 * options.repeats
    playedgames = 0
    totalpoints = 0

    print('Playing {} games:'.format(int(totalgames)))
    for a, b in matches:
        for r in range(options.repeats):

            p = [a, b] if random.choice([True, False]) else [
                b, a
            ]  # randomly chooses who starting_state applies to
            #p = [a, b]  # starting state applied to second player
            #p = [b, a]  # starting state applied to first player

            # add starting_state argument here, most of them apply to second player
            starting_state = None  # None, one_marriage, two marriage, all_jacks, all_aces, same_suit, all_ace_jack
            state = State.generate(phase=int(options.phase),
                                   starting_state=starting_state)

            winner, score = engine.play(bots[p[0]],
                                        bots[p[1]],
                                        state,
                                        options.max_time * 1000,
                                        verbose=False,
                                        fast=options.fast)

            if winner is not None:
                winner = p[winner - 1]
                wins[winner] += score
                ranks[winner] += 1
                totalpoints += score

            playedgames += 1
            #print('Played {} out of {:.0f} games ({:.0f}%): {} \r'.format(playedgames, totalgames, playedgames/float(totalgames) * 100, wins))

    for i in range(len(bots)):
        ranks[i] = wins[i] / (totalpoints)
        #ranks[i] = ranks[i]/(totalgames) # i think points are more important than just games won
        print('    bot {}: {} points, {:04.2f} rank'.format(
            bots[i], wins[i], ranks[i]))
示例#15
0
	def test_exchange_move(self):
		s = State.generate(11)
		moves = s.moves()
		s1 = s.next(moves[5])

		if s.moves() == s1.moves():
			raise RuntimeError("The available moves should have changed.")
		if s.whose_turn() is not s1.whose_turn():
			raise RuntimeError("The turns shifted. This should not be the case.")
		if len(s.moves()) <= len(s1.moves()):
			raise RuntimeError("The number of available moves should have decreased.")
示例#16
0
 def test_marriage_deterministic(self):
     state = State.generate(38)
     self.assertEqual(state.get_deck().get_card_states(), [
         'S', 'P2H', 'P1H', 'S', 'P1H', 'P1H', 'S', 'S', 'P2H', 'S', 'S',
         'P2H', 'P2H', 'P2H', 'S', 'S', 'S', 'S', 'P1H', 'P1H'
     ])
     self.assertEqual(state.get_perspective(1), [
         'U', 'U', 'P1H', 'U', 'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U',
         'U', 'U', 'S', 'U', 'U', 'U', 'P1H', 'P1H'
     ])
     self.assertEqual(state.get_perspective(2), [
         'U', 'P2H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2H', 'U', 'U', 'P2H',
         'P2H', 'P2H', 'S', 'U', 'U', 'U', 'U', 'U'
     ])
示例#17
0
	def test_trump_jack_non_leading(self):
		state = State.generate(6)
		trump_suit = state.get_trump_suit()

		jacks = [move for move in state.moves() if (move[0] == 4 or move[0] == 9 or move[0] == 14 or move[0] == 19)]
		trump_jacks = [move for move in jacks if util.get_suit(move[0]) == trump_suit]

		self.assertEqual(len(state.moves()), 5 + len(trump_jacks))
		self.assertEqual(len(jacks), 0)


		state = state.next(random.choice(state.moves()))

		jacks = [move for move in state.moves() if (move[0] == 4 or move[0] == 9 or move[0] == 14 or move[0] == 19)]
		trump_jacks = [move for move in jacks if util.get_suit(move[0]) == trump_suit]
示例#18
0
	def test_game_full(self):
		wins = 0
		for i in range(10000):
			state = State.generate()
			while not state.finished():
				moves = state.moves()
				# print state.get_deck().get_card_states()
				# print "p1 score: {}".format(state.get_points(1))
				# print "p2 score: {}".format(state.get_points(2))
				# print moves
				state = state.next(moves[0])

			winner, points = state.winner()
			if winner == 1:
				wins +=1
		print wins
示例#19
0
def execute(params):
    ids, bot, (map_size, seed, max_turns, asym) = params
    state, _ = State.generate(map_size, seed, symmetric=not asym)

    state_vectors = []
    i = 0
    while not state.finished() and i <= max_turns:
        state_vectors.append(features(state))
        move = bot.get_move(state)
        state = state.next(move)

        i += 1

    winner = state.winner()

    return ids, winner, state_vectors, (map_size, seed)
def run_tournament(options):

    botnames = options.players.split(",")

    bots = []
    for botname in botnames:
        bots.append(util.load_player(botname))

    n = len(bots)
    wins = [0] * len(bots)
    points = [0] * len(bots)
    matches = [(p1, p2) for p1 in range(n) for p2 in range(n) if p1 < p2]

    totalgames = (n * n - n) / 2 * options.repeats
    playedgames = 0

    print('Playing {} games:'.format(totalgames))
    for a, b in matches:
        for r in range(options.repeats):

            if random.choice([True, False]):
                p = [a, b]
            else:
                p = [b, a]

            # Generate a state with a random seed
            start = State.generate(phase=int(options.phase))

            winner = engine.play(bots[p[0]], bots[p[1]], start, verbose=False)

            #TODO: ALSO IMPLEMENT POINTS FOR WINNING
            if winner is not None:
                _, temp_points = winner
                winner = p[winner[0] - 1]
                wins[winner] += 1
                points[winner] += temp_points

            playedgames += 1
            print('Played {} out of {:.0f} games ({:.0f}%): {} \r'.format(
                playedgames, totalgames, playedgames / float(totalgames) * 100,
                wins))

    print('Results:')
    for i in range(len(bots)):
        print('    bot {}: {} wins, {} points'.format(bots[i], wins[i],
                                                      points[i]))
示例#21
0
 def heuristic_3a(self, player: int, depth: int,
                  curr_state: State) -> float:
     trumprange = range(15, 20)
     trumpamount = 0
     handstrength = 0
     if curr_state.get_trump_suit == "C":
         trumprange = range(0, 5)
     elif curr_state.get_trump_suit == "D":
         trumprange = range(5, 10)
     elif curr_state.get_trump_suit == 'H':
         trumprange = range(10, 15)
     for move in curr_state.moves():
         handstrength = 3 - ((move[0] % 5) - 2)
         if move[0] in trumprange:
             trumpamount += 1
     handstrength += 5 * trumpamount
     return handstrength
示例#22
0
	def test_clone(self):
		deck = Deck.generate(0)
		state = State(deck,True)
		clone = state.clone()

		self.assertEqual(state.finished(), clone.finished())

		self.assertEqual(state.revoked(), clone.revoked())

		self.assertEqual(state.winner(), clone.winner())

		current_deck = state.get_deck()
		clone_deck = clone.get_deck()
		self.assertEqual(current_deck.get_card_states(), clone_deck.get_card_states())


		pass
示例#23
0
    def heuristic_2a(self, player: int, depth: int,
                     curr_state: State) -> float:
        MAX_POSSIBLE_POTENTIAL_POINTS = 11
        potential_points = 0
        played_card = curr_state.get_opponents_played_card()
        if played_card is not None:
            played_card = util.get_rank(played_card)
            if played_card == 'J':
                potential_points -= 2
            elif played_card == 'Q':
                potential_points -= 3
            elif played_card == 'K':
                potential_points -= 4
            elif played_card == '10':
                potential_points -= 10
            elif played_card == 'A':
                potential_points -= 11

        return potential_points / MAX_POSSIBLE_POTENTIAL_POINTS
示例#24
0
	def test_marriage_deterministic(self):
		state = State.generate(38)
		self.assertEqual(state.get_deck().get_card_states(), ['S', 'P2H', 'P1H', 'S', 'P1H', 'P1H', 'S', 'S', 'P2H', 'S', 'S', 'P2H', 'P2H', 'P2H', 'S', 'S', 'S', 'S', 'P1H', 'P1H'])
		self.assertEqual(state.get_perspective(1), ['U', 'U', 'P1H', 'U', 'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'S', 'U', 'U', 'U', 'P1H', 'P1H'])
		self.assertEqual(state.get_perspective(2), ['U', 'P2H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2H', 'U', 'U', 'P2H', 'P2H', 'P2H', 'S', 'U', 'U', 'U', 'U', 'U'])

		state = state.next((12, 13))
		self.assertEqual(state.get_deck().get_card_states(), ['S', 'P2H', 'P1H', 'S', 'P1H', 'P1H', 'S', 'S', 'P2H', 'S', 'S', 'P2H', 'P2H', 'P2H', 'S', 'S', 'S', 'S', 'P1H', 'P1H'])
		self.assertEqual(state.get_perspective(1), ['U', 'U', 'P1H', 'U', 'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2H', 'P2H', 'S', 'U', 'U', 'U', 'P1H', 'P1H'])
		self.assertEqual(state.get_perspective(2), ['U', 'P2H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2H', 'U', 'U', 'P2H', 'P2H', 'P2H', 'S', 'U', 'U', 'U', 'U', 'U'])

		move = random.choice(state.moves())
		index = move[0]

		scores = [11, 10, 4, 3, 2]
		score = 40 + scores[12%5] + scores[index%5]

		st = state.get_deck().get_stock()


		state = state.next(move)

		card_states = ['S', 'P2H', 'P1H', 'S', 'P1H', 'P1H', 'S', 'S', 'P2H', 'S', 'S', 'P2H', 'P2W', 'P2H', 'S', 'S', 'S', 'S', 'P1H', 'P1H']
		st1 = st.pop()
		st2 = st.pop()
		card_states[index] = "P2W"
		card_states[st1] = "P2H"
		card_states[st2] = "P1H"

		p1 = ['U', 'U', 'P1H', 'U', 'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2W', 'P2H', 'S', 'U', 'U', 'U', 'P1H', 'P1H']
		p1[st2] = "P1H"
		p1[index] = "P2W"

		p2 = ['U', 'P2H', 'U', 'U', 'U', 'U', 'U', 'U', 'P2H', 'U', 'U', 'P2H', 'P2W', 'P2H', 'S', 'U', 'U', 'U', 'U', 'U']
		p2[index] = "P2W"
		p2[st1] = "P2H"

		self.assertEqual(state.get_deck().get_card_states(), card_states)
		self.assertEqual(state.get_perspective(1), p1)
		self.assertEqual(state.get_perspective(2), p2)

		self.assertEqual(state.get_points(1), 0)
		self.assertEqual(state.get_points(2), score)
示例#25
0
    def test_trump_jack_exchange_deterministic(self):
        state = State.generate(50)
        self.assertEqual(state.get_deck().get_card_states(), [
            'P1H', 'P1H', 'S', 'S', 'P2H', 'P2H', 'S', 'P2H', 'P1H', 'P2H',
            'S', 'P2H', 'S', 'S', 'P1H', 'S', 'P1H', 'S', 'S', 'S'
        ])
        self.assertEqual(state.get_perspective(1), [
            'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'P1H', 'U', 'S', 'U',
            'U', 'U', 'P1H', 'U', 'P1H', 'U', 'U', 'U'
        ])
        self.assertEqual(state.get_perspective(2), [
            'U', 'U', 'U', 'U', 'P2H', 'P2H', 'U', 'P2H', 'U', 'P2H', 'S',
            'P2H', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U'
        ])

        #Test trump jack exchange
        #Maybe needs to change if order of moves is altered
        self.assertEqual(state.whose_turn(), 1)
        self.assertEqual(state.get_points(1), 0)
        self.assertEqual(state.get_points(2), 0)
        self.assertEqual(state.get_pending_points(1), 0)
        self.assertEqual(state.get_pending_points(2), 0)

        state = state.next(state.moves().pop())
        self.assertEqual(state.whose_turn(), 1)
        self.assertEqual(state.get_deck().get_card_states(), [
            'P1H', 'P1H', 'S', 'S', 'P2H', 'P2H', 'S', 'P2H', 'P1H', 'P2H',
            'P1H', 'P2H', 'S', 'S', 'S', 'S', 'P1H', 'S', 'S', 'S'
        ])
        self.assertEqual(state.get_perspective(1), [
            'P1H', 'P1H', 'U', 'U', 'U', 'U', 'U', 'U', 'P1H', 'U', 'P1H', 'U',
            'U', 'U', 'S', 'U', 'P1H', 'U', 'U', 'U'
        ])
        self.assertEqual(state.get_perspective(2), [
            'U', 'U', 'U', 'U', 'P2H', 'P2H', 'U', 'P2H', 'U', 'P2H', 'P1H',
            'P2H', 'U', 'U', 'S', 'U', 'U', 'U', 'U', 'U'
        ])

        self.assertEqual(state.get_points(1), 0)
        self.assertEqual(state.get_points(2), 0)
        self.assertEqual(state.get_pending_points(1), 0)
        self.assertEqual(state.get_pending_points(2), 0)
def train_bot(player):

    for g in range(GAMES):

        # Randomly generate a state object starting in specified phase.
        state = State.generate(phase=PHASE)

        state_vectors = []

        while not state.finished():

            # Give the state a signature if in phase 1, obscuring information that a player shouldn't see.
            given_state = state.clone(signature=state.whose_turn()
                                      ) if state.get_phase() == 1 else state

            # Add the features representation of a state to the state_vectors array
            state_vectors.append(features(given_state))

            # Advance to the next state
            move = player.get_move(given_state)
            state = state.next(move)

        winner, score = state.winner()

        for state_vector in state_vectors:
            data.append(state_vector)

            if winner == 1:
                result = 'won'

            elif winner == 2:
                result = 'lost'

            target.append(result)

        sys.stdout.write(".")
        sys.stdout.flush()
        if g % (GAMES / 10) == 0:
            print("")
            print('game {} finished ({}%)'.format(g, (g / float(GAMES) * 100)))
示例#27
0
def call_engine(options):

    # Create player 1
    player1 = util.load_player(options.player1)

    # Create player 2
    player2 = util.load_player(options.player2)

    # Generate or load the map
    state, id = State.generate(int(options.num_planets), symmetric=not options.asym)

    if not options.quiet:
        print('-- Using map with id {} '.format(id))
        print('   Start state: ' + str(state))

    # Play the game
    viz = (options.outputfile.lower() == 'none')
    outfile = options.outputfile # type: str
    if not outfile.endswith('.pdf'):
        outfile += '.pdf'

    engine.play(player1, player2, state=state, max_time=options.max_time*1000, verbose=(not options.quiet), outfile=outfile)
def call_engine(options):

    # Create player 1
    player1 = util.load_player(options.player1)

    # Create player 2
    player2 = util.load_player(options.player2)

    # Generate or load the map
    state = State.generate(phase=int(options.phase))

    if not options.quiet:
        # print('-- Using map with id {} '.format(id))
        print('   Start state: ' + str(state))

    # Play the game

    engine.play(player1,
                player2,
                state=state,
                max_time=options.max_time * 1000,
                verbose=(not options.quiet))
示例#29
0
def run_tournament(options):

    botnames = options.players.split(",")

    bots = []
    for botname in botnames:
        bots.append(util.load_player(botname))

    n = len(bots)
    wins = [0] * len(bots)
    matches = [(p1, p2) for p1 in range(n) for p2 in range(n) if p1 < p2]

    totalgames = (n*n - n)/2 * options.repeats
    playedgames = 0

    print('Playing {} games:'.format(totalgames))
    for a, b in matches:
        for r in range(options.repeats):

            if random.choice([True, False]):
                p = [a, b]
            else:
                p = [b, a]

            start, _ = State.generate(int(options.num_planets), symmetric=not options.asym)

            winner = engine.play(bots[p[0]], bots[p[1]], start, verbose=False, outfile=None)

            if winner is not None:
                winner = p[winner - 1]
                wins[winner] += 1

            playedgames += 1
            print('Played {} out of {:.0f} games ({:.0f}%): {} \r'.format(playedgames, totalgames, playedgames/float(totalgames) * 100, wins))

    print('Results:')
    for i in range(len(bots)):
        print('    bot {}: {} wins'.format(bots[i], wins[i]))
示例#30
0
def features(state):
    # type: (State) -> tuple[float, ...]
    """
    Extract features from this state. Remember that every feature vector returned should have the same length.

    :param state: A state to be converted to a feature vector
    :return: A tuple of floats: a feature vector representing this state.
    """

    feature_set = []

    # Add player 1's points to feature set
    p1_points = State.get_points(state, 1)

    # Add player 2's points to feature set
    p2_points = State.get_points(state, 2)

    # Add player 1's pending points to feature set
    p1_pending_points = State.get_pending_points(state, 1)

    # Add plauer 2's pending points to feature set
    p2_pending_points = State.get_pending_points(state, 2)

    # Get trump suit
    trump_suit = State.get_trump_suit(state)

    # Add phase to feature set
    phase = State.get_phase(state)

    # Add stock size to feature set
    stock_size = State.get_stock_size(state)

    # Add leader to feature set
    leader = State.leader(state)

    # Add whose turn it is to feature set
    whose_turn = State.whose_turn(state)

    # Add opponent's played card to feature set
    opponents_played_card = State.get_opponents_played_card(state)

    ################## You do not need to do anything below this line ########################

    perspective = state.get_perspective()

    # Perform one-hot encoding on the perspective.
    # Learn more about one-hot here: https://machinelearningmastery.com/how-to-one-hot-encode-sequence-data-in-python/
    perspective = [
        card if card != 'U' else [1, 0, 0, 0, 0, 0] for card in perspective
    ]
    perspective = [
        card if card != 'S' else [0, 1, 0, 0, 0, 0] for card in perspective
    ]
    perspective = [
        card if card != 'P1H' else [0, 0, 1, 0, 0, 0] for card in perspective
    ]
    perspective = [
        card if card != 'P2H' else [0, 0, 0, 1, 0, 0] for card in perspective
    ]
    perspective = [
        card if card != 'P1W' else [0, 0, 0, 0, 1, 0] for card in perspective
    ]
    perspective = [
        card if card != 'P2W' else [0, 0, 0, 0, 0, 1] for card in perspective
    ]

    # Append one-hot encoded perspective to feature_set
    feature_set += list(chain(*perspective))

    # Append normalized points to feature_set
    total_points = p1_points + p2_points
    feature_set.append(p1_points / total_points if total_points > 0 else 0.)
    feature_set.append(p2_points / total_points if total_points > 0 else 0.)

    # Append normalized pending points to feature_set
    total_pending_points = p1_pending_points + p2_pending_points
    feature_set.append(
        p1_pending_points /
        total_pending_points if total_pending_points > 0 else 0.)
    feature_set.append(
        p2_pending_points /
        total_pending_points if total_pending_points > 0 else 0.)

    # Convert trump suit to id and add to feature set
    # You don't need to add anything to this part
    suits = ["C", "D", "H", "S"]
    trump_suit_onehot = [0, 0, 0, 0]
    trump_suit_onehot[suits.index(trump_suit)] = 1
    feature_set += trump_suit_onehot

    # Append one-hot encoded phase to feature set
    feature_set += [1, 0] if phase == 1 else [0, 1]

    # Append normalized stock size to feature set
    feature_set.append(stock_size / 10)

    # Append one-hot encoded leader to feature set
    feature_set += [1, 0] if leader == 1 else [0, 1]

    # Append one-hot encoded whose_turn to feature set
    feature_set += [1, 0] if whose_turn == 1 else [0, 1]

    # Append one-hot encoded opponent's card to feature set
    opponents_played_card_onehot = [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ]
    opponents_played_card_onehot[
        opponents_played_card if opponents_played_card is not None else 20] = 1
    feature_set += opponents_played_card_onehot

    for index, element in enumerate(feature_set):
        if element == None:
            feature_set[index] = 0
    # Return feature set
    return feature_set