13 Optimizing Schnapsen Strategy - Report
13 Optimizing Schnapsen Strategy - Report
Abstract. This paper aims to find the most effective features for a ma-
chine learning model. This is done by training machine learning models
with a training dataset of games between two rdeep bots and using per-
mutation importance to determine the importance of each feature in the
game Schnapsen. The weights of every feature were measured using the
ELI5 package. The features are ranked in a table where the features
are named. This demonstrates which characteristic is essential for each
machine learning model. The weights given by the supervised and unsu-
pervised models were substantially different. The results show that the
features that were expected to be important such as talon size and trump
suit didn’t always have a high weight and that the information about the
opponent’s cards was more valuable than the information about the bot’s
cards.
1 Introduction
This research paper investigates the relationship between model and feature se-
lection in machine learning. To answer the research question: ”How do various
machine learning models influence feature selection for Schnapsen, and which
features effectively enhance bot performance?” We tested various machine learn-
ing algorithms, including Neural Networks, Linear Logistic Regression, Decision
Tree Regression, Decision Tree Classifier, Random Forest Classification, Random
Forest Regression, and K-means Clustering, in the game of Schnapsen. The re-
sults of this study will provide valuable insight into the role that feature selection
plays in developing effective machine-learning models for the game Schnapsen.
2 Background Information
2.1 Schnapsen
Schnapsen is a turn-based card game played with two people. The game is played
with 20 cards, 5 of each suit: the aces, kings, queens, jacks and tens. The aces
are worth 11 points, the tens are worth 10 points, the kings are worth 4 points,
the queens are worth 3 points, and the jacks are worth 2 points. To win, one of
the players needs to get seven-game points. Game points can be won by earning
66 points. The number of game points won depends on how the opponent did: if
the opponent scored 33 points or higher, the winner gets one game point. If the
opponent scores below 33 points but has scored at least one trick, the winner
receives two game points; if the opponent has not won a trick, the winner gets
three game points. Getting 66 points can be done in two ways: winning tricks
against your opponent and declaring marriages. You must play a card worth
more than your opponent’s to win a trick. The player that starts the trick is
called the leader; before the first trick, the players draw a card. The player with
the best card is the dealer. The other player will be the leader of the first trick.
After each trick, the other player becomes the leader of the new trick. Each
player has five cards. The remaining cards are turned upside down and put in
the talon(stock).[2]
The dealer deals the cards by giving the other player three cards, taking three
cards, then one of the cards is turned right side up, after which the last two
cards are dealt to both hands, and the remaining cards are put upside down on
top of the card that is right side up, that pile of cards is the talon. The card that
is placed right side up is the trump card, and the suit of that card is the trump
suit. At the start of the trick, the leader can (if possible) do a trump exchange,
where the jack of the trump suit can be exchanged with the trump card. The
leader can also declare a marriage at the start of the trick. A marriage can be
declared if the leader has a king and a queen in the same suit. A marriage is
worth 20 points, and a trump marriage (a marriage with a king and queen of
the trump suit) is worth 40 points. The game has two phases: before, the talon
is empty, and after, the talon is empty. In the second phase, the rules change:
the second player has to play a card of the same suit as the card played by the
leader and beat that card if possible. If the second player does not have a card
of the same suit, a card of the trump suit must be played, and if that is not
possible, any card may be played.[2]
3 ML Models
3.1 Neural Network Machine Learning Model
MLPClassifier is a classifier in the Scikit-Learn library and a type of neural net-
work. It is a supervised machine learning model that processes data through
layers of interconnected nodes (neurons). Each neuron receives input from the
Artificial Intelligence 2024 3
previous layer and applies a weighted sum to the inputs. The final layer outputs
represent the predicted class probabilities, and the class with the highest proba-
bility is assigned as the prediction. In the case of Schnapsen, it would select the
class with the highest probability of winning the game with a probability closest
to 1. [4]
Decision Trees are a supervised learning method used for classification and re-
gression. The goal is to create a model that predicts the value of a target variable
by learning simple decision rules inferred from the data features. Decision Tree
Classifier is a class capable of performing multi-class classification on a dataset.[5]
4 Research Question
In the game of Schnapsen there are a large number of features the machine
learning bots utilize. Some are more important and some are less. The goal of
this research paper is to find out what are the most effective features for a given
machine learning model and how the model choice affects the feature selection.
The features will be judged based on the value each machine learning model
assigned to them after their respective training.
4.1 Hypothesis
The hypothesis of this research is that there will be specific features that have a
higher value and importance compared to the other features. It is also expected
that the top features for the classification and regression models will differ due
to their distinct approaches in machine learning. The expected features that
are predicted to be generally weighed higher than others among most of the
machine learning models are high cards in opponent’s and bot’s hand, talon size
and trump suit.
5 Experiment
Using the training dataset of games between two rdeep bots, the machine learn-
ing models were trained. The models were then fitted to the data and targets
using the permutation importance method. Using permutation importance the
relative importance of each feature was determined and ranked accordingly. Fea-
tures with high weights can be considered as important predictors in the model.1
Furthermore, an ANOVA test was conducted using the combined weight values
of each individual model excluding K-means clustering due to its significantly
larger weight values compared to the other models. This test shows whether
there are statistically significant differences in the mean weight values of the
machine learning models. The test was preformed using scipy.stats.foneway from
the SciPy module.[9] 2
6 Results
The weights of every feature were measured using the eli5 package by refitting the
machine learning model with our dataset. One-hot encoding helped to categorize
all features that the machine learning models have used. The First column refers
to weights assigned by the models, and the second is the feature to which the
weight belongs.
First, high cards in opponents’ hands, followed by players’ cards, were expected
to be among the top features of each bot since they often give the highest amount
of points outside marriage. Furthermore the suit of cards in different features
1
Relevant code can be found in section 8.1 of appendix
2
Code can be found in section 8.2 of appendix
Artificial Intelligence 2024 6
don’t seem to affect the weight of the features in any way since they are evenly
distributed.
Second, contrary to the expected outcome, half of the machine learning models
did not give high weights to the trump suits or talon size, which are critical
features in knowing what card is most likely to win or help predict the outcome
of the game. Additionally, certain machine learning models gave high weights to
opponent’s and player’s points which were not predicted to be among the top
expected features.
Third, features concerned about opponents’ card states are almost always higher
weighted than the players. For example, an Ace held by an opponent always
weighs more than an Ace that is unknown or held by the player. The same
occurrence follows every card and even the points of each player.
Lastly, the only unsupervised model K-means Clustering had weights in the
hundreds of thousands, while other bots gave weights that were always less than
one. Likely caused by the eli5 package not being made for an unsupervised model,
which might overstate the importance of some features. Furthermore K-means
Clustering machine learning model preferred most of the game features which
are not containing information about cards such as card in opponent’s hand.
Next this bot weighed higher all opponent’s cards, of same points, than it’s own
cards.
7 Findings
The unsupervised model K-Means Clustering gave the highest points to al-
most all the basic features, unlike other supervised learning models, which often
preferred high-value cards over trump suits, for example. Features that show op-
ponents’ cards, scores or if its leader always had a higher value than the player’s
features of the same type. This indicates that knowledge of opponents’ features
gives an advantage in the game.
This ANOVA test on the permutation importance values of all models excluding
K-means clustering resulted in a F-statistic score of 8.95 and P-value of 6.28e-
07. The P-value is significantly below the significance level of 0.05. This implies
that there are significant variations in the feature importance values across the
different machine learning models as it rejects the null-hypothesis that there are
no significant differences in the mean importance values of features among the
different machine learning models. This suggests that certain features have a
more significant role in predicting game outcomes depending on the model. For
example, a model might have a higher importance of features related to game
phase or player status. While another model might prioritize features related to
trump suits. The statistical testing displays the importance of selecting the most
appropriate machine learning model and features for a Schnapsen playing bot
and in other applications of machine learning bots. 3
8 Conclusion
The Permutation Importance testing was carried out on the same dataset used
for training the models. As a result, these values may not accurately reflect the
importance of features in different scenarios within the game. Nevertheless, it is
still able to provide a good representation of the preferred features for each type
of model and can help be a guide to feature selection and model selection.
This research on machine learning models feature selection which might not
correlate with how their win ratio change if they played games without these
features. For example, their features that weighed the most might not affect their
win ratios as much as different lower weight features. Further research on how
successful are machine learning bots without certain features need to be done to
holistically solve the question regarding effective feature and model selection.
Artificial Intelligence 2024 12
References
1. Permutation Importance — ELI5 0.11.0 documentation. (n.d.).
https://eli5.readthedocs.io/en/latest/blackbox/permutation-importance.html
2. McLeod, J. (2022, December 1). Schnapsen - card game rules.
https://www.pagat.com/marriage/schnaps.html
3. sklearn.linear-model.LogisticRegression. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/generated/sklearn.linear-model.LogisticRegression.html
4. sklearn.neural-network.MLPClassifier. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/generated/sklearn.neural-network.MLPClassifier.html
5. 1.10. Decision Trees. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/tree.html
6. sklearn.ensemble.RandomForestClassifier. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html
7. sklearn.ensemble.RandomForestRegressor. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html
8. sklearn.cluster.KMeans. (n.d.). Scikit-learn. https://scikit-
learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
9. scipy.stats.f oneway — SciPy v1.12.0 Manual. (n.d.). Numpy
and Scipy Documentation — Numpy and Scipy documentation.
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.f oneway.html
Artificial Intelligence 2024 13
Appendix
8.1 ml-bot.py
c l a s s MLPlayingBot ( Bot ) :
”””
This c l a s s l o a d s a t r a i n e d ML model and u s e s i t t o
play
”””
: param m o d e l l o c a t i o n : The f i l e c o n t a i n i n g t h e
model .
”””
super () . i n i t ( name )
model location = model location
a s s e r t m o d e l l o c a t i o n . e x i s t s ( ) , f ” Model c o u l d not
be found a t : { m o d e l l o c a t i o n }”
# l o a d model
Artificial Intelligence 2024 14
d e f get move ( s e l f , p e r s p e c t i v e : P l a y e r P e r s p e c t i v e ,
l e a d e r m o v e : O p t i o n a l [ Move ] ) −> Move :
# get the s a t e f e a t u r e r e p r e s e n t a t i o n
state representation = get state feature vector (
perspective )
# g e t t h e l e a d e r ’ s move r e p r e s e n t a t i o n , even i f
i t i s None
leader move representation =
get move feature vector ( leader move )
# g e t a l l my v a l i d moves
my valid moves = p e r s p e c t i v e . v a l i d m o v e s ( )
# g e t t h e f e a t u r e r e p r e s e n t a t i o n s f o r a l l my
v a l i d moves
my move representations : l i s t [ l i s t [ int ] ] = [ ]
f o r my move i n my valid moves :
m y m o v e r e p r e s e n t a t i o n s . append (
g e t m o v e f e a t u r e v e c t o r ( my move ) )
# c r e a t e a l l model i n p u t s , f o r a l l bot ’ s v a l i d
moves
action state representations : l i s t [ l i s t [ int ] ] =
[]
i f perspective . am i leader () :
follower move representation =
g e t m o v e f e a t u r e v e c t o r ( None )
f o r my move representation in
my move representations :
a c t i o n s t a t e r e p r e s e n t a t i o n s . append (
state representation +
my move representation +
follower move representation )
else :
f o r my move representation in
my move representations :
a c t i o n s t a t e r e p r e s e n t a t i o n s . append (
state representation +
leader move representation +
my move representation )
model output = s e l f . m o d e l . p r e d i c t p r o b a (
action state representations )
Artificial Intelligence 2024 15
w i n n i n g p r o b a b i l i t i e s o f m o v e s = [ outcome prob [ 1 ]
f o r outcome prob i n model output ]
h i g h e s t v a l u e : f l o a t = −1
best move : Move
f o r index , v a l u e i n enumerate (
winning probabilities of moves ) :
i f value > highest value :
highest value = value
best move = my valid moves [ i n d e x ]
a s s e r t best move i s not None
r e t u r n best move
c l a s s MLDataBot ( Bot ) :
”””
This c l a s s i s d e f i n e d t o a l l o w t h e c r e a t i o n o f a
t r a i n i n g s c h na p s e n bot d a t a s e t , t h a t a l l o w s us t o
t r a i n a Machine L e a r n i n g (ML) Bot
P r a c t i c a l l y , i t h e l p s us r e c o r d how t h e game p l a y s
out a c c o r d i n g t o a p r o v i d e d Bot b e h a v i o u r ; b u i l d
what i s c a l l e d a ” r e p l a y memory”
In more d e t a i l , we c r e a t e one t r a i n i n g sample f o r
each d e c i s i o n t h e bot makes w i t h i n a game , where a
d e c i s i o n i s an a c t i o n s e l e c t i o n f o r a s p e c i f i c
game s t a t e .
Then we r e l a t e each d e c i s i o n with t h e outcome o f t h e
game , i . e . whether t h i s bot won o r not .
This way we can then t r a i n a bot a c c o r d i n g t o t h e
assumption t h a t :
” d e c i s i o n s i n e a r l i e r games t h a t ended up i n
v i c t o r i e s s h o u l d be p r e f e r r e d o v e r d e c i s i o n s
t h a t l e a d t o l o s t games ”
This c l a s s o n l y r e c o r d s t h e d e c i s i o n s and game
outcomes o f t h e p r o v i d e d bot , a c c o r d i n g t o i t s own
p e r s p e c t i v e − i n c o m p l e t e game s t a t e knowledge .
”””
d e f get move ( s e l f , p e r s p e c t i v e : P l a y e r P e r s p e c t i v e ,
l e a d e r m o v e : O p t i o n a l [ Move ] ) −> Move :
”””
This f u n c t i o n s i m p l y c a l l s t h e get move o f
t h e p r o v i d e d bot
”””
r e t u r n s e l f . bot . get move ( p e r s p e c t i v e=p e r s p e c t i v e ,
l e a d e r m o v e=l e a d e r m o v e )
d e f n o t i f y g a m e e n d ( s e l f , won : bool , p e r s p e c t i v e :
P l a y e r P e r s p e c t i v e ) −> None :
”””
When t h e game ends , t h i s f u n c t i o n r e t r i e v e s t h e
game h i s t o r y and more s p e c i f i c a l l y a l l t h e
r e p l a y memories t h a t can
be d e r i v e d from i t , and s t o r e s them i n t h e form
o f s t a t e −a c t i o n s v e c t o r r e p r e s e n t a t i o n s and
t h e c o r r e s p o n d i n g outcome o f t h e game
# we i t e r a t e o v e r a l l t h e rounds o f t h e game
for round player perspective , round trick in
game history :
else :
leader move = round trick . leader move
follower move = round trick . follower move
# we do not want t h i s r e p r e s e n t a t i o n t o
i n c l u d e a c t i o n s t h a t f o l l o w e d . So i f t h i s
a g e n t was t h e l e a d e r , we i g n o r e t h e
f o l l o w e r s move
i f round player perspective . am i leader () :
f o l l o w e r m o v e = None
# append r e p l a y memory t o f i l e
with open ( f i l e = s e l f . r e p l a y m e m o r y f i l e p a t h ,
mode=”a ” ) a s r e p l a y m e m o r y f i l e :
# replay memory line : l i s t [ tuple [ l i s t ,
number ] ] = [
state actions representation ,
won label ]
# w r i t i n g t o r e p l a y memory f i l e i n t h e
form ” [ f e a t u r e l i s t ] | | i n t ( w o n l a b e l )
]
r e p l a y m e m o r y f i l e . w r i t e ( f ”{ s t r (
s t a t e a c t i o n s r e p r e s e n t a t i o n ) [1: −1]}
| | { i n t ( w o n l a b e l ) }\n ” )
d e f train ML model ( r e p l a y m e m o r y l o c a t i o n : O p t i o n a l [
p a t h l i b . Path ] ,
m o d e l l o c a t i o n : O p t i o n a l [ p a t h l i b . Path
],
m o d e l c l a s s : L i t e r a l [ ”NN” , ”LR” , ”DTC
” , ”DTR” , ”RFC” , ”RFR” , ”KMC” ] = ”
LR”
) −> None :
”””
Train t h e ML model f o r t h e MLPlayingBot based on
r e p l a y memory s t o r e d byt t h e MLDataBot .
Artificial Intelligence 2024 18
This i m p l e m e n t a t i o n has t h e o p t i o n t o t r a i n a n e u r a l
network model , a model based on l i n e a r r e g r e s s i o n ,
a d e c i s i o n t r e e c l a s s i f i e r model , a d e c i s i o n t r e e
r e g r e s s o r model , a random f o r e s t c l a s s i f i e r model
, a random f o r e s t r e g r e s s o r model o r a K−means
c l u s t e r i n g model .
The model c l a s s e s used i n t h i s i m p l e m n t a t i o n a r e not
n e c e s a r i l y optimal .
: param r e p l a y m e m o r y l o c a t i o n : L o c a t i o n o f t h e games
s t o r e d by MLDataBot , d e f a u l t p a t h l i b . Path ( ’
ML replay memories ’ ) / ’ t e s t r e p l a y m e m o r y ’
: param m o d e l l o c a t i o n : L o c a t i o n where t h e model w i l l
be s t o r e d , d e f a u l t p a t h l i b . Path ( ” ML models ” ) / ’
test model ’
: param m o d e l c l a s s : The machine l e a r n i n g model c l a s s
t o be used , e i t h e r ’NN’ f o r a n e u r a l network , ’LR’
f o r a l i n e a r r e g r e s s i o n , ’DTC’ f o r a d e c i s i o n
t r e e c l a s s i f i e r , ’DTR’ , f o r a d e c i s i o n t r e e
r e g r e s s o r , ’RFC’ f o r a random f o r e s t c l a s s i f i e r , ’
RFR’ f o r a random f o r e s t r e g r e s s o r and ’KMC’ f o r K
−means c l u s t e r i n g .
: param o v e r w r i t e : Whether t o o v e r w r i t e a p o s s i b l y
e x i s t i n g model .
”””
i f r e p l a y m e m o r y l o c a t i o n i s None :
r e p l a y m e m o r y l o c a t i o n = p a t h l i b . Path ( ’
ML replay memories ’ ) / ’ t e s t r e p l a y m e m o r y ’
i f m o d e l l o c a t i o n i s None :
m o d e l l o c a t i o n = p a t h l i b . Path ( ” ML models ” ) / ’
test model ’
a s s e r t m o d e l c l a s s == ’NN’ o r m o d e l c l a s s == ’LR’ o r
m o d e l c l a s s == ’DTC’ o r m o d e l c l a s s == ’DTR’ o r
m o d e l c l a s s == ’RFC’ o r m o d e l c l a s s == ’RFR’ o r
m o d e l c l a s s == ’KMC’ , ”Unknown model c l a s s ”
# Check i f model e x i s t s a l r e a d y
i f model location . exists () :
r a i s e ValueError (
Artificial Intelligence 2024 19
f ” Model a t { m o d e l l o c a t i o n } e x i s t s a l r e a d y
and o v e r w r i t e i s s e t t o F a l s e . \nNo new
model w i l l be t r a i n e d , p r o c e s s t e r m i n a t e s
”)
data : l i s t [ l i s t [ i n t ] ] = [ ]
targets : l i s t [ int ] = [ ]
with open ( f i l e =r e p l a y m e m o r y l o c a t i o n , mode=”r ” ) a s
replay memory file :
for l i n e in replay memory file :
feature string , won label str = line . split
(”||”)
feature list strings : l i s t [ str ] =
feature string . s p l i t (” ,”)
f e a t u r e l i s t = [ int ( feature ) for feature in
feature list strings ]
won label = int ( won label str )
data . append ( f e a t u r e l i s t )
t a r g e t s . append ( w o n l a b e l )
p r i n t (” Dataset S t a t i s t i c s : ” )
s a m p l e s o f w i n s = sum ( t a r g e t s )
samples of losses = len ( targets ) − samples of wins
p r i n t ( ” Samples o f wins : ” , s a m p l e s o f w i n s )
p r i n t ( ” Samples o f l o s s e s : ” , s a m p l e s o f l o s s e s )
# F e e l f r e e t o e x p e r i m e n t with d i f f e r e n t number
o f n e u r a l l a y e r s o r d i f f e r n t type o f n e u r o n s
per l a y e r
# Tips : more n e u r o n s o r more l a y e r s o f n e u r o n s
c r e a t e a more c o m p l i c a t e d model t h a t t a k e s
more time t o t r a i n and
# n e e d s a b i g g e r d a t a s e t , but i f you f i n d t h e
c o r r e c t c o m b i n a t i o n o f n e u r o n s and n e u r a l
l a y e r s and p r o v i d e a b i g enough t r a i n i n g
d a t a s e t can l e a d t o b e t t e r p e r f o r m a n c e
# one l a y e r o f 30 n e u r o n s
hidden layer sizes = (30)
# two l a y e r s o f 30 and 5 n e u r o n s r e s p e c t i v e l y
# h i d d e n l a y e r s i z e s = (30 , 5)
# Train a n e u r a l network
l e a r n e r = M L P C l a s s i f i e r ( h i d d e n l a y e r s i z e s=
h i d d e n l a y e r s i z e s , l e a r n i n g r a t e i n i t=
learning rate ,
a l p h a=
regularization strength
, v e r b o s e=True ,
e a r l y s t o p p i n g=True ,
n i t e r n o c h a n g e =6,
a c t i v a t i o n =’ tanh ’ )
e l i f m o d e l c l a s s == ’LR ’ :
# Train a s i m p l e r L i n e a r L o g i s t i c R e g r e s s i o n
model
# l e a r n more about t h e model o r how t o u s e b e t t e r
u s e i t by c h e c k i n g out i t s documentation
# h t t p s : / / s c i k i t −l e a r n . o r g / s t a b l e / modules /
generated / sklearn . linear model .
L o g i s t i c R e g r e s s i o n . html#s k l e a r n . l i n e a r m o d e l .
LogisticRegression
Artificial Intelligence 2024 21
p r i n t ( ” T r a i n i n g a Simple ( L i n e a r L o g i s t i c
R e g r e s s i o n model ) ” )
# U s u a l l y t h e r e i s no r e a s o n t o change t h e
h y p e r p a r a m e t e r s o f such a s i m p l e model but
f i l l f r e e to experiment :
l e a r n e r = L o g i s t i c R e g r e s s i o n ( m a x i t e r =1000)
e l i f m o d e l c l a s s == ’DTC’ :
# Train a d e c i s i o n t r e e c l a s s i f i e r model
( ” T r a i n i n g a D e c i s i o n Tree C l a s s i f i e r model . ” )
learner = DecisionTreeClassifier ()
e l i f m o d e l c l a s s == ’DTR’ :
# Train a d e c i s i o n t r e e r e g r e s s o r model
p r i n t ( ” T r a i n i n g a D e c i s i o n Tree R e g r e s s o r model
.”)
learner = DecisionTreeRegressor ()
e l i f m o d e l c l a s s == ”RFC” :
# Train a random f o r e s t c l a s s i f i e r model
p r i n t ( ” T r a i n i n g a Random F o r e s t C l a s s i f i e r model
.”)
learner = RandomForestClassifier ()
e l i f m o d e l c l a s s == ’RFR’ :
# Train a random f o r e s t r e g r e s s o r model
( ” T r a i n i n g a Random F o r e s t R e g r e s s o r model . ” )
l e a r n e r = RandomForestRegressor ( )
e l i f m o d e l c l a s s == ’KMC’ :
# Train a K−means c l u s t e r i n g model
p r i n t ( ” T r a i n i n g a K−Means C l u s t e r i n g model . ” )
l e a r n e r = GaussianMixture ( n components = 2 ,
i n i t p a r a m s = ”kmeans ” )
else :
r a i s e A s s e r t i o n E r r o r ( ” Unknown model c l a s s ” )
s t a r t = time . time ( )
p r i n t ( ” S t a r t i n g t r a i n i n g phase . . . ” )
model = l e a r n e r . f i t ( data , t a r g e t s )
# Save t h e model i n a f i l e
Artificial Intelligence 2024 22
# C r e a t e html f i l e with e l i 5 p e r m u t a t i o n i m p o r t a n c e
v a l u e s / w e i g h t s when t r a i n e d
perm = PermutationImportance ( model ) . f i t ( data , t a r g e t s
)
html = e l i 5 . f o r m a t a s h t m l ( e l i 5 . e x p l a i n w e i g h t s ( perm ,
top = 3 0 0 ) )
with open ( ’ s i m p l e . html ’ , ’ w’ ) a s f :
f . w r i t e ( html )
j o b l i b . dump( model , m o d e l l o c a t i o n )
end = time . time ( )
p r i n t ( ’ The model was t r a i n e d i n ’ , ( end − s t a r t ) /
6 0 , ’ minutes . ’ )
d e f g e t o n e h o t e n c o d i n g o f c a r d s u i t ( c a r d s u i t : S u i t ) −>
l i s t [ int ] :
”””
T r a n s l a t i n g t h e s u i t o f a c a r d i n t o one hot v e c t o r
encoding of s i z e 4 .
”””
card suit one hot : l i s t [ int ]
i f c a r d s u i t == S u i t .HEARTS:
Artificial Intelligence 2024 23
d e f g e t o n e h o t e n c o d i n g o f c a r d r a n k ( c a r d r a n k : Rank ) −>
l i s t [ int ] :
”””
T r a n s l a t i n g t h e rank o f a c a r d i n t o one hot v e c t o r
encoding of s i z e 13.
”””
card rank one hot : l i s t [ int ]
i f c a r d r a n k == Rank .ACE:
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 1]
e l i f c a r d r a n k == Rank .TWO:
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 1 , 0]
e l i f c a r d r a n k == Rank .THREE:
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 1 , 0 , 0]
e l i f c a r d r a n k == Rank .FOUR:
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
1 , 0 , 0 , 0]
e l i f c a r d r a n k == Rank . FIVE :
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ,
0 , 0 , 0 , 0]
e l i f c a r d r a n k == Rank . SIX :
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ,
0 , 0 , 0 , 0]
e l i f c a r d r a n k == Rank .SEVEN:
card rank one hot = [0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 ,
0 , 0 , 0 , 0]
e l i f c a r d r a n k == Rank . EIGHT :
card rank one hot = [0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ,
0 , 0 , 0 , 0]
e l i f c a r d r a n k == Rank . NINE :
Artificial Intelligence 2024 24
i f move i s None :
move type one hot encoding numpy array = [ 0 , 0 ,
0]
card rank one hot encoding numpy array = [0 , 0 ,
0 , 0]
card suit one hot encoding numpy array = [0 , 0 ,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Artificial Intelligence 2024 25
else :
move type one hot encoding : l i s t [ int ]
# i n c a s e t h e move i s a m a r r i a g e move
i f move . i s m a r r i a g e ( ) :
move type one hot encoding = [0 , 0 , 1]
c a r d = move . q u e e n c a r d
# i n c a s e t h e move i s a trump exchange move
e l i f move . i s t r u m p e x c h a n g e ( ) :
move type one hot encoding = [0 , 1 , 0]
c a r d = move . j a c k
# i n c a s e i t i s a r e g u l a r move
else :
move type one hot encoding = [1 , 0 , 0]
c a r d = move . c a r d
move type one hot encoding numpy array =
move type one hot encoding
card rank one hot encoding numpy array =
g e t o n e h o t e n c o d i n g o f c a r d r a n k ( c a r d . rank )
card suit one hot encoding numpy array =
g e t o n e h o t e n c o d i n g o f c a r d s u i t ( card . s u i t )
def g e t s t a t e f e a t u r e v e c t o r ( perspective :
P l a y e r P e r s p e c t i v e ) −> l i s t [ i n t ] :
”””
This f u n c t i o n g a t h e r s a l l s u b j e c t i v e i n f o r m a t i o n
t h a t t h i s bot has a c c e s s to , t h a t can be used
t o d e c i d e i t s next move , i n c l u d i n g :
− points of this player ( int )
− p o i n t s o f t h e opponent ( i n t )
− pending p o i n t s o f t h i s p l a y e r ( i n t )
− pending p o i n t s o f opponent ( i n t )
− t h e trump s u i t (1− hot e n c o d i n g )
− phase o f game (1−hoy e n c o d i n g )
− talon s i ze ( int )
− i f t h i s p l a y e r i s l e a d e r (1− hot e n c o d i n g )
− What i s t h e s t a t u s o f each c a r d o f t h e deck (
where i t i s , o r i f i t s l o c a t i o n i s unknown )
I t s h o u l d o n l y i n c l u d e any e a r l i e r a c t i o n s o f
other agents ( so the a c t i o n of the other agent
in case that i s the l e a d e r )
”””
# a l i s t of a l l the f e a t u r e s that c o n s i s t the s t a t e
f e a t u r e s e t , o f type np . ndarray
s t a t e f e a t u r e l i s t : l i s t [ int ] = [ ]
# add t h e f e a t u r e s t o t h e f e a t u r e s e t
s t a t e f e a t u r e l i s t += [ p l a y e r p o i n t s ]
s t a t e f e a t u r e l i s t += [ p l a y e r p e n d i n g p o i n t s ]
# add t h e f e a t u r e s t o t h e f e a t u r e s e t
s t a t e f e a t u r e l i s t += [ o p p o n e n t s p o i n t s ]
s t a t e f e a t u r e l i s t += [ o p p o n e n t s p e n d i n g p o i n t s ]
# − talon s i ze ( int )
talon size = perspective . get talon size ()
# add t h i s f e a t u r e s t o t h e f e a t u r e s e t
Artificial Intelligence 2024 27
s t a t e f e a t u r e l i s t += [ t a l o n s i z e ]
# − i f t h i s p l a y e r i s l e a d e r (1− hot e n c o d i n g )
i am leader = [0 , 1] i f perspective . am i leader ()
else [1 , 0]
# add t h i s f e a t u r e s t o t h e f e a t u r e s e t
s t a t e f e a t u r e l i s t += i a m l e a d e r
# g a t h e r a l l known deck i n f o r m a t i o n
hand cards = p e r s p e c t i v e . get hand ( ) . cards
trump card = p e r s p e c t i v e . g e t t r u m p c a r d ( )
won cards = p e r s p e c t i v e . g e t w o n c a r d s ( ) . g e t c a r d s ( )
opponent won cards = p e r s p e c t i v e .
get opponent won cards () . get cards ()
opponent known cards = p e r s p e c t i v e .
get known cards of opponent hand () . get cards ()
# each c a r d can e i t h e r be i ) on p l a y e r ’ s hand , i i ) on
p l a y e r ’ s won c a r d s , i i i ) on opponent ’ s hand , i v )
on opponent ’ s won c a r d s
# v ) be t h e trump c a r d o r v i ) i n an unknown p o s i t i o n
−> e i t h e r on t h e t a l o n o r on t h e opponent ’ s hand
# There a r e a l l d i f f e r e n t c a s e s r e g a r d i n g card ’ s
knowledge , and we r e p r e s e n t t h e s e 6 c a s e s u s i n g
one hot e n c o d i n g v e c t o r s a s s e e n b e l l o w .
f o r c a r d i n SchnapsenDeckGenerator ( ) . g e t i n i t i a l deck
() :
card knowledge in one hot encoding : l i s t [ int ]
# i ) on p l a y e r ’ s hand
i f card in hand cards :
card knowledge in one hot encoding = [0 , 0,
0 , 0 , 0 , 1]
# i i ) on p l a y e r ’ s won c a r d s
e l i f c a r d i n won cards :
card knowledge in one hot encoding = [0 , 0,
0 , 0 , 1 , 0]
# i i i ) on opponent ’ s hand
e l i f c a r d i n opponent known cards :
card knowledge in one hot encoding = [0 , 0,
0 , 1 , 0 , 0]
# i v ) on opponent ’ s won c a r d s
e l i f card in opponent won cards :
Artificial Intelligence 2024 28
# add t h i s f e a t u r e s t o t h e f e a t u r e s e t
s t a t e f e a t u r e l i s t +=
deck knowledge in consecutive one hot encodings
return s t a t e f e a t u r e l i s t
8.2 Weight-ANOVA-test.py
import pandas a s pd
from s c i p y . s t a t s import f oneway
d f = pd . DataFrame ( data )
# ANOVA
f s t a t i s t i c , p v a l u e = f oneway ( ∗ [ group [ ” Importance ” ] f o r
name , group i n d f . groupby ( ” Model ” ) ] )
p r i n t ( ”F− s t a t i s t i c : ” , f s t a t i s t i c )
p r i n t ( ”P−v a l u e : ” , p v a l u e )
Artificial Intelligence 2024 30
0 ± 0.0000 x140
0.0004 ± 0.0001 x15
0 ± 0.0000 x161
0.0004 ± 0.0002 x133
0 ± 0.0000 x143
0.0004 ± 0.0003 x139
0 ± 0.0000 x162
0.0003 ± 0.0003 x63
0 ± 0.0000 x155
0.0003 ± 0.0001 x151
0 ± 0.0000 x154
0.0003 ± 0.0002 x125
0 ± 0.0000 x165
0.0003 ± 0.0002 x32
0 ± 0.0000 x164
0.0003 ± 0.0003 x138
0 ± 0.0000 x166
0.0003 ± 0.0002 x83
0 ± 0.0000 x68
0.0003 ± 0.0003 x57
0 ± 0.0000 x145
0.0002 ± 0.0001 x20
0 ± 0.0000 x167
0.0002 ± 0.0002 x86
0 ± 0.0000 x147
0.0002 ± 0.0002 x116
0 ± 0.0000 x146
0.0002 ± 0.0002 x128
0 ± 0.0000 x144
0.0002 ± 0.0002 x148
-0.0000 ± x149
0.0001
0.0002 ± 0.0003 x136
-0.0000 ± x76
0.0001
0.0002 ± 0.0001 x80
-0.0000 ± x74
0.0002
0.0002 ± 0.0002 x74
-0.0000 ± x80
0.0002
0.0002 ± 0.0003 x137
-0.0000 ± x90
0.0001
Artificial Intelligence 2024 37
0 ± 0.0000 x161
-0.0001 ± x88
0.0001
0 ± 0.0000 x160
-0.0001 ± x28
0.0003
0 ± 0.0000 x143
-0.0001 ± x51
0.0002
0 ± 0.0000 x144
-0.0001 ± x4
0.0002
0 ± 0.0000 x140
-0.0001 ± x6
0.0001
0 ± 0.0000 x155
-0.0001 ± x151
0.0002
0 ± 0.0000 x154
-0.0001 ± x129
0.0002
0 ± 0.0000 x145
-0.0001 ± x24
0.0003
0 ± 0.0000 x142
-0.0001 ± x99
0.0004
0 ± 0.0000 x141
Table 8:
0 ± 0.0000 x166
0 ± 0.0000 x11
0 ± 0.0000 x165
0 ± 0.0000 x163
0 ± 0.0000 x164
0 ± 0.0000 x167
0 ± 0.0000 x163
0 ± 0.0000 x162
0 ± 0.0000 x161
0 ± 0.0000 x141
0 ± 0.0000 x146
0 ± 0.0000 x161
0 ± 0.0000 x12
0 ± 0.0000 x160
0 ± 0.0000 x160
0 ± 0.0000 x155
0 ± 0.0000 x141
0 ± 0.0000 x154
0 ± 0.0000 x142
0 ± 0.0000 x147
0 ± 0.0000 x143
0 ± 0.0000 x146
0 ± 0.0000 x155
0 ± 0.0000 x145
0 ± 0.0000 x154
0 ± 0.0000 x144
0 ± 0.0000 x144
0 ± 0.0000 x143
0 ± 0.0000 x145
0 ± 0.0000 x142
0 ± 0.0000 x140
0 ± 0.0000 x140
0.0414 ± 0.0007 x0
0.5803 ± 0.0037 x2
0.0024 ± 0.0000 x10
0.1440 ± 0.0008 x12
0.0024 ± 0.0001 x102
0.1284 ± 0.0010 x11
0.0022 ± 0.0002 x72
0.1078 ± 0.0008 x102
0.0018 ± 0.0001 x132
0.0696 ± 0.0007 x132
0.0018 ± 0.0001 x37
0.0688 ± 0.0005 x72
0.0018 ± 0.0002 x99
0.0641 ± 0.0006 x42
0.0015 ± 0.0001 x42
0.0600 ± 0.0007 x148
0.0013 ± 0.0001 x97
0.0579 ± 0.0006 x5
0.0013 ± 0.0001 x39
0.0477 ± 0.0005 x6
0.0012 ± 0.0001 x123
0.0476 ± 0.0007 x37
0.0011 ± 0.0001 x11
0.0427 ± 0.0005 x4
0.0010 ± 0.0001 x127
0.0390 ± 0.0003 x97
0.0008 ± 0.0001 x125
0.0370 ± 0.0005 x7
0.0008 ± 0.0001 x73
0.0360 ± 0.0004 x67
0.0008 ± 0.0001 x101
0.0325 ± 0.0001 x48
0.0007 ± 0.0001 x67
0.0319 ± 0.0004 x139
0.0007 ± 0.0001 x41
0.0301 ± 0.0001 x78
0.0007 ± 0.0000 x93
0.0296 ± 0.0002 x108
0.0005 ± 0.0001 x69
0.0289 ± 0.0003 x43
0.0005 ± 0.0001 x153
0.0287 ± 0.0003 x18
0.0005 ± 0.0000 x18
0.0271 ± 0.0001 x73
Artificial Intelligence 2024 48
0 ± 0.0000 x165
0 ± 0.0000 x162
0 ± 0.0000 x142
0 ± 0.0000 x167
0 ± 0.0000 x164
0 ± 0.0000 x166
0 ± 0.0000 x163
0 ± 0.0000 x165
0 ± 0.0000 x161
0 ± 0.0000 x164
0.0000 ± 0.0000 x134
0 ± 0.0000 x163
0 ± 0.0000 x143
0 ± 0.0000 x145
0 ± 0.0000 x144
0 ± 0.0000 x161
0 ± 0.0000 x145
0 ± 0.0000 x160
0 ± 0.0000 x160
0 ± 0.0000 x146
0.0000 ± 0.0000 x159
0 ± 0.0000 x142
0 ± 0.0000 x146
0 ± 0.0000 x143
0 ± 0.0000 x147
0 ± 0.0000 x155
0 ± 0.0000 x154
0 ± 0.0000 x140
0 ± 0.0000 x124
0 ± 0.0000 x154
0 ± 0.0000 x118
0 ± 0.0000 x144
-0.0000 ± x52
0.0000
0 ± 0.0000 x147
-0.0000 ± x9
0.0000
0 ± 0.0000 x141
Weight Feature
420308.1599 ± x10
1971.5218
168719.7697 ± x8
307.5919
168200.0994 ± x9
604.2461
144853.2147 ± x12
631.6338
144802.2410 ± x11
638.7344
115692.1266 ± x153
869.6890
60863.9908 ± x7
222.7441
60240.3905 ± x4
408.2612
59304.7034 ± x5
144.1569
58223.5526 ± x6
278.2673
49507.8658 ± x67
279.8174
49100.0612 ± x97
152.6299
49046.1333 ± x91
154.8688
49019.6117 ± x121
279.4339
48860.8884 ± x31
131.4751
48797.1800 ± x61
137.5981
48739.0284 ± x49
199.9117
48729.2202 ± x79
267.3027
48592.6339 ± x109
147.5623
48456.5323 ± x19
249.8473
47049.4670 ± x152
136.4209
Artificial Intelligence 2024 56
46962.3773 ± x150
216.2798
46944.3395 ± x151
160.3331
46666.9040 ± x149
76.6054
46363.3166 ± x43
228.4948
46125.1898 ± x103
161.8891
46066.5738 ± x73
105.2912
46053.0753 ± x13
247.2568
43072.4011 ± x127
141.9424
42998.5247 ± x37
194.3583
42838.2103 ± x85
340.5838
42793.7027 ± x115
214.4736
42701.4972 ± x55
121.2418
42533.6230 ± x25
373.9765
41771.6522 ± x72
121.8775
41352.0775 ± x126
161.4020
41339.3938 ± x66
277.6899
41298.6509 ± x102
147.1281
41269.9624 ± x36
219.1722
41247.8974 ± x96
240.3171
39896.1059 ± x24
173.7288
39841.9951 ± x84
125.2870
39766.0380 ± x114
198.2308
Artificial Intelligence 2024 57
39756.2993 ± x54
168.4137
38396.6087 ± x42
240.8642
38260.9820 ± x132
106.7866
38103.3501 ± x48
184.8240
37909.7689 ± x18
99.6934
37793.9234 ± x78
166.0346
37760.5721 ± x90
196.4883
37734.3883 ± x108
136.7099
37708.8216 ± x120
142.9657
37652.6453 ± x30
116.6496
37646.1722 ± x60
58.1251
33065.8235 ± x137
251.7146
32377.3105 ± x138
133.7776
32143.6539 ± x51
163.3497
32124.2603 ± x53
104.1482
32110.9218 ± x113
203.7616
32104.3048 ± x148
92.3177
32071.1699 ± x83
147.5913
32017.0288 ± x111
142.9326
32014.9845 ± x81
123.9986
31872.9000 ± x139
180.0975
31813.1713 ± x21
142.1148
Artificial Intelligence 2024 58
31773.8008 ± x23
247.7797
30819.2551 ± x65
186.5750
30803.7064 ± x63
168.8378
30668.7501 ± x95
80.2514
30646.8145 ± x93
209.6231
30548.7257 ± x33
133.6079
30538.2256 ± x136
100.4262
30529.3420 ± x35
29.5517
30404.0409 ± x99
181.6270
30393.6184 ± x125
161.8075
30391.7563 ± x101
105.2591
30365.2264 ± x123
82.7435
29938.3315 ± x69
141.0582
29856.6414 ± x71
102.6385
29414.8589 ± x27
98.5629
29400.6603 ± x29
68.2986
29104.2161 ± x57
85.6045
29099.3971 ± x119
152.4607
29097.3653 ± x117
129.6012
29078.8096 ± x59
141.4013
28942.2383 ± x89
74.9019
28919.8960 ± x87
131.8561
Artificial Intelligence 2024 59
28530.5207 ± x75
94.5690
28498.5219 ± x39
130.0983
28448.3075 ± x129
45.5075
28422.5563 ± x77
144.8911
28414.2082 ± x41
81.5877
28405.2021 ± x131
91.2084
28396.0382 ± x15
170.3150
28370.0028 ± x107
82.6514
28361.2497 ± x105
136.1323
28328.2373 ± x17
84.9356
28260.0626 ± x45
91.7907
28227.0409 ± x47
106.8331
26572.4406 ± x70
145.6861
26237.9685 ± x124
266.0094
26201.0758 ± x34
206.6818
26099.6413 ± x100
192.7798
25819.1745 ± x64
285.0211
25701.9789 ± x94
151.6474
23358.7390 ± x170
34.2192
23340.7800 ± x169
125.2808
23224.7835 ± x22
137.0599
23213.5238 ± x171
73.9985
Artificial Intelligence 2024 60
23211.9618 ± x172
92.8742
23084.9442 ± x82
149.1148
22950.3683 ± x46
290.1383
22858.7767 ± x106
125.9910
22832.9635 ± x76
137.1438
22820.0627 ± x16
196.1715
22734.7478 ± x112
184.2227
22554.8841 ± x52
133.6600
16572.5238 ± x168
60.0438
16450.4633 ± x159
73.3357
16431.2873 ± x133
162.3274
15634.3760 ± x156
43.2353
15307.5631 ± x158
55.6226
15028.8766 ± x157
56.7018
14989.7410 ± x20
217.2009
14728.3909 ± x104
75.8380
14282.3171 ± x14
44.1813
13981.8141 ± x74
53.2403
13879.7776 ± x44
65.5766
13458.8925 ± x110
92.5679
13223.6312 ± x130
54.4273
13193.0326 ± x32
97.6119
Artificial Intelligence 2024 61
13157.5630 ± x40
28.5600
13075.7362 ± x88
32.6480
13021.7112 ± x122
57.1614
12907.6308 ± x50
79.3466
12819.2426 ± x58
51.6681
12815.5325 ± x80
159.2604
12729.1920 ± x118
38.6092
12685.7919 ± x98
94.9549
12656.5577 ± x28
59.4703
12654.2194 ± x56
130.9794
12557.6649 ± x92
97.8118
12520.4220 ± x38
102.7318
12443.3832 ± x86
115.8954
12356.1254 ± x128
163.7202
12080.9775 ± x62
79.0429
12068.3206 ± x116
115.1230
12021.4671 ± x26
149.0293
11754.6019 ± x68
148.3217
10002.0522 ± x135
75.9444
8588.1981 ± x134
67.3481
55.3999 ± x2
0.2181
35.2083 ± x0
0.2392
Artificial Intelligence 2024 62
0.3962 ± 0.0168 x3
0.1059 ± 0.0108 x1
0 ± 0.0000 x142
0 ± 0.0000 x143
0 ± 0.0000 x140
0 ± 0.0000 x167
0 ± 0.0000 x166
0 ± 0.0000 x165
0 ± 0.0000 x164
0 ± 0.0000 x163
0 ± 0.0000 x162
0 ± 0.0000 x161
0 ± 0.0000 x160
0 ± 0.0000 x155
0 ± 0.0000 x154
0 ± 0.0000 x141
0 ± 0.0000 x146
0 ± 0.0000 x145
0 ± 0.0000 x144
0 ± 0.0000 x147