Skip to content

constraints

constraints

Constraints for squad selection.

SquadQuotas

Position quotas for the 15-player FPL squad.

Rules: - 2 GK, 5 DEF, 5 MID, 3 FWD (exactly). - Total = 15 players.

FormationConstraints

Formation constraints for FPL squad.

Rules: - Exactly 11 players - 1 GK - 3-5 DEF - 2-5 MID - 1-3 FWD

validate classmethod

validate(players: list[Player]) -> bool

Check if squad satisfies formation constraints.

PARAMETER DESCRIPTION
players

List of players in squad

TYPE: list[Player]

RETURNS DESCRIPTION
bool

True if valid formation

Source code in fplx/selection/constraints.py
@classmethod
def validate(cls, players: list[Player]) -> bool:
    """
    Check if squad satisfies formation constraints.

    Parameters
    ----------
    players : list[Player]
        List of players in squad

    Returns
    -------
    bool
        True if valid formation
    """
    if len(players) != cls.TOTAL_PLAYERS:
        return False
    counts = {"GK": 0, "DEF": 0, "MID": 0, "FWD": 0}
    for p in players:
        counts[p.position] += 1
    return all(lo <= counts[pos] <= hi for pos, (lo, hi) in cls.POSITION_LIMITS.items())

get_valid_formations classmethod

get_valid_formations() -> list[str]

Get list of valid formation strings.

RETURNS DESCRIPTION
List[str]

Valid formations (e.g., "3-4-3", "4-3-3")

Source code in fplx/selection/constraints.py
@classmethod
def get_valid_formations(cls) -> list[str]:
    """
    Get list of valid formation strings.

    Returns
    -------
    List[str]
        Valid formations (e.g., "3-4-3", "4-3-3")
    """
    formations = []
    for d in range(3, 6):
        for m in range(2, 6):
            for f in range(1, 4):
                if d + m + f == 10:
                    formations.append(f"{d}-{m}-{f}")
    return formations

BudgetConstraint

BudgetConstraint(max_budget: float = 100.0)

Budget constraint for FPL squad (applied to 15-player squad).

Source code in fplx/selection/constraints.py
def __init__(self, max_budget: float = 100.0):
    self.max_budget = max_budget

TeamDiversityConstraint

TeamDiversityConstraint(max_from_team: int = 3)

Max players from same real-world team (default 3).

Source code in fplx/selection/constraints.py
def __init__(self, max_from_team: int = 3):
    self.max_from_team = max_from_team