-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimization fantacalcio team.R
122 lines (98 loc) · 4.04 KB
/
optimization fantacalcio team.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
library(lpSolve)
library(RCurl)
library(stringr)
library(jsonlite)
library(dplyr)
#Load statistics of previous season https://www.fantacalcio.it/statistiche-serie-a/2019-20/fantacalcio/riepilogo
#and the mean price of player in auction (https://www.fantacalcio-online.com/it/asta-fantacalcio-stima-prezzi)
df=read.csv("stat_fanta_2019-2020.csv")
#delete player that didn't play in the last season
row_to_keep = which(!is.na(df$Mv))
df=df[row_to_keep,]
#delete player with NA value (i.e. players that are purchased less than 10 times)
row_to_keep = which(!is.na(df$X500K..10.))
df=df[row_to_keep,]
#delete player with less than 4 presence
row_to_keep = which(df$Pg>4 | df$R == "P")
df=df[row_to_keep,]
###SELECT FIRST TEAM PLAYER
# Fitting Constraints
num_gk <- 1
num_def <- 3
num_mid <- 4
num_fwd <- 3
max_cost <- 450
min_pg_gk = 30
min_pg_def = 100
min_pg_mid = 120
min_pg_fwd = 100
# Create vectors to constrain by position
df$Goalkeeper <- ifelse(df$R == "P", 1, 0)
df$Defender <- ifelse(df$R == "D", 1, 0)
df$Midfielder <- ifelse(df$R == "C", 1, 0)
df$Forward <- ifelse(df$R == "A", 1, 0)
df$Pg=as.numeric(df$Pg)
df$Mf=as.numeric(df$Mf)
# The vector to optimize on
objective <- df$Mf
# next we need the constraint directions
const_dir <- c("=", "=", "=", "=", "<=", ">=",">=",">=",">=")
# Now put the complete matrix together
const_mat <- matrix(c(df$Goalkeeper, df$Defender, df$Midfielder, df$Forward,
df$X500K..10.,df$Pg*df$Goalkeeper,df$Pg*df$Defender,df$Pg*df$Midfielder,df$Pg*df$Forward ),
nrow=9, byrow=TRUE)
const_rhs <- c(num_gk, num_def, num_mid, num_fwd, max_cost, min_pg_gk,min_pg_def,min_pg_mid, min_pg_fwd)
# then solve the matrix
x <- lp ("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE)
# And this is our team!
solution_first <- df %>%
mutate(solution_first = x$solution) %>%
filter(solution_first == 1) %>%
select(Nome, Squadra.x, Goalkeeper ,Defender,Midfielder,Forward, X500K..10., Mf,Pg) %>%
arrange( X500K..10.)
print(solution_first)
solution_first %>% summarise(total_price = sum( X500K..10.)) %>% print
solution_first %>% summarise(total_points = sum(Mf)) %>% print
#delete obs of first team
row_to_keep=which(df$Nome%in%setdiff(df$Nome,solution_first$Nome))
df=df[row_to_keep,]
#SELECT bench GK, same team as first team choice
gk2=which(df$Squadra.x[which(df$Goalkeeper==1)]==solution_first$Squadra.x[which(solution_first$Goalkeeper==1)])
select_gk2=df[which(df$Goalkeeper==1),][gk2,]
solution_gk2 <- select_gk2 %>% select(Nome, Squadra.x, Goalkeeper ,Defender,Midfielder,Forward, X500K..10., Mf,Pg)
print(solution_gk2)
remaing_credits=500-solution_gk2$X500K..10. - sum( solution_first$X500K..10.)
###SELECT REPLACEMENT PLAYERS
# Fitting Constraints
num_gk <- 0
num_def <- 5
num_mid <- 4
num_fwd <- 3
max_cost <- remaing_credits
min_pg_gk = 0
min_pg_def = 100
min_pg_mid = 80
min_pg_fwd = 60
# Create vectors to constrain by position
df$Goalkeeper <- ifelse(df$R == "P", 1, 0)
df$Defender <- ifelse(df$R == "D", 1, 0)
df$Midfielder <- ifelse(df$R == "C", 1, 0)
df$Forward <- ifelse(df$R == "A", 1, 0)
# The vector to optimize on
objective <- df$Mf
# next we need the constraint directions
const_dir <- c("=", "=", "=", "=", "<=", ">=",">=",">=",">=")
# Now put the complete matrix together
const_mat <- matrix(c(df$Goalkeeper, df$Defender, df$Midfielder, df$Forward,
df$X500K..10.,df$Pg*df$Goalkeeper,df$Pg*df$Defender,df$Pg*df$Midfielder,df$Pg*df$Forward),
nrow=9, byrow=TRUE)
const_rhs <- c(num_gk, num_def, num_mid, num_fwd, max_cost, min_pg_gk,min_pg_def,min_pg_mid, min_pg_fwd)
# then solve the matrix
x <- lp ("max", objective, const_mat, const_dir, const_rhs, all.bin=TRUE, all.int=TRUE)
# solution reserve team
solution <- df %>%
mutate(solution = x$solution) %>%
filter(solution == 1) %>%
select(Nome, Squadra.x,Goalkeeper ,Defender,Midfielder,Forward, X500K..10., Mf,Pg) %>%
arrange( X500K..10.)
print(solution)