Commit 5a170b12 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Resolve "Provide a regressor"

parent b78035da
......@@ -49,6 +49,7 @@ apt-get install -y \
org-mode \
pkg-config \
r-cran-ggplot2 \
r-cran-glmnet \
texinfo \
valac \
valgrind \
......
......@@ -24,6 +24,7 @@ TANGLED = \
$(srcdir)/%reldir%/xml.c \
$(srcdir)/%reldir%/tarot-tune \
$(srcdir)/%reldir%/tarot-learn-tree \
$(srcdir)/%reldir%/tarot-learn-linear \
$(srcdir)/tuneconfig.h \
$(srcdir)/%reldir%/tarot-figures/convergence-parameter.eps \
$(srcdir)/%reldir%/tarot-figures/convergence-parameter.png \
......@@ -32,7 +33,7 @@ TANGLED = \
$(srcdir)/%reldir%/tarot-figures/convergence-random.eps \
$(srcdir)/%reldir%/tarot-figures/convergence-random.png
dist_noinst_SCRIPTS += %reldir%/tarot-tune %reldir%/tarot-learn-tree
dist_noinst_SCRIPTS += %reldir%/tarot-tune %reldir%/tarot-learn-tree %reldir%/tarot-learn-linear
EXTRA_DIST += \
$(srcdir)/%reldir%/tarot.texi \
......@@ -44,7 +45,11 @@ EXTRA_DIST += \
%reldir%/maintainer-tree-3p.xml \
%reldir%/maintainer-tree-4p.xml \
%reldir%/maintainer-tree-5p.xml \
%reldir%/maintainer-tree-5pc.xml
%reldir%/maintainer-tree-5pc.xml \
%reldir%/maintainer-linear-3p.xml \
%reldir%/maintainer-linear-4p.xml \
%reldir%/maintainer-linear-5p.xml \
%reldir%/maintainer-linear-5pc.xml
MAINTAINERCLEANFILES += \
$(srcdir)/%reldir%/tarot.texi \
......@@ -53,7 +58,11 @@ MAINTAINERCLEANFILES += \
%reldir%/maintainer-tree-3p.xml \
%reldir%/maintainer-tree-4p.xml \
%reldir%/maintainer-tree-5p.xml \
%reldir%/maintainer-tree-5pc.xml
%reldir%/maintainer-tree-5pc.xml \
%reldir%/maintainer-linear-3p.xml \
%reldir%/maintainer-linear-4p.xml \
%reldir%/maintainer-linear-5p.xml \
%reldir%/maintainer-linear-5pc.xml
.PHONY: tune
......@@ -84,6 +93,26 @@ $(srcdir)/%reldir%/maintainer-tree-5pc.xml: %reldir%/tarot-learn-tree $(dist_5pc
$(learn_verbose) (cd $(srcdir) && $(abs_top_builddir)/pre-inst-env %reldir%/tarot-learn-tree $(dist_5pcdataset_DATA) | $(XMLLINT) --format - > %reldir%/maintainer-tree-5pc.xml-t)
@mv $(srcdir)/%reldir%/maintainer-tree-5pc.xml-t $(srcdir)/%reldir%/maintainer-tree-5pc.xml
$(srcdir)/%reldir%/maintainer-linear-3p.xml: %reldir%/tarot-learn-linear $(dist_3pdataset_DATA)
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot
$(learn_verbose) (cd $(srcdir) && $(abs_top_builddir)/pre-inst-env %reldir%/tarot-learn-linear $(dist_3pdataset_DATA) | $(XMLLINT) --format - > %reldir%/maintainer-linear-3p.xml-t)
@mv $(srcdir)/%reldir%/maintainer-linear-3p.xml-t $(srcdir)/%reldir%/maintainer-linear-3p.xml
$(srcdir)/%reldir%/maintainer-linear-4p.xml: %reldir%/tarot-learn-linear $(dist_4pdataset_DATA)
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot
$(learn_verbose) (cd $(srcdir) && $(abs_top_builddir)/pre-inst-env %reldir%/tarot-learn-linear $(dist_4pdataset_DATA) | $(XMLLINT) --format - > %reldir%/maintainer-linear-4p.xml-t)
@mv $(srcdir)/%reldir%/maintainer-linear-4p.xml-t $(srcdir)/%reldir%/maintainer-linear-4p.xml
$(srcdir)/%reldir%/maintainer-linear-5p.xml: %reldir%/tarot-learn-linear $(dist_5pdataset_DATA)
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot
$(learn_verbose) (cd $(srcdir) && $(abs_top_builddir)/pre-inst-env %reldir%/tarot-learn-linear $(dist_5pdataset_DATA) | $(XMLLINT) --format - > %reldir%/maintainer-linear-5p.xml-t)
@mv $(srcdir)/%reldir%/maintainer-linear-5p.xml-t $(srcdir)/%reldir%/maintainer-linear-5p.xml
$(srcdir)/%reldir%/maintainer-linear-5pc.xml: %reldir%/tarot-learn-linear $(dist_5pcdataset_DATA)
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot
$(learn_verbose) (cd $(srcdir) && $(abs_top_builddir)/pre-inst-env %reldir%/tarot-learn-linear $(dist_5pcdataset_DATA) | $(XMLLINT) --format - > %reldir%/maintainer-linear-5pc.xml-t)
@mv $(srcdir)/%reldir%/maintainer-linear-5pc.xml-t $(srcdir)/%reldir%/maintainer-linear-5pc.xml
$(srcdir)/%reldir%/org-stamp: %reldir%/tarot.org %reldir%/setup.el %reldir%/convergence.csv
$(org_verbose) cd $(srcdir) && $(EMACS) --batch \
-l %reldir%/setup.el \
......
......@@ -2427,7 +2427,52 @@ print_tree <- function (tree, strategy) {
parse_tree_print_xml (capture.output (print (tree)), strategy)
}
#+END_SRC
*** The learning script, to learn a regression tree
*** A linear model with lasso penalty
This is a simple regression, but we add two specificities:
- we use the interaction between all features and the event type (the
number of players will be handled separately as it makes the
required memory explode);
- we add a Lasso penalty.
This is most easily done with the R =glmnet= package.
#+name: r-function-learn-lasso
#+BEGIN_SRC R :eval no
learn_lasso <- function (data) {
x <- model.matrix (score ~ (type_eq_TAROT_BID_EVENT + type_eq_TAROT_DECL_EVENT + type_eq_TAROT_CALL_EVENT
+ type_eq_TAROT_DISCARD_EVENT + type_eq_TAROT_HANDFUL_EVENT + type_eq_TAROT_CARD_EVENT)
,* .,
data = data)
y <- data$score
fit <- glmnet::cv.glmnet (x, y)
c <- coef (fit)
cselected <- c[which (c[,1] != 0), 1]
cm <- as.matrix (cselected)
data.frame (term = row.names (cm),
weight = c (cm))
}
#+END_SRC
#+name: r-function-save-lasso
#+BEGIN_SRC R :eval no
print_lasso <- function (model) {
cat ("<linear-model>")
for (i in seq_len (nrow (model))) {
name <- as.character (model$term[i])
value <- model$weight[i]
names <- strsplit (name, ":")[[1]]
cat (sprintf ("<interaction weight=\"%f\">", value))
for (name in names) {
cat (sprintf ("<variable name=\"%s\" />", name))
}
cat ("</interaction>")
}
cat ("</linear-model>")
}
#+END_SRC
*** The learning script
There are two learning scripts: one to learn a tree, and one to learn
a linear model.
#+BEGIN_SRC R :tangle tarot-learn-tree :shebang "#!/usr/bin/env Rscript" :noweb yes :eval no
options (warn = 1)
<<r-function-load-game-features>>
......@@ -2446,6 +2491,16 @@ print_tree <- function (tree, strategy) {
print_tree (learn_tree (ds_play), "strategy_play")
cat ("</tree>")
#+END_SRC
#+BEGIN_SRC R :tangle tarot-learn-linear :shebang "#!/usr/bin/env Rscript" :noweb yes :eval no
options (warn = 1)
<<r-function-load-game-features>>
whole_dataset <- do.call (rbind, lapply (commandArgs (trailingOnly = TRUE), load_game_features))
<<r-function-learn-lasso>>
<<r-function-save-lasso>>
cat ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
print_lasso (learn_lasso (whole_dataset))
#+END_SRC
* GNU Free Documentation License
:PROPERTIES:
:APPENDIX: t
......
......@@ -511,7 +511,7 @@ namespace Tarot {
rebuilt[i] = new Tarot.Game.alloc ();
}
if (encoded) {
for (int i = 0; i < 3; i++) {
for (int i = 3; i <= 5; i++) {
my_output += "@attribute n_players_eq_%d numeric\n".printf (i);
}
my_output += "@attribute with_call numeric\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment