예제 #1
0
    prev_percentage = 0
    for i in range(len(winner_of_generation)):
        winner_of_generation[i].set_name(i + n_ai)
        ai.append(winner_of_generation[i])
    score = np.zeros(len(ai), dtype='int')
    print('Complete: ', end='', flush=True)
    for i in range(len(ai)):
        percentage = int(i / len(ai) * 100)
        [
            print('#', end='', flush=True)
            for x in range(percentage - prev_percentage)
        ]
        prev_percentage = percentage
        for j in range((i + 1), len(ai)):
            game = Othello()
            winner = game.play_self(ai[i], ai[j])
            score[winner.get_name()] += 1
    print('| 100%')
    winners = score.argsort()[-n_winners:][::-1]
    ai_winners = []
    for winner in winners:
        ai_winners.append(ai[winner])
    print('Ai-{} won {} out of {} ({:.2f}%) games.\n'.format(
        winners[0], score[winners[0]],
        len(ai) - 1, score[winners[0]] / (len(ai) - 1) * 100))

    # Adds the best AI of this generation to the list of contenders
    if ai_winners[0] not in winner_of_generation and (score[winners[0]] /
                                                      (len(ai) - 1)) > 0.625:
        winner_of_generation.append(ai_winners[0])
        file = open('weights\\best_gen.txt', 'a')
예제 #2
0
from Othello import Othello
from Othello import AI
import numpy as np
import matplotlib.pyplot as plt

generation = int(input('Which generation do you wish to play against:'))
w1 = np.load('weights\weight-Gen{:03d}-1.npy'.format(generation))
w2 = np.load('weights\weight-Gen{:03d}-2.npy'.format(generation))
ai = AI(weight1=w1, weight2=w2)
player = int(
    input(
        'Which player do you wish to be (black: -1, white: 1) (or choose another ai):'
    ))
game = Othello()
print(np.average(np.abs(w1)))
print(np.average(np.abs(w2)))
if player > 1:
    w1 = np.load('weights\weight-Gen{:03d}-1.npy'.format(player))
    w2 = np.load('weights\weight-Gen{:03d}-2.npy'.format(player))
    ai2 = AI(weight1=w1, weight2=w2)
    ai.set_name('Gen' + str(generation))
    ai2.set_name('Gen' + str(player))
    game.play_self(ai, ai2, display_board=True)
else:
    game.play(ai, player)

# n, bins, patches = plt.hist(w1, 50, normed=1, facecolor='green', alpha=0.75)
# plt.show()