Ooglely / pugBot

Discord bot to run TF2 pick up games | pugbot.tf
https://pugbot.tf
22 stars 1 forks source link

Improve elo team gen function #39

Closed Ooglely closed 10 months ago

Ooglely commented 10 months ago

Currently the team generation functions for generating teams with close elo average does not do its job correctly most of the time... trying to find a way to get the best answer without brute forcing every possible combination would be great

def find_subset(
    arr: List[PugPlayer], total: int, tolerance: int = 1
) -> List[PugPlayer]:
    """Find a subset of a list that adds up to a total.

    Args:
        arr (List[EloPlayer]): The list of all players
        total (int): The total elo of the teams

    Raises:
        ValueError: Subset not found

    Returns:
        List[EloPlayer]: One subset of players that adds up to the total elo
    """
    if (tolerance * -1) <= total <= tolerance:
        return arr
    if arr is None:
        raise ValueError("Subset not found")
    for item in arr:
        new_arr: List = arr.copy()
        new_arr.remove(item)
        new_sum = total - item.elo
        if new_sum < 0:
            continue
        if find_subset(new_arr, new_sum) is not None:
            return find_subset(new_arr, new_sum)
        continue
    raise ValueError("Subset not found")