Fantasy football (oops, soccer)
[This article was first published on Enterprise Software Doesn't Have to Suck, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Recently a colleague asked if I could use R/statistics to form a dream soccer team from a pool of soccer players, given basic player information like name, club, cost, points.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The idea is to form a team with your preferred configuration of number of defenders, mid fielders and strikers to maximize your total team points without exceeding your budget.
I wrote some R code (linear optimization) to get the answer. Check it out. The code allows configurable constraints to let you create your own dream team.
1) Number of goalkeepers
2) Number of defenders
3) Number of mid fielders
4) Number of strikers
5) Total team cost ($$$ you’ll spend on this team)
6) Maximum number of players from a team
Here’s a team I put together based on this code with following configuration.
1) Number of goalkeepers = 1
2) Number of defenders = 4
3) Number of mid fielders = 3
4) Number of strikers = 3
5) Total team cost = 50 GBP
6) Maximum number of players from a team = 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Player Club Pos Price Pts | |
A Carroll Newcastle STR 2.0 84 | |
A Cole Chelsea DEF 7.0 92 | |
C Tevez Man City STR 7.0 104 | |
D Berbatov Man Utd STR 6.0 95 | |
F Malouda Chelsea MID 5.5 106 | |
G Bale Tottenham DEF 5.0 122 | |
J Hart Man City GK 4.0 86 | |
P Zabaleta Man City DEF 3.0 71 | |
S Nasri Arsenal MID 4.0 121 | |
S Parker West Ham MID 3.5 96 | |
V Kompany Man City DEF 3.0 88 |
DISCLAIMER: Soccer isn’t my favorite sport (its Cricket, if you’re curious 😉 and I don’t play fantasy football.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load lpsolve library | |
# install.packages('lpSolve') | |
library('lpSolve') | |
# Read player data (name, position, price, points) | |
players <- read.csv("http://spreadsheets.google.com/pub?key=0AvZWqry9LOMddDY3OVNLeDVNWTEtTEczbkRtUmx5SFE&hl=en&single=true&gid=0&output=csv", sep=",", header=TRUE) | |
# OBJECTIVE FUNCTION: Points of all players | |
f.obj <- players$Pts | |
# CONSTRAINTS: | |
num_goalkeepers <- 1 | |
num_defenders <- 4 | |
num_midfielders <- 3 | |
num_strikers <- 3 | |
max_team_cost <- 50 | |
max_player_from_a_team <- 4 # set "max_player_from_a_team <- 11" to ignore this constraint | |
# Create constraint vectors | |
defenders <- players$Pos=="DEF" | |
defenders[defenders==T] <- 1 | |
defenders[defenders==F] <- 0 | |
goalkeepers <- players$Pos=="GK" | |
goalkeepers[goalkeepers==T] <- 1 | |
goalkeepers[goalkeepers==F] <- 0 | |
midfielders <- players$Pos=="MID" | |
midfielders[midfielders==T] <- 1 | |
midfielders[midfielders==F] <- 0 | |
strikers <- players$Pos=="STR" | |
strikers[strikers==T] <- 1 | |
strikers[strikers==F] <- 0 | |
# constraint for max # players from a team | |
clubs <- sort(unique(players$Club)) | |
team_constraint_vector <- c() | |
team_constraint_dir <- c() | |
team_constraint_rhs <- c() | |
for (i in 1:length(clubs)){ | |
temp <- players$Club==as.character(clubs[i]) | |
temp[temp==T] <- 1 | |
temp[temp==F] <- 0 | |
team_constraint_vector <- c(team_constraint_vector, temp) | |
team_constraint_dir <- c(team_constraint_dir, "<=") | |
team_constraint_rhs <- c(team_constraint_rhs, max_player_from_a_team) | |
} | |
f.con <- matrix (c(goalkeepers, defenders, midfielders, strikers, players$Price, team_constraint_vector), nrow=(5+length(clubs)), byrow=TRUE) | |
f.dir <- c("=", "=", "=", "=", "<=", team_constraint_dir) | |
f.rhs <- c(num_goalkeepers, num_defenders, num_midfielders, num_strikers, max_team_cost, team_constraint_rhs) | |
x <- lp ("max", f.obj, f.con, f.dir, f.rhs, all.bin=TRUE) | |
x | |
x$solution | |
players[which(x$solution==1),] |
To leave a comment for the author, please follow the link and comment on their blog: Enterprise Software Doesn't Have to Suck.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.