通过循环解决数据

时间:2018-03-05 19:06:08

标签: r loops

我在R中有一个优化器,它根据一组标准为我提供了最佳分数,

  

职位,#团队成员,预测和薪水

它导入一个csv文件,其中有一个播放器列表听到的是一个小样本。 `

  

Col A- Pos ... Col B-Name ... Col C- Salary ... Col D- Team ... Col   E-AVGA1。 Col F-AVGA2等一直到AVG500

我已经包含的代码解决了E列中的数据我怎样才能将它带到"循环"然后解决F列中的数据等问题,而无需复制和粘贴并进行500次单独交易。

Pos     Name                    Salary  Team   AVGA1           AVGA2
PG      Russell Westbrook       11300   OKC    58.86094565     54.55   
PGSG    James Harden            11200   HOU    57.71092287     32.45
SFPF    Giannis Antetokounmpo   10500   MIL    56.05945537     16.11
PFC     Anthony Davis           11700   NOR    54.76162186     21.12
PGSG    Victor Oladipo           9000   IND    47.06204442     14.55

setwd("c:/r data/NBA/DK")
library('lpSolve')
data <-read.csv ("NBAAVG-A1.CSV")

#Convert salary to numeric
data$Salary <- as.numeric(gsub(",","",data$Salary), data$Salary)

#Add binary valeus for positions  'Constraint Vectors'
data <- cbind(data, X=1)
data <- cbind(data, PG=ifelse(data$Pos=="PG",1,0))
data <- cbind(data, SG=ifelse(data$Pos=="SG", 1,0))
data <- cbind(data, SF=ifelse(data$Pos=="SF", 1,0))
data <- cbind(data, PF=ifelse(data$Pos=="PF", 1,0))
data <- cbind(data, C=ifelse(data$Pos=="C", 1,0))
data <- cbind(data, PGSG=ifelse(data$Pos=="PGSG", 1,0))
data <- cbind(data, PGSF=ifelse(data$Pos=="PGSF", 1,0))
data <- cbind(data, SGSF=ifelse(data$Pos=="SGSF", 1,0))
data <- cbind(data, SFPF=ifelse(data$Pos=="SFPF", 1,0))
data <- cbind(data, PFC=ifelse(data$Pos=="PFC", 1,0))


#Objective Function. sum of proj pts
f.obj <- data$AVGA1

#Constraints
num_X <- 8
num_PG <- 1
num_SG <- 1
num_SF <- 1
num_PF <- 1
num_C <- 1
num_G <- 3
num_F <- 3
num_SGSF <- 2
num_PGSF <- 2
num_PFC <- 2
max_PGSG <- 4
max_PFC <-4
max_C <- 2
max_pg <- 3
max_PGC <- 4
max_team_cost <- 50000
cur_max <- 5000
max_player_from_a_team <- 4

#Constraints for max players from team
clubs <- sort(unique(data$Team))

team_constraint_vector <- c()
team_constraint_dir <- c()
team_constraint_rhs <- c()

for(i in 1:length(clubs)){
  temp <- data$Team==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)
}

solutions <- list()
solutionsMatrix <- matrix(, nrow=0, ncol=11)

for(i in 1:1){
  f.con <- matrix (c(data$X, data$PG + data$PGSG +data$PGSF, data$SG + data$PGSG + data$SGSF, data$SF + data$SGSF + data$SFPF + data$PGSF, data$PF + data$SFPF + data$PFC, data$C + data$PFC, data$PG + data$SG + data$PGSG + data$SGSF + data$PGSF, data$SF + data$PF + data$SGSF + data$SFPF+ data$PFC +data$PGSF, data$SG + data$PGSG + data$SGSF + data$SF + data$SFPF, data$PG + data$PGSF + data$PGSG + data$SF + data$SFPF +data$SGSF, data$PF + data$SFPF + data$PFC + data$C, data$C + data$PFC + data$PF,  data$C, data$PG, data$PG + data$C, data$PG + data$SG + data$PGSG, data$Salary, data$AVGA1, team_constraint_vector), nrow=(18+length(clubs)), byrow=TRUE)
  f.dir <- c("=", ">=", ">=", ">=", ">=", ">=", ">=", ">=", ">=", ">=", ">=","<=", "<=", "<=", "<=", "<=", "<=", "<=", team_constraint_dir)
  f.rhs <- c(num_X, num_PG, num_SG, num_SF, num_PF, num_C, num_G, num_F, num_SGSF, num_PGSF, num_PFC, max_PFC, max_C, max_pg, max_PGC, max_PGSG, max_team_cost, cur_max, team_constraint_rhs)

  x <- lp ("max", f.obj, f.con, f.dir, f.rhs, all.bin=TRUE)
  x

  solutions[[i]] <- data[which(x$solution==1),]
  solutionsMatrix <- rbind(solutionsMatrix, c("AVGB1", sum(solutions[[i]]$AVGA1), sum(solutions[[i]]$Salary), toString(solutions[[i]]$Name[1]), toString(solutions[[i]]$Name[2]), toString(solutions[[i]]$Name[3]), toString(solutions[[i]]$Name[4]), toString(solutions[[i]]$Name[5]), toString(solutions[[i]]$Name[6]), toString(solutions[[i]]$Name[7]), toString(solutions[[i]]$Name[8])))

  cur_max <- sum(solutions[[i]]$AVGA1) -.0001
}

solutions[[1]]

#Solutions Matrix Optimization
solutionsMatrix

write.csv(solutionsMatrix, file="S1/AVG1A.CSV")

##########################################

0 个答案:

没有答案