Python Forum
Loop to find the best combination/score
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop to find the best combination/score
#1
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 Score1
0 Luka Dončić 57.0 66.4
1 Michael Porter Jr. 23.0 36.7
2 Kevin Porter Jr. 46.0 38.5
3 Desmond Bane 34.0 34.7
4 Bismack Biyombo 8.0 0.0
5 Wendell Carter Jr. 38.0 35.9
6 Max Strus 23.0 39.5
7 Jaden McDaniels 18.0 25.2
8 Brook Lopez 22.0 40.0
9 Dewayne Dedmon 6.0 5.1
10 Bruce Brown 33.0 28.7
11 Drew Eubanks 33.0 10.6
12 Luke Kennard 21.0 13.2
13 Nic Claxton 24.0 48.2
14 Landry Shamet 9.0 15.7
15 Mo Bamba 33.0 16.2
16 Zach Collins 25.0 29.299999999999997
17 Jevon Carter 7.0 7.199999999999999
18 Omer Yurtseven 6.0 0.0
19 Dalano Banton 4.0 7.0
Sorry, 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!

Attached Files

Thumbnail(s)
   
Reply
#2
Moved to Homework
See the Homework/No Effort posts link in my signature.
Reply
#3
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.
Reply
#4
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
Reply
#5
(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.
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

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
28 Nikola Jokić 60.0 82.5
0 Luka Dončić 58.0 76.6
75 Jayson Tatum 48.0 66.9
76 Joel Embiid 57.0 64.9
39 Jaylen Brown 47.0 53.9
62 LaMelo Ball 42.0 49.6
30 Ja Morant 45.0 49.4

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
Reply
#6
You didn't answer my question. How do you determine which team is better, average or score?
Reply
#7
Score is what determine the better team.
I would like to find the highest score possible in the limit of MVP + 120
Reply
#8
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.
Reply
#9
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)
Reply
#10
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.2
The more combination possible, the more time it will take, but seems to work now.
If someone as better/faster solution :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,391 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  Algorithm to generate a combination guvinicius2 5 2,483 Aug-15-2020, 10:42 PM
Last Post: deanhystad
  loops in combination with lists Juru 4 2,758 May-07-2020, 02:58 PM
Last Post: Marbelous
  Average score MartinEvtimov 5 6,717 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020