Loop to find the best combination/score - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Loop to find the best combination/score (/thread-39077.html) |
Loop to find the best combination/score - KoinKoin - Dec-29-2022 Hello everyone, I need your help because I can't a good solution of my problem I have a dataframe, with name of some NBA players, average number and score, it looks like that : Quote: PLAYERS 10Aver1 Score1Sorry, seems my table doesn't look good here, don't know how to fix it, I added a picture maybe it's better in the pic. What I would like to do is to find the best output within some criterias: - Requires five-player (5) rosters - Points Cap limit is 120 - Each player has a Points Per Game average that counts toward the Points Cap (10Aver1 in my case) - The player you select with the highest Points Per Game average will be automatically marked as your MVP. The MVP does not count toward your Points Cap So in the end it's a MVP Player + 4 players in the limit of 120. How could I loop between everything and find the best 5 players with the highest score possible in the MVP+120 limit ? Thanks in advance for you big help! RE: Loop to find the best combination/score - Yoriz - Dec-29-2022 Moved to Homework See the Homework/No Effort posts link in my signature. RE: Loop to find the best combination/score - KoinKoin - Dec-29-2022 Oh it was moved... It's not a homework, so I think you consider that as no effort... I can't put everything that I have in my head, sorry If you want to know, one of my best solution would be to take 5 between the 20, test the MVP + limit, if ok, keep the roster + total score. Then take another 5 and do the same. If score is higher, replace the old result by this one... Since i have 20 lines, and I need to find 5. It means : C(20,5) = 20! / (5! * 15!) = 15,504 possibilities, if no mistakes. But what will happen if I have 70 players and need the 5 bests (over 12 millions of possibilites)... I never asked to get the solution, I just want to know what could be the best solution to find what I look for, some tips and advices. RE: Loop to find the best combination/score - deanhystad - Dec-29-2022 The MVP will be the same player for every team. If Luka costs nothing, Luka will be the MVP for all top teams. Now you need to create teams of 4 from the remaining 19 players, or 69 players. C(19,4) = 3874. C(69, 4) = 864501 These are large, but not huge numbers. Am I correct thinking that you use "Score1" to measure the value of the player? Is Brook Lopez better than Max Strus? Brook has a higher score but a lower average. If you sort values by score, you can grab the top 4 players (minus Luka) and their point cap is < 120 RE: Loop to find the best combination/score - KoinKoin - Dec-29-2022 (Dec-29-2022, 07:48 PM)deanhystad Wrote: The MVP will be the same player for every team. If Luka costs nothing, Luka will be the MVP for all top teams. Now you need to create teams of 4 from the remaining 19 players, or 69 players. Yes, I could sort and get the first 5 in that case. But I have other cases, when you have this : Quote: PLAYERS 10Aver19 Score19 And this time, it's not working well with the MVP + 120 limit haha So far, I'm trying something like this : s = df["10Aver1"].dropna().tolist() from itertools import combinations c2 = [i for i in combinations(s,5)] res = [] for i in c2: li = list(i) li.remove(max(li)) if sum(li) < 121: res.append(i)That way, it should keep every "10Aver" under MVP +120. But I need to get the player and score now RE: Loop to find the best combination/score - deanhystad - Dec-29-2022 You didn't answer my question. How do you determine which team is better, average or score? RE: Loop to find the best combination/score - KoinKoin - Dec-29-2022 Score is what determine the better team. I would like to find the highest score possible in the limit of MVP + 120 RE: Loop to find the best combination/score - deanhystad - Dec-29-2022 Still fuzzy. The sum(average) - MVP(average) is what must be less than 120, correct? sum(score) is used to determine the quality of the team, correct? Adding players from your recent post to the list, this is the best team Nikola Jokić, avg=60.0, score=82.5, value=1.375 Jayson Tatum, avg=48.0, score=66.9, value=1.394 Nic Claxton, avg=24.0, score=48.2, value=2.008 Brook Lopez, avg=22.0, score=40.0, value=1.818 Max Strus, avg=23.0, score=39.5, value=1.717 score=277.1, points=117.0 This is the a sorted list of the best players. Notice some do not appear on the best team. They are great players, but they are expensive hits to the cap. They have poor value. Nikola Jokić, avg=60.0, score=82.5, value=1.375 Jayson Tatum, avg=48.0, score=66.9, value=1.394 Luka Dončić, avg=57.0, score=66.4, value=1.165 Joel Embiid, avg=57.0, score=64.9, value=1.1386 Jaylen Brown, avg=47.0, score=53.9, value=1.147 LaMelo Ball, avg=42.0, score=49.6, value=1.181 Ja Morant, avg=45.0, score=49.4, value=1.098 Nic Claxton, avg=24.0, score=48.2, value=2.008 Brook Lopez, avg=22.0, score=40.0, value=1.818 Max Strus, avg=23.0, score=39.5, value=1.717 There are a lot of great players can be replaced by nearly as good players who are a smaller cap hit. It's Moneyball. Maybe you need to compute a value metric. In the list above value = score / avg, but this can make a poor, low average player look like a steal. ]Nic Claxton, avg=24.0, score=48.2, value=2.0083333333333333 Brook Lopez, avg=22.0, score=40.0, value=1.8181818181818181 Dalano Banton, avg=4.0, score=7.0, value=1.75 Landry Shamet, avg=9.0, score=15.7, value=1.7444444444444445 Max Strus, avg=23.0, score=39.5, value=1.7173913043478262 Michael Porter Jr., avg=23.0, score=36.7, value=1.5956521739130436 Jaden McDaniels, avg=18.0, score=25.2, value=1.4 Jayson Tatum, avg=48.0, score=66.9, value=1.39375 Nikola Jokić, avg=60.0, score=82.5, value=1.375 Even though these players have a good value metric, they are not good enough to have a spot on a 5 man roster. Hopefully this highlights that this problem is complicated. I think brute force may be the best solution. You could cull the list of players before generating combinations. For example, I have a list of 26 players from your posts. If I ignore all players with value < 1 or score < 20 I am left with 14 players. This reduces the number of potential teams from C(25, 4) = 12558 to C(13, 4) = 715. RE: Loop to find the best combination/score - KoinKoin - Dec-29-2022 Yes, it is correct, sorry that my words are difficult to understand Sum should be the highest possible. And the 5 players selected to get the highest score should be in the limit of "MVP + 120" (or sum(average 4 players) - MVP(average) >= 120) RE: Loop to find the best combination/score - KoinKoin - Dec-29-2022 Finally, I found some solution : I'm doing the combination on all my dataframe, then for each combination, calculate the 10average and if it's under 121, comparing with previous bestSum... bestSum = 0 bestIndex = () for index in list(combinations(df1.index,5)): currentSum = 0 if df1.loc[index,:]["10Aver1"].sum(axis=0) - df1.loc[index,:]["10Aver1"].max(axis=0) < 121: currentSum = df1.loc[index,:]["Score1"].sum(axis=0) if currentSum > bestSum: bestSum = currentSum bestIndex = index print(df1.loc[bestIndex,:])And this is the result I have : PLAYERS 10Aver1 Score1 0 Luka Dončić 57 69.7 2 Kevin Porter Jr. 46 40.4 6 Max Strus 23 41.5 8 Brook Lopez 22 42.0 13 Nic Claxton 24 50.6 CAP : 115 SCORE : 244.2The more combination possible, the more time it will take, but seems to work now. If someone as better/faster solution :) |