Пример #1
0
def learn_game(game_path: str, num_iterations: int, refresh: bool):
    game = parser.parse_xml(game_path)
    game.start([randomplayer.RandomPlayer(0), randomplayer.RandomPlayer(1)])
    input_mapper_path = game.name + INPUT_MAPPER_FILE_SUFFIX
    input_mapper = NeuralNetworkInput(game) if refresh or not os.path.exists(input_mapper_path) \
        else read_mapper(input_mapper_path)
    output_mapper_path = game.name + OUTPUT_MAPPER_FILE_SUFFIX
    output_mapper = NeuralNetworkOutputMapper(game) if refresh or not os.path.exists(output_mapper_path) \
        else read_mapper(output_mapper_path)
    while output_mapper.missing_mappings():
        players = [ExploratoryPlayer(0, output_mapper), ExploratoryPlayer(1, output_mapper)]
        parser.parse_xml(game_path).start(players)
    chooser_path = game.name + CHOICE_FILE_SUFFIX
    chooser = create_neural_network(input_mapper.input_length, output_mapper.output_length) if refresh \
        else load_or_create_neural_network(chooser_path, input_mapper.input_length, output_mapper.output_length)
    scorer_path = game.name + SCORER_FILE_SUFFIX
    scorer = create_neural_network(input_mapper.input_length, 1) if refresh \
        else load_or_create_neural_network(scorer_path, input_mapper.input_length, 1)

    players = [
        LearningPlayer(0, input_mapper, output_mapper, scorer, chooser),
        manualplayer.ManualPlayer(1)
    ]
    wins = 0
    losses = 0

    def save():
        save_mapper(input_mapper_path, input_mapper)
        save_mapper(output_mapper_path, output_mapper)
        chooser.save(chooser_path)
        scorer.save(scorer_path)
    for i in range(int(num_iterations)):
        print("Game "+str(i))
        winners = parser.parse_xml(game_path).start(players)
        wins += int(all(isinstance(winner, LearningPlayer) for winner in winners))
        losses += int(not(any(isinstance(winner, LearningPlayer) for winner in winners)))
        if i % 100 == 0:
            save()
    print("Winners:"+str(wins))
    print("Losers:"+str(losses))
    save()
Пример #2
0
def run_game(game_path, players):
    game = parser.parse_xml(game_path)
    game.start(players)
    return game