[JAVA] Creating a position estimation model for the Werewolf Intelligence Tournament using machine learning

Create a model that uses machine learning to estimate the position of an agent in order to win the Werewolf Intelligence Tournament

I have been participating in the Werewolf Intelligence Tournament as a hobby for several years, but my grades are not good. I think it's time for our team to break away from the rule base and create a human wolf intelligence agent that uses machine learning.

As the first step for that, we will try to make a classifier that determines whether or not it is a human wolf by extracting features from past battle logs with reference to "AI programming learned by human wolf intelligence". However, we have not yet incorporated the created model into the human wolf intelligence agent.

Considering the ease of experimentation, we decided to use Java code (a minor modification of the officially distributed sample code) for feature extraction from past battle logs, and scikit-learn for learning and estimation. did. The author of this document is not a machine learning expert, so I would be very grateful if you could point out any mistakes or misunderstandings.

The sites and books that I referred to are summarized at the end of the sentence.

The tasks performed this time are as follows.

--Download match log --Label from the battle log and extract features (almost official Java code) --Learning / estimation of binary classification problem of whether or not it is a human wolf (Python scikit-learn)

Download match log

Download your favorite tournament from "Past Tournament Log" in Information for Developers of Human Wolf Intelligence Project. This time I downloaded CEDEC 2017. * When using it as learning data, it is preferable to use the results of competitions that the developers have tuned seriously, rather than small-scale competitions. Unzip this log.

find cedec2017/ -type f -name "*.log.gz" -exec gunzip -d {} \;

When you unzip the battle log, you will find the numbered directory and the actual log file (* .log) contained in it. There are 5 and 15 players in the Werewolf Intelligence Tournament, but in each case, 5 agents / 15 players will be fixed and 100 battles will be held while changing their positions, so the logs in each directory are the combination of the participating agents. Corresponds to the same 100 battles. This time, we will try machine learning using a part of 100 battles with the same combination of agents participating in the 15-player battle as training data and the rest as test data.

This time, in order to try machine learning with small data, create a directory called cedec2017_small, copy the directory of 004 in cedec2017, and use it.

ls cedec2017_small/004
// 0300.log ... 0399.100 files are displayed up to log

Label from the battle log and extract features

--The official LogdataToVector of the human wolf intelligence was difficult to use as it is, so it was slightly modified. --Official GitHub https://github.com/sonodaatom/aiwolfBook --When I try to read the unzipped CEDEC 2017 log, the program stops because it contains a garbage file. --In the recent Werewolf Intelligence Tournament, 15 villages and 5 villages will be held in the qualifying, but LogdataToVector seems to support only 15 villages, so it is necessary to exclude the battle log of 5 villages. --Code that slightly modified the above two points https://github.com/sunmoonStern/aiwolfBook/tree/mybranch -(I tried adding a new feature in the commented out part, but there was not much improvement in performance) --When executed as follows, libSVM format data was created. The data / directory should be mkdir. --Labeling is -1 for human wolf, 1 for non-human wolf

java -jar /{path_to_jar}/AIbook.jar book.LogdataToVector /{path_to_log}/cedec2017_small/ data/

--Combine the 100 files created above into one (may not be needed)

import os
import subprocess
import sys

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Usage: # python %s input_dir output_file' % argvs[0])
        quit()
    dir_name = sys.argv[1] # '/{path_to_data}/data/'
    files = os.listdir(dir_name)
    out_file = sys.argv[2] # '/{path_to_outfile}/new.log.txt'
    os.system('rm ' + out_file)
    for fin in files:
        files_with_path = dir_name + fin
        subprocess.call('cat ' + files_with_path + ' >> ' + out_file, shell=True)

――The completed libSVM file looks like this ――For the meaning of each feature, it is quick to read the source code of LogdataToVector. --The point to keep in mind when adding your own features is that you should not use system information that is not open to the player during the Werewolf game from the log. For example, if you focus on the whisper line that can only be used by human wolf from the log, you can definitely hit human wolf, but this is because you want to create a strong agent who can assign the position of another agent from the information disclosed to the player. It goes against the purpose.

$ head -5 mini.log.txt
1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0
1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0
1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0
1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0
-1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0

Learning / estimation

--Install scikit-learn

pip install numpy
pip install scipy
pip install -U scikit-learn

--Refer to the scikit-learn document and try using it as a black box for the time being.

from sklearn.datasets import load_svmlight_file
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import classification_report, accuracy_score

x,y = load_svmlight_file('/{path_to_log}/new.log.txt')
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
clf = svm.SVC(cache_size = 2000)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print accuracy_score(y_test, y_pred)
print classification_report(y_test, y_pred)

――I got this result ――Since the positive and negative cases are biased, it is unclear whether the result of precision 0.82 can be received at face value. Also, I was worried that the recall when the agent was a human wolf was extremely bad.

             precision    recall  f1-score   support

       -1.0       0.74      0.20      0.31       626
        1.0       0.84      0.98      0.90      2614

avg / total       0.82      0.83      0.79      3240

What I left behind

--Experiment by adding more features ――Evaluate the model by cross-validation instead of the crude method of using 30% of the data as test data. --When I tried to learn using the entire CEDEC 2017 log, it didn't work, so find a way to scale it. ――I tried using random forest and it was fast, but the performance was a little bad. —— Experiment with different algorithms to find the right parameters --Use only the final log, in which only strong agents are expected to participate in the battle log (it is unknown whether the 100 races in qualifying and the final can be distinguished from the log)

reference

-AI programming learned by human wolf intelligence --Introduction book for human wolf intelligence development --Refer to Chapter 5 especially for how to make a human wolf intelligence agent using machine learning. -Human Wolf Intelligence Project --You can download the schedule of the Werewolf Intelligence Tournament, announcements of related events such as seminars and training camps, logs of past tournaments, and source code of past tournaments. --scikit-learn SVM documentation

Recommended Posts

Creating a position estimation model for the Werewolf Intelligence Tournament using machine learning
Creating a learning model using MNIST
Creating a development environment for machine learning
Memo for building a machine learning environment using Python
xgboost: A valid machine learning model for table data
I tried using Tensorboard, a visualization tool for machine learning
Inversely analyze a machine learning model
Creating artificial intelligence by machine learning using TensorFlow from zero knowledge-Introduction 1
Introduction to Deep Learning for the first time (Chainer) Japanese character recognition Chapter 3 [Character recognition using a model]
Find the dates for a jarring tournament
[Machine learning] Supervised learning using kernel density estimation
(Reading the paper) Jukebox: A Generative Model for Music Prafulla (Music sampling using VQ-VAE)
I tried to implement various methods for machine learning (prediction model) using scikit-learn.
Implementation of a model that predicts the exchange rate (dollar-yen rate) by machine learning
A memorandum of method often used in machine learning using scikit-learn (for beginners)
A story about simple machine learning using TensorFlow
[Machine learning] Supervised learning using kernel density estimation Part 2
Creating an interactive application using a topic model
Face image dataset sorting using machine learning model (# 3)
Created a fooling image for the caption generative model
Image collection Python script for creating datasets for machine learning
Creating a graph using the plotly button and slider
Machine Learning with Caffe -1-Category images using reference model
[Machine learning] Text classification using Transformer model (Attention-based classifier)
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried to compress the image using machine learning
(Note) A story about creating a question answering system using Spring Boot and machine learning (SVM)
[Day 9] Creating a model
The story of creating a VIP channel for in-house chatwork
Avoiding the pitfalls of using a Mac (for Linux users?)
Build an environment for machine learning using Python on MacOSX
Try to evaluate the performance of machine learning / regression model
Build a seq2seq model using keras's Functional API Model building & learning
Try a similar search for Image Search using the Python SDK [Search]
Try to evaluate the performance of machine learning / classification model
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I made a VGG16 model using TensorFlow (on the way)
Create a python machine learning model relearning mechanism with mlflow
Try to model a multimodal distribution using the EM algorithm
Build a PyData environment for a machine learning study session (January 2017)
The story of creating a database using the Google Analytics API
A story about a student who does not know the machine learning machine learned machine learning (deep learning) for half a year