0

Sorry, here is what I tried with code. The LP works for creating 2 teams. If LP is not the way to go, I am open to different directions. I want to create all possible fantasy combinations about a specified point total. In this case it is 150. Data I used can be found HERE.

> library(Rglpk)
 maxpts = 10000 # Set a max point limit to loop
> obj <- final$FPts # Objective Funtion
matrix <- rbind(as.numeric(final$Pos == "QB"), # num QB
+            as.numeric(final$Pos == "RB"), # num RB
+            as.numeric(final$Pos == "RB"), # num RB
+            as.numeric(final$Pos == "WR"), # num WR
+            as.numeric(final$Pos == "WR"), # num WR
+            as.numeric(final$Pos == "TE"), # num TE
+            as.numeric(final$Pos == "TE"), # num TE
+            as.numeric(final$Pos %in% c("RB", "WR", "TE")),  # Num RB/WR/TE
+            as.numeric(final$Pos == "DST"),# num DEF
+           final$Sal,   # total cost
+            final$FPts)                       
> direction <- c("==",
+          ">=",
+          "<=",
+          ">=",
+          "<=",
+          ">=",
+          "<=",
+          "==",
+          "==",
+          "<=",
+          "<=")
> rhs <- c(1, # Quartbacks
+        2, # RB Min
+        3, # RB Max
+        3, # WR Min
+        4, # WR Max
+        1, # TE Min
+        2, # TE Max
+        7, # RB/WR/TE
+        1, # Defense
+        50000,
+        maxpts)                
> sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
+                       types = var.types, max = TRUE)

> teamlp <- function(){
+ sol <- Rglpk_solve_LP(obj = obj, mat = matrix, dir = direction, rhs = rhs,
+                       types = var.types, max = TRUE)
+ finalteam <- final[sol$solution==1,]
+ teams <- rbind(teams, finalteam)
+ maxpts = sum(finalteam$FPts) - .001
+ }  # Put LP in a funtion

getteams <- function(){
+ while(maxpts > 150) {
+ teamlp
+ }
+ maxpts = sum(finalteam$FPts) - .001
+ }  # Created a while loop to create teams until Total Team points dropped below 150

Was hoping to figure this out and have it print out like this

Team   QB   RB   RB  WR  WR   WR  TE  FLEX(RB/WR/TE)   DEF  Totoal PTS

1      QB1  RB1  RB1 WR1 WR1  WR1 TE1  FLEX1           DEF1  190

2      QB2  RB2  RB2 WR2 WR2  WR2 TE2  FLEX2           DEF2  189.95

.

.

.

N      QBn  RBn  RBn  WRn  WRn  WRN  TEn  FLEXn       DEFn  150.01

I think the code I wrote is an infinite loop. Program has been running for 30 minutes. I ran the LP independently and that works. I think I also messed up the function that contains the LP. I got this error when I ran that by itself.

Error in rbind(teams, finalteam) : object 'teams' not found

Again my goal is to find all possible combinations of teams that have points above 150. Teams are comprised of 1 QB, 2 RB, 3 WR, 1 TE, 1 FLEX (RB/WR/TE), and 1 DEF. I tried to use combination (comb) first but seemed like I was using too much of the memory and was getting a error so I went to a LP in a loop path after seeing an example, but it was written to only produce 20 teams. Any suggestion will be much appreciated.

989
  • 12,579
  • 5
  • 31
  • 53
JB17
  • 65
  • 1
  • 8
  • 1
    Please make this reproducible: http://stackoverflow.com/help/mcve http://stackoverflow.com/help/how-to-ask http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – G. Grothendieck Oct 25 '15 at 21:46
  • What is the question? What is not working? Please note SO is not a code-writing service. Please show some effort so we can troubleshoot. – Parfait Oct 26 '15 at 00:53
  • I updated my original post with what I have been working on. I think I have written an infinite loop. THe LP works, and i can change the max points to get different teams. – JB17 Oct 27 '15 at 23:34
  • I would start by actually calling the teamlp function, using teamlp() in your infinite loop. – Jack Wasey Oct 27 '15 at 23:56
  • So take out the team lp function and just put the lp into the while loop? – JB17 Oct 28 '15 at 00:14
  • I realized another issue I am having. If multiple teams have the same amount of points I think I am only getting 1 team outputted is there a solution to that? – JB17 Oct 28 '15 at 10:12
  • Just thought of a solution for my past comment. Right now I have the loop based on max points. Is there a way to make it so that the team created cannot be the same as any previous team created? I'm going to play around with that tonight but would love to hear some suggestions on how to write the program in a loop like that. I've never used loops before so I'm kind of lost on how to do that and guides I see online are rather simplified. – JB17 Oct 28 '15 at 11:47

0 Answers0