jeudi 16 juin 2016

R: Can't figure out how to loop with different variables [on hold]

I want to take this code which gives me a 10 line answer and run it with aris<-3, chcs<-3,etc. When aris<-3, I still want the rest of the team variables to equal 0. Then when I make chcs<-3, I want aris to revert to 0.

I have tried lapply and for loop and it won't work. Can you guys help me out?

library(lpSolve) 
library(data.table)
library(dplyr)


#teams
aris    <-  0 
chcs    <-  0
atls    <-  0
bals    <-  0
boss    <-  0
chws    <-  0
cins    <-  0
cles    <-  0
cols    <-  0
dets    <-  0
hous    <-  0
kcrs    <-  0
laas    <-  0
lads    <-  0
mias    <-  0
mils    <-  0
mins    <-  0
nyms    <-  0
nyys    <-  0
oaks    <-  0
phis    <-  0
pits    <-  0
sdps    <-  0
seas    <-  0
sfgs    <-  0
stls    <-  0
tbrs    <-  0
texs    <-  0
tors    <-  0
wass    <-  0


#load data from excel shee
df <- fread("baseball_data6 9 16.csv")


vals<-c()

#create matrix for left side
mm<-matrix(c(df$'P',df$'C',df$'C',df$'1B',df$'1B',df$'2B',df$'2B',df$'3B',df$'3B',df$'SS',df$'SS',df$'OF',df$'OF',df$Salary,df$count,df$'1B/2B',df$'1B/3B',df$'1B/C',df$'1B/OF',df$'2B/3B',df$'2B/SS',df$'2B/OF',df$'2B/C',df$'3B/SS',df$'SS/OF',df$'3B/C',df$'3B/OF',df$'C/OF',df$'1B/SS',df$'1B/2B/3B',df$'1B/2B/SS',df$'1B/2B/OF',df$'1B/3B/OF',df$'1B/3B/SS',df$'1B/C/OF',df$'2B/3B/SS',df$'2B/3B/OF',df$'2B/3B/C',df$'3B/SS/OF',df$'1B/2B/SS/3B',df$'1B/2B/3B/OF',df$'2B/3B/SS/OF',df$ARIH,df$ARI,df$CHCH,df$CHC,df$ATLH,df$ATL,df$BALH,df$BAL,df$BOSH,df$BOS,df$CHWH,df$CHW,df$CINH,df$CIN,df$CLEH,df$CLE,df$COLH,df$COL,df$DETH,df$DET,df$HOUH,df$HOU,df$KCRH,df$KCR,df$LAAH,df$LAA,df$LADH,df$LAD,df$MIAH,df$MIA,df$MILH,df$MIL,df$MINH,df$MIN,df$NYMH,df$NYM,df$NYYH,df$NYY,df$OAKH,df$OAK,df$PHIH,df$PHI,df$PITH,df$PIT,df$SDPH,df$SDP,df$SEAH,df$SEA,df$SFGH,df$SFG,df$STLH,df$STL,df$TBRH,df$TBR,df$TEXH,df$TEX,df$TORH,df$TOR,df$WASH,df$WAS),ncol=200,nrow=102,byrow=TRUE)

#assign the objective as select multiplied by fantasy points
obj <- df$FP
#assign the direction of the constraints
dir <- c('=', '>=', '<=', '>=', '<=', '>=', '<=','>=', '<=','>=', '<=','>=', '<=','<=','=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=','>=','<=')



#loop for 200lineups
for(i in 1:1){
  #right hand constraint
  rhs <- c(2,1,8,1,8,1,8,1,8,1,8,3,8,50000,10,2,2,2,4,2,2,4,2,2,4,2,4,4,2,3,3,5,5,3,5,3,5,3,5,4,6,6,aris,5,chcs,5,atls,5,bals,5,boss,5,chws,5,cins,5,cles,5,cols,5,dets,5,hous,5,kcrs,5,laas,5,lads,5,mias,5,mils,5,mins,5,nyms,5,nyys,5,oaks,5,phis,5,pits,5,sdps,5,seas,5,sfgs,5,stls,5,tbrs,5,texs,5,tors,5,wass,5)
  #linear programming, maximize, objective value equals Fantasy points, all binary, right hand constraint is rhs, set direction, matrix is mm. 
  lp <- lp(direction = 'max',
           objective.in = obj,
           all.bin = T,
           const.rhs = rhs,
           const.dir = dir,
           const.mat = mm)
  #put all the binaries into the vector "vals".
  vals <- c(vals, lp$solution)

}

table<- data.frame(as.data.frame(df),vals)
lines<-filter(table,vals==1)


r1<- lines[1:10,1:10]

Aucun commentaire:

Enregistrer un commentaire